In: Computer Science
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods you write above.
public abstract class Shape { abstract double getArea(); }
public class Rectangle extends Shape{ private int width; private int height; public Rectangle(int width, int height) { this.width = width; this.height = height; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } double getArea() { return width*height; } public String toString() { return "Rectangle{" + "width=" + width + ", height=" + height + '}'; } }
import java.util.Scanner; public class TestCode { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter width: "); int width = scanner.nextInt(); System.out.print("Enter height: "); int height = scanner.nextInt(); Rectangle rectangle = new Rectangle(width, height); System.out.println("Width = "+rectangle.getWidth()); System.out.println("Height = "+rectangle.getHeight()); System.out.println("Area = "+rectangle.getArea()); System.out.println(rectangle); } }