-
Notifications
You must be signed in to change notification settings - Fork 1
/
Shape.java
116 lines (96 loc) · 3.43 KB
/
Shape.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.util.Scanner;
class Shape {
protected String name;
public Shape(String name) {
this.name = name;
}
public void draw() {
System.out.println("Drawing a " + name);
}
}
class Circle extends Shape {
private double radius;
public Circle(String name, double radius) {
super(name);
this.radius = radius;
}
@Override
public void draw() {
System.out.println("Drawing a Circle with radius " + radius);
}
}
class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(String name, double length, double width) {
super(name);
this.length = length;
this.width = width;
}
@Override
public void draw() {
System.out.println("Drawing a Rectangle with length " + length + " and width " + width);
}
}
class Triangle extends Shape {
private double base;
private double height;
public Triangle(String name, double base, double height) {
super(name);
this.base = base;
this.height = height;
}
@Override
public void draw() {
System.out.println("Drawing a Triangle with base " + base + " and height " + height);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of shapes: ");
int numShapes = scanner.nextInt();
scanner.nextLine();
Shape[] shapes = new Shape[numShapes];
for (int i = 0; i < numShapes; i++) {
System.out.println("\nShape #" + (i + 1));
System.out.print("Enter the shape name: ");
String shapeName = scanner.nextLine();
System.out.print("Enter the shape type (1 - Circle, 2 - Rectangle, 3 - Triangle): ");
int shapeType = scanner.nextInt();
scanner.nextLine();
switch (shapeType) {
case 1:
System.out.print("Enter the radius: ");
double radius = scanner.nextDouble();
scanner.nextLine();
shapes[i] = new Circle(shapeName, radius);
break;
case 2:
System.out.print("Enter the length: ");
double length = scanner.nextDouble();
scanner.nextLine();
System.out.print("Enter the width: ");
double width = scanner.nextDouble();
scanner.nextLine();
shapes[i] = new Rectangle(shapeName, length, width);
break;
case 3:
System.out.print("Enter the base: ");
double base = scanner.nextDouble();
scanner.nextLine();
System.out.print("Enter the height: ");
double height = scanner.nextDouble();
scanner.nextLine();
shapes[i] = new Triangle(shapeName, base, height);
break;
default:
System.out.println("Invalid shape type. Skipping shape.");
break;
}
}
System.out.println("\nDrawing shapes:");
for (Shape shape : shapes) {
shape.draw();
}
scanner.close();
}
}