|
| 1 | +\com\in28minutes\program1\Planet.java |
| 2 | +``` |
| 3 | +package com.in28minutes.program1; |
| 4 | +
|
| 5 | +//Question Answered : What is Class? What is Object? |
| 6 | +public class Planet { |
| 7 | + // name |
| 8 | + // distance from sun |
| 9 | +
|
| 10 | + public static void main(String[] args) { |
| 11 | + Planet earth = new Planet(); |
| 12 | + Planet mars = new Planet(); |
| 13 | + Planet venus = new Planet(); |
| 14 | + } |
| 15 | +} |
| 16 | +
|
| 17 | +// Exercise 1 -> Create another instance of planet. |
| 18 | +// Exercise 2 -> What are the other characteristics of a planet? |
| 19 | +// Debug - Main is starting point of the program |
| 20 | +``` |
| 21 | +\com\in28minutes\program2\Aeroplane.java |
| 22 | +``` |
| 23 | +package com.in28minutes.program2; |
| 24 | +
|
| 25 | +//Learning : What is state of object? Each object has individual State. |
| 26 | +public class Aeroplane { |
| 27 | + int currentSpeed; |
| 28 | +
|
| 29 | + public static void main(String[] args) { |
| 30 | + Aeroplane aeroplane1 = new Aeroplane(); |
| 31 | + aeroplane1.currentSpeed = 500; |
| 32 | + Aeroplane aeroplane2 = new Aeroplane(); |
| 33 | + aeroplane2.currentSpeed = 0; |
| 34 | + Aeroplane aeroplane3 = new Aeroplane(); |
| 35 | + aeroplane3.currentSpeed = 600; |
| 36 | +
|
| 37 | + aeroplane2.currentSpeed = 300; |
| 38 | + } |
| 39 | +} |
| 40 | +
|
| 41 | +// int currentSpeed |
| 42 | +
|
| 43 | +// Exercise 1 -> Create another instance of Aeroplance. |
| 44 | +// Exercise 2 -> Change the speed of aeroplane3 to 1000? |
| 45 | +// Debug and see the values |
| 46 | +// We are breaking a few good programming principles!!! |
| 47 | +``` |
| 48 | +\com\in28minutes\program3\Cycle.java |
| 49 | +``` |
| 50 | +package com.in28minutes.program3; |
| 51 | +
|
| 52 | +//Learning : What is state of object? Each object has individual State. |
| 53 | +//Learning : We use methods to change state of object. Behavior. |
| 54 | +public class Cycle { |
| 55 | + int currentSpeed; |
| 56 | +
|
| 57 | + void increaseSpeed() { |
| 58 | + currentSpeed = currentSpeed + 10; |
| 59 | + } |
| 60 | +
|
| 61 | + public static void main(String[] args) { |
| 62 | + Cycle cycle1 = new Cycle(); |
| 63 | + cycle1.currentSpeed = 500; |
| 64 | +
|
| 65 | + Cycle cycle2 = new Cycle(); |
| 66 | + cycle2.currentSpeed = 600; |
| 67 | +
|
| 68 | + cycle1.increaseSpeed(); |
| 69 | +
|
| 70 | + cycle2.increaseSpeed(); |
| 71 | + } |
| 72 | +} |
| 73 | +
|
| 74 | +// Focus on Assignment Operator |
| 75 | +// What is void? |
| 76 | +// Debug and see the values |
| 77 | +// We are breaking a few good programming principles!!! |
| 78 | +// Exercise 1 -> Create a new method in cycle to decrease speed. |
| 79 | +``` |
| 80 | +\com\in28minutes\program4\MotorBike.java |
| 81 | +``` |
| 82 | +package com.in28minutes.program4; |
| 83 | +
|
| 84 | +//Learning : Creating this class from scratch. From Zero |
| 85 | +//Learning : Creating more methods and more variables |
| 86 | +public class MotorBike { |
| 87 | + int currentSpeed; |
| 88 | + int currentGear; |
| 89 | +
|
| 90 | + void increaseSpeed() { |
| 91 | + currentSpeed = currentSpeed + 10; |
| 92 | + } |
| 93 | +
|
| 94 | + void decreaseSpeed() { |
| 95 | + currentSpeed = currentSpeed - 10; |
| 96 | + } |
| 97 | +
|
| 98 | + void nextGear() { |
| 99 | + currentGear = currentGear + 1; |
| 100 | + } |
| 101 | +
|
| 102 | + void prevGear() { |
| 103 | + currentGear = currentGear - 1; |
| 104 | + } |
| 105 | +
|
| 106 | + public static void main(String[] args) { |
| 107 | + MotorBike ducati = new MotorBike(); |
| 108 | + ducati.currentSpeed = 500; |
| 109 | +
|
| 110 | + MotorBike honda = new MotorBike(); |
| 111 | + honda.currentSpeed = 600; |
| 112 | +
|
| 113 | + ducati.increaseSpeed(); |
| 114 | +
|
| 115 | + honda.increaseSpeed(); |
| 116 | + } |
| 117 | +} |
| 118 | +
|
| 119 | +// Debug and see the values |
| 120 | +// We are breaking a few good programming principles!!! |
| 121 | +// We do not have limits on Speed or Gears!!! We will get there soon! |
| 122 | +// Exercise 1 -> Create the prevGear method |
| 123 | +``` |
| 124 | +\com\in28minutes\program5\MotorBike.java |
| 125 | +``` |
| 126 | +package com.in28minutes.program5; |
| 127 | +
|
| 128 | +//Learning : Special Method => Constructor |
| 129 | +//Learning : Better Encapsulation |
| 130 | +public class MotorBike { |
| 131 | + int currentSpeed; |
| 132 | + int currentGear; |
| 133 | +
|
| 134 | + public MotorBike(int currentSpeed) { |
| 135 | + this.currentSpeed = currentSpeed; |
| 136 | + } |
| 137 | +
|
| 138 | + void increaseSpeed() { |
| 139 | + currentSpeed = currentSpeed + 10; |
| 140 | + } |
| 141 | +
|
| 142 | + void decreaseSpeed() { |
| 143 | + currentSpeed = currentSpeed - 10; |
| 144 | + } |
| 145 | +
|
| 146 | + void nextGear() { |
| 147 | + currentGear = currentGear + 1; |
| 148 | + } |
| 149 | +
|
| 150 | + void prevGear() { |
| 151 | + currentGear = currentGear - 1; |
| 152 | + } |
| 153 | +
|
| 154 | + public static void main(String[] args) { |
| 155 | + MotorBike ducati = new MotorBike(500); |
| 156 | + MotorBike honda = new MotorBike(600); |
| 157 | + ducati.increaseSpeed(); |
| 158 | + honda.increaseSpeed(); |
| 159 | + } |
| 160 | +} |
| 161 | +
|
| 162 | +// add 500 to line 30 and show how |
| 163 | +// We are breaking a few good programming principles!!! |
| 164 | +// How is constructor different from a normal method? |
| 165 | +// Default value for a object member variable |
| 166 | +// This is the first program that we created with good encapsulation! |
| 167 | +// Exercise 1 -> Create a constructor with both current speed and current gear! |
| 168 | +// Exercise 2 -> Enhance earlier examples with constructors and use them! |
| 169 | +``` |
0 commit comments