Question

Step One The most general object in this hierarchy is the Shape. Begin by creating a...

  1. Step One

    The most general object in this hierarchy is the Shape. Begin by creating a class for a Shape. It will have data members xand y, which define the upper left-hand corner of the console where the Shape will be drawn.  For example is x is 5 and y is 7, the upper left-hand corner of the Shape is 5 spaces from the left margin and 7 spaces from the top margin. Due to the limited size of the standard console window, plus leaving a little room on the bottom, ensure that 0 <= x <= 20, 0 <= y <= 10.If your constructor parameter violates these bounds, set it to 0.

    Your Shape class will also need a boolean data member called filled, to indicate whether the Shape should be filled or hollow when drawn.

    Provide a default constructor and a constructor that takes parameters for x, y, and filled. Your Shape class will need a draw function member. Since your Shape class has no specific shape, its specific drawing behavior is undefined, and needs to be redefined for any inherited object types so this should be an abstract method. Because Shape has an abstract method, Shape will be an abstract class.

    Step Two

    Create a Rectangle class that inherits from Shape. In addition to the class members of Shape, a Rectangle needs to know its height (0 <  height, y + height <= 40) and width (0 < width, x + width <= 60). Provide a default constructor that sets height and width to 1, and a constructor that takes parameters for x, y, filled, height and width(utilize the Shape constructor). The Rectangle will, of course, need to override the draw method that it inherits from Shape. Draw your rectangle with asterisks. Your rectangle should be drawn with its upper left-hand corner at position x, y in the console. So begin by printing y blank lines.  Then each row in your Rectangle should be indented x spaces.  

    Step Three

    Create a VerticalLine class that inherits from Rectangle. A VerticalLine differs from a Rectangle in that its width must be 1 and it is (trivially) filled. So the constructor only needs x, y, and height (0 <  height, y + height <= 40). Provide appropriate methods.

    .

    Step Four

    Create a HorizontalLine class that inherits from Rectangle. A HorizontalLine differs from a Rectangle in that its height must be 1 and it is (trivially) filled. So the constructor only needs x, y, andwidth (0 < width, x + width <= 60).  Provide appropriate methods.

    Step Five                                                                                                                                 

    Create a Triangle class that inherits from Shape.  See the example output for how to draw a triangle. You will only need to get the width (0 < width, x + width <= 60).  Additionally, width must be odd (it will be easier to draw), so enforce this. The constructor will need x, y, filled, and width. Provide appropriate methods.

    Step Six

    For this assignment, I will provide a main program, but you will need to add exactly 5 lines of code to this main program.  See the comments numbered 1 to 5. Do not modify the main program in any other way.  Specifically, do not add any additional variables.

    Notes

  2. For this exercise, the purpose of the class methods is to write to the screen, so this is a situation in which System.out.print statements are appropriate to have in your methods.
  3. Utilize methods of the superclass wherever possible to avoid duplication of code. No subclass should perform any work that the superclass can do.
  4. You do not need to provide any getters or setters for this homework.
  5. You do need to provide JavaDoc comments.
  6. You do not need to create a package for this assignment.

The main program

import java.util.Scanner;

/**

* This is a program that tests our Shape heirarchy of classes

*

*/

public class TestInheritance

{

    

    /**

     * This is the main program

     *

     * @param  args  the command line arguments

     */

    public static void main (String[] args)

