Question

In: Computer Science

Both homework are linked. Create a class (Shapes) that prompts the user to select a shape...

Both homework are linked.

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 # 2
New requirements from Homework # 1:

You must create at least one class without main method. The main method is only for testing your class.

Every time a user creates a shape the output needs to be written to a file.

Create an enumeration (enum) for the Menu method.

Create an Array of object (Shapes) to stores the objects created by the user.

Remember to make the array large so that you don't have to run out of space.

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

Submit the UML diagram and Java files

Solutions

Expert Solution

ANSWER:-

GIVEN THAT:-

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 ());


}

}

OUTPUT:-


Related Solutions

Create a class (Shapes) that prompts the user to select a shape to be drawn on...
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...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape,...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape, Two Dimensional Shape, Three Dimensional Shape, Square, Circle, Cube, Rectangular Prism, and Sphere. Cube should inherit from Rectangular Prism. The two dimensional shapes should include methods to calculate Area. The three dimensional shapes should include methods to calculate surface area and volume. Use as little methods as possible (total, across all classes) to accomplish this, think about what logic should be written at which...
create a Python script that prompts the user for a title, description, and filename of your...
create a Python script that prompts the user for a title, description, and filename of your Python program and add the following to the bottom of the existing homepage: Add the post title with an emphasis Add the post description beneath the post title Create a hyperlink to the Python file using the filename input Create another hyperlink to the page you will create in the Web Showcase assignment
Create a CubesSum application that prompts the user for a non-negative integer and then displays the...
Create a CubesSum application that prompts the user for a non-negative integer and then displays the sum of the cubes of the digits.   b) Modify the application to determine what integers of two, three, and four digits are equal to the sum of the cubes of their digits.(Java Programming )
Java Programming Create a program that prompts the user for an integer number and searches for...
Java Programming Create a program that prompts the user for an integer number and searches for it within an array of 10 elements. What is the average number of comparisons required to find an element in the array? Your program should print the number of comparisons required to find the number or determine that the number does not exist. Try finding the first and last numbers stored in the array. Run your program several times before computing the average.
(JAVA) Create a program that prompts the user for an age input. The program defines the...
(JAVA) Create a program that prompts the user for an age input. The program defines the age group of the user. Follow the table below to construct the output for your program. Age Age Group 0 Baby 1-3 Toddler 4-11 Child 12-17 Teenager 18-21 Young Adult 22-64 Adult 65+ Senior Negative Number Invalid Input Sample Input Enter an age: 18 Sample Output: You are a young adult. Sample Input Enter an age: 29 Sample Output: You are an adult. Sample...
C# Create a console application that prompts the user to enter a regular expression, and then...
C# Create a console application that prompts the user to enter a regular expression, and then prompts the user to enter some input and compare the two for a match until the user presses Esc: The default regular expression checks for at least one digit. Enter a regular expression (or press ENTER to use the default): ^[a- z]+$ Enter some input: apples apples matches ^[a-z]+$? True Press ESC to end or any key to try again. Enter a regular expression...
JAVA Homework 1) Create a die class. This is similar to the coin class , but...
JAVA Homework 1) Create a die class. This is similar to the coin class , but instead of face having value 0 or 1, it now has value 1,2,3,4,5, or 6. Also instead of having a method called flip, name it roll (you flip a coin but roll a die). You will NOT have a method called isHeads, but you will need a method (call it getFace ) which returns the face value of the die. Altogether you will have...
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...
Write a Java class called GuessMyNumber that prompts the user for an integer n, tells the...
Write a Java class called GuessMyNumber that prompts the user for an integer n, tells the user to think of a number between 0 and n−1, then makes guesses as to what the number is. After each guess, the program must ask the user if the number is lower, higher, or correct. You must implement the divide-and-conquer algorithm from class. In particular, you should round up when the middle of your range is in between two integers. (For example, if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT