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[] rects = readRectangles();
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;
}
private static Rectangle[] readRectangles()
{
// 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();
}
}