Question

Using Java Write only “stubs” for methods in the Figure, Rectangle, and Triangle classes: Consider a...

Using Java

Write only “stubs” for methods in the Figure, Rectangle, and Triangle classes:

Consider a graphics system that has classes for various figures — say, rectangles, boxes, triangles, circles, and so on. For example, a rectangle might have data members’ height, width, and either a center point or upper-left corner point, while a box and circle might have only a center point (or upper-right corner point) and an edge length or radius, respectively. In a well-designed system, these would be derived from a common class, Figure.

The class Figure is an abstract base class. In this class, create abstract methods for draw, erase, center, equals, and toString.

Add Rectangle and Triangle classes derived from Figure. Each class has stubs for methods erase, draw, and center. In these "stubs", the method simply prints a message telling the name of the class and which method has been called in that class. Because these are just stubs, they do nothing more than output this message. The method center calls the erase and draw methods to erase the object at its current location and redraw the figure at the center of the picture being displayed. Because you have only stubs for erase and draw, center will not do any real “centering” but simply will call the methods erase and draw, which will allow you to see which versions of draw and center it calls. Add an output message in the method center that announces that center is being called. The methods should take no arguments.

For a working system, you would have to replace the definition of each of these methods with code to do the actual drawing.  

Define a demonstration program for your classes which contains main. Test your programs:

  • creating at least two Rectangle objects and two Triangle objects.
    • All instantiated classes have an equals and toString methods because these are abstract in the Figure class.
    • In this part of the homework, equals can simply return true, and toString can returns a String announcing that toString has been invoked.
  • "draw" all of the objects you just created
  • "center" at least one Rectangle and one Triangle object

In main, make sure that you tell the user what's going on at each step.

Homework Answers

Answer #1
Thanks for the question.


Below is the code you will be needing  Let me know if you have any doubts or if you need anything to change.


Here are the 4 classes - Figure.java, Rectangle.java , Triangle.java and Canvas.java


Thank You !!


===========================================================================


public abstract class Figure {

    public abstract void draw();
    public abstract void erase();
    public abstract void center();
    public abstract boolean equals(Figure figure);
    public abstract String toString();


}

========================================================

public class Rectangle extends Figure {

    public Rectangle() {

        System.out.println("Class Triangle, Method: constructor(). Object created");
    }

    @Override
    public void draw() {
        System.out.println("Triangle class, Method: draw(). Drawing the Rectangle");
    }

    @Override
    public void erase() {
        System.out.println("Triangle class, Method: erase(0. Currently deleting the figure.)");
    }

    @Override
    public void center() {
        System.out.println("Triangle class, Method: center(). We are centering the image");
        erase();
        draw();
    }

    @Override
    public boolean equals(Figure figure) {
        System.out.println("Triangle class, Method: equals(). We are checking two objects are same or not");
        return true;
    }

    @Override
    public String toString() {
        return "Triangle class , Method : toString()";
    }
}

========================================================

public class Triangle extends Figure {


    public Triangle() {

        System.out.println("Class Triangle, Method: constructor(). Object created");
    }

    @Override
    public void draw() {
        System.out.println("Triangle class, Method: draw(). Drawing the Triangle");
    }

    @Override
    public void erase() {
        System.out.println("Triangle class, Method: erase(0. Currently deleting the figure.)");
    }

    @Override
    public void center() {
        System.out.println("Triangle class, Method: center(). We are centering the image");
        erase();
        draw();
    }

    @Override
    public boolean equals(Figure figure) {
        System.out.println("Triangle class, Method: equals(). We are checking two objects are same or not");
        return true;
    }

    @Override
    public String toString() {
        return "Triangle class , Method : toString()";
    }
}

========================================================

public class  Canvas{


    public static void main(String[] args) {

        Figure figures [] = new Figure[4];
        figures[0] = new Rectangle();
        figures[1] = new Rectangle();
        figures[2] = new Triangle();
        figures[3] = new Triangle();


        for(Figure fig:figures){
            System.out.println(fig);
        }

        figures[0].draw();
        figures[3].draw();

        figures[1].center();
        figures[2].center();


    }


}

========================================================

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following...
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following 4 fields: int x (x-coordinate for its top left corner) int y (y coordinate for its top left corner) double height (height of the rectangle) double width (width of the rectangle) Your rectangle objects should have the following methods: public Rectangle(int newx, int newy, int newwidth, int newheight) Constructor that initializes the x-coordinate, y-coordinate for the top left corner and its width and height....
(Use JAVA) For this program, you need to create three classes, one of which is a...
(Use JAVA) For this program, you need to create three classes, one of which is a main class that includes aggregation from the other two classes. Each of the classes must include at least two fields, multiple constructors, accessor and mutator methods, and toString. Methods in your main class must include: 1. .equals 2. .copy 3. One that passes an object other than the .copy method 4. One that returns an object 5. toString that calls the toStrings from the...
Java code Problem 1. Create a Point class to hold x and y values for a...
Java code Problem 1. Create a Point class to hold x and y values for a point. Create methods show(), add() and subtract() to display the Point x and y values, and add and subtract point coordinates. Tip: Keep x and y separate in the calculation. Create another class Shape, which will form the basis of a set of shapes. The Shape class will contain default functions to calculate area and circumference of the shape, and provide the coordinates (Points)...
In this assignment, you will be building upon the work that you did in Lab #5A...
In this assignment, you will be building upon the work that you did in Lab #5A by expanding the original classes that you implemented to represent circles and simple polygons. Assuming that you have completed Lab #5A (and do not move on to this assignment unless you have!), copy your Circle, Rectangle, and Triangle classes from that assignment into a new NetBeans project, then make the following changes: Create a new Point class containing X and Y coordinates as its...
in JAVA Write a class Store which includes the attributes: store name and sales tax rate....
in JAVA Write a class Store which includes the attributes: store name and sales tax rate. Write another class encapsulating a Yarn Store, which inherits from Store. A Yarn Store has the following additional attributes: how many skeins of yarn are sold every year and the average price per skein. Code the constructor, accessors, mutators, toString and equals method of the super class Store and the subclass Yarn Store; In the Yarn Store class, also code a method returning the...
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write...
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method in it. The following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassenges, isConvertable, numberSeats, maxWeightLoad, and numberAxels. Characteristics that are applicable to all vehicles should be instance variables in the Vehicle class. The others should be in the class(s) where they belong. Class vehicle should have...
MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...
MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms. Detailed specifications: Define an...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is the length times the...
Java Programming In this lab students continue to write programs using multiple classes. By the end...
Java Programming In this lab students continue to write programs using multiple classes. By the end of this lab, students should be able to Write classes that use arrays and ArrayLists of objects as instance variables Write methods that process arrays and ArrayLists of objects Write getter and setter methods for instance variables Write methods using object parameters and primitive types Question- class StringSet. A StringSet object is given a series of up to 10 String objects. It stores these...
Using Java Create a class named Movie that can be used in your video rental business....
Using Java Create a class named Movie that can be used in your video rental business. The Movie class should track the Motion Picture Association of America (MPAA) rating (e.g., Rated G, PG-13, R), ID Number, and movie title with appropriate accessor and mutator methods. Also create an equals() method that overrides Object ’s equals() method, where two movies are equal if their ID number is identical. Next, create three additional classes named Action, Comedy, and Drama that are derived...