Hi guys, Im trying to write a program that find the biggest rectangle by its area. I've tried and it doesnt seem to compile. Can you help me please, thanks.

``````public class Rectangle
{
private int width;
private int height;

public Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}

// How big is this rectangle?
public int getArea()
{
return width * height;
}

public void setWidth(int width)
{
this.width = width;
}

public int getWidth()
{
return width;
}

public void setHeight(int height)
{
this.height = height;
}

public int getHeight()
{
return height;
}

public String toString()
{
return width + "x" + height;
}
}``````
``````import javax.swing.*;
import java.util.*;

public class BiggestRectangleProgram
{
private static final Scanner keyboard = new Scanner(System.in);

public static void main(String[] args)
{
Rectangle[] biggest = findBiggest();
System.out.println("The biggest rectangle is " + biggest);
}

private static Rectangle findBiggest(Rectangle[] rects)
{
Rectangle biggestSoFar = rects[0];
for (int i = 1; i < rects.length; i++)
{
//if (this one is bigger than biggestSoFar)
if(rects[i] > biggestSoFar)
//    biggestSoFar = this one
biggestSoFar = rects[i];
}
return biggestSoFar;
}

{
// This method contains both syntax errors *and* logic errors
//
int howMany = getInt("How many rectangles?");
Rectangle[] rects = new Rectangle();
for (int i = 0; i < rects[i]; i++)
{
int width = getInt("Width of rectangle #"+i+":");
int height = getInt("Height of rectangle #"+i+":");
rects[i] = new Rectangle(width, height);
}
return Rectangle();
}

private static int getInt(String prompt)
{
System.out.print(prompt + " ");
return keyboard.nextInt();
}
}``````

Read the error messages that you get. The method "findBiggest" returns a Rectangle. So why do you assign it to an array when you call it.

Also you cannot use: '>' with objects: `if(rects[i] > biggestSoFar)` If you want to compare them based on their area, then call the getArea() method in order to compare the area of the rectangulars

Hi I've fixed it but still having problems with the line Rectangle biggest = findBiggest();, and the line return Rectangle[]; in readRectangles() method. Can you help me please. thanks

``````import javax.swing.*;
import java.util.*;

public class BiggestRectangleProgram
{
private static final Scanner keyboard = new Scanner(System.in);

public static void main(String[] args)
{
Rectangle biggest = findBiggest();
System.out.println("The biggest rectangle is " + biggest);
}

private static Rectangle findBiggest(Rectangle[] rects)
{
Rectangle biggestSoFar = rects[0];
for (int i = 1; i < rects.length; i++)
{
//if (this one is bigger than biggestSoFar)
if(rects[i].getArea() > biggestSoFar.getArea())
//    biggestSoFar = this one
biggestSoFar = rects[i];
}
return biggestSoFar;
}

{
// This method contains both syntax errors *and* logic errors
//
int howMany = getInt("How many rectangles?");
Rectangle[] rects = new Rectangle[howMany];
for (int i = 0; i < rects.length; i++)
{
int width = getInt("Width of rectangle #"+i+":");
int height = getInt("Height of rectangle #"+i+":");
rects[i] = new Rectangle(width, height);
}
return Rectangle[]; // problem here
}

private static int getInt(String prompt)
{
System.out.print(prompt + " ");
return keyboard.nextInt();
}
}``````

You need to return a variable that holds an array of Rectanges. Rectangle[] isn't a variable. Think now - what is the variable that holds your array of Rectangles?

Thanks i've solved that. I still have problem with

``Rectangle biggest = findBiggest();``

i get an error "findBiggest(Rectangle[]) in BiggestRectangleProgram cannot be applied to ()" Thanks

When you call a method you have to supply parameters that exactly match the parameters listed in the method declaration.
Now look at your declaration of the findBiggest method, then look at your attempted call to that method.

Thanks guys, it helped alot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.