In: Computer Science
Create a program to input the length and width of a rectangle and calculate and print the perimeter and area of the rectangle. To do this you will need to write a Rectangle class and a separate runner class. Your program should include instance variables, constructors, an area method, a perimeter method, a toString method, accessor and mutator methods, and user input. Your runner class should include 3 Rectangle objects. One default rectangle, one coded rectangle, and one user input rectangle. All methods should be tested in the runner class.
//Rectangle.java
public class Rectangle {
int length, width;
public Rectangle() {
length = 0;
width = 0;
}
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public String toString() {
return "Rectangle{" +
"length=" + length +
", width=" + width +
'}';
}
public int area(){
return length*width;
}
public int perimeter(){
return 2*(length+width);
}
}
///////////////////////////////////////////////////////////
//RectangleRunner.java
import java.util.Scanner;
public class RectangleRunner {
public static void main(String[] args) {
Rectangle rectangle1 = new Rectangle();
System.out.println("Area of rectangle 1 = "+rectangle1.area());
System.out.println("Perimeter of rectangle 1 = "+rectangle1.perimeter());
System.out.println();
Rectangle rectangle2 = new Rectangle(2,3);
System.out.println("Area of rectangle 2 = "+rectangle2.area());
System.out.println("Perimeter of rectangle 2 = "+rectangle2.perimeter());
System.out.println();
Scanner scanner = new Scanner(System.in);
int length, width;
System.out.print("Enter length: ");
length = scanner.nextInt();
System.out.print("Enter width: ");
width = scanner.nextInt();
Rectangle rectangle3 = new Rectangle(length, width);
System.out.println("Area of rectangle 3 = "+rectangle3.area());
System.out.println("Perimeter of rectangle 3 = "+rectangle3.perimeter());
System.out.println();
}
}


