Question
please use java to program, sample code: //File: Rectangle.jav public class Rectangle { //instance variables private double length; private double width; //Constructor public Rectangle (double
please use java to program,
sample code:
//File: Rectangle.jav
public class Rectangle
{
//instance variables
private double length;
private double width;
//Constructor
public Rectangle (double l, double w)
{
//initialize instance variables using the parameters received in the constructor
length = l;
width = w;
}
//accessor methods
public double getLength ( )
{
return length;
}
public double getWidth ( )
{
return length;
}
//other instance methods
public double computeArea ( )
{
double a=0;
a = length * widthi
return a;
}
public double computePerim ( )
{
double p=0;
//write the code below to compute perimeter
//end code
return p;
}
}
//File: TestRectangle
public class TestRectangle
{
public static void main (String [] args)
{
String in, out;
double l, w;
double area, perim;
//Input length for the first object
//It will be in text form. So, convert it to a double form
in=JOptionPane.showInputDialog ("Enter length");
l=Double.parseDouble (in);
//Input width for the first object
//It is a in a String format. Convert it to double
in=JOptionPane.showInputDialog ("Enter width");
w=Double.parseDouble (in);
//create the first Rectangle object
Rectangle r1 = new Rectangle (l, w);
//Input length for the second object
//It is in String format. Convert it to a double
in=JOptionPane.showInputDialog ("Enter length");
l=Double.parseDouble (in);
//Input width for the second object
//create the second Rectangle object
Rectangle r2 = new Rectangle (l, w);
//Call methods of the first Rectangle object
l = r1.getLength ( );
w= r1.getWidth ( );
area= r1.computeArea ( );
perim= r1.computePerim ( );
//build the output string in variable out
out="First Rectangle: ";
out=out + "Length: " + length + " ";
out= out + "Width: " + width + " ";
out= out + "Area: " + area + " ";
out= out + "Perimeter: " + perim + " ";
//call methods of the second Rectangle objects
//continue building output string
//concatenate the output value relating to second object
out=out+" Second Rectangle: ";
//display the value of variable out in a single dialog box
JOptionPane.showMessageDialog (null, out);
System.exit(0);
}
Description Write a program that will create a class Rectangle. The class will have two private instance variables length and width and a public constructor to initiate their values. It will have accessor (getter) methods for accessing instance variables. It will also have methods for finding area and perimeter. The main task of the program will be to input length and width of two different rectangles and compute their area and perimeter using two objects of the class Rectangle. For this purpose, the program will input length and width of the first rectangle and will create the first object of the class Rectangle using these values. It will input the length and width of the second rectangle and will create the second object of the class Rectangle using these values. Thus, the two Rectangle objects will be created each carrying in it its own length and width values. The program will find area and parameter of the two rectangles by calling computeArea and computePerim methods of each of the two Rectangle objects. Finally, it will display the length, width, area and perimeter of each of the two rectangles
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started