-
July 13th, 2009, 02:37 PM #1Junior 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; }
}
-
July 14th, 2009, 03:57 AM #2Member
- 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.
-
July 14th, 2009, 02:14 PM #3
gwinters is correct. I too would start there (just putting in my 2 cents to back up his/her claim)
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Java Help code included :(
By frolony in forum Webmastering and ProgrammingReplies: 1Last Post: March 30th, 2009, 10:55 AM -
Java Code
By mstabiggzlilmama in forum Applications and Operating SystemsReplies: 0Last Post: November 19th, 2008, 03:38 PM -
Can someone help me with this Java Code?
By quantumlight in forum Webmastering and ProgrammingReplies: 4Last Post: November 9th, 2008, 04:02 PM -
The Java Code Thread
By xelnanga in forum Webmastering and ProgrammingReplies: 11Last Post: December 10th, 2007, 09:46 PM -
Need help with some Java Code :P
By Soheils91 in forum General Tech DiscussionReplies: 2Last Post: October 20th, 2004, 07:57 PM



LinkBack URL
About LinkBacks



Reply With Quote

lulz. ^_^ Today is 5/22. Two year anniversary of the Joplin tornado. :( In better news, Daft Punk's album is out and my LP is in the mail. :cool:
Is It Just Me? v233893843