diff --git a/AbstractionDemo1.java b/AbstractionDemo1.java new file mode 100644 index 0000000..41a2516 --- /dev/null +++ b/AbstractionDemo1.java @@ -0,0 +1,25 @@ +abstract class Shape{ + abstract void draw(); + public void paint(){ + System.out.println("rainbow..."); + } +} +class Rectangle extends Shape{ + public void draw(){ + System.out.println("Draw a rectangle..."); + } +} +class Circle extends Shape{ + public void draw(){ + System.out.println("Draw a circle..."); + } +} +class AbstractionDemo1{ + public static void main(String[] args){ + Shape shape = new Rectangle(); + shape.draw(); + shape.paint(); + shape = new Circle(); + shape.draw(); + } +} \ No newline at end of file diff --git a/AbstractionDemo2.java b/AbstractionDemo2.java new file mode 100644 index 0000000..dd79050 --- /dev/null +++ b/AbstractionDemo2.java @@ -0,0 +1,25 @@ +interface Shape{ + void draw(); + public static void paint(){ + System.out.println("rainbow..."); + } +} +class Rectangle implements Shape{ + public void draw(){ + System.out.println("Draw a rectangle..."); + } +} +class Circle implements Shape{ + public void draw(){ + System.out.println("Draw a circle..."); + } +} +class AbstractionDemo2{ + public static void main(String[] args){ + Shape shape = new Rectangle(); + shape.draw(); + Shape.paint(); + shape = new Circle(); + shape.draw(); + } +} \ No newline at end of file diff --git a/Abstraction_demo/AbstractDemo.java b/Abstraction_demo/AbstractDemo.java new file mode 100644 index 0000000..c561f05 --- /dev/null +++ b/Abstraction_demo/AbstractDemo.java @@ -0,0 +1,30 @@ +package pck2; +import pck1.Car; +import pck1.Vehicle; +import pck1.MotorCycle; +import java.util.Scanner; + +class AbstractDemo{ + public static void main(String[] args){ + String brand; + int d; + Scanner sc = new Scanner(System.in); + System.out.println("Enter gaadi name : "); + brand = sc.next(); + System.out.println("Kitna chalaoge : "); + d = sc.nextInt(); + Car car = new Car(brand); + Vehicle v = car; + MotorCycle bike = new MotorCycle(brand); + Buyer mansi = new Buyer(bike); + + Buyer ekansh = new Buyer(new Vehicle(){ + public void move(int distance){ + System.out.println("dhrooommmmmmmmm : "+distance); + } + }); + ekansh.gaadiChal(1000); + mansi.gaadiChal(d); + } +} + diff --git a/Abstraction_demo/Buyer.java b/Abstraction_demo/Buyer.java new file mode 100644 index 0000000..d355764 --- /dev/null +++ b/Abstraction_demo/Buyer.java @@ -0,0 +1,13 @@ +package pck2; +import pck1.Vehicle; + +class Buyer{ + Vehicle gaadi; + Buyer(Vehicle v){ + gaadi = v; + } + public void gaadiChal(int d){ + gaadi.move(d); + } +} + diff --git a/Abstraction_demo/Car.java b/Abstraction_demo/Car.java new file mode 100644 index 0000000..3b072cc --- /dev/null +++ b/Abstraction_demo/Car.java @@ -0,0 +1,13 @@ +package pck1; + +public class Car implements Vehicle{ + String brand; + public Car(String b){ + brand = b; + } + public void move(int distance){ + System.out.println("brand : "+brand); + System.out.println("distance : "+distance); + } +} + diff --git a/Abstraction_demo/MotorCycle.java b/Abstraction_demo/MotorCycle.java new file mode 100644 index 0000000..715d545 --- /dev/null +++ b/Abstraction_demo/MotorCycle.java @@ -0,0 +1,13 @@ +package pck1; + +public class MotorCycle implements Vehicle{ + String brand; + public MotorCycle(String b){ + brand = b; + } + public void move(int distance){ + System.out.println("Motor brand : "+brand); + System.out.println("distance : "+distance); + } +} + diff --git a/Abstraction_demo/RealWorldInterfaceEx.java b/Abstraction_demo/RealWorldInterfaceEx.java new file mode 100644 index 0000000..7a67565 --- /dev/null +++ b/Abstraction_demo/RealWorldInterfaceEx.java @@ -0,0 +1,59 @@ +import java.util.Scanner; + +interface Transportation{ + int getRatePerKm(); + int getFixedCharge(); + default float getFare(float distance){ + float fare = getFixedCharge() + (distance - 1) * getRatePerKm(); + return fare; + } +} +class Metro implements Transportation{ + public int getRatePerKm(){ + return 5; + } + public int getFixedCharge(){ + return 10; + } +} +class Ola implements Transportation{ + public int getRatePerKm(){ + return 6; + } + public int getFixedCharge(){ + return 25; + } +} + +class ByWalk implements Transportation{ + public int getRatePerKm(){ + return 0; + } + public int getFixedCharge(){ + return 0; + } +} + +public class RealWorldInterfaceEx{ + public static void main(String[] args){ + Scanner sc = new Scanner(System.in); + System.out.println("Hello please select mode of transportation : "); + System.out.println("Options are : "); + System.out.println("1. Metro"); + System.out.println("2. Ola"); + int choice = sc.nextInt(); + Transportation transportation = null; + switch(choice){ + case 1 : transportation = new Metro();break; + case 2 : transportation = new Ola();break; + default : transportation = new ByWalk();break; + } + System.out.println("Enter distance : "); + int distance = sc.nextInt(); + float fare = transportation.getFare(distance); + System.out.println("fare : "+fare); + } +} + + + diff --git a/Abstraction_demo/Vehicle.java b/Abstraction_demo/Vehicle.java new file mode 100644 index 0000000..45d5a6a --- /dev/null +++ b/Abstraction_demo/Vehicle.java @@ -0,0 +1,6 @@ +package pck1; + +public interface Vehicle{ + public void move(int distance); +} + diff --git a/AggregationDemo2.java b/AggregationDemo2.java new file mode 100644 index 0000000..0410a44 --- /dev/null +++ b/AggregationDemo2.java @@ -0,0 +1,37 @@ +class Address{ + int hNo; + String street; + int pincode; + String city; + public Address(int hNo,String street,int pincode,String city){ + this.hNo = hNo; + this.street = street; + this.pincode = pincode; + this.city = city; + } + @Override + public String toString(){ + return "Address{\n house no : "+hNo+",\n street : "+street+",\n pincode : "+pincode+",\n city : "+city+"}"; + } +} +class Student{ + int rn; + String name; + Address addr; + public Student(int rn,String name,Address addr){ + this.rn = rn; + this.name = name; + this.addr = addr; + } + @Override + public String toString(){ + return "Student{\n roll no :"+rn+",\n name : "+name+",\n address : "+addr+"}"; + } +} +class AggregationDemo2{ + public static void main(String[] args){ + Address addr = new Address(108,"A- Block",110033,"Delhi"); + Student s = new Student(1,"Tanishq",addr); + System.out.println(s); + } +} \ No newline at end of file diff --git a/Comparator and Comparable demo/ComparatorAndComparabaleDemo.java b/Comparator and Comparable demo/ComparatorAndComparabaleDemo.java new file mode 100644 index 0000000..6a55a84 --- /dev/null +++ b/Comparator and Comparable demo/ComparatorAndComparabaleDemo.java @@ -0,0 +1,46 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package comparatorandcomparabaledemo; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * + * @author cm + */ +public class ComparatorAndComparabaleDemo { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + // TODO code application logic here + List rankingList = new ArrayList<>(); + rankingList.add(23); + rankingList.add(2); + rankingList.add(43); + rankingList.add(86); + rankingList.add(8); + rankingList.add(89); + rankingList.add(65); + rankingList.add(265); + Collections.sort(rankingList); + //System.out.println(rankingList); + List list = new ArrayList<>(); + list.add(new Student(1,"Rinku",100)); + list.add(new Student(2,"Piyush",10)); + list.add(new Student(4,"Ramu",60)); + list.add(new Student(5,"Ashna",80)); + list.add(new Student(3,"Sanyam",22)); + list.add(new Student(6,"Tushar",1)); + MarksComparator marksComparator = new MarksComparator(); + Collections.sort(list,marksComparator); + System.out.println(list); + } + +} diff --git a/Comparator and Comparable demo/MarksComparator.java b/Comparator and Comparable demo/MarksComparator.java new file mode 100644 index 0000000..809b146 --- /dev/null +++ b/Comparator and Comparable demo/MarksComparator.java @@ -0,0 +1,27 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package comparatorandcomparabaledemo; + +import java.util.Comparator; + +/** + * + * @author cm + */ +public class MarksComparator implements Comparator{ + + @Override + public int compare(Student o1, Student o2) { + if(o1.getMarks()>o2.getMarks()){ + return -1; + }else if(o1.getMarks()==o2.getMarks()){ + return 0; + }else{ + return 1; + } + } + +} diff --git a/Comparator and Comparable demo/Student.java b/Comparator and Comparable demo/Student.java new file mode 100644 index 0000000..fcaa013 --- /dev/null +++ b/Comparator and Comparable demo/Student.java @@ -0,0 +1,70 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package comparatorandcomparabaledemo; + +/** + * + * @author cm + */ +public class Student implements Comparable{ + private int rn; + private String name; + private float marks; + + public Student() { + } + + public Student(int rn, String name, float marks) { + this.rn = rn; + this.name = name; + this.marks = marks; + } + + + + public int getRn() { + return rn; + } + + public void setRn(int rn) { + this.rn = rn; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public float getMarks() { + return marks; + } + + public void setMarks(float marks) { + this.marks = marks; + } + + @Override + public String toString() { + return "Student{" + "rn=" + rn + ", name=" + name + ", marks=" + marks + '}'; + } + + @Override + public int compareTo(Student o) { + int anotherRn = o.getRn(); + if(rn>anotherRn){ + return 1; + }else if(anotherRn == rn){ + return 0; + }else{ + return -1; + } + } + + +} diff --git a/ConsoleDemo.java b/ConsoleDemo.java new file mode 100644 index 0000000..fbee957 --- /dev/null +++ b/ConsoleDemo.java @@ -0,0 +1,36 @@ +package com.cmpundhir; + +import java.io.Console; + +class ConsoleDemo{ + private static String savedUsername = "cmpundhir"; + private static char[] savedPassword = {'s','t','r','o','n','g'}; + + private static boolean isVerified(String un,char[] ps){ + if(un.equals(savedUsername)){ + for(int i=0;i vector = new Vector(); + vector.add(fin1); + vector.add(fin2); + vector.add(fin3); + vector.add(fin4); + + Enumeration enm = vector.elements(); + SequenceInputStream bin = new SequenceInputStream(enm); + int a = 0; + while((a = bin.read())!= -1){ + fout.write(a); + } + fin1.close(); + fin2.close(); + fout.close(); + bin.close(); + }catch(Exception e){ + System.err.println(e.getMessage()); + } + } +} \ No newline at end of file diff --git a/Input.java b/Input.java new file mode 100644 index 0000000..6e56c0b --- /dev/null +++ b/Input.java @@ -0,0 +1,9 @@ +import java.util.Scanner; + +class Input{ + public static void main(String[] args){ + Scanner sc = new Scanner(System.in); + String name = sc.next(); + int a = sc.nextInt(); + } +} \ No newline at end of file diff --git a/MultipleInheritance.java b/MultipleInheritance.java new file mode 100644 index 0000000..a9464bd --- /dev/null +++ b/MultipleInheritance.java @@ -0,0 +1,40 @@ +interface A{ + public void method(); +} +interface B{ + public void method(); +} +class C implements B,A{ + public void method(){ + System.out.println("I am A"); + } +} +class MultipleInheritance{ + public static void main(String[] args){ + C c = new C(); + c.method(); + + A a = new A(){ + @Override + public void method(){ + System.out.println("Hello Method"); + } + }; + a.method(); + + //annonymous object + + new C().method(); + + new A(){ + @Override + public void method(){ + System.out.println("I am annonymously created and overriden"); + } + }.method(); + } +} + + + + diff --git a/Oops4.java b/Oops4.java new file mode 100644 index 0000000..3302d7c --- /dev/null +++ b/Oops4.java @@ -0,0 +1,46 @@ +abstract class BeingSundar{ + abstract void goods(); + abstract void bads(); + public void complications(){ + System.out.println("it's complicated..."); + } +} +class Mayank extends BeingSundar{ + @Override + public void goods(){ + System.out.println("1. Sundar ladkiyo ki kami nhi..."); + System.out.println("2. Overconfidence ki me bahot sundar hu..."); + System.out.println("3. Modeling kr skte hai..."); + } + @Override + public void bads(){ + System.out.println("1. Dosro ko judge ya comment krna ..."); + System.out.println("2. Shoes ka color match krna chahiye"); + System.out.println("3. ladkiyo ki kami nhi..."); + } + public void intro(){ + System.out.println("i am mayank..."); + } +} +class Priya extends BeingSundar{ + public void goods(){ + System.out.println("1. kuchh bhi pahan lo achhe lagte ho..."); + } + @Override + public void bads(){ + System.out.println("1. ladko ki lammbi line..."); + } +} + +class Oops4{ + public static void main(String[] args){ + BeingSundar sundar = new Mayank(); + sundar.goods(); + sundar.bads(); + sundar.complications(); + + sundar = new Priya(); + sundar.goods(); + sundar.bads(); + } +} \ No newline at end of file diff --git a/Oops5.java b/Oops5.java new file mode 100644 index 0000000..39ede09 --- /dev/null +++ b/Oops5.java @@ -0,0 +1,72 @@ +interface BeingSundar{ + void goods(); + void bads(); + /*public void complications(){ + System.out.println("it's complicated..."); + }*///interface cannot have a body method + default void complications(){ + System.out.println("it's complicated..."); + } +} +class Mayank implements BeingSundar{ + @Override + public void goods(){ + System.out.println("1. Sundar ladkiyo ki kami nhi..."); + System.out.println("2. Overconfidence ki me bahot sundar hu..."); + System.out.println("3. Modeling kr skte hai..."); + } + @Override + public void bads(){ + System.out.println("1. Dosro ko judge ya comment krna ..."); + System.out.println("2. Shoes ka color match krna chahiye"); + System.out.println("3. ladkiyo ki kami nhi..."); + } + public void intro(){ + System.out.println("i am mayank..."); + } +} +class Priya implements BeingSundar{ + public void goods(){ + System.out.println("1. kuchh bhi pahan lo achhe lagte ho..."); + } + @Override + public void bads(){ + System.out.println("1. ladko ki lammbi line..."); + } +} + +class Oops5{ + public static void main(String[] args){ + BeingSundar sundar = new Mayank(); + sundar.goods(); + sundar.bads(); + sundar.complications(); + + sundar = new Priya(); + sundar.goods(); + sundar.bads(); + + sundar = new BeingSundar(){ + public void goods(){ + System.out.println("1. Bachhe bhi sundar hoge"); + } + @Override + public void bads(){ + System.out.println("1. koi gaurantee nhi hai bachha gora hoga..."); + } + }; + sundar.goods(); + sundar.bads(); + + new BeingSundar(){ + public void goods(){ + System.out.println("1. Ease to interact and get attention easily"); + System.out.println("2. Easy friendship"); + } + @Override + public void bads(){ + System.out.println("1. Jalne wale log..."); + } + }.goods(); + } +} \ No newline at end of file diff --git a/PackageDemo.java b/PackageDemo.java new file mode 100644 index 0000000..abedccc --- /dev/null +++ b/PackageDemo.java @@ -0,0 +1,9 @@ +package com.cmpundhir; + +class PackageDemo{ + public static void main(String[] args){ + System.out.println("command to create a package and run program is given below"); + System.out.println("1. javac -d . PackageDemo.java"); + System.out.println("2. java com.cmpundhir.PackageDemo"); + } +} \ No newline at end of file diff --git a/PolymorphismDemo1.java b/PolymorphismDemo1.java new file mode 100644 index 0000000..93feda4 --- /dev/null +++ b/PolymorphismDemo1.java @@ -0,0 +1,23 @@ +//Type of polymorphism +//1. static - overloading +//2. Duynamic - overriding +class PolymorphismDemo1{ + + public static int add(int a,int b){ + return a+b; + } + //overloading by changing number of parameters + public static int add(int a,int b,int c){ + return a+b+c; + } + //overloading by changing datatype + public static float add(float a, float b){ + return a+b; + } + public static void main(String[] args){ + System.out.println(add(1,2)); + System.out.println(add(1,2,3)); + System.out.println(add(1.0f,2.3f)); + System.out.println(add(1,2.3f));//automatic type casting / type promotion + } +} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..4bfbe16 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# JavaExamples +Java examples for beginners + +Hello coders , if you are beginner in coding and want to try few programs in java then here are some basic programs on java concepts. So you can try it. + +Happy coding. diff --git a/SynchronisationDemo.java b/SynchronisationDemo.java new file mode 100644 index 0000000..a605aab --- /dev/null +++ b/SynchronisationDemo.java @@ -0,0 +1,28 @@ +class Table{ + public synchronized void printTable(int n){ + for(int i=1;i<11;i++){ + System.out.println(n*i); + try{Thread.sleep(200);}catch(Exception e){} + } + } +} +class MyThread extends Thread{ + Table t; + int n; + MyThread(Table t,int n){ + this.t = t; + this.n = n; + } + public void run(){ + t.printTable(n); + } +} +class SynchronisationDemo{ + public static void main(String[] args){ + Table tb = new Table(); + MyThread t = new MyThread(tb,5); + t.start(); + MyThread t2 = new MyThread(tb,50); + t2.start(); + } +} \ No newline at end of file diff --git a/SynchronizationDemo2.java b/SynchronizationDemo2.java new file mode 100644 index 0000000..9daf382 --- /dev/null +++ b/SynchronizationDemo2.java @@ -0,0 +1,31 @@ +class Table{ + public static synchronized void printTable(int n){ + for(int i=1;i<11;i++){ + System.out.println(n*i); + try{Thread.sleep(200);}catch(Exception e){} + } + } +} +class MyThread extends Thread{ + Table t; + int n; + MyThread(Table t,int n){ + this.t = t; + this.n = n; + } + public void run(){ + t.printTable(n); + } +} +class SynchronizationDemo2{ + public static void main(String[] args){ + Table tb = new Table(); + MyThread t = new MyThread(tb,5); + t.start(); + //created another table object so now both thread have different resources so we + //need static synchronization + Table tb2 = new Table(); + MyThread t2 = new MyThread(tb2,50); + t2.start(); + } +} \ No newline at end of file diff --git a/SynchronizationDemo3.java b/SynchronizationDemo3.java new file mode 100644 index 0000000..122a1ad --- /dev/null +++ b/SynchronizationDemo3.java @@ -0,0 +1,31 @@ +class Table{ + public void printTable(int n){ + synchronized(this){ + for(int i=1;i<11;i++){ + System.out.println(n*i); + try{Thread.sleep(200);}catch(Exception e){} + } + } + } +} +class MyThread extends Thread{ + Table t; + int n; + MyThread(Table t,int n){ + this.t = t; + this.n = n; + } + public void run(){ + t.printTable(n); + } +} +class SynchronizationDemo3{ + public static void main(String[] args){ + Table tb = new Table(); + MyThread t = new MyThread(tb,5); + t.start(); + + MyThread t2 = new MyThread(tb,50); + t2.start(); + } +} \ No newline at end of file diff --git a/SynchronizationDemo4DeadLock.java b/SynchronizationDemo4DeadLock.java new file mode 100644 index 0000000..463f914 --- /dev/null +++ b/SynchronizationDemo4DeadLock.java @@ -0,0 +1,37 @@ +class SynchronizationDemo4DeadLock{ + + public static void main(String[] args){ + final String res1 = "Icecream"; + final String res2 = "Chocolate"; + Thread t1 = new Thread(){ + public void run(){ + synchronized(res1){ + System.out.println(res1+" is locked by "+getName()); + try{Thread.sleep(200);}catch(Exception e){} + synchronized(res2){ + System.out.println(res2+" is locked by "+getName()); + try{Thread.sleep(200);}catch(Exception e){} + System.out.println(res2+" is released by "+getName()); + } + System.out.println(res1+" is released by "+getName()); + } + } + }; + t1.start(); + Thread t2 = new Thread(){ + public void run(){ + synchronized(res2){ + System.out.println(res2+" is locked by "+getName()); + try{Thread.sleep(200);}catch(Exception e){} + synchronized(res1){ + System.out.println(res1+" is locked by "+getName()); + try{Thread.sleep(200);}catch(Exception e){} + System.out.println(res1+" is released by "+getName()); + } + System.out.println(res2+" is released by "+getName()); + } + } + }; + t2.start(); + } +} \ No newline at end of file