Question
I submitted the following to my professor but they came back with additional updates that are required. I've been attempting to work through the list
I submitted the following to my professor but they came back with additional updates that are required. I've been attempting to work through the list but I keep stumbling when it comes to adjusting "most of the coding" from the main to the child classes, and then additional error messages keep popping up when i try to move the } //end of parent, etc. Can you please assist in understanding how the following adjustments should be made to appease the professors requirements?
"Program runs well and is great concept.
There are still issues:
- The } //end of parent should be after the second child to make the two children part of the parent class.
- Remove the line public static class Main { and its closing } from the program. Not necessary and confusing.
- Most of the coding in the main () should be in the appropriate child class,
- Never use single characters (s, r, t) for variables. All variables should be descriptive.
- The first prompt is confusing to most users. There is nothing in the prompt to suggest that the user should enter more than one value on the input line. Normally more than one value on a line is not user-friendly and probably should not be used unless there is a good reason. Like the prompts for the triangle."
***********************
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; } //public abstract double getArea() ;
public double getArea() { return 0; } //methods to override subclasses so that i can instantiate parent class in the main public double getPerimeter() { return 0;};
public String getTriangleType() { return "" ;} } //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 Shape 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 Shape 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
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