Question

In: Computer Science

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. In this homework assignment, you will implement such a system.

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.

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.

Your output should consist primarily of messages from your stub methods. An example of the output from your program might be the following (note: output from main method is shown below in italics):

  ** Create 2 triangles **
  Entering Figure Constructor for triangle 0
  Running Triangle constructor for triangle 0
  Entering Figure Constructor for triangle 1
  Running Triangle constructor for triangle 1

  ** Create 2 rectangles **
  Entering Figure Constructor for rectangle 0
  Running Rectangle constructor for rectangle 0
  Entering Figure Constructor for rectangle 1
  Running Rectangle constructor for rectangle 1

  ** Draw both triangles **
  Entering draw() method for Triangle 0
  Entering draw() method for Triangle 1

  ** Draw both rectangles **
  Entering draw() method for Rectangle 0
  Entering draw() method for Rectangle 1

  ** Center one triangle **
  Entering center() method for Triangle 0
  center() calling erase()
  Entering erase() method for Triangle 0>
  center() calling setCenter() to reset center.  
  Entering Figure's setCenter() for figure 0
  center() calling draw()
  Entering draw() method for Triangle 0

  ** Center one rectangle **
  Entering center() method for Rectangle 1
  center() calling erase()
  Entering erase() method for Rectangle 1
  center() calling setCenter()
  Entering Figure's setCenter() for figure 1
  center() calling draw()
  Entering draw() method for Rectangle 1

Solutions

Expert Solution

Program

abstract class Figure
{
   Figure(String s)
   {
       System.out.println("Entering Figure Constructor for " + s);
   }
   abstract void erase();
   abstract void draw();
   abstract void center();
   abstract boolean equals();
   abstract String tostring();
  
   void setCenter()
   {
       System.out.println("Entering Figure's setCenter() for figure");
   }
}

class Rectangle extends Figure
{
   double height, width;
   int n;
  
   Rectangle(int i)
   {
       super("rectangle " + i);
       n = i;
       System.out.println("Entering Rectangle Constructor for rectangle " + i);
   }

   void draw()
   {
       System.out.println("Entering draw() method for Rectangle " + n);
   }
  
   void erase()
   {
       System.out.println("Entering erase() method for Rectangle " + n);
   }
      
   void center()
   {
       System.out.println("Entering center() method for Rectangle " + n);
      
       System.out.println("center() calling erase()");
      
       erase();
      
       System.out.println("center() calling setCenter()");
      
       setCenter();
      
       System.out.println("center() calling draw()");

       draw();
   }
  
   boolean equals()
   {
       return true;
   }
  
   String tostring()
   {
       return "toString() has been revoked";
   }
}

class Triangle extends Figure
{
   double radius;
   int n;
  
   Triangle(int i)
   {
       super("triangle " + i);
       n = i;
       System.out.println("Entering Triangle Constructor for triangle " + i);
   }
  
   void draw()
   {
       System.out.println("Entering draw() method for Triangle " + n);
   }
  
   void erase()
   {
       System.out.println("Entering erase() method for Triangle " + n );
   }
  
   void setCenter()
   {
       System.out.println("Entering setCenter() method for Triangle");
   }
  
   void center()
   {
       System.out.println("Entering center() method for Triangle " + n);
      
       System.out.println("center() calling erase()");
      
       erase();
      
       System.out.println("center() calling setCenter() for reset center.");
      
       setCenter();
      
       System.out.println("center() calling draw()");

       draw();
   }
  
   boolean equals()
   {
       return true;
   }
  
   String tostring()
   {
       return "toString() has been revoked";
   }
}

class Driver
{
   public static void main (String[] args)
   {
       System.out.println("\n** Create 2 triangles **");
       Triangle t1 = new Triangle(0);
       Triangle t2 = new Triangle(1);

       System.out.println("\n** Create 2 rectangles **");      
       Rectangle r1 = new Rectangle(0);
       Rectangle r2 = new Rectangle(1);
      
       System.out.println("\n** Draw both triangles **");
       t1.draw();
       t2.draw();
      
       System.out.println("\n** Draw both rectangles **");
       r1.draw();
       r2.draw();
      
       System.out.println("\n** Center one triangle **");
       t1.center();
      
       System.out.println("\n** Center one rectangle **");
       r2.center();  
   }
}

Output:

** Create 2 triangles **
Entering Figure Constructor for triangle 0
Entering Triangle Constructor for triangle 0
Entering Figure Constructor for triangle 1
Entering Triangle Constructor for triangle 1

** Create 2 rectangles **
Entering Figure Constructor for rectangle 0
Entering Rectangle Constructor for rectangle 0
Entering Figure Constructor for rectangle 1
Entering Rectangle Constructor for rectangle 1

** Draw both triangles **
Entering draw() method for Triangle 0
Entering draw() method for Triangle 1

** Draw both rectangles **
Entering draw() method for Rectangle 0
Entering draw() method for Rectangle 1

** Center one triangle **
Entering center() method for Triangle 0
center() calling erase()
Entering erase() method for Triangle 0
center() calling setCenter() for figure
Entering setCenter() method for Triangle
center() calling draw()
Entering draw() method for Triangle 0

** Center one rectangle **
Entering center() method for Rectangle 1
center() calling erase()
Entering erase() method for Rectangle 1
center() calling setCenter()
Entering Figure's setCenter() for figure
center() calling draw()
Entering draw() method for Rectangle 1


Related Solutions

(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4)...
(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4) Circle 5) TestAllShapes (create 1 object of each type and print the Area for each of them.)
Write a program in java which is in main and uses no classes, methods, loops or...
Write a program in java which is in main and uses no classes, methods, loops or if statements Ask the user to enter their first name Ask the user to enter their last name Print their initials followed by periods (ie. Clark Kent = C. K.) Print the number of letters in their last name
Using Java Write the class RecursiveProbs, with the methods listed below. Write all the methods using...
Using Java Write the class RecursiveProbs, with the methods listed below. Write all the methods using recursion, NOT LOOPS. You may use JDK String Methods like substring() and length(), but do not use the JDK methods to avoid coding algorithms assigned. For example, don’t use String.revers(). public boolean recursiveContains(char c, String s) { if (s.length() == 0) return false; if (s.charAt(s.length() - 1) == c) return true; else return recursiveContains(c, s.substring(0, s.length() - 1)); } public boolean recursiveAllCharactersSame(String s) return...
Java Write a class called Triangle that can be used to represent a triangle. Write a...
Java Write a class called Triangle that can be used to represent a triangle. Write a class called Describe that will interface with the Triangle class The Server • A Triangle will have 3 sides. It will be able to keep track of the number of Triangle objects created. It will also hold the total of the perimeters of all the Triangle objects created. • It will allow a client to create a Triangle, passing in integer values for the...
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The...
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The program to present the user with a menu where one of the shapes can be selected. Based on the selection made, the user enters the proper input, the program validates the input (i.e all entries must be greater than zero). Once the input is entered and validated, the intended area is calculated and the entered information along with the area are displayed. Area of...
Using classes and array data structure write methods with algorithms for a software that an airline...
Using classes and array data structure write methods with algorithms for a software that an airline can use to view available/booked seats, management booking, canceling booking and reorder seats. The solution should consist of a minimum of two classes with one class containing the main method and second class for managing a manipulating data. The choice of data structure for this assignment will be static one dimension arrays. in C++
write a java program for area and perimeter of right triangle
write a java program for area and perimeter of right triangle
Write the methods that insert and remove an element at the kth position in Java using...
Write the methods that insert and remove an element at the kth position in Java using recursion (NOT iteration) (Hint for the remove method: we have to recursive position -1 times, and each time we do the recursion, we have to create a method to move the head to the right) public void insertRecursive(int position, int data) { //enter code here } public int removeAtRecursive(int position) { //enter code here } Here is the given class for the Node: public...
Use Java to: 1. Write a method with a Rectangle and a Point as parameters. Without...
Use Java to: 1. Write a method with a Rectangle and a Point as parameters. Without using methods from the Rectangle class, return true if the point is inside the rectangle and false otherwise. 2. Write a second method with the same functionality as Exercise 1. However, use a method from the Rectangle class this time. 3. Normally, the == operator cannot be used to compare two strings for equality. There are 2 main exceptions we talked about. The first...
Write code in java using the LinkedList connecting two different classes. For example, Employee and EmployeeList...
Write code in java using the LinkedList connecting two different classes. For example, Employee and EmployeeList are two different classes that are used to create the assignment.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT