Sfoglia il codice sorgente

update with latest

Eric Foster 6 anni fa
parent
commit
0b54f3a3c7
8 ha cambiato i file con 558 aggiunte e 31 eliminazioni
  1. BIN
      .DS_Store
  2. 67
    0
      Book.java
  3. 71
    0
      Heater.java
  4. 52
    0
      Person.java
  5. 95
    0
      Student.java
  6. 189
    3
      TicketMachine.java
  7. 42
    14
      bluej.pkg
  8. 42
    14
      package.bluej

BIN
.DS_Store Vedi File


+ 67
- 0
Book.java Vedi File

@@ -0,0 +1,67 @@
1
+/**
2
+ * A class that maintains information on a book.
3
+ * This might form part of a larger application such
4
+ * as a library system, for instance.
5
+ *
6
+ * @author (Insert your name here.)
7
+ * @version (Insert today’s date here.)
8
+ */
9
+public class Book
10
+{
11
+    // The fields.
12
+    private String author; 
13
+    private String title;
14
+    private int pages;
15
+    private String refNumber;
16
+    private int borrowed;
17
+    /**
18
+     * Set the author and title fields when this object
19
+     * is constructed.
20
+     */
21
+
22
+    public Book(String bookAuthor, String bookTitle, int bookPages)
23
+    {
24
+        author = bookAuthor;
25
+        title = bookTitle;
26
+        pages = bookPages;
27
+        refNumber = "";
28
+    }
29
+
30
+    public void printAuthor(){
31
+        System.out.println(author);
32
+    }
33
+    
34
+    public void printTitle(){
35
+        System.out.println(title);
36
+    }
37
+    
38
+    public int getPages(){
39
+        return pages;
40
+    }
41
+    
42
+    public void setRefNumber(String ref){
43
+        if(ref.length()>=3){
44
+            refNumber = ref;
45
+        } else {
46
+            System.out.print("enter a ref of 3 or more characters");
47
+        }
48
+    }
49
+    
50
+    public int getBorrowed(){
51
+        return  borrowed;
52
+    }
53
+    
54
+    public void setBorrowed(String ref){
55
+        borrowed += 1;
56
+    }
57
+    
58
+    public String getRefNumber(){
59
+        return refNumber;
60
+    }
61
+    
62
+    public void printDetails(){
63
+        System.out.println("Title: " + title + ", Author: " + author + ", Pages: " + pages + ", Checkouts: " + borrowed);
64
+    }
65
+}
66
+    
67
+    

+ 71
- 0
Heater.java Vedi File

@@ -0,0 +1,71 @@
1
+
2
+/**
3
+ * Write a description of class Heater here.
4
+ *
5
+ * @author (your name)
6
+ * @version (a version number or a date)
7
+ */
8
+public class Heater
9
+{
10
+    // instance variables - replace the example below with your own
11
+    private int temperature;
12
+    private int min;
13
+    private int max;
14
+    private int increment;
15
+
16
+    /**
17
+     * Constructor for objects of class Heater
18
+     */
19
+    public Heater(int minTemp, int maxTemp)
20
+    {
21
+        // initialise instance variables
22
+        temperature = 15;
23
+        min = minTemp;
24
+        max = maxTemp;
25
+        increment = 5;
26
+    }
27
+
28
+    /**
29
+     * increase temperature by increment
30
+     */
31
+    public void warmer()
32
+    {
33
+        // put your code here
34
+        if(temperature + increment <= max){
35
+            temperature += increment;
36
+        } else {
37
+            System.out.print("cannot set temperature above max");
38
+        }
39
+    }
40
+    
41
+    /**
42
+     * decrease temperature by increment
43
+     */
44
+    public void cooler()
45
+    {
46
+        // put your code here
47
+        if(temperature - increment >= min){
48
+            temperature -= increment;
49
+        } else {
50
+            System.out.print("cannot set temperature below min");
51
+        }
52
+    }
53
+    
54
+    /**
55
+     * return temperature
56
+     */
57
+    public int getTemperature()
58
+    {
59
+        // put your code here
60
+        return temperature;
61
+    }
62
+    
63
+    /**
64
+     * increase temperature by increment
65
+     */
66
+    public void setIncrement(int newIncrement)
67
+    {
68
+        // put your code here
69
+        increment = newIncrement;
70
+    }
71
+}

+ 52
- 0
Person.java Vedi File

@@ -0,0 +1,52 @@
1
+
2
+/**
3
+ * Write a description of class Person here.
4
+ *
5
+ * @author (your name)
6
+ * @version (a version number or a date)
7
+ */
8
+public class Person
9
+{
10
+    // instance variables - replace the example below with your own
11
+    private String name;
12
+    private int age;
13
+    private String code;
14
+    private int credits;
15
+
16
+    /**
17
+     * Constructor for objects of class Person
18
+     */
19
+    public Person(String myName, int myAge)
20
+    {
21
+        // initialise instance variables
22
+        name = myName;
23
+        age = myAge;
24
+    }
25
+
26
+    /**
27
+     * accessor method returning name
28
+     */
29
+    public String getName()
30
+    {
31
+        // put your code here
32
+        return name;
33
+    }
34
+    
35
+    /**
36
+     * mutator method returning name
37
+     */
38
+    public void setAge(int myAge)
39
+    {
40
+        // put your code here
41
+        age = myAge;
42
+    }
43
+    
44
+    /**
45
+     * method to print details of name
46
+     */
47
+    public void printDetails()
48
+    {
49
+        // put your code here
50
+        System.out.println("The name of this person is " + name);
51
+    }
52
+}

+ 95
- 0
Student.java Vedi File

@@ -0,0 +1,95 @@
1
+/**
2
+ * The Student class represents a student in a
3
+ * student administration system.
4
+ * It holds the student details relevant in our context.
5
+ *
6
+ * @author Michael Kölling and David Barnes
7
+ * @version 2006.03.30
8
+ */
9
+public class Student {
10
+    // the student’s full name
11
+    private String name;
12
+    // the student ID
13
+    private String id;
14
+    // the amount of credits for study taken so far private int credits;
15
+    private int credits;
16
+    /**
17
+     * Create a new student with a given name and ID number.
18
+     */
19
+    public Student(String fullName, String studentID) {
20
+        name = fullName;
21
+        id = studentID;
22
+        credits = 0;
23
+    }
24
+
25
+    /**
26
+     * Return the full name of this student.
27
+     */
28
+    public String getName() {
29
+        return name; }
30
+
31
+    /**
32
+     * Set a new name for this student.
33
+     */
34
+    public void changeName(String newName) {
35
+        name = newName;
36
+    }
37
+
38
+    /**
39
+     * Return the student ID of this student.
40
+     */
41
+    public String getStudentID() {
42
+        return id; }
43
+
44
+    /**
45
+     * Add some credit points to the student’s
46
+     * accumulated credits.
47
+     */
48
+    public void addCredits(int newCreditPoints) {
49
+        credits += newCreditPoints;
50
+    }
51
+
52
+    /**
53
+     * Return the number of credit points this student
54
+     * has accumulated.
55
+     */
56
+    public int getCredits() {
57
+        return credits; }
58
+
59
+    /**
60
+     * Return the login name of this student.
61
+     * The login name is a combination
62
+     * of the first four characters of the
63
+     * student’s name and the first three
64
+     * characters of the student’s ID number.
65
+     */
66
+    public String getLoginName() {
67
+        if(name.length() < 4 && id.length() < 3){
68
+            return name + id;
69
+        } else if (name.length() < 4 && id.length() >= 3){
70
+            return name + id.substring(0,3);
71
+        } else if (name.length() >= 3 && id.length() <3){
72
+            return name.substring(0,4) + id;
73
+        } else {
74
+
75
+            return name.substring(0,4) + id.substring(0,3);
76
+        }
77
+    }
78
+
79
+    /**
80
+     * Print the student’s name and ID number
81
+     * to the output terminal.
82
+     */
83
+    public void print() {
84
+        System.out.println(name + " (" + id + ")");
85
+    }
86
+
87
+    /**
88
+     * Return the number of characters in this string.
89
+     */
90
+    public void length() {
91
+        if(name.length()<4 || id.length()<3){
92
+            System.out.println("length of name or ID are too small");    
93
+        }
94
+    }
95
+}

+ 189
- 3
TicketMachine.java Vedi File

@@ -17,19 +17,54 @@ 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
+    // Exercise 2.15
21
+    private int status;
20 22
 
21 23
     /**
22 24
      * Create a machine that issues tickets of the given price.
23 25
      * Note that the price must be greater than zero, and there
24 26
      * are no checks to ensure this.
25 27
      */
26
-    public TicketMachine(int ticketCost)
28
+    public TicketMachine()
27 29
     {
28
-        price = ticketCost;
30
+        //2.39 set price to 1000 and remove input parameter
31
+        //2.42 constructor with parameter to specify price
32
+        //2.58 Better ticket machine
33
+        price = 0;
29 34
         balance = 0;
30 35
         total = 0;
31 36
     }
32 37
 
38
+    //public TicketMachine()
39
+    //{
40
+        //2.42 constructor with default value of price
41
+        //price = 500;
42
+    //}
43
+
44
+    /*
45
+     * Exercise 2.30/2.41: Assign a value to the price field
46
+     */
47
+    public void setPrice(int ticketCost)
48
+    {
49
+        price = ticketCost;
50
+    }
51
+
52
+    /*
53
+     * Exercise 2.31: Add a value to the score field
54
+     */
55
+    public void increase(int points)
56
+    {
57
+        int score = points;
58
+    }
59
+
60
+    /*
61
+     * Exercise 2.32: Subtract a value from the price field
62
+     */
63
+    public void discount(int amount)
64
+    {
65
+        price = price - amount;
66
+    }
67
+
33 68
     /**
34 69
      * Return the price of a ticket.
35 70
      */
@@ -52,7 +87,55 @@ public class TicketMachine
52 87
      */
53 88
     public void insertMoney(int amount)
54 89
     {
55
-        balance = balance + amount;
90
+        if(amount >= 0){
91
+            balance = balance + amount;
92
+        }
93
+    }
94
+
95
+    /**
96
+     * Return the total amount of money collected by this machine.
97
+     */
98
+    //Exercise 2.24
99
+    public int getTotal()
100
+    {
101
+        return total;
102
+    }
103
+
104
+    /**
105
+     * Print message when incorrect amount of money is inserted
106
+     */
107
+    //Exercise 2.33
108
+    public void prompt()
109
+    {
110
+        System.out.println("Please insert the correct amount of money.");
111
+    }
112
+
113
+    /**
114
+     * Print message showing price of a ticket
115
+     */
116
+    //Exercise 2.34
117
+    public void showPrice()
118
+    {
119
+        System.out.println("The price of a ticket is " + price + " cents.");
120
+    }
121
+
122
+    /**
123
+     * Print message showing price of a ticket
124
+     */
125
+    //Exercise 2.40
126
+    public void empty()
127
+    {
128
+        total = 0;
129
+    }
130
+
131
+    /**
132
+     * Print message showing price of a ticket
133
+     */
134
+    //Exercise 2.55
135
+    public int emptyMachine()
136
+    {
137
+        return total;
138
+        //total=0;
56 139
     }
57 140
 
58 141
     /**
@@ -62,6 +145,7 @@ public class TicketMachine
62 145
      */
63 146
     public void printTicket()
64 147
     {
148
+        /*
65 149
         // Simulate the printing of a ticket.
66 150
         System.out.println("##################");
67 151
         System.out.println("# The BlueJ Line");
@@ -74,5 +158,107 @@ public class TicketMachine
74 158
         total = total + balance;
75 159
         // Clear the balance.
76 160
         balance = 0;
161
+         */
162
+
163
+        /*
164
+        if(balance >= price) {
165
+        System.out.println("##################");
166
+        System.out.println("# The BlueJ Line");
167
+        System.out.println("# Ticket");
168
+        System.out.println("# " + price + " cents.");
169
+        System.out.println("##################");
170
+        System.out.println();
171
+        // Update the total collected with the price.
172
+        total = total + price;
173
+        // Reduce the balance by the price.
174
+        balance = balance - price;
175
+        }
176
+        else {
177
+        System.out.println("You must insert at least: " +
178
+        (price - balance) + " more cents.");
179
+        }
180
+         */
181
+
182
+        int amountLeftToPay = price - balance;
183
+
184
+        if(amountLeftToPay <=0){
185
+            System.out.println("##################");
186
+            System.out.println("# The BlueJ Line");
187
+            System.out.println("# Ticket");
188
+            System.out.println("# " + price + " cents.");
189
+            System.out.println("##################");
190
+            System.out.println();
191
+            // Update the total collected with the price.
192
+            total = total + price;
193
+            // Reduce the balance by the price.
194
+            balance = balance - price;
195
+        } else {
196
+            System.out.println("You must insert at least: " +
197
+                (price - balance) + " more cents.");
198
+        }
77 199
     }
200
+
78 201
 }
202
+
203
+/* EXERCISES
204
+ * 2.1: call void printTicket()
205
+ * 2.2: price that was entered
206
+ * 2.3: The ticket prints regardless
207
+ * 2.4: [practice ticket machine]
208
+ * 2.5: Yes, price is different.
209
+ * 2.6: public class Student; public class LabClass
210
+ * 2.7: Order of statement matters
211
+ * 2.8: It is possible to leave this out, the class will be assigned default access modifier
212
+ * 2.9: fields: price, balance, total; 
213
+ * constructors: public TicketMachine(int ticketCost); 
214
+ * methods: getPrice, getBalance, insertMoney, printTicket
215
+ * 2.10: no return type defined
216
+ * 2.11: int, Student, Server
217
+ * 2.12: alive, tutor, game
218
+ * 2.13: Order of statement matters
219
+ * 2.14: field declaration requires a semicolon
220
+ * 2.15: Shown in code
221
+ * 2.16: Student 
222
+ * 2.17: title of type String, and price of type double
223
+ * 2.18: fields: title, price, author, publisher, publish date
224
+ *  types: string, double, string, string, date 
225
+ * 2.19: name = petsName;
226
+ * 2.20: The problem when adding int is the ticketCost was not recognized as price
227
+ *  Assignments should not re-declare the field type. This needs to be done in fields section.
228
+ * 2.21: getPrice returns a passed value and getBalance returns a set value.
229
+ * 2.22: getBalance - "How much money do I owe or am I owed?"
230
+ * 2.23: No, the variable does not need to have the same name as the method
231
+ * 2.24: new getTotal method created above.
232
+ * 2.25: error "missing return statement"
233
+ * 2.26: getPrice returns an integer, printTicket does not return anything (void)
234
+ * 2.27: They are actions and don't return anything.
235
+ * 2.28: (Practicing methods)
236
+ * 2.29: There is a return type included
237
+ * 2.30-2.34: Shown in Code
238
+ * 2.35: Different outputs because the price is variable
239
+ * 2.36: "price" string would display instead of a variable
240
+ * 2.37: Same output as 2.36
241
+ * 2.38: No, they cannot show variable prices because they are outputing a string
242
+ * 2.39: Objects cannot pass a parameter to the ticketmachine
243
+ * 2.40: Mutator method
244
+ * 2.41: Setter method
245
+ * 2.42: Shown in code
246
+ * 2.43: (Practicing methods)
247
+ * 2.44: negative values do not affect balance
248
+ * 2.45: Booleans are well suited to be controlled by a type with only two different values
249
+ * 2.46: Shown in code
250
+ * 2.47: No because there is a check to make sure price is not greater than balance
251
+ * 2.48: +, -, /, *, %
252
+ * 2.49: saving = price * discount;
253
+ * 2.50: mean = total / count;
254
+ * 2.51: if(price > budget){System.out.print("Too expensive")}else {System.out.print("Just right")}
255
+ * 2.52: if(price > budget){System.out.print("Too expensive, your budget is only " + budget)}else {System.out.print("Just right")}
256
+ * 2.53: it is setting balance as 0 before returning it.
257
+ * 2.54: return statement ends the execution of a method
258
+ * 2.55: Shown in code
259
+ * 2.56: It is an accessor because method ends after return statement
260
+ * 2.57: Shown in code
261
+ * 2.58: setPrice method can be called to set price based on user selection
262
+ */
263
+
264
+

+ 42
- 14
bluej.pkg Vedi File

@@ -1,20 +1,20 @@
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
2
+editor.fx.0.height=709
3
+editor.fx.0.width=626
4
+editor.fx.0.x=619
5
+editor.fx.0.y=26
6
+objectbench.height=164
7 7
 objectbench.width=461
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=595
13
+package.editor.y=61
14 14
 package.frame.height=600
15 15
 package.frame.width=800
16 16
 package.numDependencies=0
17
-package.numTargets=1
17
+package.numTargets=5
18 18
 package.showExtends=true
19 19
 package.showUses=true
20 20
 project.charset=UTF-8
@@ -24,9 +24,37 @@ readme.width=47
24 24
 readme.x=10
25 25
 readme.y=10
26 26
 target1.height=50
27
-target1.name=TicketMachine
27
+target1.name=Book
28 28
 target1.showInterface=false
29 29
 target1.type=ClassTarget
30
-target1.width=120
31
-target1.x=80
32
-target1.y=50
30
+target1.width=80
31
+target1.x=220
32
+target1.y=120
33
+target2.height=50
34
+target2.name=Heater
35
+target2.showInterface=false
36
+target2.type=ClassTarget
37
+target2.width=80
38
+target2.x=20
39
+target2.y=200
40
+target3.height=50
41
+target3.name=TicketMachine
42
+target3.showInterface=false
43
+target3.type=ClassTarget
44
+target3.width=120
45
+target3.x=80
46
+target3.y=50
47
+target4.height=50
48
+target4.name=Student
49
+target4.showInterface=false
50
+target4.type=ClassTarget
51
+target4.width=80
52
+target4.x=120
53
+target4.y=120
54
+target5.height=50
55
+target5.name=Person
56
+target5.showInterface=false
57
+target5.type=ClassTarget
58
+target5.width=80
59
+target5.x=20
60
+target5.y=120

+ 42
- 14
package.bluej Vedi File

@@ -1,20 +1,20 @@
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
2
+editor.fx.0.height=709
3
+editor.fx.0.width=626
4
+editor.fx.0.x=619
5
+editor.fx.0.y=26
6
+objectbench.height=164
7 7
 objectbench.width=461
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=595
13
+package.editor.y=61
14 14
 package.frame.height=600
15 15
 package.frame.width=800
16 16
 package.numDependencies=0
17
-package.numTargets=1
17
+package.numTargets=5
18 18
 package.showExtends=true
19 19
 package.showUses=true
20 20
 project.charset=UTF-8
@@ -24,9 +24,37 @@ readme.width=47
24 24
 readme.x=10
25 25
 readme.y=10
26 26
 target1.height=50
27
-target1.name=TicketMachine
27
+target1.name=Book
28 28
 target1.showInterface=false
29 29
 target1.type=ClassTarget
30
-target1.width=120
31
-target1.x=80
32
-target1.y=50
30
+target1.width=80
31
+target1.x=220
32
+target1.y=120
33
+target2.height=50
34
+target2.name=Heater
35
+target2.showInterface=false
36
+target2.type=ClassTarget
37
+target2.width=80
38
+target2.x=20
39
+target2.y=200
40
+target3.height=50
41
+target3.name=TicketMachine
42
+target3.showInterface=false
43
+target3.type=ClassTarget
44
+target3.width=120
45
+target3.x=80
46
+target3.y=50
47
+target4.height=50
48
+target4.name=Student
49
+target4.showInterface=false
50
+target4.type=ClassTarget
51
+target4.width=80
52
+target4.x=120
53
+target4.y=120
54
+target5.height=50
55
+target5.name=Person
56
+target5.showInterface=false
57
+target5.type=ClassTarget
58
+target5.width=80
59
+target5.x=20
60
+target5.y=120