'Write a java program that prints a circle, rectangle or triangle depending on the user's choice after input from the user on the dimensions
Write a java program that prompts the user to chose a shape that will be displayed on the screen. The user picks a triangle, circle or rectangle and prompts the user to provide dimensions to be used for calculation then goes on ahead and prints the shape on the screen.
I tried but unfortunately failed. Here is the code.
import java.*;
import java.util.Scanner;
public class shape {
public static void main(String[] args) {
int P;
int x, y;
int r;
r = 0;
double shape;
int c;
int height;
int width;
Scanner reader = new Scanner(System.in);
System.out.print("Choose a shape : ");
shape = reader.nextInt();
double circle;
circle = 0;
double rectangle;
rectangle = 0;
double triangle;
triangle = 0;
if (shape == circle) {
P = 2 * r;
for (int i = 0; i <= P; i++) {
for (int j = 0; j <= P; j++) {
x = r - i;
y = r - j;
c = x*x + y*y ;
if (c <= r * r + 1) {
System.out.print("* ");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
else if(shape == rectangle) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number of widths: ");
width = scan.nextInt();
// get input from the user for width
System.out.print("Enter the number of widths: ");
height = scan.nextInt();
// get input from the user for column
for (int i = 1; i <= width; i++) {
for (int j = 1; j <= height; j++) {
System.out.print("*");
}
System.out.print("\n");
}
if (shape == triangle) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows to be printed");
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = rows; j >= i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
}
}
Solution 1:[1]
I figured it out.
import java.util.Scanner;
public class shapes2 {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter c to calculate area of circle");
System.out.println("Enter s to calculate area of square");
System.out.println("Enter r to calculate area of rectangle");
System.out.print("Enter your choice: ");
char choice = in.next().charAt(0);
switch(choice) {
case 'c':
double distance;
int rad;
Scanner sc = new Scanner(System.in);
System.out.print("Enter any value for radius: ");
rad = sc.nextInt();
System.out.println("Circle Pattern:\n");
for (int row = 0; row <= 2 * rad; row++) {
for (int col = 0; col <= 2 * rad; col++) {
distance = Math.sqrt((row - rad) * (row - rad) + (col - rad) * (col - rad));
if (distance > rad - 0.5 && distance < rad + 0.5)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
break;
case 's':
int side, i, j;
Scanner scs = new Scanner(System.in);
System.out.print(" Please Enter any Side of a Square : ");
side = scs.nextInt();
for(i = 1; i <= side; i++)
{
for(j = 1; j <= side; j++)
{
System.out.print("*");
}
System.out.print("\n");
}
break;
case 'r':
int rows, columns, x, y;
sc = new Scanner(System.in);
System.out.print(" Please Enter Number of Rows : ");
rows = sc.nextInt();
System.out.print(" Please Enter Number of Columns : ");
columns = sc.nextInt();
for(x = 1; x <= rows; x++)
{
for(y = 1; y <= columns; y++)
{
System.out.print("* ");
}
System.out.print("\n");
}
break;
default:
System.out.println("Wrong choice!");
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Daniel Nyagaka |