1
+ /*
2
+ * Chapter 16.2
3
+ */
4
+ import javax .swing .JFrame ;
5
+
6
+ //??????(10)WHY A SUPERCLASS if it only does so little???????????????
7
+ //IDEA:I believe it's to show how refactoring and abstract works!!! We
8
+ //wanted to remove repeated code by refactoring it. Then in order for
9
+ //our repeated code to work it required the mainloop() to be present
10
+ //and by adding this, it needed a method(update) from each class so we
11
+ //used abstract to access the update() from each class. ?IN ORDER to
12
+ //abstract something from another class(subclass) we ?need to make the
13
+ //superclass abstract? so I think it was easier to show and implement
14
+ //it here in this way!!?????
15
+ //??If yes then why not abstract the mainloop???? I feel like adding it
16
+ //here is only for organization because I think it would be faster to
17
+ //leave them where they are as is and here it's just more computations
18
+ //and also at putting more at risk??????????????
19
+ //???Why not just inherit because this way for this problem seems dumb!??
20
+ public abstract class Automaton
21
+ {
22
+ /*
23
+ * BOOK:There are a few problems with the current design
24
+ *
25
+ * 1. The grid attribute is private, making it inaccessible in Conway
26
+ * and Langton. We could make it public, but then other (unrelated)
27
+ * classes would have access to it as well.
28
+ *
29
+ * 2. The Automaton class has no constructors, and even if it did, there
30
+ * would be no reason to create an instance of this class.
31
+ *
32
+ * 3. The Automaton class does not provide an implementation of update.
33
+ * In order to work properly, subclasses need to provide one.
34
+ *
35
+ * Java provides language features to solve these problems:
36
+ *
37
+ * 1. We can make the grid attribute protected, which means it's accessible
38
+ * to subclasses but not other classes.
39
+ *
40
+ * 2. We can make the class abstract, which means it cannot be instantiated.
41
+ * If you attempt to create an object for an abstract class, you will get
42
+ * a compiler error.
43
+ *
44
+ * 3. We can declare update as an abstract method, meaning that it must be
45
+ * overridden in subclasses. If the subclass does not override an abstract
46
+ * method, you will get a compiler error.
47
+ */
48
+ //??#2.1 To have/be an instance do you need a constructor??????????
49
+ //??#1.1&1.2 By making it public other classes would have access to it too
50
+ //but our solution is to just allow only subclasses access SOOOOO..... what's
51
+ //stopping someone from just extending this super as well and boom now it's a
52
+ //subclass of this class? OR just make a subclass to the subclass????????????
53
+ //??#2.1 (Question still stands) Why is there no reason to create an instance
54
+ //for this class!? If it's not an instance then what is it????? What is the
55
+ //purpose or benefit of creating an instance and not?????????
56
+ //??#2.2 Abstract classes can't be instantiated(make a new instance); again
57
+ //what is the purpose or benefit in doing this????????
58
+ //??#3.2 Why do the methods in the subclasses need to be overridden?? To me
59
+ //overridden means that a method is written multiple times so override is
60
+ //used to make a particular method the priority, but here we define the
61
+ //object, we wish to access with, this; meaning that we know which object
62
+ //is currently in use so we will find each update() in that objects class??
63
+ //???After adding update() I was wondering that if only 1 class had update,
64
+ //because it was a necessity for it, that if this would still be
65
+ //necessary/needed? ALSO what if each class has update but different return
66
+ //values for it then what?????????
67
+
68
+
69
+ //BOOK:Automaton declares grid as an instance variable, so
70
+ //every Automaton "has a" GridCanvas
71
+ //???define the HAS-A here??? The variable grid has a value
72
+ //that is = to a reference(?or instance?) that is from GridCanvas
73
+ //private GridCanvas grid;
74
+ protected GridCanvas grid ;
75
+
76
+ //BOOK:The declaration specifies the name, arguments, and return type. But
77
+ //it does not provide an implementation, because it is an abstract method.
78
+ //Any class that extends Automaton must provide an implementation of update;
79
+ //the declaration here allows the compiler to check.
80
+ //??How does the compiler check??? Aside from that, does the compiler check
81
+ //if a method like this exists; without the abstract, and if so run the code.
82
+ //(Read the Aside part like, What does the compiler check for?)
83
+ public abstract void update ();
84
+
85
+ //????(11)Why a method and not a constructor???? I know it's
86
+ //supposed to be a super but doesn't that only further my point??
87
+ //BOOK:rate="frame rate", the # of time steps to show per second
88
+ public void run (String title , int rate )
89
+ {
90
+ JFrame frame = new JFrame (title );
91
+ //???Why JFrame.EXIT and not frame????
92
+ //frame is a variable here and not there???
93
+ frame .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
94
+ frame .setResizable (false );
95
+ frame .add (this .grid );
96
+ frame .pack ();
97
+ frame .setVisible (true );
98
+ this .mainloop (rate );
99
+ }
100
+
101
+ private void mainloop (int rate )
102
+ {
103
+ while (true )
104
+ {
105
+ this .update ();
106
+ grid .repaint ();
107
+
108
+ try {
109
+ Thread .sleep (1000 / rate );
110
+ } catch (InterruptedException e ) {
111
+ //do nada
112
+ }
113
+ }
114
+ }
115
+ }
0 commit comments