+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    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; }
    }

  2. #2
    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.

  3. #3
    Member theGlitch's Avatar
    Join Date
    Jul 2008
    Location
    Boston, MA
    Posts
    185
    Blog Entries
    1
    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

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

Tags for this Thread

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Recommended Sites: ResellerRatings Store Reviews