瀏覽代碼

Naive Ticket FINISHED

Nathan Hall 6 年之前
父節點
當前提交
f445ce7ab5
共有 5 個檔案被更改,包括 154 行新增29 行删除
  1. 二進制
      .DS_Store
  2. 75
    0
      NaiveTicket.txt
  3. 63
    3
      TicketMachine.java
  4. 8
    13
      bluej.pkg
  5. 8
    13
      package.bluej

二進制
.DS_Store 查看文件


+ 75
- 0
NaiveTicket.txt 查看文件

@@ -0,0 +1,75 @@
1
+Responses
2
+
3
+Exercise 2.29 How can we tell from just its header that setPrice is a method and not a constructor?
4
+public void setPrice(int ticketCost)
5
+
6
+	Has a void.
7
+	Takes a parameter.
8
+
9
+
10
+
11
+
12
+Exercise 2.30 Complete the body of the setPrice method so that it assigns the
13
+value of its parameter to the price field.
14
+
15
+	public void setPrice(int ticketCost){
16
+		price = ticketCost;
17
+	}
18
+
19
+
20
+
21
+
22
+Exercise 2.31 Complete the body of the following method, whose purpose is to
23
+add the value of its parameter to a field named score.
24
+  /**
25
+   * Increase score by the given number of points.
26
+   */
27
+  public void increase(int points)
28
+  {
29
+  	points += points;
30
+
31
+ }
32
+
33
+
34
+
35
+
36
+Exercise 2.32 Can you complete the following method, whose purpose is to sub- tract the value of its parameter from a field named price?
37
+  /**
38
+   * Reduce price by the given amount.
39
+   */
40
+  public void discount(int amount)
41
+  {
42
+
43
+  	price -= amount;
44
+
45
+... }
46
+
47
+
48
+Exercise 2.33 Add a method called prompt to the TicketMachine class. This should have a void return type and take no parameters. The body of the method should print something like:
49
+  Please insert the correct amount of money.
50
+
51
+
52
+
53
+Exercise 2.34 Add a showPrice method to the TicketMachine class. This should have a void return type and take no parameters. The body of the method should print something like:
54
+The price of a ticket is xyz cents.
55
+where xyz should be replaced by the value held in the price field when the method
56
+is called.
57
+
58
+
59
+
60
+Exercise 2.35 Create two ticket machines with differently priced tickets. Do calls to their showPrice methods show the same output, or different? How do you explain this effect?
61
+
62
+
63
+Exercise 2.36 What do you think would be printed if you altered the fourth state- ment of printTicket so that price also has quotes around it, as follows?
64
+  System.out.println("# " + "price" + " cents.");
65
+
66
+
67
+
68
+
69
+Exercise 2.37 What about the following version? System.out.println("# price cents.");
70
+
71
+
72
+
73
+Exercise 2.38 Could either of the previous two versions be used to show the price of tickets in different ticket machines? Explain your answer.
74
+
75
+

+ 63
- 3
TicketMachine.java 查看文件

@@ -1,3 +1,5 @@
1
+package NaiveTicket;
2
+
1 3
 /**
2 4
  * TicketMachine models a naive ticket machine that issues
3 5
  * flat-fare tickets.
@@ -18,18 +20,31 @@ public class TicketMachine
18 20
     // The total amount of money collected by this machine.
19 21
     private int total;
20 22
 
23
+
21 24
     /**
22 25
      * Create a machine that issues tickets of the given price.
23 26
      * Note that the price must be greater than zero, and there
24 27
      * are no checks to ensure this.
25 28
      */
26
-    public TicketMachine(int ticketCost)
29
+    /*public TicketMachine()
27 30
     {
28
-        price = ticketCost;
31
+        price = 1000;
29 32
         balance = 0;
30 33
         total = 0;
31 34
     }
32
-
35
+    */
36
+   
37
+   public TicketMachine(int ticketCost)
38
+    {
39
+        price = ticketCost;
40
+    }
41
+    
42
+   public TicketMachine()
43
+    {
44
+        price = 700;
45
+    }
46
+   
47
+   
33 48
     /**
34 49
      * Return the price of a ticket.
35 50
      */
@@ -48,11 +63,40 @@ public class TicketMachine
48 63
     }
49 64
 
50 65
     /**
66
+     * Define getTotal
67
+     */
68
+
69
+    public int getTotal()
70
+    {
71
+        return total;
72
+    }
73
+
74
+    /**
51 75
      * Receive an amount of money in cents from a customer.
52 76
      */
53 77
     public void insertMoney(int amount)
54 78
     {
55 79
         balance = balance + amount;
80
+        total += amount;
81
+    }
82
+
83
+    /**
84
+     * Complete setPrice
85
+     */
86
+
87
+    public void setPrice(int ticketCost)
88
+    {
89
+        price = ticketCost;
90
+    }
91
+    
92
+    /**
93
+     * Implement empty method
94
+     */
95
+
96
+    public void empty()
97
+    {
98
+        total = 0;
99
+        balance = 0;
56 100
     }
57 101
 
58 102
     /**
@@ -74,5 +118,21 @@ public class TicketMachine
74 118
         total = total + balance;
75 119
         // Clear the balance.
76 120
         balance = 0;
121
+        
122
+    }
123
+    
124
+    public void prompt(){
125
+    
126
+        System.out.println("Please insert the correct amount of money");
127
+        
77 128
     }
129
+    
130
+    public void showPrice(){
131
+    
132
+        System.out.println("The price of a ticket is " + price + " cents.");
133
+        
134
+    }
135
+
78 136
 }
137
+
138
+

+ 8
- 13
bluej.pkg 查看文件

@@ -1,23 +1,18 @@
1 1
 #BlueJ package file
2
-editor.fx.0.height=722
3
-editor.fx.0.width=800
4
-editor.fx.0.x=709
5
-editor.fx.0.y=113
6 2
 objectbench.height=101
7
-objectbench.width=461
3
+objectbench.width=551
8 4
 package.divider.horizontal=0.6
9
-package.divider.vertical=0.8007380073800738
10
-package.editor.height=427
11
-package.editor.width=674
12
-package.editor.x=1067
13
-package.editor.y=119
14
-package.frame.height=600
15
-package.frame.width=800
5
+package.divider.vertical=0.7605321507760532
6
+package.editor.height=336
7
+package.editor.width=449
8
+package.editor.x=0
9
+package.editor.y=23
10
+package.frame.height=509
11
+package.frame.width=575
16 12
 package.numDependencies=0
17 13
 package.numTargets=1
18 14
 package.showExtends=true
19 15
 package.showUses=true
20
-project.charset=UTF-8
21 16
 readme.height=58
22 17
 readme.name=@README
23 18
 readme.width=47

+ 8
- 13
package.bluej 查看文件

@@ -1,23 +1,18 @@
1 1
 #BlueJ package file
2
-editor.fx.0.height=722
3
-editor.fx.0.width=800
4
-editor.fx.0.x=709
5
-editor.fx.0.y=113
6 2
 objectbench.height=101
7
-objectbench.width=461
3
+objectbench.width=551
8 4
 package.divider.horizontal=0.6
9
-package.divider.vertical=0.8007380073800738
10
-package.editor.height=427
11
-package.editor.width=674
12
-package.editor.x=1067
13
-package.editor.y=119
14
-package.frame.height=600
15
-package.frame.width=800
5
+package.divider.vertical=0.7605321507760532
6
+package.editor.height=336
7
+package.editor.width=449
8
+package.editor.x=0
9
+package.editor.y=23
10
+package.frame.height=509
11
+package.frame.width=575
16 12
 package.numDependencies=0
17 13
 package.numTargets=1
18 14
 package.showExtends=true
19 15
 package.showUses=true
20
-project.charset=UTF-8
21 16
 readme.height=58
22 17
 readme.name=@README
23 18
 readme.width=47