Selaa lähdekoodia

committing the code I worked through while reading.

Allison Ziegler 6 vuotta sitten
vanhempi
commit
ed362b0d1e
6 muutettua tiedostoa jossa 190 lisäystä ja 21 poistoa
  1. 8
    0
      README.TXT
  2. 31
    1
      TicketMachine.java
  3. 10
    10
      bluej.pkg
  4. BIN
      doc/BlueJ-objects-first-ch2.pdf
  5. 131
    0
      doc/labnotes.txt
  6. 10
    10
      package.bluej

+ 8
- 0
README.TXT Näytä tiedosto

@@ -1,3 +1,11 @@
1
+Allison Ziegler
2
+5/24/18
3
+I'm also including my extra notes that I took while I was doing the exercises!
4
+
5
+The code contains updates that I did while completing the exercises (They're
6
+in the docs folder).
7
+
8
+
1 9
 Project: naive-ticket-machine
2 10
 Authors: David Barnes and Michael Kolling
3 11
 

+ 31
- 1
TicketMachine.java Näytä tiedosto

@@ -17,6 +17,8 @@ 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
+    
21
+    private int status;
20 22
 
21 23
     /**
22 24
      * Create a machine that issues tickets of the given price.
@@ -29,6 +31,15 @@ public class TicketMachine
29 31
         balance = 0;
30 32
         total = 0;
31 33
     }
34
+    
35
+    public TicketMachine()
36
+    {
37
+        price = 1000; //default price
38
+    }
39
+    
40
+    public void setPrice(int ticketCost) {
41
+        price = ticketCost;
42
+    }
32 43
 
33 44
     /**
34 45
      * Return the price of a ticket.
@@ -54,7 +65,26 @@ public class TicketMachine
54 65
     {
55 66
         balance = balance + amount;
56 67
     }
57
-
68
+    
69
+    /**
70
+     * Return the value of total
71
+     */
72
+    public int getTotal(){
73
+        return total;
74
+    }
75
+    
76
+    public void prompt(){
77
+        System.out.println("Please insert correct change");
78
+    }
79
+    
80
+    public void showPrice(){
81
+        System.out.println("The price of a ticket is " + price + " cents.");
82
+    }
83
+    
84
+    public void empty(){
85
+        total = 0;
86
+    }
87
+    
58 88
     /**
59 89
      * Print a ticket.
60 90
      * Update the total collected and

+ 10
- 10
bluej.pkg Näytä tiedosto

@@ -1,16 +1,16 @@
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
-objectbench.height=101
7
-objectbench.width=461
2
+editor.fx.0.height=0
3
+editor.fx.0.width=0
4
+editor.fx.0.x=0
5
+editor.fx.0.y=0
6
+objectbench.height=164
7
+objectbench.width=776
8 8
 package.divider.horizontal=0.6
9
-package.divider.vertical=0.8007380073800738
10
-package.editor.height=427
9
+package.divider.vertical=0.6845018450184502
10
+package.editor.height=364
11 11
 package.editor.width=674
12
-package.editor.x=1067
13
-package.editor.y=119
12
+package.editor.x=224
13
+package.editor.y=84
14 14
 package.frame.height=600
15 15
 package.frame.width=800
16 16
 package.numDependencies=0

BIN
doc/BlueJ-objects-first-ch2.pdf Näytä tiedosto


+ 131
- 0
doc/labnotes.txt Näytä tiedosto

@@ -0,0 +1,131 @@
1
+Chapter 2! Woo!!
2
+
3
+2.1 First created the ticket object w/ a ticket price of 200 cents. I inserted 600 cents, and then printed a ticket. Subsequent balance is 0 cents. Bummer about exact change.
4
+
5
+2.2 The balance is zero cents after the ticket has been provided.
6
+
7
+2.3 NO REFUNDS. NO EXTRA TICKETS. WHAT AM I SUPPOSED TO DO WITH MY 6 DOLLAR BILL.
8
+
9
+2.4 Well, it's nice that if you insert too little money, it still gives you the ticket. You can even print the ticket without giving any money at all (no checks for the balance of the machine seem to exist at all. It just sets the balance to zero every time you get a ticket, and adds to the balance whenever you add money.)
10
+
11
+2.5 The only difference is that it reflects the correct price at the bottom of the ticket.
12
+
13
+2.6 classes for Student or LabClass --
14
+
15
+	public class Student {
16
+	}
17
+	public class LabClass {
18
+	}
19
+
20
+2.7 No good! If you change the order of public and class in the outer wrapper of the class, you get a complier error.
21
+
22
+2.8 It says "class has to be compiled to set breakpoints, which isn't really super duper helpful. 
23
+
24
+2.9 Fields in TicketMachine:
25
+
26
+	private int price;
27
+	private int balance;
28
+	private int total;
29
+
30
+    Constructor:
31
+	
32
+	public TicketMachine(int ticketCost) {code}
33
+
34
+    Methods:
35
+
36
+	getPrice();
37
+	getBalance();
38
+	insertMoney();
39
+	printTicket();
40
+
41
+2.10 The constructor is named after the class!
42
+
43
+2.11 
44
+	private int count;
45
+  	private Student representative;
46
+  	private Server host;
47
+
48
+	count is an int, representative is a Student, host is a Server
49
+
50
+2.12 	private boolean alive;
51
+  	private Person tutor;
52
+  	private Game game;
53
+
54
+	the names are alive, tutor, and game.
55
+
56
+2.13 It super matters what the order is. Getting the same "Class has to be compiled to set breakpoints" again, which is starting to mean something to me. Woo!
57
+
58
+2.14 It is extra important to have semi-colons. 
59
+
60
+2.15 declared an int named status.
61
+
62
+2.16 public Student(String name) -- the constructor belongs to class Student.
63
+
64
+2.17 public Book(String title, double price) -- there are two parameters, one of type thing, and the other of type double.
65
+
66
+2.18 Book's fields might be something like length, title, author genre, etc. They might be mostly Strings.
67
+
68
+2.19 public Pet(String petsName) { name = petsName; }
69
+
70
+2.20 price is already a declared variable within the scope of Class TicketMachine. You'll get a compiler error if you try to declare it again. 
71
+
72
+2.21 getBalance and getPrice return the value of two different variables.
73
+
74
+2.22 getBalance is "How much money is in the machine?"
75
+
76
+2.23 Nope. You can still return balance on getAmount. It's the same thing.
77
+
78
+2.24 added a getTotal method.
79
+
80
+2.25 missing return statement if you say your method will return something but it doesn't.
81
+
82
+2.26 Print ticket doesn't return anything (void)
83
+
84
+2.27 insertMoney and printTicket don't have return statements because they aren't supposed to return anything, per their method signatures.
85
+
86
+2.28 Yep. That's how it works.
87
+
88
+2.29 Constructors don't have return indicators.
89
+
90
+2.30 did it.
91
+
92
+2.31   /**
93
+   * Increase score by the given number of points.
94
+   */
95
+  public void increase(int points)
96
+  {
97
+	score += points;
98
+  }
99
+
100
+2.32 /**
101
+   * Reduce price by the given amount.
102
+   */
103
+  public void discount(int amount)
104
+  {
105
+	price -= amount;
106
+  }
107
+
108
+2.33 public void prompt(){
109
+        System.out.println("Please insert correct change");
110
+    }
111
+
112
+2.34 public void showPrice(){
113
+        System.out.println("The price of a ticket is " + price + " cents.");
114
+    }
115
+
116
+2.35 They have different outputs, because the output depends on the value of price. (Different objects of the same class)
117
+
118
+2.36 If you put quotes around a variable name, it ceases to be a variable name, and becomes a String. So it would just display "price", not the value of price.
119
+
120
+2.37 That's the same as above.
121
+
122
+2.38 No, because you're not referring to a variable. 
123
+
124
+2.39 If you modify the constructor that way, you'll generate a TicketMachine with no input required, and the price of tickets will always be 1000 cents. You can modify this with the setPrice method.
125
+
126
+2.40 made the method. It doesn't need parameters, because it will always do the same thing. 
127
+
128
+2.41 I feel like this was already an exercise.
129
+
130
+2.42 cool. Done.
131
+

+ 10
- 10
package.bluej Näytä tiedosto

@@ -1,16 +1,16 @@
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
-objectbench.height=101
7
-objectbench.width=461
2
+editor.fx.0.height=0
3
+editor.fx.0.width=0
4
+editor.fx.0.x=0
5
+editor.fx.0.y=0
6
+objectbench.height=164
7
+objectbench.width=776
8 8
 package.divider.horizontal=0.6
9
-package.divider.vertical=0.8007380073800738
10
-package.editor.height=427
9
+package.divider.vertical=0.6845018450184502
10
+package.editor.height=364
11 11
 package.editor.width=674
12
-package.editor.x=1067
13
-package.editor.y=119
12
+package.editor.x=224
13
+package.editor.y=84
14 14
 package.frame.height=600
15 15
 package.frame.width=800
16 16
 package.numDependencies=0