Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Please help with Inheritance problem...

Mahnaz51Mahnaz51 United StatesPosts: 1Member

I need help with my program. Here is what I need to do:

Develop the ‘Shape’ application such that: • ‘Rectangle’, ‘Ellipse’, and ‘Triangle’ classes inherit from the ‘Shape’ class.
• Develop the ‘Square’ and ‘Circle’ class where ‘Square’ inherits from ‘Rectangle’ and ‘Circle’ inherits from ‘Ellipse’. ‘Triangle’ has no derived class. • For each class, implement the overridden methods ‘draw’, ‘move’, and ‘erase’. Each method should only have an output statement such as “Rectangle – draw method” that will be displayed when the method is invoked.
• Implement the default constructors for each class with a corresponding message to be displayed when invoked. No initializations are required; that is, the output message will be the only executable statement in the constructors. • Do not implement any other methods for these classes ( i.e., ‘toString’, ‘equals’, getters and setters ). • Implement a ‘ShapeTest’ class which will instantiate an object of each class. • Exercise each of the ‘draw’, ‘move’, and ‘erase’ methods of each class and here is my code: package shape;

abstract class Shape { Shape() { System.out.println("Initialize generic shape parameters"); }

            public abstract void draw();
            public abstract void move();
            public abstract void erase();
        }

        class Rectangle extends Shape
        {
            Rectangle(){
            super();
            System.out.println("Rect-constructor");
        }

        @Override
        public void draw(){ System.out.println("draw rect");}
        @Override
        public void erase(){ System.out.println("erase rect");}
        @Override
        public void move(){ System.out.println("move rect");}

        }

        class Triangle extends Shape
        {
        Triangle(){
        super();
        System.out.println("Tri-constructor");
        }
        @Override
        public void draw(){ System.out.println("draw tri");}
        @Override
        public void erase(){ System.out.println("erase tri");}
        @Override
        public void move(){ System.out.println("move tri");}
        }

        class Ellipse extends Shape
        {
        Ellipse(){
        super();
        System.out.println("Ellipse-constructor");
        }
        @Override
        public void draw(){ System.out.println("draw el");}
        @Override
        public void erase(){ System.out.println("erase el");}
        @Override
        public void move(){ System.out.println("move el");}
        }

        class Square extends Rectangle
        {
        Square(){
        super(); //square parameters of course
        System.out.println("square constructor");
        }

        class Circle extends Ellipse
        {
        Circle(){
        super(); //circ parameters of course
        System.out.println("circ constructor");
        }

        }
        }

      When I run the program I get the run project error:

shape.Shape class wasn't found in Shape project.

Comments

  • sunlotussunlotus Posts: 55Member

    I had no error. This is the ShapeTest class:

    `

    public class ShapeTest {

    public static void main(String[] args) {
    
        Rectangle rect = new Rectangle();
    
        rect.draw();
        rect.move();
        rect.erase();
    
        System.out.println();
    
        Ellipse el = new Ellipse();
    
        el.draw();
        el.move();
        el.erase();
    
        System.out.println();
    
        Triangle tri = new Triangle();
    
        tri.draw();
        tri.move();
        tri.erase();
    
        System.out.println();
    
        Square sq = new Square();
    
        System.out.println();
    
        Circle cir = new Circle();
    }
    

    }`

Sign In or Register to comment.