home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Asking for help with java code.....

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1908
Discussions: 200,991, Posts: 2,379,899, Members: 246,360
Old July 13th, 2009, 03:37 PM   Digg it!   #1 (permalink)
Junior Member
 
Join Date: Jul 2009
Posts: 2
Asking for help with java code.....

I am receiving an error when compiling and I do not understand why. Can anyone help out and let me know why this might be happening.

The error I am getting:
//ERROR: 'class' or 'interface' expected

the line error occurs is:

public static double getShortestWidth(MyRectangle r[])



Entire code:

import java.util.Scanner;
import java.util.Random;
import java.io.*;

public class PGM3
{
public static void main(String[] args) throws IOException
{
Printwriter pw; int c; MyRectangle [] x = new MyRectangle[20];
Scanner xyz = new Scanner(System.in);
System.out.println("Output filename? ");
fname = xyz.nextLine();
PrintWriter pw = new PrintWriter(fname);

for(c = 0; c<20; c++)
x[c] = new MyRectangle();

pw.println("Unsorted Rectangles Info\n");
writeMyRectangleInfo(x, pw);

pw.printf("\nShortest Length = %5.1f",getShortestLength(x));
pw.printf(" Longest Length = %5.1f",getLongestLength(x));

pw.printf("\nShortest Width = %5.1f".getShortestWidth(x));
pw.printf(" Longest Width = %5.1f\n",getLongestWidth(x));

MyRectangle y = getSmallestRectangle(x);
pw.printf("\nSmallest Area = %8.1f",y.getArea());

y = getLargestRectangle(x);
pw.printf("\nLargest Area = %8.1f",y.getArea());

selectionSort(x);

pw.printf("\n\n\nSorted Rectangles Info - In Ascending Order Of Area\n");
writeMyRectangleInfo(x, pw);

} //end of main
}

public static double getShortestWidth(MyRectangle r[])
{
int startScan; int index; int minIndex; double minValue;

for(startScan = 0; startScan < (r.length-1); startScan++)
{
minIndex=startScan;
minValue=r[startScan].getWidth();
for(index = startScan + 1; index < r.length; index++)
{
if (r[index].getWidth() < minValue)
{
minValue = r[index].getWidth();
}
}
r[minIndex] = r[startScan];
r[startScan] = minValue;
}
return minValue;
}

public static double getLongestWidth(MyRectangle r[])
{
int startScan; int index; int minIndex; double maxValue;

for(startScan = 0; startScan < (r.length-1); startScan++)
{
minIndex = startScan;
maxValue = r[startScan].getWidth();
for(index = startScan + 1; index < r.length; index++)
{
if(r[index].getWidth() > maxValue)
{
maxValue = r[index].getWidth();
}
}
r[minIndex] = r[startScan];
r[startScan] = maxValue;
}
return maxValue;
}

public static double getShortestLength(MyRectangle r[])
{
int startScan; int index; int minIndex; double minValue;

for(startScan = 0; startScan < (r.length-1); startScan++)
{
minIndex=startScan;
minValue=r[startScan].getLength();
for(index = startScan + 1; index < r.length; index++)
{
if (r[index].getLength() < minValue)
{
minValue = r[index].getLength();
}
}
r[minIndex] = r[startScan];
r[startScan] = minValue;
}
return minValue;
}

public static double getLongestLength(MyRectangle r[])
{
int startScan; int index; int minIndex; double maxValue;

for(startScan = 0; startScan < (r.length-1); startScan++)
{
minIndex = startScan;
maxValue = r[startScan].getLength();
for(index = startScan + 1; index < r.length; index++)
{
if(r[index].getLength() > maxValue)
{
maxValue = r[index].getLength();
}
}
r[minIndex] = r[startScan];
r[startScan] = maxValue;
}
return maxValue;
}

public static void writeMyRectangleInfo(MyRectangle r[], PrintWriter y)
{
for(index = 0; index < r.length; index++)
{
System.out.print("Length: " + r[index].getLength());
System.out.print("\t\tWidth: " + r[index].getWidth());
double area;
area = length * width;
System.out.print("\t\tArea: " + r[index].getArea());
}
}



