Skip to content

Commit f283175

Browse files
authored
Update Level1.md
1 parent 84dfe60 commit f283175

1 file changed

Lines changed: 63 additions & 6 deletions

File tree

Level1.md

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,15 @@ public class Cycle {
7676
// Debug and see the values
7777
// We are breaking a few good programming principles!!!
7878
// Exercise 1 -> Create a new method in cycle to decrease speed.
79+
// We are going a little slow
7980
```
8081
\com\in28minutes\program4\MotorBike.java
8182
```
8283
package com.in28minutes.program4;
8384
84-
//Learning : Creating this class from scratch. From Zero
85+
//Learning : Creating this class from scratch. From Zero
8586
//Learning : Creating more methods and more variables
87+
//Learning : Print current state of an object
8688
public class MotorBike {
8789
int currentSpeed;
8890
int currentGear;
@@ -99,8 +101,9 @@ public class MotorBike {
99101
currentGear = currentGear + 1;
100102
}
101103
102-
void prevGear() {
103-
currentGear = currentGear - 1;
104+
@Override
105+
public String toString() {
106+
return "MotorBike [currentSpeed=" + currentSpeed + ", currentGear=" + currentGear + "]";
104107
}
105108
106109
public static void main(String[] args) {
@@ -111,13 +114,16 @@ public class MotorBike {
111114
honda.currentSpeed = 600;
112115
113116
ducati.increaseSpeed();
114-
115117
honda.increaseSpeed();
118+
ducati.nextGear();
119+
System.out.println(ducati);
116120
}
121+
117122
}
118123
119124
// Debug and see the values
120125
// We are breaking a few good programming principles!!!
126+
// State of an object should be changed only by a method on the object
121127
// We do not have limits on Speed or Gears!!! We will get there soon!
122128
// Exercise 1 -> Create the prevGear method
123129
```
@@ -132,6 +138,9 @@ public class MotorBike {
132138
int currentGear;
133139
134140
public MotorBike(int currentSpeed) {
141+
// currentSpeed is called parameter
142+
// this is a special reference variable to access
143+
// values from current object
135144
this.currentSpeed = currentSpeed;
136145
}
137146
@@ -151,6 +160,11 @@ public class MotorBike {
151160
currentGear = currentGear - 1;
152161
}
153162
163+
@Override
164+
public String toString() {
165+
return "MotorBike [currentSpeed=" + currentSpeed + ", currentGear=" + currentGear + "]";
166+
}
167+
154168
public static void main(String[] args) {
155169
MotorBike ducati = new MotorBike(500);
156170
MotorBike honda = new MotorBike(600);
@@ -159,11 +173,54 @@ public class MotorBike {
159173
}
160174
}
161175
162-
// add 500 to line 30 and show how
163-
// We are breaking a few good programming principles!!!
164176
// How is constructor different from a normal method?
165177
// Default value for a object member variable
166178
// This is the first program that we created with good encapsulation!
179+
// There are still minor things that need to fixed! We will discuss them next!
180+
// add 500 to line 30 and show how eclipse can do magic
167181
// Exercise 1 -> Create a constructor with both current speed and current gear!
168182
// Exercise 2 -> Enhance earlier examples with constructors and use them!
169183
```
184+
\com\in28minutes\program6\MotorBike.java
185+
```
186+
package com.in28minutes.program6;
187+
188+
//Introduce If Condition
189+
public class MotorBike {
190+
int currentSpeed;
191+
int currentGear;
192+
193+
public MotorBike(int currentSpeed) {
194+
this.currentSpeed = currentSpeed;
195+
}
196+
197+
void increaseSpeed() {
198+
currentSpeed = currentSpeed + 10;
199+
}
200+
201+
void decreaseSpeed() {
202+
currentSpeed = currentSpeed - 10;
203+
}
204+
205+
void nextGear() {
206+
currentGear = currentGear + 1;
207+
}
208+
209+
void prevGear() {
210+
currentGear = currentGear - 1;
211+
}
212+
213+
@Override
214+
public String toString() {
215+
return "MotorBike [currentSpeed=" + currentSpeed + ", currentGear=" + currentGear + "]";
216+
}
217+
218+
public static void main(String[] args) {
219+
MotorBike ducati = new MotorBike(500);
220+
MotorBike honda = new MotorBike(600);
221+
ducati.increaseSpeed();
222+
honda.increaseSpeed();
223+
ducati.currentSpeed = ducati.currentSpeed + 10;
224+
}
225+
}
226+
```

0 commit comments

Comments
 (0)