Skip to content

Commit 63fe38d

Browse files
author
Rocky Bhai
committed
Committing all Java files
0 parents  commit 63fe38d

27 files changed

Lines changed: 1258 additions & 0 deletions

.classpath

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-13">
4+
<attributes>
5+
<attribute name="module" value="true"/>
6+
</attributes>
7+
</classpathentry>
8+
<classpathentry kind="src" path="javabasic"/>
9+
<classpathentry kind="src" path="Inheritence"/>
10+
<classpathentry kind="src" path="testinstance"/>
11+
<classpathentry kind="src" path="pppexample"/>
12+
<classpathentry kind="output" path="bin"/>
13+
</classpath>

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Learning</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=13
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=13
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
13+
org.eclipse.jdt.core.compiler.release=enabled
14+
org.eclipse.jdt.core.compiler.source=13

Inheritence/sample/Car01.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package sample;
2+
3+
// "extends" keyword makes Car class the child of Machine class.
4+
5+
public class Car01 extends Machine {
6+
public void startWiper() {
7+
System.out.println("Starting the wiper.");
8+
}
9+
10+
// To override the restart method from Machine class,
11+
// Use the same definition as in Machine class in Car01 class.
12+
13+
// Another way to create an override method is:
14+
// By clicking on "Source" menu and selecting "Override/Implement Methods..."
15+
// Choose the desired method and click on "OK".
16+
public void restart() {
17+
System.out.println("Car is stopping and starting.");
18+
}
19+
20+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package sample;
2+
3+
// Example to test inheritance in Java
4+
5+
public class InheritMain01 {
6+
7+
public static void main(String[] args) {
8+
System.out.println("Printing values of Machine class.");
9+
Machine mac1 = new Machine();
10+
mac1.start();
11+
mac1.stop();
12+
mac1.restart();
13+
14+
Car01 c1 = new Car01();
15+
System.out.println("Printing values of Car class.");
16+
17+
// Object c1 inherits the start methods of Machine class.
18+
c1.start();
19+
// Object c1 directly runs the startWiper in Car01 class.
20+
c1.startWiper();
21+
c1.stop();
22+
// Method restart exists in Machine and Car01 class.
23+
// Method restart is overridden in Car01 class.
24+
c1.restart();
25+
26+
}
27+
28+
}

Inheritence/sample/Machine.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package sample;
2+
3+
public class Machine {
4+
public void start() {
5+
System.out.println("Machine started.");
6+
}
7+
public void stop() {
8+
System.out.println("Machine stopped.");
9+
}
10+
11+
public void restart() {
12+
System.out.println("Machine is restarting.");
13+
}
14+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package sample;
2+
3+
// This test is to find how casting can be done.
4+
5+
public class CastingTypeTest {
6+
7+
public static void main(String[] args) {
8+
byte byteValue = 20;
9+
short shortValue = 55;
10+
int intValue = 888;
11+
int intValue1 = 888;
12+
long longValue = 235555;
13+
14+
float floatValue = 465.23f;
15+
double doubleValue = 324585.442;
16+
17+
// Can use these classes to find MAX / MIN values.
18+
System.out.println("Max Byte Value: " + Byte.MAX_VALUE);
19+
System.out.println("Min Byte Value: " + Byte.MIN_VALUE);
20+
System.out.println("Max Integer Value: " + Integer.MAX_VALUE);
21+
System.out.println("Min Integer Value: " + Integer.MIN_VALUE);
22+
23+
intValue = (int)longValue;
24+
System.out.println(intValue);
25+
26+
doubleValue = intValue;
27+
System.out.println(doubleValue); // Will add a decimal 0 behind the int value
28+
29+
intValue1 = (int)doubleValue;
30+
System.out.println(intValue1);
31+
32+
byteValue = (byte)128;
33+
34+
// Max byte value is 127
35+
// Here byte value has moved to -128 (left most number)
36+
// This is due to overflow
37+
System.out.println(byteValue);
38+
39+
System.out.println("Max Short Value: " + Short.MAX_VALUE);
40+
System.out.println("Min Short Value: " + Short.MIN_VALUE);
41+
intValue = 888;
42+
43+
// This will convert from int to short
44+
shortValue = (short)intValue;
45+
System.out.println(shortValue);
46+
47+
shortValue = (short)longValue;
48+
System.out.println(shortValue);
49+
50+
floatValue = (float)doubleValue;
51+
System.out.println(floatValue);
52+
53+
System.out.println("Max float Value: " + Float.MAX_VALUE);
54+
System.out.println("Min float Value: " + Float.MIN_VALUE);
55+
56+
57+
}
58+
59+
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
package sample;
2+
3+
//This program will test the constructor method and "this" keyword.
4+
5+
class People {
6+
7+
// Need to use the set methods to set the values for the variables.
8+
// Else, you can provide the values while creating the variables.
9+
// There cannot be ny code inside the class.
10+
11+
// The following are called "Instance variable".
12+
String name;
13+
int age;
14+
double weight;
15+
double height;
16+
String address;
17+
18+
// Using constructors.
19+
// Constructors should have the same name as the class name.
20+
// Constructors will be called by default whenever a new object of the class is created.
21+
// Constructors can have multiple parameters.
22+
// Constructors don't need to mention any return parameters like void, int, etc.
23+
// One constructor can call another constructor on its
24+
// first line by passing a different set of parameters.
25+
26+
// Default constructor
27+
public People() {
28+
System.out.println("Default constructor setting default values.");
29+
this.name = "Rocky Bhai";
30+
this.age = 42;
31+
this.weight = 78.0;
32+
this.height = 5.4;
33+
this.address = "Bhandup West, Mumbai";
34+
}
35+
36+
// Constructor with one argument
37+
public People(String name) {
38+
System.out.println("Constructor with one argument: Name");
39+
this.name = name;
40+
}
41+
42+
// Constructor with three arguments
43+
public People(String name, int age, double height) {
44+
// Will call the default constructor - People()
45+
// Calling another constructor should always be done on the first line.
46+
this();
47+
48+
System.out.println("Constructor with three arguments: Name, age and height");
49+
this.name = name;
50+
this.age = age;
51+
this.height = height;
52+
}
53+
54+
// Constructor with two arguments
55+
public People(double weight, String address) {
56+
// Calling another constructor with three arguments.
57+
this("Sachin Tendulkar", 36, 5.7);
58+
this.weight = weight;
59+
this.address = address;
60+
}
61+
62+
// Setters: Using the set method to set the value of the variable.
63+
64+
public void setName(String name) {
65+
this.name = name;
66+
}
67+
68+
public void setAge(int age) {
69+
// this.age refers to the age variable which is set at the class level.
70+
// "this" keyword is used when two variables have the same name.
71+
this.age = age;
72+
}
73+
74+
public void setWeight(double weight) {
75+
// weight is a local variable.
76+
// Local variable is providing value to instance variable.
77+
this.weight = weight;
78+
}
79+
80+
public void setHeight(double height) {
81+
this.height = height;
82+
}
83+
84+
public void setAddress(String address) {
85+
this.address = address;
86+
}
87+
88+
// Getters: Using the get method to give out value of the variable.
89+
90+
public String getName() {
91+
return this.name;
92+
}
93+
94+
public int getAge() {
95+
// this.age refers to the age variable which is set at the class level.
96+
// "this" keyword is used when two variables have the same name.
97+
return this.age;
98+
}
99+
100+
public double getWeight() {
101+
return this.weight;
102+
}
103+
104+
public double getHeight() {
105+
return height; // Ignoring this keyword as it is referring to the variable within the class.
106+
}
107+
108+
public String getAddress() {
109+
return address;
110+
}
111+
}
112+
113+
public class ConstructorMethod01 {
114+
115+
public static void main(String[] args) {
116+
117+
// This will invoke the default constructor.
118+
People p1 = new People();
119+
p1.name = "Thomas Edison";
120+
p1.setAge(32);
121+
p1.setWeight(65);
122+
p1.setHeight(5.5);
123+
p1.setAddress("Lokhandwala Complex, Mumbai, India");
124+
125+
System.out.printf("Name: %s \n", p1.getName());
126+
System.out.printf("Age: %d (years) \n", p1.getAge());
127+
System.out.printf("Weight : %.2f (Kgs) \n", p1.getWeight());
128+
System.out.printf("Height : %.2f (Feet) \n", p1.getHeight());
129+
System.out.printf("Address: %s \n", p1.getAddress());
130+
System.out.println("-----------------------");
131+
132+
// This will invoke the constructor with one argument.
133+
People p2 = new People("Dwyane Johnson");
134+
p2.setAge(45);
135+
p2.setWeight(86.3);
136+
p2.setHeight(6.3);
137+
p2.setAddress("Block 415, Bedok North, Singapore - 460415");
138+
139+
// This will print "Dwyane Johnson".
140+
System.out.printf("Name: %s \n", p2.getName());
141+
System.out.printf("Age: %d (years) \n", p2.getAge());
142+
System.out.printf("Weight : %.2f (Kgs) \n", p2.getWeight());
143+
System.out.printf("Height : %.2f (Feet) \n", p2.getHeight());
144+
System.out.printf("Address: %s \n", p2.getAddress());
145+
System.out.println("-----------------------");
146+
147+
// This will invoke the default constructor.
148+
People p3 = new People();
149+
// This will print default values set by the constructor.
150+
System.out.printf("Name: %s \n", p3.getName());
151+
System.out.printf("Age: %d (years) \n", p3.getAge());
152+
System.out.printf("Weight : %.2f (Kgs) \n", p3.getWeight());
153+
System.out.printf("Height : %.2f (Feet) \n", p3.getHeight());
154+
System.out.printf("Address: %s \n", p3.getAddress());
155+
System.out.println("-----------------------");
156+
157+
// This will invoke the constructor with 3 arguments
158+
// but will set the remaining values as default
159+
// by calling the default constructor.
160+
161+
People p4 = new People("Scarlett Johansson", 23, 5.1);
162+
System.out.printf("Name: %s \n", p4.getName());
163+
System.out.printf("Age: %d (years) \n", p4.getAge());
164+
System.out.printf("Weight : %.2f (Kgs)", p4.getWeight());
165+
System.out.printf(" ---> Default value \n");
166+
System.out.printf("Height : %.2f (Feet) \n", p4.getHeight());
167+
System.out.printf("Address: %s ", p4.getAddress());
168+
System.out.printf(" ---> Default value \n");
169+
System.out.println("-----------------------");
170+
171+
People p5 = new People(62.5, "Los Angeles, United States of America.");
172+
System.out.printf("Name: %s ", p5.getName());
173+
System.out.printf(" ---> Value set by another constructor \n");
174+
System.out.printf("Age: %d (years) ", p5.getAge());
175+
System.out.printf(" ---> Value set by another constructor \n");
176+
System.out.printf("Weight : %.2f (Kgs) \n", p5.getWeight());
177+
System.out.printf("Height : %.2f (Feet) ", p5.getHeight());
178+
System.out.printf(" ---> Value set by another constructor \n");
179+
System.out.printf("Address: %s \n", p5.getAddress());
180+
System.out.println("-----------------------");
181+
}
182+
}

0 commit comments

Comments
 (0)