Skip to content

Commit 7d1c7ee

Browse files
Motta ShivaMotta Shiva
authored andcommitted
java add programs - 4th Sep20
1 parent 796208f commit 7d1c7ee

File tree

13 files changed

+379
-3
lines changed

13 files changed

+379
-3
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package learnAbstract;
2+
3+
4+
/*Refer the site for final keywords: https://beginnersbook.com/2014/07/final-keyword-java-final-variable-method-class/
5+
*/
6+
public class FinalExample {
7+
8+
final String place = "Chennai";
9+
final int accNo;
10+
final String rollNo;
11+
12+
13+
FinalExample(int aNum, String rNum) { // initializing the final value in constructor by passing parameter variables
14+
accNo = aNum;
15+
rollNo = rNum;
16+
}
17+
18+
19+
void empData() {
20+
System.out.println("Acc number is: "+accNo);
21+
System.out.println("Emp roll number is: "+rollNo);
22+
}
23+
24+
void myPlace() {
25+
System.out.println(place);
26+
}
27+
28+
29+
public static void main(String[] args) {
30+
FinalExample finalKey = new FinalExample(1234, "student1234");
31+
finalKey.empData();
32+
finalKey.myPlace();
33+
/*finalKey.findPlaces();
34+
finalKey.likePlace();*/
35+
36+
}
37+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package learnAbstract;
2+
3+
public class FinalKeywordExample {
4+
String name = "Charan";
5+
final int i =20;
6+
int j = 30;
7+
8+
// public abstract void add();
9+
10+
public void multiples(int i, int j) {
11+
int product = i * j;
12+
System.out.println("Product value is: "+product);
13+
}
14+
15+
16+
public static void main(String[] args) {
17+
FinalKeywordExample finalKey = new FinalKeywordExample();
18+
finalKey.multiples(50, 40); // need to check how come the final variable value is getting changed/updated.
19+
20+
21+
}
22+
23+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package learnAbstract;
2+
3+
public interface InterfaceExample {
4+
5+
int num = 123;
6+
String name = "aji";
7+
int age = 35;
8+
9+
void student();
10+
void employee();
11+
public void person_details();
12+
13+
}
14+
15+

JavaBasicPrograms/src/main/java/learnConstructors/ParameterizedConstructor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class ParameterizedConstructor {
1414

1515

1616
public void aboutAnimal() {
17-
System.out.println("Animal Name is - "+animal_name+"; " + "Animal ID is - "+animal_id);
17+
System.out.println("Animal Name is: " +animal_name +"; " +"Animal ID is - "+animal_id);
1818
}
1919

2020
public static void main(String[] args) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package learnFinalKeyword;
2+
3+
final class FinalClassExample {
4+
5+
public void car() {
6+
System.out.println("final class example with sample");
7+
}
8+
9+
final void bike() {
10+
System.out.println("Chekc for the final class functionality");
11+
}
12+
13+
public static void main(String[] args) {
14+
FinalClassExample fc = new FinalClassExample();
15+
fc.car();
16+
fc.bike();
17+
}
18+
19+
}
20+
21+
// cannot inherit the final class to any other sub class - compilation error will be thrown
22+
23+
// class Test extends FinalClassExample { //compilation error is thrown
24+
class Test {
25+
26+
void demoPrg() {
27+
System.out.println("Check if we can inherit the 'final class' ");
28+
}
29+
30+
void bike() { // this method is not overridden as not able to inherit the final class
31+
System.out.println("check for the overridden method");
32+
}
33+
34+
public static void main(String[] args) {
35+
Test test = new Test();
36+
// test.car();
37+
test.bike();
38+
test.demoPrg();
39+
40+
}
41+
42+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package learnFinalKeyword;
2+
3+
public class FinalKeywordExample {
4+
5+
final String place = "Chennai";
6+
final int accNo = 4500;
7+
final String rollNo;
8+
9+
FinalKeywordExample() { // initializing the final value in constructor
10+
// accNo = 3000; // final variable should be initialized either in class level or in constructor, Not in methods
11+
rollNo = "abc123";
12+
}
13+
14+
public void findPlaces() {
15+
String place = "Madurai";
16+
System.out.println("New place name is: "+place);
17+
}
18+
19+
public void accNumber() {
20+
// accNo = 500; // cannot change or modify the final value
21+
// rollNo = "123edf";
22+
System.out.println(accNo);
23+
System.err.println(rollNo);
24+
}
25+
26+
public static void main(String[] args) {
27+
28+
FinalKeywordExample finalKey = new FinalKeywordExample();
29+
finalKey.findPlaces();
30+
finalKey.accNumber();
31+
32+
33+
34+
}
35+
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package learnFinalKeyword;
2+
3+
class FinalMethodExample {
4+
final int marks = 100;
5+
6+
final void sample1() {
7+
int marks = 150;
8+
System.out.println("It is a final method - sample1");
9+
System.out.println("Max marks is: "+marks);
10+
}
11+
12+
public void sample2() {
13+
System.out.println("It is a sample 2 method");
14+
}
15+
16+
}
17+
18+
class FinalMethod extends FinalMethodExample {
19+
20+
/* void sample1() { // we cannot inherit the final methods into other class
21+
System.out.println("Method 2 new sample");
22+
}*/
23+
24+
public void sample2() {
25+
System.out.println("Overrriding the method");
26+
}
27+
28+
public static void main(String[] args) {
29+
FinalMethod fm = new FinalMethod();
30+
fm.sample1();
31+
fm.sample2();
32+
33+
}
34+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package learnFinalKeyword;
2+
3+
public class FinalMethodExample2 extends FinalMethodExample {
4+
5+
/*void sample1() { // unable to override the final methods into other class
6+
System.out.println("Trying to override the 'final' method from parent class");
7+
}*/
8+
9+
@Override
10+
public void sample2() {
11+
System.out.println("This is method is overridden from the parent class");
12+
13+
}
14+
15+
public void marks() {
16+
System.out.println("Max Marks declared is: " +marks);
17+
}
18+
19+
20+
public static void main(String[] args) {
21+
FinalMethodExample2 fm2 = new FinalMethodExample2();
22+
fm2.sample2();
23+
fm2.sample1();
24+
25+
}
26+
27+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package learnMap;
2+
3+
import java.util.HashMap;
4+
5+
public class HashMapExample {
6+
7+
public static void main(String[] args) {
8+
9+
HashMap<Integer, String> employees = new HashMap<Integer, String>();
10+
11+
employees.put(101, "Rakesh");
12+
employees.put(102, "Ramu");
13+
employees.put(123, "Ajith");
14+
employees.put(101, "Charan");
15+
employees.put(null, "abcd");
16+
employees.put(145, "Charan");
17+
System.out.println("Employees details: " +employees);
18+
19+
20+
HashMap<Integer, String> duplicate = new HashMap<Integer, String>();
21+
duplicate.putAll(employees);
22+
System.out.println(duplicate);
23+
24+
//clear all values in duplicate
25+
duplicate.clear();
26+
System.out.println(duplicate);
27+
28+
// check if a key present
29+
System.out.println("Does it have 123 key? " +employees.containsKey(123));
30+
31+
32+
// check if a key present
33+
System.out.println("Does it have value Charan? " +employees.containsValue("Charan"));
34+
35+
//clone the values
36+
System.out.println("clone the values:" +employees.clone());
37+
38+
39+
}
40+
41+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package learnMap;
2+
3+
import java.util.TreeMap;
4+
5+
public class TreeMapExample {
6+
7+
public static void main(String[] args) {
8+
9+
TreeMap<String, String> places = new TreeMap<String, String>();
10+
places.put("Gopalapuram", "Chennai"); // key -- value pair combination data
11+
places.put("Guindy", "Chennai");
12+
places.put("Royapet", "Chennai");
13+
places.put("DMS", "Chennai");
14+
places.put("Chennai", "Tambaram");
15+
places.put("Chennai", "PVM");
16+
places.put(null, "Chennai"); // null pointer exception will be thrown during runtime
17+
18+
System.out.println(places);
19+
20+
21+
22+
}
23+
24+
}

0 commit comments

Comments
 (0)