-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
73 lines (63 loc) · 2.99 KB
/
Copy pathMain.java
File metadata and controls
73 lines (63 loc) · 2.99 KB
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
import Classes.Circle;
import Classes.Rectangle;
import Classes.Triangle;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Random;
public class Main {
public static void main(String[] args) {
System.out.println("Shape Heritance Part 2");
System.out.println("=".repeat(15));
//Generate a color array and a random object.
String[] colors = {"Red", "Blue", "Green", "Yellow", "Violet", "Orange", "Black", "White"};
Random random = new Random();
// Create an ArrayList for each shape type.
ArrayList<Triangle> triangles = new ArrayList<>();
ArrayList<Rectangle> rectangles = new ArrayList<>();
ArrayList <Circle> circles = new ArrayList<>();
// This portion of the Main.java will provide the input GUI for the user.
String input = JOptionPane.showInputDialog("Enter the number of shapes to create:");
int x;
try {
x = Integer.parseInt(input);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Invalid input.");
x = 3;
}
// Then we will generate the number of shapes as determined by the user.
for (int i = 0; i < x; i++) {
String randomColor = colors[random.nextInt(colors.length)];
// Create a triangle
triangles.add(new Triangle("Triangle", randomColor, random.nextDouble(5, 15), random.nextDouble(5,15)));
// Create a Rectangle
rectangles.add(new Rectangle("Rectangle", randomColor, random.nextDouble(5, 15), random.nextDouble(5,15)));
// Create a Circle
circles.add(new Circle("Circle", randomColor, random.nextDouble(5, 15)));
}
// The final thing to do is build and display a GUI report.
StringBuilder report = new StringBuilder("Shapes in Memory: \n\n");
int totalShapes = 0;
report.append("Triangles\n");
for (Triangle t : triangles) {
report.append(String.format("%s - Color: %s. My height is %.2f and my base is %.2f. My area is: %.2f\n", t.getShapeName(), t.getColor(), t.getHeight(), t.getBase(), t.getArea()));
totalShapes++;
}
report.append("\nRectangles\n");
for (Rectangle r : rectangles) {
report.append(String.format("%s - Color: %s. My length is %.2f and my width is %.2f. My area is: %.2f\n", r.getShapeName(), r.getColor(), r.getLength(), r.getWidth(), r.getArea()));
totalShapes++;
}
report.append("\nCircles\n");
for (Circle c : circles) {
report.append(String.format("%s - Color: %s. My diameter is %.2f. My area is: %.2f\n", c.getShapeName(), c.getColor(), c.getDiameter(), c.getArea()));
totalShapes++;
}
report.append("\nTotal Shapes: " + totalShapes);
JOptionPane.showMessageDialog(
null,
report.toString(),
"Shape Report",
JOptionPane.INFORMATION_MESSAGE
);
}
}