Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The following program needs to be reworked to meet inheritance requirement. Most of the coding in the main should be either in the parent or

The following program needs to be reworked to meet inheritance requirement. Most of the coding in the main should be either in the parent or the children. Only the parent class should be instantiated in the main():

package Wk3AfetseM;

import java.util.Scanner;

public class Wk3AfetseM { //parent class public static class Shape { //attribute private String name; //constructors public Shape() { this.name = "We are working with 2D Shapes!";; } public Shape(String name) { this.name = name; } //get method public String getName() { return this.name; } //set method public void setName(String name) { this.name = name; } } //end of parent

//subclass one public static class Rectangle extends Shape { //attributes private double length; private double breadth; //constructor public Rectangle(double length, double breadth) { //set method shape name "Rectangle" by calling super class super("Rectangle"); //set length and breadth this.length = length; this.breadth = breadth; }

//find area of rectangle public double getArea() { return length * breadth; } //find perimeter of rectangle public double getPerimeter() { return 2.0 * (length + breadth); } } //end of subclass one

//subclass two public static class Triangle extends Shape { //attribute private double side1; private double side2; private double side3; //constructor public Triangle(double side1, double side2, double side3) { //set method super("Triangle"); //set side this.side1 = side1; this.side2 = side2; this.side3 = side3; } //find area of a triangle public double getArea() { double perimeter = side1 + side2 + side3; double s = perimeter/2; double area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3)); return area; } //find perimeter of triangle public double getPerimeter() { double perimeter = side1 + side2 + side3; return perimeter; } public String getTriangleType() { if(side1 == side2 && side2 == side3) { return "Equilateral"; } else if(side1 == side2 || side2 == side3 || side1 == side3) { return "Isosceles"; } else { return "Scalene"; } } }// end of subclass two public static class Main { public static void main(String[] args) { //create scanner class to request input from user Scanner input = new Scanner(System.in); //create object for Parent class Shape s = new Shape(); //call the getName() method of Parent System.out.println(s.getName()); //prompts the user to enter length and breadth of rectangle System.out.println(" Please enter the length and breadth of a Rectangle in cm: "); double length, breadth; //read response from user length = input.nextDouble(); breadth = input.nextDouble(); //create child class object for rectangle Rectangle r = new Rectangle(length,breadth); //call the getName() method inherited from parent System.out.println(" Name: " + r.getName()); //Call the getArea() and getPerimeter() methods of rectangle class System.out.println("Area: " + r.getArea() + " cm"); System.out.println("Perimeter: " + r.getPerimeter()+ " cm");

//prompts the user to enter side of triangle System.out.println(" Please enter side 1 of a Triangle in cm: "); double side1 = input.nextDouble(); System.out.println(" Please enter side 2 of a Triangle in cm: "); double side2 = input.nextDouble(); System.out.println(" Please enter side 3 of a Triangle in cm: "); double side3 = input.nextDouble(); //create child class object for triangle Triangle t = new Triangle(side1, side2, side3); //call the getName() method inherited from parent System.out.println(" Name: " + t.getName()); //call the getArea() and getPerimeter() methods of triangle class System.out.println("Area: " + t.getArea() + " cm"); System.out.println("Perimeter: " + t.getPerimeter() + " cm"); System.out.println("Triangle Type: "+t.getTriangleType()); System.out.println(" Thank You for working with 2d Shapes!"); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java How To Program Late Objects Version

Authors: Paul Deitel, Deitel & Associates

8th Edition

0136123716, 9780136123712

More Books

Students also viewed these Programming questions