    {

        Shape myShape;

        int x=0, y=0, h=1, w=1, f;

        boolean fillIt=true;

        int choice;

        Scanner in = new Scanner(System.in);

        do {

            System.out.println ("0) quit program");

            System.out.println ("1) draw a rectangle");

            System.out.println ("2) draw a horizontal line");

            System.out.println ("3) draw a vertical line");

            System.out.println ("4) draw a triangle");

            choice = in.nextInt();

            

            if (choice == 1 || choice == 2 || choice == 3 || choice == 4) {

                System.out.println ("Enter x value of upper left corner: ");

                x = in.nextInt();

                System.out.println ("Enter y value of upper left corner: ");

                y = in.nextInt();

                if (choice == 1 || choice == 2 || choice == 4) {

                    System.out.println ("Enter the width you would like: ");

                    w = in.nextInt();

                }

                if (choice == 1 || choice == 3) {

                    System.out.println ("Enter the height you would like: ");

                    h = in.nextInt();

                }

                if (choice == 1 || choice == 4) {

                    System.out.println ("Enter 1 if you want it filled, 0 otherwise: ");

                    f = in.nextInt();

                    fillIt = f == 1;

                }

               

                if (choice == 1) {

                    // 1) Write the statement to create a Rectangle

                }

                else if (choice == 2) {

                    // 2) Write the statement to create a HorizontalLine

                }

                else if (choice == 3) {

                    // 3) Write the statement to create a VerticalLiine

                }

                else {

                    // 4) Write the statement to create a Triangle

                   

                }

                // 5) Write the statement here to draw the shape

               

            }

            

            

        } while (choice != 0);

    }

}

Example output

0) quit program

1) draw a rectangle

2) draw a horizontal line

3) draw a vertical line

4) draw a triangle

1

Enter x value of upper left corner: 5

Enter y value of upper left corner: 3

Enter the width you would like: 15

Enter the height you would like: 3

Enter 1 if you want it filled, 0 otherwise: 1

     ***************

     ***************

     ***************

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//step one

public abstract class Shape {
   int x;
   int y;
   boolean filled;

   public Shape() {
       super();
   }

   public Shape(int x, int y, boolean filled) {
       super();
       if (x >= 20 || x <= 0) {
           this.x = 0;
       } else {
           this.x = x;
       }
       if (y >= 10 || y <= 0) {
           this.y = 0;
       } else {

           this.y = y;
       }
       this.filled = filled;
   }

   public abstract void draw();

}

//step two

public class Rectangle extends Shape {

   int height;
   int width;

   public Rectangle(int x, int y, boolean filled, int height, int width) {
       super();
       if (x >= 20 || x <= 0) {
           this.x = 0;
       } else {
           this.x = x;
       }
       if (y >= 10 || y <= 0) {
           this.y = 0;
       } else {

           this.y = y;
       }
       this.filled = filled;
       if ((height + y) >= 40 || height <= 0) {
           this.height = 0;
       } else {
           this.height = height;
       }
       if ((width + x) >= 60 || width <= 0) {
           this.width = 0;
       } else {
           this.width = width;
       }
   }

   public Rectangle() {
       super();
       this.height = 1;
       this.width = 1;
   }

  

@Override
   public void draw() {
       for (int i = 0; i < y; i++) {
           System.out.println();
       }

       if (filled==true) {
           for (int n = 0; n < height; n++) {

               for (int m = 0; m < (x + width); m++) {
                   if (m < x) {
                       System.out.print(" ");
                   } else {
                       System.out.print("*");
                   }
               }
               System.out.println();
           }
       } else {
           for (int n = 0; n < height; n++) {

               for (int m = 0; m < (x + width); m++) {
                   if (m < x) {
                       System.out.print(" ");
                   } else {
                       if(n == 0|| n == height-1||m == 0|| m == (x + width)-1)
                       System.out.print("*");
                       else
                           System.out.print(" ");
                   }
               }
               System.out.println();
           }
       }
   }

}


//step three

public class VerticleLine extends Rectangle {

   public VerticleLine() {
       super();
   }

   public VerticleLine(int x, int y, int height) {

       if (x >= 20 || x <= 0) {
           this.x = 0;
       } else {
           this.x = x;
       }
       if (y >= 10 || y <= 0) {
           this.y = 0;
       } else {

           this.y = y;
       }
       if ((height + y) >= 40 || height <= 0) {
           this.height = 0;
       } else {
           this.height = height;
       }
   }

