Use ?GRASR to design and implement the following programs (from the end of Chapt
ID: 3712988 • Letter: U
Question
Use ?GRASR to design and implement the following programs (from the end of Chapter 9 in the textbook) and understand what they do Following the instructions in the problem statement, design and implement a Java program for programming exercise 9.1, page 360 (name it Rectangle.java). Next, develop a test program in a separate file (call it TestRectangle.java)to create two Rectangle objects as stated in the problem statement. In addition to creating the two Rectangle objects required by the textbook problem statement, create a Rectangle object (at least one of the three Rectangle objects must be created using the no-arg constructor and at least one must be created using the constructor that has parameters specifying the objects length and width). Do not declare the fields width and height with the private keyword, change and display their values without using mutator or accessor methods. Display the width, height, area, and perimeter of each rectangle object using proper labels for the outputs. Document your code and organize the output using appropriate formatting techniquesExplanation / Answer
Rectangle.java
public class Rectangle {
//Declaring variables
double width;
double height;
//Zero argumented constructor
public Rectangle() {
width = 1;
height = 1;
}
//Parameterized constructor
public Rectangle(double w, double h) {
width = w;
height = h;
}
//This metod will calculate the area of the Rectangle
public double getArea() {
return width * height;
}
//This metod will calculate the perimeter of the Rectangle
public double getPerimeter() {
return 2 * (width + height);
}
}
_________________
TestRectangle.java
public class TestRectangle {
public static void main(String[] args) {
//Creating an instance of Rectangle class
Rectangle r1=new Rectangle();
r1.width=4;
r1.height=40;
//Creating an instance of Rectangle class
Rectangle r2=new Rectangle(3.5,35.9);
//Displaying the Rectangle class info
System.out.println("Rectangle#1");
System.out.println("Width:"+r1.width);
System.out.println("Height:"+r1.height);
System.out.println("Area:"+r1.getArea());
System.out.println("Perimeter:"+r1.getPerimeter());
//Displaying the Rectangle class info
System.out.println(" Rectangle#2");
System.out.println("Width:"+r2.width);
System.out.println("Height:"+r2.height);
System.out.println("Area:"+r2.getArea());
System.out.println("Perimeter:"+r2.getPerimeter());
}
}
__________________
Output:
Rectangle#1
Width:4.0
Height:40.0
Area:160.0
Perimeter:88.0
Rectangle#2
Width:3.5
Height:35.9
Area:125.64999999999999
Perimeter:78.8
______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.