Question

Create a Shape (Object) Class Create a class (Shapes) that prompts the user to select a...

Create a Shape (Object) Class Create a class (Shapes) that prompts the user to select a shape to be drawn on the screen. Objects: Circle, X, box, box with an x inside, or any other object of your choice. Depending on the user’s choice, you will then ask for the number of rows, columns or any requirements needed to draw the shape. Finally, you will display the shape selected by the user to the screen. Your class should have at least one instance variable. Your class needs to check for a correct user input. Your class most have methods to get/set the value from the instance variables Your class should have a method to display the shape. Use the asterisk character to draw the shape.

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

Submit the UML diagram and Java files

Homework Answers

Answer #1

Hey! here is my code..... please give positive rating to appreciate my work....

Shapes.java

import java.util.*;

class Shapes
{

  private int radius;
  private int width;
  private int height;
  private int row;
  private int side;

  public int getWidth ()
  {
    return width;
  }


  public int getHeight ()
  {
    return height;
  }

  public int getRadius ()
  {
    return radius;
  }

  public int getRow ()
  {
    return row;
  }

  public int getSide ()
  {
    return side;
  }


  public int setWidth (int w)
  {
    return width = w;
  }

  public int setLength (int h)
  {
    return height = h;
  }

  public int setRadius (int r)
  {
    return radius = r;
  }

  public int setRow (int rows)
  {
    return row = rows;
  }

  public int setSide (int sides)
  {
    return side = sides;
  }

  public int Display_shapes ()
  {

    Scanner sc = new Scanner (System.in);
    int ch;

    while (true)
      {
        System.out.println ("\nSelect a shape to be drawn on the screen:");
        System.out.println ("1.) Circle");
        System.out.println ("2.) X");
        System.out.println ("3.) box");
        System.out.println ("4.) box with an x inside");
        System.out.println ("5.) Triangle");
        System.out.println ("6.) Square");
        System.out.println ("7.) Exit");
        System.out.println ("Enter your choice");
        ch = sc.nextInt ();


        switch (ch)
          {

          case 1:
            System.out.println ("Enter Radius of circle:");

            radius = sc.nextInt ();


            // dist represents distance to the center 
            double dist;

            // for horizontal movement 
            for (int i = 0; i <= 2 * radius; i++)
              {

                // for vertical movement 
                for (int j = 0; j <= 2 * radius; j++)
                  {
                    dist =
                      Math.sqrt ((i - radius) * (i - radius) +
                                 (j - radius) * (j - radius));

                    // dist should be in the range (radius - 0.5) 
                    // and (radius + 0.5) to print stars(*) 
                    if (dist > radius - 0.5 && dist < radius + 0.5)
                      {
                        System.out.print ("*");
                      }
                    else
                      {
                        System.out.print (" ");
                      }
                  }

                System.out.print ("\n");
              }

            break;

          case 2:
            System.out.println ("Enter height of X : ");
            int height = sc.nextInt ();

            int k = height * 2 - 1;

            for (int i = 1; i <= k; i++)
              {

                for (int j = 1; j <= k; j++)

                  {
                    if (j == i || j == k - i + 1)
                      System.out.print ("*");
                    System.out.print (" ");
                  }


                System.out.println ();

              }
            break;

          case 3:
            System.out.println ("Enter width of box:");
            int w = sc.nextInt ();

            System.out.println ("Enter height of box:");
            int h = sc.nextInt ();
        

            for (int i = 0; i < h; i++)
              {
                System.out.println ();
                for (int j = 0; j < w; j++)
                  {
                    // Print * if this is first  
                    // row or last row. Or this 
                    // column is first or last. 
                    if (i == 0 || i == h - 1 || j == 0 || j == w - 1)
                      {
                        System.out.print ("*");
                      }
                    else
                      {
                        System.out.print (" ");
                      }
                  }
              }
            break;

          case 4:
            System.out.println ("Enter height of X : ");
            int n = sc.nextInt ();

            for (int i = 0; i < n; i++)
              {
                for (int j = 0; j < n; j++)
                  {
                    if (i == 0 || i == n - 1 || j == 0 || j == n - 1 || i == j
                        || i == n - 1 - j)
                      {
                        System.out.print ("*");
                      }
                    else
                      {
                        System.out.print (" ");
                      }


                  }

                System.out.println ();
              }

            break;

          case 5:

            System.out.println ("Enter the number of rows to be printed");
            int rows = sc.nextInt ();

            // loop to iterate for the given number of rows 
            for (int i = 1; i <= rows; i++)
              {

                // loop to print the number of spaces before the star 
                for (int j = rows; j >= i; j--)
                  {
                    System.out.print (" ");
                  }

                // loop to print the number of stars in each row 
                for (int j = 1; j <= i; j++)
                  {
                    System.out.print ("* ");
                  }

                // for new line after printing each row 
                System.out.println ();
              }

            break;

          case 6:
            System.out.println ("Enter the sides of Square: ");
            int s = sc.nextInt ();

            int i, j;
            for (i = 1; i <= s; i++)
              {
                for (j = 1; j <= s; j++)
                  {
                    if (i == 1 || i == s || j == 1 || j == s)
                      {
                        System.out.print ("*");
                      }
                    else
                      {
                        System.out.print (" ");
                      }
                  }
          System.out.println ();
              }

            break;

          case 7:
            System.exit (0);

          default:
            System.out.print ("\nPlease Enter valid choice\n");


          }
      }


  }

  public static void main (String[]args)
  {
    Shapes s1 = new Shapes ();
    System.out.println (s1.Display_shapes ());



  }

}

Outputs:--

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
Java Create a new Drive class. * Create a Menu class. * The menu class will...
Java Create a new Drive class. * Create a Menu class. * The menu class will use the previous 4 classes you created in GCD, LCM, FACTORIAL, DIGITS The Menu will display a menu that give you the option of selecting: 1) Greatest Common Denominator 2) Lowest Common Multiple 3) Factorial 4) Number of Digits in an Integer Enter 1,2,3 or 4: When the User enter the choice, then the correct function/method is called for the class and asks 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)...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String),...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String), Monthly Salary (double)). Create a constructor that initializes these variables. Define set and get methods for each variable. If the monthly salary is not positive, do not set the salary. Create a separate test class called EmployeeTest that will use the Employee class. By running this class, create two employees (Employee object) and print the annual salary of each employee (object). Then set the...
Design a class with the following requirements: 1- give the class a name from your choice....
Design a class with the following requirements: 1- give the class a name from your choice. 2- write some code that creates three instance variables, choose a name and type for each one of them. (must have two private and one public variables) 3- Initialize the variables using two different constructors. 4- Use mutator and accessor methods (set and get) for the private data members. 5- Display the value of each member variable using a method. 6- In the main,...
Your solutions will be graded on the bassis of originality, completeness, and compliance to the object...
Your solutions will be graded on the bassis of originality, completeness, and compliance to the object oriented programming concepts. No Java code is required for this section. Question 1:   Assume you are required to write an object oriented program for purchasing household items for a new house. Identify at least five classes of household items such as decoration items, washroom items etc. Each of these classes must have at least two types of items. Draw a UML class diagram identifying...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. // A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. // At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will be filled with Car objects. It will call the displayMenu method to display the menu. Use a sentinel-controlled loop to read the user’s choice from stdin, call the appropriate method based on their choice, and redisplay the menu by calling displayMenu. Be sure to use the most appropriate statement for this type of repetition. displayMenu Parameters:             none Return value:          none Be sure to use...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT