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
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:--
Get Answers For Free
Most questions answered within 1 hours.