nafis nibir пре 6 година
родитељ
комит
241c713dbf
7 измењених фајлова са 324 додато и 26 уклоњено
  1. 92
    0
      Book.java
  2. 58
    0
      Heater.java
  3. 58
    0
      Person.java
  4. 104
    14
      TicketMachine.java
  5. 6
    6
      bluej.pkg
  6. BIN
      doc/BlueJ-objects-first-ch2.pdf
  7. 6
    6
      package.bluej

+ 92
- 0
Book.java Прегледај датотеку

@@ -0,0 +1,92 @@
1
+
2
+/**
3
+ * Write a description of class Book here.
4
+ *
5
+ * @author (your name)
6
+ * @version (a version number or a date)
7
+ */
8
+public class Book
9
+{
10
+    // instance variables - replace the example below with your own
11
+    private String author;
12
+    private String title;
13
+    private int pages;
14
+    private String refNumber;
15
+    private int borrowed;
16
+
17
+    /**
18
+     * Constructor for objects of class Book
19
+     */
20
+    public Book(String bookAuthor, String bookTitle, int bookPages)
21
+    {
22
+        // initialise instance variables
23
+        author = bookAuthor;
24
+        title = bookTitle;
25
+        pages = bookPages;
26
+        refNumber = "";
27
+        borrow();
28
+    }
29
+    
30
+    public void borrow(){
31
+        borrowed++;
32
+    }
33
+    
34
+    public int getBorrow(){
35
+        return borrowed;
36
+    }
37
+    
38
+    public void setRefNumber(String ref){
39
+        
40
+        if(ref.length()>=3){
41
+          refNumber = ref;
42
+        } else {
43
+           System.out.println("Reference must be at least 3 characters."); 
44
+        }
45
+    }
46
+    
47
+    public String getRefNumber(){
48
+        
49
+        return refNumber;
50
+        
51
+    }
52
+
53
+    /**
54
+     * An example of a method - replace this comment with your own
55
+     *
56
+     * @param  y  a sample parameter for a method
57
+     * @return    the sum of x and y
58
+     */
59
+    public String printAuthor()
60
+    {
61
+        // put your code here
62
+        return author;
63
+    }
64
+    
65
+    public int printPages()
66
+    {
67
+        // put your code here
68
+        return pages;
69
+    }
70
+    
71
+    public String printTitle()
72
+    {
73
+        // put your code here
74
+        return title;
75
+    }
76
+    
77
+    public void printDetails(){
78
+        
79
+      System.out.println("The title is : " + title);
80
+      System.out.println("The author is : " + author);
81
+      System.out.println("There are " + pages + " pages");
82
+      if (refNumber.length()==0){
83
+         System.out.println("ZZZ"); 
84
+        } else {
85
+         System.out.println("The reference number is " + refNumber);   
86
+        }
87
+     
88
+
89
+    }
90
+}
91
+    
92
+

+ 58
- 0
Heater.java Прегледај датотеку

@@ -0,0 +1,58 @@
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 minimum, int maximum)
20
+    {
21
+        // initialise instance variables
22
+        temperature = 15;
23
+        min = minimum;
24
+        max = maximum;
25
+        increment = 5;
26
+        
27
+    }
28
+    
29
+    public void setIncrement (int newIncrement){
30
+        if (newIncrement>0){
31
+        increment = newIncrement;
32
+    } else {
33
+        System.out.println("error");
34
+    }
35
+    }
36
+
37
+    /**
38
+     * An example of a method - replace this comment with your own
39
+     *
40
+     * @param  y  a sample parameter for a method
41
+     * @return    the sum of x and y
42
+     */
43
+    public void warmer()
44
+    {
45
+        // put your code here
46
+        temperature+=5;
47
+    }
48
+    
49
+    public void cooler(){
50
+        temperature-=5;
51
+    }
52
+    
53
+    public int getTemperature(){
54
+        return temperature;
55
+    }
56
+        
57
+        
58
+}

+ 58
- 0
Person.java Прегледај датотеку

@@ -0,0 +1,58 @@
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
+     * public Module(String moduleCode){
28
+     *     
29
+     *     code = moduleCode;
30
+     *     
31
+     *     }
32
+     */
33
+
34
+    /*
35
+     * An example of a method - replace this comment with your own
36
+     *
37
+     * @param  y  a sample parameter for a method
38
+     * @return    the sum of x and y
39
+     */
40
+    public String getName()
41
+    {
42
+        // put your code here
43
+        return name;
44
+    }
45
+    
46
+    public void setAge(int newAge)
47
+    {
48
+        // put your code here
49
+        age = newAge;
50
+    }
51
+    
52
+    public String printDetails()
53
+    {
54
+        // put your code here
55
+        return "The name of this person is "+ name;
56
+    }
57
+    
58
+}

+ 104
- 14
TicketMachine.java Прегледај датотеку

