Sfoglia il codice sorgente

Completed exercises

Demetrius Murray 6 anni fa
parent
commit
0a6f92f3dd
5 ha cambiato i file con 131 aggiunte e 19 eliminazioni
  1. BIN
      .DS_Store
  2. 89
    1
      README.md
  3. 28
    4
      TicketMachine.java
  4. 7
    7
      bluej.pkg
  5. 7
    7
      package.bluej

BIN
.DS_Store Vedi File


+ 89
- 1
README.md Vedi File

@@ -19,18 +19,27 @@ so once you have inserted enough money, call the printTicket method. A
19 19
 facsimile ticket should be printed in the BlueJ terminal window.
20 20
 * Exercise 2.2 What value is returned if you check the machine’s balance after it
21 21
 has printed a ticket?
22
+  - 0 is returned as the balance
23
+
22 24
 * Exercise 2.3 Experiment with inserting different amounts of money before printing
23 25
 tickets. Do you notice anything strange about the machine’s behavior? What happens
24 26
 if you insert too much money into the machine – do you receive any refund? What
25 27
 happens if you do not insert enough and then try to print a ticket?
28
+  - no refund when I insert too much.
29
+  - ticket still prints when you insert too little.
30
+
26 31
 * Exercise 2.4 Try to obtain a good understanding of a ticket machine’s behavior by
27 32
 interacting with it on the object bench before we start looking at how the
28 33
 TicketMachine class is implemented in the next section.
29 34
 * Exercise 2.5 Create another ticket machine for tickets of a different price. Buy a
30 35
 ticket from that machine. Does the printed ticket look different?
36
+  - it acts exactly the same, except the price has changed, thus printing the new price when I print ticket.
31 37
 
32 38
 * Exercise 2.6 Write out what you think the outer wrappers of the Student and
33 39
 LabClass classes might look like – do not worry about the inner part.
40
+  - public class Student
41
+  - public class LabClass
42
+
34 43
 * Exercise 2.7 Does it matter whether we write
35 44
 `public class TicketMachine`
36 45
 or
@@ -38,10 +47,16 @@ or
38 47
 in the outer wrapper of a class? Edit the source of the TicketMachine class to
39 48
 make the change and then close the editor window. Do you notice a change in the
40 49
 class diagram?
50
+  - yes, it does matter. I received an error when compiling.
51
+
41 52
 What error message do you get when you now press the Compile button? Do you think
42 53
 this message clearly explains what is wrong?
54
+  - "<identifier> expected"
55
+  - the error doesn't specifically say, "you switched it around and you need to switch it back", but it's letting me know that it's expecting something that's not there - because i put it where it shouldn't be
56
+
43 57
 * Exercise 2.8 Check whether or not it is possible to leave out the word public
44 58
 from the outer wrapper of the TicketMachine class.
59
+  - yes, it is possible
45 60
 
46 61
 * Exercise 2.9 From your earlier experimentation with the ticket machine objects
47 62
 within BlueJ you can probably remember the names of some of the methods –
@@ -49,14 +64,23 @@ printTicket, for instance. Look at the class definition in Code 2.1 and use this
49 64
 knowledge, along with the additional information about ordering we have given you,
50 65
 to try to make a list of the names of the fields, constructors, and methods in the
51 66
 TicketMachine class. Hint: There is only one constructor in the class.
67
+  - Fields: price, balance, total
68
+  - Constructors: TicketMachine(ticketCost)
69
+  - Methods: getPrice(), getBalance(), insertMoney(amount), printTicket()
70
+
52 71
 Exercise 2.10 Do you notice any features of the constructor that make it significantly
53 72
 different from the other methods of the class?
73
+  - constructor carries the same name as the class (by definition)
74
+  - constructor is the only method to initialize variables
54 75
 
55 76
 * Exercise 2.11 What do you think is the type of each of the following fields?
56 77
 ```
57 78
 private int count;
79
+  - integer
58 80
 private Student representative;
81
+  - object of class Student, titled "representative"
59 82
 private Server host;
83
+  - object of class Server, titled "host"
60 84
 ```
61 85
 * Exercise 2.12 What are the names of the following fields?
62 86
 ```
@@ -64,6 +88,10 @@ private boolean alive;
64 88
 private Person tutor;
65 89
 private Game game;
66 90
 ```
91
+  - "alive"
92
+  - "tutor"
93
+  - "game"
94
+
67 95
 * Exercise 2.13 In the following field declaration from the TicketMachine class
