Skip to content

Commit f99a262

Browse files
authored
Add files via upload
1 parent 22b6a39 commit f99a262

12 files changed

+396
-0
lines changed

ObjectOrdering/ComparableMovie.java

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package ObjectOrdering;
2+
import java.util.Arrays;
3+
4+
class Movie implements Comparable<Movie> {
5+
private double rating;
6+
private String name;
7+
private int year;
8+
9+
public Movie(String n, double rt, int y) {
10+
this.name = n;
11+
this.rating = rt;
12+
this.year = y;
13+
}
14+
// compare on the basis of rating
15+
/*
16+
* public int compareTo(Movie arg) { return this.year - arg.year; }
17+
*/
18+
19+
// compare on the basis of name
20+
public int compareTo(Movie m) {
21+
return this.name.compareTo(m.name); // this will call compareTo method of String Class
22+
}
23+
24+
public double getRating() {
25+
return rating;
26+
}
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public int getYear() {
33+
return year;
34+
}
35+
}
36+
37+
public class ComparableMovie {
38+
39+
public static void main(String[] args) {
40+
Movie[] mov=new Movie[4];
41+
mov[0]=new Movie("Inception",8.9,2010);
42+
mov[1]=new Movie("The Dark Knight",9.0,2008);
43+
mov[2]=new Movie("Intersteller",8.7,2014);
44+
mov[3]=new Movie("The Prestige",8.2,2005);
45+
Arrays.sort(mov);
46+
System.out.println("Movie list in sorted order: ");
47+
for (Movie mv : mov) {
48+
System.out.println(
49+
"Name: " + mv.getName() + "\t\t[Rating: " + mv.getRating() + "\tYear: " + mv.getYear() + "]");
50+
}
51+
}
52+
}

ObjectOrdering/ComparatorMovie.java

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package ObjectOrdering;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Comparator;
6+
7+
8+
class MovieCom {
9+
private double rating;
10+
private String name;
11+
private int year;
12+
13+
public MovieCom(String n, double rt, int y) {
14+
this.name = n;
15+
this.rating = rt;
16+
this.year = y;
17+
}
18+
19+
public double getRating() {
20+
return rating;
21+
}
22+
23+
public String getName() {
24+
return name;
25+
}
26+
27+
public int getYear() {
28+
return year;
29+
}
30+
public String toString() {
31+
return "Name: "+name+"\tRating: "+rating+"\tYear: "+year;
32+
}
33+
}
34+
35+
class RatingCompare implements Comparator<MovieCom> {
36+
public int compare(MovieCom m1, MovieCom m2) {
37+
return (m1.getRating() < m2.getRating()) ? -1 : ((m1.getRating() > m2.getRating()) ? 1 : 0);
38+
}
39+
}
40+
41+
class NameCompare implements Comparator<MovieCom> {
42+
public int compare(MovieCom m1, MovieCom m2) {
43+
return m1.getName().compareTo(m2.getName());
44+
}
45+
}
46+
47+
public class ComparatorMovie {
48+
49+
public static void main(String[] args) {
50+
ArrayList<MovieCom> mov = new ArrayList<>();
51+
mov.add(new MovieCom("Inception ", 8.9, 2010));
52+
mov.add(new MovieCom("The Dark Knight", 9.0, 2008));
53+
mov.add(new MovieCom("Intersteller", 8.7, 2014));
54+
mov.add(new MovieCom("The Prestige", 8.2, 2005));
55+
System.out.println("Sorted Movie list on the basis of Name.");
56+
Collections.sort(mov,new NameCompare());
57+
for(MovieCom m:mov)
58+
System.out.println(m);
59+
System.out.println("\nSorted Movie list on the basis of Rating.");
60+
Collections.sort(mov,new RatingCompare());
61+
for(MovieCom m:mov)
62+
System.out.println(m);
63+
}
64+
}

Reflection/ReflectionEx.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package Reflection;
2+
3+
//import java.lang.reflect.Method;
4+
//import java.lang.reflect.Field;
5+
//import java.lang.reflect.Constructor;
6+
7+
class Test {
8+
private String s;
9+
10+
public Test() {
11+
s = "Hey! I'm Initialized";
12+
}
13+
public Test(String s) {
14+
this.s=s;
15+
}
16+
public void method1() {
17+
System.out.println("The string is " + s);
18+
}
19+
20+
public void method2(int n) {
21+
System.out.println("The number is " + n);
22+
}
23+
24+
// private void method3() {
25+
// System.out.println("Private method invoked");
26+
// }
27+
}
28+
29+
public class ReflectionEx {
30+
31+
public static void main(String[] args) {
32+
Test obj=new Test();
33+
System.out.println("The public methods of class are : "+obj);
34+
}
35+
}

abstractKey/Example1.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package abstractKey;
2+
3+
/* If a class is not having any abstract method
4+
* then it is possible to have no abstract method
5+
*
6+
* But if a class have one or more abstract method
7+
* then class should be abstract.
8+
* */
9+
abstract class Car{
10+
abstract void start();// Abstract method must be implemented
11+
// in subclasses
12+
}
13+
class Audi extends Car{
14+
@Override
15+
void start(){
16+
System.out.println("Audi Car ");
17+
}
18+
}
19+
class BMW extends Car{
20+
@Override
21+
void start(){
22+
System.out.println("BMW Car");
23+
}
24+
}
25+
class Drive{
26+
void drive(Car c) {
27+
c.start();
28+
}
29+
}
30+
31+
public class Example1 {
32+
33+
public static void main(String[] args) {
34+
Drive d=new Drive();
35+
d.drive(new Audi());
36+
d.drive(new BMW());
37+
}
38+
39+
}

boxing/BoxingUnBoxing.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package boxing;
2+
3+
public class BoxingUnBoxing {
4+
public static void main(String args[]) {
5+
// Boxing
6+
byte a = 1;
7+
Byte byteobj = new Byte(a);
8+
int b = 10;
9+
Integer intobj = new Integer(b);
10+
float c = 18.6f;
11+
Float floatobj = new Float(c);
12+
double d = 250.5;
13+
Double doubleobj = new Double(d);
14+
char e = 'a';
15+
Character charobj = e;
16+
System.out.println("Values of Boxed objects (printing as objects)");
17+
System.out.println("Byte object byteobj: " + byteobj);
18+
System.out.println("Integer object intobj: " + intobj);
19+
System.out.println("Float object floatobj: " + floatobj);
20+
System.out.println("Double object doubleobj: " + doubleobj);
21+
System.out.println("Character object charobj: " + charobj);
22+
23+
// UnBoxing
24+
byte bv = byteobj;
25+
int iv = intobj;
26+
float fv = floatobj;
27+
double dv = doubleobj;
28+
char cv = charobj;
29+
System.out.println("\nUnBoxed values (printing as data types)");
30+
System.out.println("byte value, bv: " + bv);
31+
System.out.println("int value, iv: " + iv);
32+
System.out.println("float value, fv: " + fv);
33+
System.out.println("double value, dv: " + dv);
34+
System.out.println("char value, cv: " + cv);
35+
}
36+
}