@@ -17,18 +17,61 @@ 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
+    // yumhohum
21
+    private int status;
22
+    private int score;
20 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;
32
+        balance = 0;
33
+        total = 0;
34
+    }
35
+
36
+    public TicketMachine(int ticketPrice)
37
+    {
38
+        price = ticketPrice;
29 39
         balance = 0;
30 40
         total = 0;
31 41
     }
42
+    // saving = price * discount;
43
+    // mean = total/count;
44
+    //if (price>budget){return "Too expensive, budget is :"+budget;}else {return "Just right";}
45
+    //
46
+    /**
47
+     * Empties money in the machine
48
+     */
49
+
50
+    public int emptyMachine(){
51
+        total = 0;
52
+        return total;
53
+    }
54
+
55
+    /**
56
+     * Increase score by the given number of points.
57
+     */
58
+    public void increase(int points){
59
+        score += points; 
60
+    }
61
+
62
+    /**
63
+     * Reduce price by the given amount.
64
+     */
65
+    public void discount(int amount){
66
+        price-=amount;
67
+    }
68
+
69
+    /**
70
+     * Set price of ticket.
71
+     */
72
+    public void setPrice(int ticketCost){
73
+        price = ticketCost;
74
+    }
32 75
 
33 76
     /**
34 77
      * Return the price of a ticket.
@@ -48,11 +91,24 @@ public class TicketMachine
48 91
     }
49 92
 
50 93
     /**
94
+     * Return the total.
95
+     */
96
+    public int getTotal(){
97
+        return total;  
98
+    }
99
+
100
+    /**
51 101
      * Receive an amount of money in cents from a customer.
52 102
      */
53 103
     public void insertMoney(int amount)
54 104
     {
55
-        balance = balance + amount;
105
+        if(amount > 0) {
106
+            balance = balance + amount;
107
+        }
108
+        else {
109
+            System.out.println("Use a positive amount: " +
110
+                amount);
111
+        }
56 112
     }
57 113
 
58 114
     /**
@@ -62,17 +118,51 @@ public class TicketMachine
62 118
      */
63 119
     public void printTicket()
64 120
     {
121
+        int amountLeftToPay = price - balance;
122
+       
65 123
         // Simulate the printing of a ticket.
66
-        System.out.println("##################");
67
-        System.out.println("# The BlueJ Line");
68
-        System.out.println("# Ticket");
69
-        System.out.println("# " + price + " cents.");
70
-        System.out.println("##################");
71
-        System.out.println();
72
-
73
-        // Update the total collected with the balance.
74
-        total = total + balance;
75
-        // Clear the balance.
76
-        balance = 0;
124
+        if (amountLeftToPay<=0){
125
+            System.out.println("##################");
126
+            System.out.println("# The BlueJ Line");
127
+            System.out.println("# Ticket");
128
+            System.out.println("# " + price + " cents.");
129
+            System.out.println("##################");
130
+            System.out.println();
131
+
132
+            // Update the total collected with the balance.
133
+            total = total + balance;
134
+            // Clear the balance.
135
+            balance = balance-price;
136
+        }else {
137
+            System.out.println("You must insert at least: " +
138
+                amountLeftToPay+ " cents.");
139
+        }
140
+    }
141
+
142
+    public int refundBalance() {
143
+        int amountToRefund = balance;
144
+        //amountToRefund = balance; 
145
+        //balance = 0;
146
+
147
+        return amountToRefund;
148
+        // balance = 0; anything after return is unreachable
149
+    }
150
+
151
+    /**
152
+     * Print price of tickets
153
+     */
154
+
155
+    public void showPrice(){
156
+        System.out.println("The price of a ticket is " + price + " cents.");
157
+
158
+    }
159
+
160
+    /**
161
+     * Print instructions for amount of money
162
+     */
163
+
164
+    public void prompt(){
165
+        System.out.println("Please insert the correct amount of money.");
166
+
77 167
     }
78 168
 }

+ 6
- 6
bluej.pkg Прегледај датотеку

@@ -1,16 +1,16 @@
1 1
 #BlueJ package file
2
-editor.fx.0.height=722
2
+editor.fx.0.height=714
3 3
 editor.fx.0.width=800
4
-editor.fx.0.x=709
5
-editor.fx.0.y=113
4
+editor.fx.0.x=689
5
+editor.fx.0.y=23
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=144
13
+package.editor.y=177
14 14
 package.frame.height=600
15 15
 package.frame.width=800
16 16
 package.numDependencies=0

BIN
doc/BlueJ-objects-first-ch2.pdf Прегледај датотеку


+ 6
- 6
package.bluej Прегледај датотеку

@@ -1,16 +1,16 @@
1 1
 #BlueJ package file
2
-editor.fx.0.height=722
2
+editor.fx.0.height=714
3 3
 editor.fx.0.width=800
4
-editor.fx.0.x=709
5
-editor.fx.0.y=113
4
+editor.fx.0.x=689
5
+editor.fx.0.y=23
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=144
13
+package.editor.y=177
14 14
 package.frame.height=600
15 15
 package.frame.width=800
16 16
 package.numDependencies=0