   @Override
   public void draw() {

       for (int i = 0; i < y; i++) {
           System.out.println();
       }

       for (int n = 0; n < height; n++)

       {
           for (int m = 0; m < (x + 1); m++) {
               if (m < x) {
                   System.out.print(" ");
               } else {
                   System.out.print("*");
               }
           }
           System.out.println();
       }
   }

}

//step Four

public class HorizonatalLine extends Rectangle {

   public HorizonatalLine() {
       super();
   }

   public HorizonatalLine(int x, int y, int width) {
       if (x >= 20 || x <= 0) {
           this.x = 0;
       } else {
           this.x = x;
       }
       if (y >= 10 || y <= 0) {
           this.y = 0;
       } else {

           this.y = y;
       }
       if ((width + x) >= 60 || width <= 0) {
           this.width = 0;
       } else {
           this.width = width;
       }
   }
   @Override
   public void draw() {
       for(int i=0;i<y;i++)
       {
           System.out.println();
       }

       for(int m=0;m<(x+width);m++)
       {
               if(m<x)
               {
                   System.out.print(" ");
               }else
               {
                   System.out.print("*");
               }
       }
       System.out.println();
   }

}

//step Five

public class Triangle extends Shape {

   int width;

   public Triangle(int x, int y, boolean filled, int width) {
       super();
       if (x >= 20 || x <= 0) {
           this.x = 0;
       } else {
           this.x = x;
       }
       if (y >= 10 || y <= 0) {
           this.y = 0;
       } else {

           this.y = y;
       }
       if ((width + x) >= 60 || width <= 0 || (width % 2 == 0)) {
           this.width = 0;
       } else {
           this.width = width;
       }
       this.filled = filled;
   }

@Override
   public void draw() {
       for (int i = 0; i < y; i++) {
           System.out.println();
       }

       if (filled==true) {
           for (int n = 0; n < (x + width); n++)

           {
               for (int m = n; m >= 0; m--) {
                   if (m < x) {
                       System.out.print(" ");
                   } else {

                       System.out.print("*");

                   }
               }
               System.out.println();
           }
       } else {
           for (int n = 0; n < (x + width); n++)

           {
               for (int m = n; m >= 0; m--) {
                   if (m < x) {
                       System.out.print(" ");
                   } else {
                       if (n == 0 || n == (x + width) - 1 || m == n || m == (0))
                           System.out.print("*");
                       else
                           System.out.print(" ");

                   }
               }
               System.out.println();
           }
       }
   }

}


//step Six

import java.util.Scanner;

public class TestInheritance

{ /**

* This is the main program

*

* @param args the command line arguments

*/
public static void main (String[] args)
{
Shape myShape;

int x=0, y=0, h=1, w=1, f;

boolean fillIt=true;

int choice;

Scanner in = new Scanner(System.in);

do {

System.out.println ("0) quit program");

System.out.println ("1) draw a rectangle");

System.out.println ("2) draw a horizontal line");

System.out.println ("3) draw a vertical line");

System.out.println ("4) draw a triangle");

choice = in.nextInt();

if (choice == 1 || choice == 2 || choice == 3 || choice == 4) {

System.out.println ("Enter x value of upper left corner: ");

x = in.nextInt();

System.out.println ("Enter y value of upper left corner: ");

y = in.nextInt();

if (choice == 1 || choice == 2 || choice == 4) {

System.out.println ("Enter the width you would like: ");

w = in.nextInt();

}

if (choice == 1 || choice == 3) {

System.out.println ("Enter the height you would like: ");

h = in.nextInt();

}

if (choice == 1 || choice == 4) {

System.out.println ("Enter 1 if you want it filled, 0 otherwise: ");

f = in.nextInt();

fillIt = f == 1;

}

  
if (choice == 1) {
// 1) Write the statement to create a Rectangle


myShape=new Rectangle(x, y, fillIt, h, w);
  

}

else if (choice == 2) {
// 2) Write the statement to create a HorizontalLine


   myShape=new HorizonatalLine(x, y, w);
}

else if (choice == 3) {
// 3) Write the statement to create a VerticalLiine


   myShape=new VerticleLine(x, y, h);
}

else {
// 4) Write the statement to create a Triangle


   myShape=new Triangle(x, y, fillIt, w);

}

myShape.draw();

}

} while (choice != 0);

}
}

