Skip to content

Commit 0c253ec

Browse files
author
Rocky Bhai
committed
Uploaded on 8th-Feb - 1st commit.
1 parent 937deb0 commit 0c253ec

File tree

6 files changed

+216
-1
lines changed

6 files changed

+216
-1
lines changed

.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
<classpathentry kind="src" path="file-handling"/>
1313
<classpathentry kind="src" path="handlingexception"/>
1414
<classpathentry kind="src" path="AbstractClassTest"/>
15+
<classpathentry kind="src" path="innerclasses"/>
1516
<classpathentry kind="output" path="bin"/>
1617
</classpath>

file-handling/filemanagement/BufferedReader1.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ public static void main(String[] args) {
4040
System.out.println("File not found: " + f1.toString());
4141
} catch (IOException e) {
4242
System.out.println("Unable to read file: " + f1.toString());
43-
} finally {
43+
}
44+
// finally will always be executed towards the end of the exception
45+
// to help the program end gracefully.
46+
47+
finally {
4448
try {
4549
br.close();
4650
} catch (IOException e) {

handlingexception/exceptionmanagement/HandlingException.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ public static void main(String[] args) throws IOException {
2828
// on the same line as the main method.
2929
fr1.close();
3030
} catch (FileNotFoundException e1) {
31+
System.out.println("Inside the exception...");
3132
System.out.println(e1.getMessage());
33+
} finally {
34+
System.out.println("Program closing successfully!");
3235
}
3336
}
3437
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package innerclass;
2+
3+
// Test for inner classes
4+
5+
public class AppClass {
6+
7+
public static void main(String[] args) {
8+
9+
Robot rone = new Robot(1);
10+
rone.start();
11+
12+
Robot.Brain bone = rone.new Brain();
13+
bone.think();
14+
15+
Robot.Battery battery = new Robot.Battery();
16+
battery.charge();
17+
}
18+
}

innerclasses/innerclass/Robot.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package innerclass;
2+
3+
// Creating Brain and Battery class inside Robot class.
4+
// Creating a Temp class within a method.
5+
6+
public class Robot {
7+
8+
private int id;
9+
10+
public class Brain {
11+
public void think() {
12+
System.out.println("Robot " + id + ": is thinking...");
13+
}
14+
}
15+
16+
public static class Battery {
17+
public void charge() {
18+
System.out.println("Battery of Robot is charging...");
19+
}
20+
}
21+
22+
public Robot(int id) {
23+
this.id = id;
24+
}
25+
26+
public void start() {
27+
System.out.println("Robot " + id + ": is starting...");
28+
29+
Brain bone = new Brain();
30+
bone.think();
31+
32+
String name = "Robert King";
33+
34+
// Local class within a method.
35+
36+
class Temp
37+
{
38+
public void printSomething()
39+
{
40+
System.out.println("ID is " + id);
41+
System.out.println("My name is " + name);
42+
}
43+
}
44+
45+
Temp t1 = new Temp();
46+
t1.printSomething();
47+
48+
Battery btwo = new Battery();
49+
btwo.charge();
50+
}
51+
}

javabasic/sample/EqualsMethod.java

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package sample;
2+
3+
// Program to test the equals method.
4+
// You can use the equals method of the Object class or the equals
5+
// method of the String or other non primitive class or can override
6+
// the equals method in the class whose object is being created.
7+
8+
class Personnel {
9+
private int id;
10+
private String name;
11+
12+
public Personnel(int id, String name) {
13+
this.id = id;
14+
this.name = name;
15+
}
16+
17+
@Override
18+
public String toString() {
19+
System.out.println("Inside toString() method.");
20+
return "Person [id=" + id + ", name=" + name + "]";
21+
}
22+
23+
@Override
24+
public int hashCode() {
25+
System.out.println("Inside hash code");
26+
final int prime = 31;
27+
int result = 1;
28+
int result1 = 0;
29+
result = prime * result + id;
30+
System.out.println(result);
31+
result1 = prime * result + name.hashCode();
32+
System.out.println(result1);
33+
result = prime * result + ((name == null) ? 0 : name.hashCode());
34+
System.out.println(result);
35+
System.out.println(name.hashCode());
36+
return result;
37+
}
38+
39+
// Right click, select "Source" and select "Generate hashCode() and equals()"
40+
// This will create the equals method below.
41+
42+
// If this method is not present then the program will call the
43+
// equals method from the Object class.
44+
45+
@Override
46+
public boolean equals(Object obj) {
47+
System.out.println("--> Person [id=" + id + ", name=" + name + "]");
48+
if (this == obj)
49+
return true;
50+
System.out.println("A"); // Included to find which code is getting executed.
51+
if (obj == null)
52+
return false;
53+
System.out.println("B");
54+
if (getClass() != obj.getClass())
55+
return false;
56+
System.out.println("C");
57+
Personnel other = (Personnel) obj;
58+
if (id != other.id)
59+
return false;
60+
System.out.println("D");
61+
if (name == null) {
62+
if (other.name != null)
63+
System.out.println("E");
64+
return false;
65+
} else if (!name.equals(other.name))
66+
return false;
67+
System.out.println("F");
68+
return true;
69+
}
70+
71+
}
72+
73+
public class EqualsMethod {
74+
75+
public static void main(String[] args) {
76+
Personnel p1 = new Personnel(2, "John");
77+
Personnel p2 = new Personnel(2, "John");
78+
79+
System.out.print("Is object p1 = p2? ");
80+
System.out.println(p1 == p2);
81+
System.out.println("-----------------------------");
82+
83+
// Try the below statement by commenting the equals method
84+
// mentioned in the Personnel class above. The output will be "false" as
85+
// it calls the equals method from the Object class by default.
86+
87+
// Hence you need to override the Object.equals() method
88+
// as seen in the Personnel class above.
89+
System.out.println("Checking again with eguals method - " + p1.equals(p2));
90+
System.out.println("-----------------------------");
91+
92+
p1 = p2;
93+
System.out.print("Is object p1 = p2? ");
94+
System.out.println(p1 == p2);
95+
System.out.println("-----------------------------");
96+
System.out.println("Checking again with eguals method - " + p1.equals(p2));
97+
System.out.println("-----------------------------");
98+
99+
p2 = new Personnel(4, "Michael");
100+
System.out.print("Is object p1 = p2? ");
101+
System.out.println(p1 == p2);
102+
System.out.println("-----------------------------");
103+
System.out.println("Checking again with eguals method - " + p1.equals(p2));
104+
System.out.println("-------------DOUBLE----------------");
105+
106+
Double v1 = 7.2;
107+
Double v2 = 7.2;
108+
109+
System.out.println(v1 == v2);
110+
System.out.println(v1.equals(v2));
111+
System.out.println("-------------INTEGER---------------");
112+
113+
Integer n1 = 6;
114+
Integer n2 = 6;
115+
System.out.println(n1 == n2);
116+
System.out.println(n1.equals(n2));
117+
System.out.println("--------------STRING--------------");
118+
119+
String t1 = "Hello";
120+
String t2 = "Hello";
121+
String t3 = new String("Hello");
122+
String t4 = "Helloabskj".substring(0, 5);
123+
124+
System.out.println("Compare t1 with t2: " + t1 == t2);
125+
System.out.println("Compare t1 with t2: " + t1.equals(t2));
126+
System.out.println("Compare t1 with t3: " + t1 == t3);
127+
System.out.println("Compare t1 with t3: " + t1.equals(t3));
128+
System.out.println("t4 = " + t4);
129+
System.out.println("Compare t1 with t4: " + t1 == t4);
130+
System.out.println("Compare t1 with t4: " + t1.equals(t4));
131+
132+
System.out.println("------------HASH CODE-----------------");
133+
System.out.println(new Object());
134+
Object obj1 = new Object();
135+
System.out.println(obj1.hashCode());
136+
System.out.println(p2.hashCode());
137+
}
138+
}

0 commit comments

Comments
 (0)