-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStoreCADState.java
More file actions
154 lines (113 loc) · 4.08 KB
/
StoreCADState.java
File metadata and controls
154 lines (113 loc) · 4.08 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package io;
import java.io.*;
import java.util.*;
/**
* RUN:
* javac io/StoreCADState.java && java io.StoreCADState
*
* OUTPUT:
* [class io.Circlecolor[3] xPos[58] yPos[55] dim[93]
* , class io.Squarecolor[3] xPos[61] yPos[61] dim[29]
* , class io.Linecolor[3] xPos[68] yPos[0] dim[22]
* , class io.Circlecolor[3] xPos[7] yPos[88] dim[28]
* , class io.Squarecolor[3] xPos[51] yPos[89] dim[9]
* , class io.Linecolor[3] xPos[78] yPos[98] dim[61]
* , class io.Circlecolor[3] xPos[20] yPos[58] dim[16]
* , class io.Squarecolor[3] xPos[40] yPos[11] dim[22]
* , class io.Linecolor[3] xPos[4] yPos[83] dim[6]
* , class io.Circlecolor[3] xPos[75] yPos[10] dim[42]
* ]
*/
public class StoreCADState {
public static void main(String[] args) throws Exception {
// List<Class<? extends Shape>> shapeTypes = new ArrayList<Class<? extends Shape>>();
// shapeTypes.add(Circle.class);
// shapeTypes.add(Square.class);
// shapeTypes.add(Line.class);
List<Shape> shapes = new ArrayList<Shape>();
for (int i = 0; i < 10; i++) {
shapes.add(Shape.randomFactory());
}
for (int i = 0; i < 10; i++) {
((Shape)shapes.get(i)).setColor(Shape.GREEN);
}
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream("CADState.out")
);
// out.writeObject(shapeTypes);
Line.serializeStaticState(out);
out.writeObject(shapes);
System.out.println(shapes);
}
}
abstract class Shape implements Serializable {
public static final int RED = 1, BLUE = 2, GREEN = 3;
private int xPos, yPos, dimension;
private static Random rand = new Random(47);
private static int counter = 0;
public abstract void setColor(int newColor);
public abstract int getColor();
public Shape(int xVal, int yVal, int dim) {
xPos = xVal;
yPos = yVal;
dimension = dim;
}
public String toString() {
return getClass() + "color["+getColor()+"] xPos["
+ xPos +"] yPos["+ yPos + "] dim["+ dimension +"]\n";
}
public static Shape randomFactory() {
int xVal = rand.nextInt(100);
int yVal = rand.nextInt(100);
int dim = rand.nextInt(100);
switch(counter++ % 3) {
default:
case 0: return new Circle(xVal, yVal, dim);
case 1: return new Square(xVal, yVal, dim);
case 2: return new Line(xVal, yVal, dim);
}
}
}
class Circle extends Shape {
private static int color = RED;
public Circle(int xVal, int yVal, int dim) {
super(xVal, yVal, dim);
}
public static void serializeStaticState(ObjectOutputStream os) throws IOException {
os.writeInt(color);
}
public static void deserializeStaticState(ObjectInputStream os) throws IOException {
color = os.readInt();
}
public void setColor(int newColor) { color = newColor; }
public int getColor() { return color; }
}
class Square extends Shape {
private static int color;
public Square(int xVal, int yVal, int dim) {
super(xVal, yVal, dim);
color = RED;
}
public static void serializeStaticState(ObjectOutputStream os) throws IOException {
os.writeInt(color);
}
public static void deserializeStaticState(ObjectInputStream os) throws IOException {
color = os.readInt();
}
public void setColor(int newColor) { color = newColor; }
public int getColor() { return color; }
}
class Line extends Shape {
private static int color = RED;
public static void serializeStaticState(ObjectOutputStream os) throws IOException {
os.writeInt(color);
}
public static void deserializeStaticState(ObjectInputStream os) throws IOException {
color = os.readInt();
}
public Line(int xVal, int yVal, int dim) {
super(xVal, yVal, dim);
}
public void setColor(int newColor) { color = newColor; }
public int getColor() { return color; }
}