Add a comment
Know the answer?
Add Answer to:
Step One The most general object in this hierarchy is the Shape. Begin by creating a...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST...

    JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST CASES(WHAT IM TRYING TO PRODUCE) BELOW THOSE IMAGES ARE THE .JAVA FILES THAT I HAVE CREATED. THESE ARE GeometircObject.Java,Point.java, and Tester.Java. I just need help making the Rectangle.java and Rectangle2D.java classes. GeometricObject.Java: public abstract class GeometricObject { private String color = "white"; // shape color private boolean filled; // fill status protected GeometricObject() { // POST: default shape is unfilled blue this.color = "blue";...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • Create a program that calculates the area of various shapes. Console Specifications Create an abstract class...

    Create a program that calculates the area of various shapes. Console Specifications Create an abstract class named Shape. This class should contain virtual member function named get_area() that returns a double type. Create a class named Circle that inherits the Shape class and contains these constructors and member functions: Circle(double radius) double get_radius() void set_radius(double radius) double get_area() Create a class named Square that inherits the Shape class and contains these constructors and member functions: Square(double width) double get_width() void...

  • I am trying to write a Geometry.java program but Dr.Java is giving me errors and I...

    I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. import java.util.Scanner; /** This program demonstrates static methods */ public class Geometry { public static void main(String[] args) { int choice; // The user's choice double value = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...

  • JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to...

    JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to maintain a list of shapes drawn as follows: Replace and upgrade all the AWT components used in the programs to the corresponding Swing components, including Frame, Button, Label, Choice, and Panel. Add a JList to the left of the canvas to record and display the list of shapes that have been drawn on the canvas. Each entry in the list should contain the name...

  • Description Create an object-oriented program that uses inheritance to perform calculations on a rectangle or a...

    Description Create an object-oriented program that uses inheritance to perform calculations on a rectangle or a square. Sample Output (Your output should be similar to the text in the following box) Rectangle Calculator Rectangle or square? (r/s): r Height: 5 Width: 10 Perimeter: 30 Area: 50 Continue? (y/n): y Rectangle or square? (r/s): s Length: 5 Perimeter: 20 Area: 25 Continue? (y/n): n Thank you for using my app Specifications Use a Rectangle class that provides attributes to store the...

  • please do in java and comments the code so i can understand Requirements: Create a Java...

    please do in java and comments the code so i can understand Requirements: Create a Java class named “MyRectangle2D.java”. Your class will have double two variables named x and y. These will represent the center point of your rectangle. Your class will have two double variables named width and height. These will represent the width and height of your rectangle. Create getter and setter methods for x, y, width, and height. Create a “no argument” constructor for your class that...

  • create a program shape.cpp that uses classes point.h and shape.h. The program should compile using the...

    create a program shape.cpp that uses classes point.h and shape.h. The program should compile using the provided main program testShape.cpp. the provided programs should not be modified. Instructions ar egiven below. Shape class The Shape class is an abstract base class from which Rectangle and Circle are derived. bool fits_in(const Rectangle& r) is a pure virtual function that should return true if the Shape fits in the Rectangle r. void draw(void) is a pure virtual function that writes the svg...

  • Design a general class GeometricObject can be used to model all geometric objects. This class contains...

    Design a general class GeometricObject can be used to model all geometric objects. This class contains the properties color and filled and their appropriate get and set methods. Assume that this class also contains toString() methods. The toString() method returns a string representation of the object. Define the Triangle, Circle, and Rectangle classes that extend the GeometricObject class. The Triangle class inherits all accessible data fields and methods from the GeometricObject class.In addition, it has three double data fields named...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT