day-34: looping statements in java
== --> Compares Values (Variables) Can we compare Objects? What is the difference between variables and objects? What is Variable? Named Container to store Data What is Object? Real Time Entity / Physical Entity / Sample of a class / Instance of a class / Memory Reference of a class / Combination of States and Behaviours Can we compare Objects? Yes, we can Objects. Passing Objects as arguments: == compare --> positive, negative, neutral equals --> true / false what is this keyword? this keyword refers to current object. How do you call non-static methods in a class? By using objects. object.method_name() player1.equals(player2); player1.equals(player2); public boolean equals(Players player2){ return false; } Where cant we use this keyword? Inside static blocks - static methods Where can we use this keyword? non-static methods == --> equals() method Primitive Datatypes --> == Non-primitive datatypes --> equals() Non-Primitive Datatypes are nothing but classes. We can pass the object as parameter: package learning1; public class Muthu { int capacity; int price; public static void main(String[] args) { Silambarasan bike = new Silambarasan(); Muthu muthu = new Muthu(); muthu.travel(bike); Muthu waterBottle = new Muthu(); waterBottle.capacity = 500; waterBottle.price = 50; muthu.drink(waterBottle); } private void drink(Muthu waterBottle) { System.out.println("Price is " + waterBottle.price); } private void travel(Silambarasan bike) { bike.ride(); System.out.println(bike.name); bike.fill_petrol(); //bike.sell(); } } package learning1; public class Silambarasan { String name = "bajaj"; private void sell(){ System.out.println("Planning to sell "); } public void ride(){ System.out.println("Long Ride to Pondy"); } public void fill_petrol(){ System.out.println("Tank Full"); } public void service_bike(){ System.out.println("Bike Service"); } } Switch Case: int day = 3; if (day == 1) { System.out.println("Monday"); } else if (day == 2) { System.out.println("Tuesday"); } else if (day == 3) { System.out.println("Wednesday"); } else if (day == 4) { System.out.println("Thursday"); } else if (day == 5) { System.out.println("Friday"); } else if (day == 6) { System.out.println("Saturday"); } else if (day == 7) { System.out.println("Sunday"); } else { System.out.println("Invalid day number"); } public class Control_FlowState { public static void main(String[] args) { int day = 3; switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; default: System.out.println("Invalid day number"); break; } } } Work around: switch-case: Task: 1) day = 1, day = 2, day = 3 Wednesday 2) Remove break from case 1, from case 2, from case 3 If we remove the break statement, process execute the all the conditions. Monday Tuesday Wednesday Invalid day number 3) Move default just above any case block. No, Changes 4) Remove break statement from default. No, Changes 5) Instead of using int day, try using float values. java.lang.Error: Unresolved compilation problem ----------------------------- End of the Blog ----------------------

== --> Compares Values (Variables)
Can we compare Objects?
What is the difference between variables and objects?
What is Variable?
Named Container to store Data
What is Object?
Real Time Entity / Physical Entity / Sample of a class / Instance of a class / Memory Reference of a class / Combination of States and Behaviours
Can we compare Objects?
Yes, we can Objects.
Passing Objects as arguments:
==
compare --> positive, negative, neutral
equals --> true / false
what is this keyword?
this keyword refers to current object.
How do you call non-static methods in a class?
By using objects.
object.method_name()
player1.equals(player2);
player1.equals(player2);
public boolean equals(Players player2){
return false;
}
Where cant we use this keyword?
Inside static blocks - static methods
Where can we use this keyword?
non-static methods
== --> equals() method
Primitive Datatypes --> ==
Non-primitive datatypes --> equals()
Non-Primitive Datatypes are nothing but classes.
We can pass the object as parameter:
package learning1;
public class Muthu {
int capacity;
int price;
public static void main(String[] args) {
Silambarasan bike = new Silambarasan();
Muthu muthu = new Muthu();
muthu.travel(bike);
Muthu waterBottle = new Muthu();
waterBottle.capacity = 500;
waterBottle.price = 50;
muthu.drink(waterBottle);
}
private void drink(Muthu waterBottle) {
System.out.println("Price is " + waterBottle.price);
}
private void travel(Silambarasan bike) {
bike.ride();
System.out.println(bike.name);
bike.fill_petrol();
//bike.sell();
}
}
package learning1;
public class Silambarasan {
String name = "bajaj";
private void sell(){
System.out.println("Planning to sell ");
}
public void ride(){
System.out.println("Long Ride to Pondy");
}
public void fill_petrol(){
System.out.println("Tank Full");
}
public void service_bike(){
System.out.println("Bike Service");
}
}
Switch Case:
int day = 3;
if (day == 1) {
System.out.println("Monday");
} else if (day == 2) {
System.out.println("Tuesday");
} else if (day == 3) {
System.out.println("Wednesday");
} else if (day == 4) {
System.out.println("Thursday");
} else if (day == 5) {
System.out.println("Friday");
} else if (day == 6) {
System.out.println("Saturday");
} else if (day == 7) {
System.out.println("Sunday");
} else {
System.out.println("Invalid day number");
}
public class Control_FlowState {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day number");
break;
}
}
}
Work around:
switch-case: Task:
1) day = 1, day = 2, day = 3
Wednesday
2) Remove break from case 1, from case 2, from case 3
If we remove the break statement, process execute the all the conditions.
Monday
Tuesday
Wednesday
Invalid day number
3) Move default just above any case block.
No, Changes
4) Remove break statement from default.
No, Changes
5) Instead of using int day, try using float values.
java.lang.Error: Unresolved compilation problem
----------------------------- End of the Blog ----------------------