From 85779b9f20c273ae3a0eef1e21429e13e73aaf7c Mon Sep 17 00:00:00 2001 From: CMPundhir Date: Mon, 17 Jun 2019 10:58:39 +0530 Subject: [PATCH 01/18] Aggregation demo 2 --- AggregationDemo2.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 AggregationDemo2.java diff --git a/AggregationDemo2.java b/AggregationDemo2.java new file mode 100644 index 0000000..241fb09 --- /dev/null +++ b/AggregationDemo2.java @@ -0,0 +1,19 @@ +{ + "vendorId":"1", + "userId":"1", + "brands": + [ + { + "brandId":"", + "productId":"" + }, + { + + }, + { + + }, + { + + } + ] \ No newline at end of file From c4180f4560294992a2123c5d77152e831f8c2ada Mon Sep 17 00:00:00 2001 From: CMPundhir Date: Mon, 17 Jun 2019 11:04:02 +0530 Subject: [PATCH 02/18] Aggregation demo 2 updated --- AggregationDemo2.java | 56 ++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/AggregationDemo2.java b/AggregationDemo2.java index 241fb09..d9a3456 100644 --- a/AggregationDemo2.java +++ b/AggregationDemo2.java @@ -1,19 +1,37 @@ -{ - "vendorId":"1", - "userId":"1", - "brands": - [ - { - "brandId":"", - "productId":"" - }, - { - - }, - { - - }, - { - - } - ] \ No newline at end of file +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 From 3259b8fd67ed65e01492cd22ca16116f25d429b6 Mon Sep 17 00:00:00 2001 From: CMPundhir Date: Mon, 17 Jun 2019 11:05:50 +0530 Subject: [PATCH 03/18] aggregation demo 2 update 3 --- AggregationDemo2.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AggregationDemo2.java b/AggregationDemo2.java index d9a3456..0410a44 100644 --- a/AggregationDemo2.java +++ b/AggregationDemo2.java @@ -7,7 +7,7 @@ public Address(int hNo,String street,int pincode,String city){ this.hNo = hNo; this.street = street; this.pincode = pincode; - // this.city = city; + this.city = city; } @Override public String toString(){ From b22e191f2abd0074411ee232bfdfba53688735dc Mon Sep 17 00:00:00 2001 From: CMPundhir Date: Mon, 17 Jun 2019 11:34:31 +0530 Subject: [PATCH 04/18] Polymorphism demo 1 --- PolymorphismDemo1.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 PolymorphismDemo1.java 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 From c16c33da640eac4b8c190a75e1d4428dbebab9c2 Mon Sep 17 00:00:00 2001 From: CMPundhir Date: Tue, 18 Jun 2019 11:39:55 +0530 Subject: [PATCH 05/18] Abstraction Demo --- AbstractionDemo1.java | 25 +++++++++++++++++++++++++ AbstractionDemo2.java | 25 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 AbstractionDemo1.java create mode 100644 AbstractionDemo2.java 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 From 974b1895e49a4cfb3e429f6f0d3c5d49f47076d0 Mon Sep 17 00:00:00 2001 From: CMPundhir Date: Thu, 20 Jun 2019 11:10:37 +0530 Subject: [PATCH 06/18] Multiple Ineritance --- MultipleInheritance.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 MultipleInheritance.java diff --git a/MultipleInheritance.java b/MultipleInheritance.java new file mode 100644 index 0000000..0aca361 --- /dev/null +++ b/MultipleInheritance.java @@ -0,0 +1,24 @@ +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(){ + public void method(){ + System.out.println("HAHhahahahahhaha"); + } + }; + a.method(); + } +} \ No newline at end of file From 30419165f202ee7aef533a586cc66bf129a5d0b5 Mon Sep 17 00:00:00 2001 From: CMPundhir Date: Thu, 20 Jun 2019 11:41:42 +0530 Subject: [PATCH 07/18] Multiple inheritance with annonymous object --- MultipleInheritance.java | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/MultipleInheritance.java b/MultipleInheritance.java index 0aca361..a9464bd 100644 --- a/MultipleInheritance.java +++ b/MultipleInheritance.java @@ -15,10 +15,26 @@ public static void main(String[] args){ c.method(); A a = new A(){ + @Override public void method(){ - System.out.println("HAHhahahahahhaha"); + 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(); } -} \ No newline at end of file +} + + + + From 77672c2a147b481b58c498f35da9e93a98e4e6a4 Mon Sep 17 00:00:00 2001 From: CMPundhir Date: Thu, 20 Jun 2019 17:04:52 +0530 Subject: [PATCH 08/18] Abstraction and interfaces demos --- Oops4.java | 46 ++++++++++++++++++++++++++++++++++ Oops5.java | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 Oops4.java create mode 100644 Oops5.java 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 From 3804ce033affcf7704bc8f5018bdfd7827ba2301 Mon Sep 17 00:00:00 2001 From: CMPundhir Date: Thu, 20 Jun 2019 17:08:11 +0530 Subject: [PATCH 09/18] Input demo using scanner --- Input.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Input.java 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 From 46f77ef39a66d9f86103081802a3b9a11d93a3f7 Mon Sep 17 00:00:00 2001 From: CMPundhir Date: Thu, 27 Jun 2019 11:32:33 +0530 Subject: [PATCH 10/18] Exception Handling --- ExceptionHandling.java | 22 ++++++++++++++++++++++ ExceptionHandling2.java | 28 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 ExceptionHandling.java create mode 100644 ExceptionHandling2.java diff --git a/ExceptionHandling.java b/ExceptionHandling.java new file mode 100644 index 0000000..46a015a --- /dev/null +++ b/ExceptionHandling.java @@ -0,0 +1,22 @@ +import java.util.*; +class ExceptionHandling{ + public static void main(String[] args){ + Scanner sc = new Scanner(System.in); + int a=0,b,c; + try{ + System.out.println("Enter a = "); + a = sc.nextInt(); + System.out.println("Enter b = "); + b = sc.nextInt(); + c = a / b; + System.out.println(c); + }catch(ArithmeticException e){ + System.out.println(e); + } + finally{ + System.out.println("I am finally and i always run..."); + } + System.out.println("program is still working..."); + + } +} \ No newline at end of file diff --git a/ExceptionHandling2.java b/ExceptionHandling2.java new file mode 100644 index 0000000..76712aa --- /dev/null +++ b/ExceptionHandling2.java @@ -0,0 +1,28 @@ +import java.util.*; +class AgeException extends Exception{ + public AgeException(String msg){ + super(msg); + } +} +class ExceptionHandling2{ + private static void verifyAge(int age) throws AgeException{ + if(age<18){ + throw new AgeException("You can not vote"); + }else{ + System.out.println("you can vote"); + } + } + public static void main(String[] args){ + Scanner sc = new Scanner(System.in); + System.out.println("Enter your age : "); + int age = sc.nextInt(); + try{ + verifyAge(age); + }catch(AgeException e){ + System.out.println(e); + } + finally{ + System.out.println("I am finally and i always run..."); + } + } +} \ No newline at end of file From 9106fa3e6049e5eff7956b8037f74a13107ab05a Mon Sep 17 00:00:00 2001 From: CMPundhir Date: Tue, 2 Jul 2019 11:45:36 +0530 Subject: [PATCH 11/18] Synchronization Demo --- SynchronisationDemo.java | 28 +++++++++++++++++++++++ SynchronizationDemo2.java | 31 ++++++++++++++++++++++++++ SynchronizationDemo3.java | 31 ++++++++++++++++++++++++++ SynchronizationDemo4DeadLock.java | 37 +++++++++++++++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 SynchronisationDemo.java create mode 100644 SynchronizationDemo2.java create mode 100644 SynchronizationDemo3.java create mode 100644 SynchronizationDemo4DeadLock.java 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 From 8157a977ab863bc8e2336a0e6829a5fe9b6e680f Mon Sep 17 00:00:00 2001 From: CMPundhir Date: Wed, 3 Jul 2019 12:19:19 +0530 Subject: [PATCH 12/18] Input Output demos --- FIleIODemo7.java | 19 +++++++++++++++++++ FileIODemo.java | 15 +++++++++++++++ FileIODemo2.java | 19 +++++++++++++++++++ FileIODemo3.java | 15 +++++++++++++++ FileIODemo4.java | 17 +++++++++++++++++ FileIODemo5.java | 22 ++++++++++++++++++++++ FileIODemo6.java | 19 +++++++++++++++++++ FileIODemo8.java | 25 +++++++++++++++++++++++++ FileIODemo9.java | 35 +++++++++++++++++++++++++++++++++++ 9 files changed, 186 insertions(+) create mode 100644 FIleIODemo7.java create mode 100644 FileIODemo.java create mode 100644 FileIODemo2.java create mode 100644 FileIODemo3.java create mode 100644 FileIODemo4.java create mode 100644 FileIODemo5.java create mode 100644 FileIODemo6.java create mode 100644 FileIODemo8.java create mode 100644 FileIODemo9.java diff --git a/FIleIODemo7.java b/FIleIODemo7.java new file mode 100644 index 0000000..58c17c9 --- /dev/null +++ b/FIleIODemo7.java @@ -0,0 +1,19 @@ +import java.io.FileInputStream; +import java.io.SequenceInputStream; +import java.util.Scanner; + +class FileIODemo7{ + public static void main(String[] args){ + try{ + FileInputStream fin1 = new FileInputStream("E:\\JAVA\\Java Classes Programs\\HelloIO5.txt"); + FileInputStream fin2 = new FileInputStream("E:\\JAVA\\Java Classes Programs\\HelloIO.txt"); + SequenceInputStream bin = new SequenceInputStream(fin1,fin2); + int a = 0; + while((a = bin.read())!= -1){ + System.out.print((char)a); + } + }catch(Exception e){ + System.err.println(e.getMessage()); + } + } +} \ No newline at end of file diff --git a/FileIODemo.java b/FileIODemo.java new file mode 100644 index 0000000..c3936a5 --- /dev/null +++ b/FileIODemo.java @@ -0,0 +1,15 @@ +import java.io.FileOutputStream; + + +class FileIODemo{ + public static void main(String[] args){ + try{ + FileOutputStream fout = new FileOutputStream("E:\\JAVA\\Java Classes Programs\\HelloIO1.txt"); + fout.write(65); + fout.close(); + System.out.println("Success"); + }catch(Exception e){ + System.err.println(e.getMessage()); + } + } +} \ No newline at end of file diff --git a/FileIODemo2.java b/FileIODemo2.java new file mode 100644 index 0000000..8715af3 --- /dev/null +++ b/FileIODemo2.java @@ -0,0 +1,19 @@ +import java.io.FileOutputStream; +import java.util.Scanner; + +class FileIODemo2{ + public static void main(String[] args){ + try{ + Scanner sc = new Scanner(System.in); + FileOutputStream fout = new FileOutputStream("E:\\JAVA\\Java Classes Programs\\HelloIO.txt"); + System.out.println("Enter a message : "); + String msg = sc.nextLine(); + byte[] arr = msg.getBytes(); + fout.write(arr); + fout.close(); + System.out.println("Success"); + }catch(Exception e){ + System.err.println(e.getMessage()); + } + } +} \ No newline at end of file diff --git a/FileIODemo3.java b/FileIODemo3.java new file mode 100644 index 0000000..71864b2 --- /dev/null +++ b/FileIODemo3.java @@ -0,0 +1,15 @@ +import java.io.FileInputStream; +import java.util.Scanner; + +class FileIODemo3{ + public static void main(String[] args){ + try{ + FileInputStream fin = new FileInputStream("E:\\JAVA\\Java Classes Programs\\HelloIO1.txt"); + int a = fin.read(); + System.out.println((char)a); + System.out.println("Success"); + }catch(Exception e){ + System.err.println(e.getMessage()); + } + } +} \ No newline at end of file diff --git a/FileIODemo4.java b/FileIODemo4.java new file mode 100644 index 0000000..b454ff2 --- /dev/null +++ b/FileIODemo4.java @@ -0,0 +1,17 @@ +import java.io.FileInputStream; +import java.util.Scanner; + +class FileIODemo4{ + public static void main(String[] args){ + try{ + FileInputStream fin = new FileInputStream("E:\\JAVA\\Java Classes Programs\\HelloIO.txt"); + int a = 0; + while((a = fin.read())!= -1){ + System.err.print((char)a); + } + System.out.println("Success"); + }catch(Exception e){ + System.err.println(e); + } + } +} \ No newline at end of file diff --git a/FileIODemo5.java b/FileIODemo5.java new file mode 100644 index 0000000..dae8f13 --- /dev/null +++ b/FileIODemo5.java @@ -0,0 +1,22 @@ +import java.io.FileOutputStream; +import java.io.BufferedOutputStream; +import java.util.Scanner; + +class FileIODemo5{ + public static void main(String[] args){ + try{ + Scanner sc = new Scanner(System.in); + FileOutputStream fin = new FileOutputStream("E:\\JAVA\\Java Classes Programs\\HelloIO5.txt"); + BufferedOutputStream buff = new BufferedOutputStream(fin); + System.out.println("Enter a message : "); + String msg = sc.nextLine(); + byte[] arr = msg.getBytes(); + buff.write(arr); + buff.flush(); + buff.close(); + System.out.println("Success"); + }catch(Exception e){ + System.err.println(e); + } + } +} \ No newline at end of file diff --git a/FileIODemo6.java b/FileIODemo6.java new file mode 100644 index 0000000..9f68aef --- /dev/null +++ b/FileIODemo6.java @@ -0,0 +1,19 @@ +import java.io.FileInputStream; +import java.io.BufferedInputStream; +import java.util.Scanner; + +class FileIODemo6{ + public static void main(String[] args){ + try{ + FileInputStream fin = new FileInputStream("E:\\JAVA\\Java Classes Programs\\HelloIO5.txt"); + BufferedInputStream bin = new BufferedInputStream(fin); + int a = 0; + while((a = bin.read())!= -1){ + System.out.print((char)a); + } + System.out.println("Success"); + }catch(Exception e){ + System.err.println(e.getMessage()); + } + } +} \ No newline at end of file diff --git a/FileIODemo8.java b/FileIODemo8.java new file mode 100644 index 0000000..8600987 --- /dev/null +++ b/FileIODemo8.java @@ -0,0 +1,25 @@ +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.SequenceInputStream; +import java.util.Scanner; + +class FileIODemo8{ + public static void main(String[] args){ + try{ + FileInputStream fin1 = new FileInputStream("E:\\JAVA\\Java Classes Programs\\HelloIO5.txt"); + FileInputStream fin2 = new FileInputStream("E:\\JAVA\\Java Classes Programs\\HelloIO.txt"); + FileOutputStream fout = new FileOutputStream("E:\\JAVA\\Java Classes Programs\\SequenceOutput.txt"); + SequenceInputStream bin = new SequenceInputStream(fin1,fin2); + 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/FileIODemo9.java b/FileIODemo9.java new file mode 100644 index 0000000..5634358 --- /dev/null +++ b/FileIODemo9.java @@ -0,0 +1,35 @@ +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.SequenceInputStream; +import java.util.*; + +class FileIODemo9{ + public static void main(String[] args){ + try{ + FileInputStream fin1 = new FileInputStream("E:\\JAVA\\Java Classes Programs\\HelloIO5.txt"); + FileInputStream fin2 = new FileInputStream("E:\\JAVA\\Java Classes Programs\\HelloIO.txt"); + FileInputStream fin3 = new FileInputStream("E:\\JAVA\\Java Classes Programs\\HelloIO5.txt"); + FileInputStream fin4 = new FileInputStream("E:\\JAVA\\Java Classes Programs\\HelloIO.txt"); + FileOutputStream fout = new FileOutputStream("E:\\JAVA\\Java Classes Programs\\SequenceOutput2.txt"); + + Vector 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 From 2842dae4553a39fd6e5176f515dbf3842b2c21e1 Mon Sep 17 00:00:00 2001 From: CMPundhir Date: Fri, 5 Jul 2019 21:06:11 +0530 Subject: [PATCH 13/18] console and package demo --- ConsoleDemo.java | 36 ++++++++++++++++++++++++++++++++++++ PackageDemo.java | 9 +++++++++ 2 files changed, 45 insertions(+) create mode 100644 ConsoleDemo.java create mode 100644 PackageDemo.java 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 Date: Thu, 11 Jul 2019 12:00:35 +0530 Subject: [PATCH 14/18] Comparable and COmparator demo --- .../ComparatorAndComparabaleDemo.java | 46 ++++++++++++ .../MarksComparator.java | 27 +++++++ Comparator and Comparable demo/Student.java | 70 +++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 Comparator and Comparable demo/ComparatorAndComparabaleDemo.java create mode 100644 Comparator and Comparable demo/MarksComparator.java create mode 100644 Comparator and Comparable demo/Student.java 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; + } + } + + +} From 6fa4a1bb1528156a74413e9d1ae4acdead4af8e7 Mon Sep 17 00:00:00 2001 From: CMPundhir Date: Sat, 20 Jul 2019 13:19:50 +0530 Subject: [PATCH 15/18] Abstraction demo complete example --- Abstraction_demo/AbstractDemo.java | 30 ++++++++++++++++++++++++++++++ Abstraction_demo/Buyer.java | 13 +++++++++++++ Abstraction_demo/Car.java | 13 +++++++++++++ Abstraction_demo/MotorCycle.java | 13 +++++++++++++ Abstraction_demo/Vehicle.java | 6 ++++++ 5 files changed, 75 insertions(+) create mode 100644 Abstraction_demo/AbstractDemo.java create mode 100644 Abstraction_demo/Buyer.java create mode 100644 Abstraction_demo/Car.java create mode 100644 Abstraction_demo/MotorCycle.java create mode 100644 Abstraction_demo/Vehicle.java 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/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); +} + From 377f374e0e63dfad4948714647a72f39528581a8 Mon Sep 17 00:00:00 2001 From: CM Pundhir Date: Sun, 15 Mar 2020 13:22:07 +0530 Subject: [PATCH 16/18] Create RealWorldInterfaceEx --- Abstraction_demo/RealWorldInterfaceEx | 59 +++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Abstraction_demo/RealWorldInterfaceEx diff --git a/Abstraction_demo/RealWorldInterfaceEx b/Abstraction_demo/RealWorldInterfaceEx new file mode 100644 index 0000000..7a67565 --- /dev/null +++ b/Abstraction_demo/RealWorldInterfaceEx @@ -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); + } +} + + + From 3d44869eeca670651e12abd0cd21fc8126b49e3c Mon Sep 17 00:00:00 2001 From: CM Pundhir Date: Sun, 15 Mar 2020 13:23:18 +0530 Subject: [PATCH 17/18] Rename RealWorldInterfaceEx to RealWorldInterfaceEx.java --- .../{RealWorldInterfaceEx => RealWorldInterfaceEx.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Abstraction_demo/{RealWorldInterfaceEx => RealWorldInterfaceEx.java} (100%) diff --git a/Abstraction_demo/RealWorldInterfaceEx b/Abstraction_demo/RealWorldInterfaceEx.java similarity index 100% rename from Abstraction_demo/RealWorldInterfaceEx rename to Abstraction_demo/RealWorldInterfaceEx.java From 2e0a8b960910dd5309e17c077e26305d55abee77 Mon Sep 17 00:00:00 2001 From: CM Pundhir Date: Sun, 15 Mar 2020 13:25:42 +0530 Subject: [PATCH 18/18] Create README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 README.md 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.