public static void selectionSort(MyRectangle[] array)
{
int startScan;
int index;
int minIndex;
int minValue;


for (startScan = 0; startScan < (array.length-1); startScan++)
{

minIndex = startScan;
minValue = array[startScan];


for(index = startScan + 1; index < array.length; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}

array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
public static MyRectangle getSmallestRectangle(MyRectangle x[])
{
int startScan; int index; int minIndex; double minValue;

for(startScan = 0; startScan < (r.length-1); startScan++)
{
minIndex=startScan;
minValue=r[startScan].getArea();
for(index = startScan + 1; index < r.length; index++)
{
if (r[index].getArea() < minValue)
{
minValue = r[index].getArea();
}
}
r[minIndex] = r[startScan];
r[startScan] = minValue;
}
return minValue;
}
public static MyRectangle getLargestRectangle(MyRectangle x[])
{
int startScan; int index; int minIndex; double maxValue;

for(startScan = 0; startScan < (r.length-1); startScan++)
{
minIndex = startScan;
maxValue = r[startScan].getArea();
for(index = startScan + 1; index < r.length; index++)
{
if(r[index].getArea() > maxValue)
{
maxValue = r[index].getArea();
}
}
r[minIndex] = r[startScan];
r[startScan] = maxValue;
}
return maxValue;
}


import java.util.Random;

public class MyRectangle
{
Random r1 = new Random();
private double length, width;

public MyRectangle()
{ length = r1.nextInt(100)%10 + 10 + r1.nextDouble();
width = r1.nextInt(100)%10 + 5 + r1.nextDouble();
}
public void setLength(double len)
{ length = len; }
public void setWidth(double wid)
{ width = wid; }
public double getLength()
{ return length; }
public double getWidth()
{ return width; }
public double getArea()
{ return length * width; }
}
lostgoat is offline   Reply With Quote
Old July 14th, 2009, 04:57 AM     #2 (permalink)
Member
 
Join Date: Oct 2001
Posts: 196
I'm going to take a wild stab at this, but shouldn't the definition be:

public static double getShortestWidth(MyRectangle[] r)

I'm not a Java guy, but I'd start there.
gwinters is offline   Reply With Quote
Old July 14th, 2009, 03:14 PM     #3 (permalink)
Member
 
theGlitch's Avatar
 
Join Date: Jul 2008
Location: Boston, MA
Posts: 183
Blog Entries: 1
Send a message via AIM to theGlitch
gwinters is correct. I too would start there (just putting in my 2 cents to back up his/her claim)
theGlitch is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Help code included :( frolony Webmastering and Programming 1 March 30th, 2009 11:55 AM
Java Code mstabiggzlilmama Applications and Operating Systems 0 November 19th, 2008 03:38 PM
Can someone help me with this Java Code? quantumlight Webmastering and Programming 4 November 9th, 2008 04:02 PM
The Java Code Thread xelnanga Webmastering and Programming 11 December 10th, 2007 09:46 PM
Need help with some Java Code :P Soheils91 General Tech Discussion 2 October 20th, 2004 08:57 PM


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (3083)
Charges against non-tippers dropped.. (20)
Health Care Rationing (13)
Delete an OS (17)
Nvidia GTX 260 problem (10)
Laptop with wireless problem. (12)
Wireless Televisions. (12)
windows vista security holes (19)
CPU fan stops spinning randomly (11)
Regular Build (11)
Point and Shoot Camera Suggestions. (8)
[F@H SPAM 11/16/09] ! 1/2 months to.. (40)
windows 7 problem (7)
Internet Lost (5)
Recent Discussions
[F@H SPAM 11/16/09] ! 1/2 months to r.. (40)
Foxconn Blackops x48 MoBo (3)
Q9650 vs. Q9550 (2)
Desktop Calendar Application (2)
Looking for new motherboard (1)
soundmon.exe (8)
Jedi Academy Problem (3)
Can a page file be "too big".. (1)
Nvidia GTX 260 problem (10)
Point and Shoot Camera Suggestions. (8)
Size after cutting 700Mb file is 2.5 .. (0)
Delete an OS (17)
windows vista security holes (19)
updating BIOS via winflash, claims fi.. (1)
New Server Configuration Suggestions (0)
cheap gaming laptop? (12)
Unallocated Space (2)
help me pls laptop just stopped worki.. (1)
C# + LINQ Help (7)
Dynex DX E-402 (3)
EVGA 9800 gtx help with finding a goo.. (12)
Multiple Restarts Required at Boot (5)
cell phone won't work (0)
Is the PSU I received dead? (15)
Can't open Word (12)


All times are GMT -4. The time now is 09:23 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28