Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello coders. I have a few codes here. I tried to solve the question in the How to program Java book. I summarized the question below:

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

When I ran the code, it didn't show the area or the calculated area. Help me to fix this. Thanks btw.

What I have tried:

MyShape class
public abstract class MyShape 
{
	public abstract void Draw();
	

}

Myline class
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 class
public abstract class MyextendedShape extends MyShape 
{
	protected double area;
	public abstract double getArea();

}

Myoval class
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();
    }

}

My rectangle class
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();
    }

}

Test
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();

	    }
}
Posted
Updated 28-Oct-18 18:40pm
v2

1 solution

Look at two things:
1) The error message.
Cannot instantiate the type MyRectangle

And
2) The class itself.
Java
public abstract class MyRectangle extends MyextendedShape {
It's declared as an abstract class which specifically means "you cannot create an instance of this class, only of concrete derived classes".

Think of it as cars for a moment: Car is an abstract concept - you can't own a "Car", you own a "Mercedes" or a "Ford". More specifically, you own a "Mercedes"."AClass"."A180 CDI", or a "Ford"."Fiesta"."Zetec 1.1"
"A180 CDI" and "Zetec 1.1" are the concrete classes which derive from the abstract "Mercedes" and "Ford" classes, both of which are derived from the abstract "Car" class.

You can only instantiate concrete classes, not abstract ones - so your app will not compile and that means it doesn't produce an executable, so none of your changes are included in the EXE you find on disk and can execute!
Remove the keyword abstract and see if it compiles cleanly - until it does, you can't run it!
 
Share this answer
 
Comments
Member 14025358 29-Oct-18 0:37am    
Hello, a few days ago, you help me to fix the error message, it worked and thank you. But my main problem is that: In the code, you clearly see I create a class is calculateArea, but in the end, it does not show anything about calculateArea, if you can help me another round, it will be great.
OriginalGriff 29-Oct-18 3:00am    
So post a new question including the relevant code fragments from your current code, and explain what you do to get it, exactly what problem you get, and what you have tried to do to fix it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900