PDA

View Full Version : Can anyone tell me way there might be a compiler error


lostgoat
07-11-2009, 02:44 AM
I meant why there is a compilier error....
The error I am getting makes no sense to me.
//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; }
}

tomws
07-11-2009, 04:53 AM
I think your class curly braces are just wrapping main() rather than the entire class.