Помогите мне исправить код.
Привет, кодеры. У меня тут есть несколько кодов. Я попытался решить этот вопрос в книге "как программировать Java". Я суммировал этот вопрос ниже:
You are required to implement the following design as well as a main() method in another class to test your implementation: Implement the hierarchy below where - MyShape is an abstract class with an abstract Draw method, - MyBoundedShape is an abstract class with an abstract GetArea method, - MyLine, MyOval, MyRectangle are concrete classes In the main() method, - Ask the user to select 5 shapes and input their dimension - Draw selected shapes - Compute and show the area of selected shapes if they are a bounded shape
Когда я запустил код, он не показывал область или вычисленную область. Помоги мне это исправить. Кстати, спасибо.
Что я уже пробовал:
Класса MyShape
public abstract class MyShape { public abstract void Draw(); }
Класс функцию myline
public class Myline extends MyShape { private int length; public Myline (int length) { length=0; } public void setlength( int length ) { length = 0; } public int getlength() { return length; } public void Draw() { System.out.printf("Drawing a line with the length",getlength()); } }
Класс Myextendedshape
public abstract class MyextendedShape extends MyShape { protected double area; public abstract double getArea(); }
Класс Myoval
public class Myoval extends MyextendedShape { private double Line1; private double Line2; public void Draw() { System.out.println("I am drawing a Oval"); } Myoval() { Line1= 0.0; Line2 = 0.0; } Myoval(double Line1, double Line2){ this.Line1 =Line1; this.Line2 = Line2; } public double getLine1() { return Line1; } public void setLine1(double Line1) { this.Line1 = Line1; } public double getLine2() { return Line2; } public void setLine2(double Line2) { this.Line2 = Line2; } @Override public double getArea() { return calculateArea(); } private double calculateArea(){ return area = 3.14*Line1 * Line2; } public String toString(){ return "The Line number 1 of the oval is: " + Line1 + " and the Line number 2 is: " + Line2 + ", " + "and the area is: " + getArea(); } }
Мой класс прямоугольника
public class MyRectangle extends MyextendedShape { private double length, width; public void Draw() { System.out.println("I am drawing a Rectangle"); } MyRectangle() { length= 0.0; width = 0.0; } MyRectangle(double length, double width){ this.length =length; this.width = width; } public double getLenght() { return length; } public void setLength(double length) { this.length = length; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } @Override public double getArea() { return calculateArea(); } private double calculateArea(){ return area = width * length; } public String toString(){ return "The width of the rectangle is: " + width + " and the length is: " + length + ", " + "and the area is: " + getArea(); } }
Тест
public class test { public static void main(String [] args) { MyShape s = new Myline(6); s.Draw(); s = new Myoval(4.0, 5.0); s.Draw(); s = new MyRectangle(4.0,6.0); s.Draw(); } }