68 96
 ```
69 97
 private int price;
@@ -73,20 +101,80 @@ try different orderings. After each change, close the editor. Does the appearanc
73 101
 class diagram after each change give you a clue as to whether or not other orderings are
74 102
 possible? Check by pressing the Compile button to see if there is an error message.
75 103
 Make sure that you reinstate the original version after your experiments!
104
+  - yes, it matters. the access modifier must come before the type, which comes before the field name.
105
+
76 106
 * Exercise 2.14 Is it always necessary to have a semicolon at the end of a field declaration?
77 107
 Once again, experiment via the editor. The rule you will learn here is an
78 108
 important one, so be sure to remember it.
109
+  - yes. IDE message says " \r;\r expected"
110
+
79 111
 * Exercise 2.15 Write in full the declaration for a field of type int whose name is
80 112
 status.
81 113
 * Exercise 2.16 To what class does the following constructor belong?
82 114
 ```
83 115
 public Student(String name)
84 116
 ```
117
+  - belongs to Student class. By definition, constructors are named after their classes
85 118
 * Exercise 2.17 How many parameters does the following constructor have and what are their types?
86 119
 ```
87 120
 public Book(String title, double price)
88 121
 ```
122
+  - two parameters of types String and double, respectively
89 123
 * Exercise 2.18 Can you guess what types some of the Book class’s fields might be? Can you assume anything about the names of its fields?
124
+  - String: book name, int: number of pages, String/int: serial number, string: author
90 125
 
91 126
 Work all Exercises from 2.19 to 2.58 that are NOT marked *Challenge exercise*.
92
-READ upto and INCLUDING section 2.15 of this chapter.
127
+- 2.19 Exercise
128
+  - publicPet(String petsName){
129
+    name = petsName;
130
+    }
131
+- 2.20 Exercise
132
+  - what's wrong with the code below is that it declares price <strong> within</strong> the constructor. price will no longer be available for use when the constructor completes its code.
133
+  <pre><code>public TicketMachine(int ticketCost) {
134
+      int price = ticketCost;
135
+      balance = 0;
136
+      total = 0;
137
+   </code></pre>
138
+  }
139
+  - in the case of implementing this constructor within <em>naive-ticket-machine</em>, it appears to be <strong>re</strong>declaring <q>price</q>. Thus, perhaps, a new price variable is created and within the scope of the constructor. While the global price that is called by <em>getPrice()</em> remains 0.
140
+  - 2.21 Exercise
141
+    -one returns balance one returns price;
142
+  - 2.22
143
+    - "How much money has been inserted thus far"
144
+  - 2.23
145
+    - no
146
+  - 2.25
147
+    - "Missing return statement"
148
+  - 2.26
149
+    - getPrice() has return type <em>int</em>, whereas printTicket() has no return type (void).
150
+  - 2.27
151
+    - they have a return type of <em>void</em>
152
+  - 2.29
153
+    - because it doesn't have the same name as the class
154
+  - 2.30
155
+    <pre><code>public void setPrice(int ticketCost){
156
+      price = ticketCost;
157
+    }</code></pre>
158
+  - 2.31
159
+    <pre><code> public void increase(int points){
160
+      score = points;
161
+    }</code></pre>
162
+  - 2.32
163
+    <pre><code> pbulic void discount(int amount){
164
+      price = price - amount;
165
+    }</code></pre>
166
+  - 2.36
167
+    -  <code>System.out.println("# \"" + price + "\" cents.");</code>
168
+  - 2.37
169
+    - <code>System.out.println("# price cents.");</code>
170
+      - the above will print everything within the quotes as is.
171
+    - <code>System.out.println("# " + "price" + " cents.");</code>
172
+      - this <strong>HERE</strong> will print the same as the coded bullet above it.
173
+  - 2.38
174
+    - neither of them, because <em>price</em> is not inserted as a variable, but rather a <strong><em>String</em></strong>
175
+  - 2.39
176
+    - by not having a parameter in the constructor, there is no dialog upon calling the constructor to set the ticket price.
177
+  - 2.4
178
+    - <em>empty()</em> method is a mutator
179
+  
180
+READ upto and INCLUDING section 2.15 of this chapter.

+ 28
- 4
TicketMachine.java Vedi File

@@ -9,7 +9,7 @@
9 9
  * @author David J. Barnes and Michael Kolling
10 10
  * @version 2008.03.30
11 11
  */
12
-public class TicketMachine
12
+class TicketMachine
13 13
 {
14 14
     // The price of a ticket from this machine.
15 15
     private int price;
@@ -17,15 +17,22 @@ public class TicketMachine
17 17
     private int balance;
18 18
     // The total amount of money collected by this machine.
19 19
     private int total;
20
+    private int status;
20 21
 
21 22
     /**
22 23
      * Create a machine that issues tickets of the given price.
23 24
      * Note that the price must be greater than zero, and there
24 25
      * are no checks to ensure this.
25 26
      */
26
-    public TicketMachine(int ticketCost)
27
+    public TicketMachine()
27 28
     {
28
-        price = ticketCost;
29
+        price = 1000;
30
+        balance = 0;
31
+        total = 0;
32
+    }
33
+
34
+    public TicketMachine(int ticketPrice){
35
+        price = ticketPrice;
29 36
         balance = 0;
30 37
         total = 0;
31 38
     }
@@ -38,6 +45,11 @@ public class TicketMachine
38 45
         return price;
39 46
     }
40 47
 
48
+    public int getTotal()
49
+    {
50
+        return total;
51
+    }
52
+
41 53
     /**
42 54
      * Return the amount of money already inserted for the
43 55
      * next ticket.
@@ -55,6 +67,18 @@ public class TicketMachine
55 67
         balance = balance + amount;
56 68
     }
57 69
 
70
+    public void prompt(){
71
+        System.out.println("Please insert the correct amount of money");
72
+    }
73
+
74
+    public void showPrice(){
75
+        System.out.println("The price of a ticket is " + price + " cents.");
76
+    }
77
+
78
+    public void empty(){
79
+        total = 0;
80
+    }
81
+
58 82
     /**
59 83
      * Print a ticket.
60 84
      * Update the total collected and
@@ -66,7 +90,7 @@ public class TicketMachine
66 90
         System.out.println("##################");
67 91
         System.out.println("# The BlueJ Line");
68 92
         System.out.println("# Ticket");
69
-        System.out.println("# " + price + " cents.");
93
+        System.out.println("# \"" + price + "\" cents.");
70 94
         System.out.println("##################");
71 95
         System.out.println();
72 96
 

+ 7
- 7
bluej.pkg Vedi File

@@ -1,16 +1,16 @@
1 1
 #BlueJ package file
2 2
 editor.fx.0.height=722
3 3
 editor.fx.0.width=800
4
-editor.fx.0.x=709
5
-editor.fx.0.y=113
4
+editor.fx.0.x=240
5
+editor.fx.0.y=24
6 6
 objectbench.height=101
7
-objectbench.width=461
7
+objectbench.width=776
8 8
 package.divider.horizontal=0.6
9 9
 package.divider.vertical=0.8007380073800738
10 10
 package.editor.height=427
11 11
 package.editor.width=674
12
-package.editor.x=1067
13
-package.editor.y=119
12
+package.editor.x=45
13
+package.editor.y=38
14 14
 package.frame.height=600
15 15
 package.frame.width=800
16 16
 package.numDependencies=0
@@ -28,5 +28,5 @@ target1.name=TicketMachine
28 28
 target1.showInterface=false
29 29
 target1.type=ClassTarget
30 30
 target1.width=120
31
-target1.x=80
32
-target1.y=50
31
+target1.x=100
32
+target1.y=60

+ 7
- 7
package.bluej Vedi File

@@ -1,16 +1,16 @@
1 1
 #BlueJ package file
2 2
 editor.fx.0.height=722
3 3
 editor.fx.0.width=800
4
-editor.fx.0.x=709
5
-editor.fx.0.y=113
4
+editor.fx.0.x=240
5
+editor.fx.0.y=24
6 6
 objectbench.height=101
7
-objectbench.width=461
7
+objectbench.width=776
8 8
 package.divider.horizontal=0.6
9 9
 package.divider.vertical=0.8007380073800738
10 10
 package.editor.height=427
11 11
 package.editor.width=674
12
-package.editor.x=1067
13
-package.editor.y=119
12
+package.editor.x=45
13
+package.editor.y=38
14 14
 package.frame.height=600
15 15
 package.frame.width=800
16 16
 package.numDependencies=0
@@ -28,5 +28,5 @@ target1.name=TicketMachine
28 28
 target1.showInterface=false
29 29
 target1.type=ClassTarget
30 30
 target1.width=120
31
-target1.x=80
32
-target1.y=50
31
+target1.x=100
32
+target1.y=60