boxing/Example1.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package boxing;
2+
3+
//In Java, values from -128 to 127 are cached,
4+
//so the same objects are returned.
5+
//The implementation of valueOf() uses cached
6+
//objects if the value is between -128 to 127.
7+
8+
public class Example1 {
9+
10+
public static String compareWc(Integer x,Integer y){
11+
return (x==y)?"Same":"Not same";
12+
}
13+
14+
public static void main(String[] args) {
15+
Integer x = 40, y = 40;
16+
Integer a=400, b=400;
17+
Integer m=new Integer(40);
18+
Integer n=new Integer(40);
19+
System.out.println("40 , 40 Interger are: "+compareWc(x, y));
20+
System.out.println("400 , 400 Interger are: "+compareWc(a, b));
21+
System.out.println("40 , 40 Interger on explicit object creation : "+compareWc(m, n));
22+
}
23+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package exceptionHandling;
2+
3+
public class CheckedUncecked {
4+
5+
//Unchecked Exception
6+
//here we don't have to declare that method
7+
// will throw exception
8+
static void m1() {
9+
throw new ArithmeticException();
10+
}
11+
//Checked Exception
12+
//Here We've to method , that this method will
13+
// throw exception
14+
static void m2() throws ClassNotFoundException {
15+
throw new ClassNotFoundException();
16+
}
17+
public static void main(String[] args) {
18+
m1(); //for Unchecked exception
19+
// compiler doesn't check for exception
20+
try {
21+
m2();
22+
}catch(ClassNotFoundException e) {
23+
System.out.println("Checked Exception handled.");
24+
}
25+
}
26+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package exceptionHandling;
2+
3+
class HighBalanceException extends Exception{
4+
private static final long serialVersionUID = 1L;
5+
6+
public HighBalanceException() {
7+
super("Customer Balance is more than limit.");
8+
}
9+
}
10+
class CustomerAccount {
11+
private int acctNum;
12+
private double balance;
13+
public static final double HIGH_CREDIT_LIMIT=2000.00;
14+
15+
public CustomerAccount(int num,double bal) throws HighBalanceException {
16+
acctNum=num;
17+
balance=bal;
18+
if(balance>HIGH_CREDIT_LIMIT) {
19+
throw new HighBalanceException();
20+
}
21+
System.out.println("Account Number: "+acctNum+"\nBalance: "+balance);
22+
}
23+
24+
public static void main(String[] args) {
25+
try {
26+
new CustomerAccount(1000211, 3000.00);
27+
}catch(HighBalanceException h){
28+
System.out.println(h.getMessage());
29+
h.getStackTrace();
30+
}
31+
}
32+
}

exceptionHandling/DivideByZero.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package exceptionHandling;
2+
3+
public class DivideByZero {
4+
5+
public static int divide(int a,int b) {
6+
return a/b;
7+
}
8+
public static int computeDivison(int a, int b) {
9+
return divide(a,b);
10+
}
11+
public static void main(String[] args) {
12+
int a=1, b=0;
13+
try {
14+
int i=computeDivison(a, b);
15+
System.out.println("Divide Result: "+i);
16+
}
17+
catch(ArithmeticException e) {
18+
System.out.println(e.getMessage());
19+
e.printStackTrace();
20+
}
21+
22+
}
23+
}

exceptionHandling/Example1.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package exceptionHandling;
2+
3+
import java.util.Scanner;
4+
5+
public class Example1 {
6+
7+
public static void main(String[] args) {
8+
Scanner sc=new Scanner(System.in);
9+
System.out.print("Enter a number: ");
10+
int a;
11+
try {
12+
a=sc.nextInt();
13+
}catch(Exception e) {
14+
System.out.println("Invalid Number!");
15+
a=0;
16+
}
17+
System.out.println("Value of a: "+a);
18+
sc.close();
19+
}
20+
}

generics/GenericClass.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package generics;
2+
3+
public class GenericClass<T> {
4+
5+
private T t;
6+
public void add(T t) {
7+
this.t=t;
8+
}
9+
public T get() {
10+
return t;
11+
}
12+
public static void main(String[] args) {
13+
GenericClass<Integer> iObj=new GenericClass<>();
14+
GenericClass<String> sObj=new GenericClass<>();
15+
iObj.add(10);
16+
sObj.add(" Hello ");
17+
System.out.println("Integer value: "+iObj.get());
18+
System.out.println("String value: "+sObj.get());
19+
}
20+
}

0 commit comments

Comments
 (0)