
Patrick B. answered 06/23/20
Math and computer tutor/teacher
import java.io.*;
class Rectangle
{
private double length;
private double width;
private Console console;
public Rectangle ()
{
length=width=0;
console = System.console();
}
public void SetLength( double L) { length=L; }
public void SetWidth( double w) { width =w; }
public double Area() { return(length*width); }
public double Perimeter() { return( 2*length+2*width); }
public void Input()
{
String inbuff;
try
{
System.out.print(" Please input the length of the rectangle :>");
inbuff = console.readLine();
length = Double.parseDouble(inbuff);
System.out.print(" Please input the width of the rectangle :>");
inbuff = console.readLine();
width = Double.parseDouble(inbuff);
}
catch(Exception ex) { System.out.println("Input exception"); }
}
public void Output(String mesgBuff)
{
System.out.println("************************************************");
if (mesgBuff!=null)
{
System.out.println(mesgBuff);
System.out.println("**************************************************");
}
System.out.println(" length = " + length);
System.out.println(" width = " + width);
System.out.println(" area = " + this.Area());
System.out.println(" perimeter = " + this.Perimeter());
}
public static void main(String args[])
{
Console console = System.console();
String inbuff;
int N;
try
{
N=-1;
while (N<1)
{
System.out.print(" How many rectangles do you have ???? :>");
inbuff = console.readLine();
N = Integer.parseInt(inbuff);
}
}
catch (Exception ex)
{
System.out.println(" Fatal input exception");
return;
}
Rectangle RectangleArray[] = new Rectangle[N];
int index_of_max_area = -1;
int index_of_max_perimeter = -1;
double max_area = -1;
double max_perimeter = -1;
for (int iLoop=0; iLoop<N; iLoop++)
{
RectangleArray[iLoop] = new Rectangle();
RectangleArray[iLoop].Input();
System.out.println(" Rectangle # " + (iLoop+1));
double curArea = RectangleArray[iLoop].Area();
double curPerimeter = RectangleArray[iLoop].Perimeter();
if (curArea>max_area)
{
max_area = curArea;
index_of_max_area = iLoop;
}
if (curPerimeter>max_perimeter)
{
max_perimeter = curPerimeter;
index_of_max_perimeter = iLoop;
}
}
String msgBuff = "Rectangle # " + (index_of_max_area+1) + " has max area";
RectangleArray[index_of_max_area].Output(msgBuff);
msgBuff = " Rectange # " + (index_of_max_perimeter+1) + " has max Perimeter";
RectangleArray[index_of_max_perimeter].Output(msgBuff);
}
}
Kojo K.
Thanks a lot. I wish it was in C++. Not so familiar with java.06/23/20