├── Question_recommendations.txt ├── Programs ├── B12 │ ├── note.txt │ ├── MsgBox.java │ └── Calci.java ├── A26 │ ├── A26_2 BankExcep │ │ ├── AgeExcep.java │ │ ├── NameExcep.java │ │ ├── NameAgeExcep.java │ │ ├── Main.java │ │ └── Bank.java │ ├── A26_3 MakeExcep │ │ ├── MyClass.java │ │ ├── CreateExcep.java │ │ └── Main.java │ ├── A26_4.java │ ├── A26_1 Finally.java │ └── A26_5.java ├── A12.java ├── A24 │ ├── Device.java │ ├── TV.java │ ├── Laptop.java │ ├── Bulb.java │ ├── FacadePatternDemo.java │ └── Extension_cord.java ├── A23 │ ├── Laptop.java │ ├── Main.java │ └── LapArr.java ├── A21 │ ├── Employee2.java │ ├── CompareExp.java │ ├── CompareSal.java │ ├── E_list.java │ ├── E_list2.java │ ├── Employee.java │ └── ArrayListSortDemo.java ├── B9.java ├── A25 │ ├── A25_1 │ │ ├── Student.java │ │ ├── Main.java │ │ └── StuArr.java │ └── A25_2.java ├── A15.java ├── A17.java ├── B3.java ├── A3.java ├── A8.java ├── A18.java ├── A5.java ├── B6.java ├── A13.java ├── A20.java ├── B11.java ├── B1.java ├── A14.java ├── A11.java ├── A19.java ├── A7.java ├── A1.java ├── A16.java ├── A10.java ├── B7.java ├── B2.java ├── B5.java ├── A22.java ├── A2.java ├── A21_V2.java ├── A6.java ├── B4.java ├── B10.java ├── B8.java ├── A9.java ├── B12_v2.java └── A4.java ├── LICENSE └── README.md /Question_recommendations.txt: -------------------------------------------------------------------------------- 1 | Want to recommend any questiona, free feel to put it here with apropirate Section+Question number 2 | -------------------------------------------------------------------------------- /Programs/B12/note.txt: -------------------------------------------------------------------------------- 1 | MsgBox-Applet program 2 | Calci-Swing program 3 | 4 | do note that in calci, evaluate() is kinda complicated and not necessary to understand 5 | it is sufficient to understand createWindow() and createUI() 6 | -------------------------------------------------------------------------------- /Programs/A26/A26_2 BankExcep/AgeExcep.java: -------------------------------------------------------------------------------- 1 | package Exceptions.BankExcep; 2 | 3 | public class AgeExcep extends Throwable { 4 | int age; 5 | 6 | AgeExcep(int age) { 7 | this.age = age; 8 | } 9 | 10 | public String toString() { 11 | return "Age below 18 ie " + age; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Programs/A26/A26_3 MakeExcep/MyClass.java: -------------------------------------------------------------------------------- 1 | package Exceptions.MakeExcep; 2 | 3 | public class MyClass { 4 | static void Sub(int a, int b) throws CreateExcep { 5 | if (a > b) 6 | System.out.println("The result is " + (a - b)); 7 | else 8 | throw new CreateExcep(a, b); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Programs/B12/MsgBox.java: -------------------------------------------------------------------------------- 1 | package applet_swing; 2 | 3 | import java.applet.*; 4 | import java.awt.*; 5 | 6 | public class MsgBox extends Applet{ 7 | 8 | /** 9 | * @param args 10 | */ 11 | public void paint(Graphics G) { 12 | // TODO Auto-generated method stub 13 | G.drawString("Hello world",30,100); 14 | 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Programs/A26/A26_2 BankExcep/NameExcep.java: -------------------------------------------------------------------------------- 1 | package Exceptions.BankExcep; 2 | 3 | public class NameExcep extends Throwable { 4 | String name; 5 | 6 | NameExcep(String name) { 7 | this.name = name; 8 | } 9 | 10 | public String toString() { 11 | return "Name must have more than 3 letters " + name; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Programs/A12.java: -------------------------------------------------------------------------------- 1 | public class ArrayDemo { 2 | public static void main(String[] args) { 3 | // Enhanced for loop demo 4 | int arr[] = new int[4]; 5 | arr[0] = 10; 6 | arr[3] = 20; 7 | arr[2] = 30; 8 | arr[1] = 40; 9 | for (int j : arr) 10 | System.out.println(j); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Programs/A26/A26_3 MakeExcep/CreateExcep.java: -------------------------------------------------------------------------------- 1 | package Exceptions.MakeExcep; 2 | 3 | public class CreateExcep extends Throwable { 4 | int a, b; 5 | 6 | CreateExcep(int a, int b) { 7 | this.a = a; 8 | this.b = b; 9 | } 10 | 11 | public String toString() { 12 | return "value of b = " + b + " is greatest"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Programs/A26/A26_3 MakeExcep/Main.java: -------------------------------------------------------------------------------- 1 | package Exceptions.MakeExcep; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | int a = 10; 6 | int b = 20; 7 | try { 8 | MyClass.Sub(a, b); 9 | } catch (CreateExcep e) { 10 | e.printStackTrace(); 11 | } 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Programs/A24/Device.java: -------------------------------------------------------------------------------- 1 | package facade_design_pattern; 2 | 3 | /* 4 | * this is the common interface that all the devices implement 5 | * 6 | * this is so that facade class can only implement the functions under these, and rest of the functions are hidden 7 | */ 8 | 9 | public interface Device { 10 | public void switchON(); 11 | public void switchOFF(); 12 | public void use(); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Programs/A26/A26_2 BankExcep/NameAgeExcep.java: -------------------------------------------------------------------------------- 1 | package Exceptions.BankExcep; 2 | 3 | public class NameAgeExcep extends Throwable { 4 | int age; 5 | String name; 6 | 7 | NameAgeExcep(int age, String name) { 8 | this.age = age; 9 | this.name = name; 10 | } 11 | 12 | public String toString() { 13 | return "Age below 18 ie " + age + "\nName must have more than 3 letters " + name; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Programs/A23/Laptop.java: -------------------------------------------------------------------------------- 1 | package Laptop; 2 | 3 | public class Laptop implements Comparable { 4 | private int ram; 5 | private int display; 6 | 7 | Laptop(int ram, int display) { 8 | this.ram = ram; 9 | this.display = display; 10 | } 11 | 12 | public int compareTo(Laptop L) { 13 | return ((int) (this.ram - L.ram)); 14 | } 15 | 16 | public String toString() { 17 | return "Ram: " + ram + "\tDisplay: " + display; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Programs/A26/A26_4.java: -------------------------------------------------------------------------------- 1 | package Exceptions; 2 | 3 | public class Throwss { 4 | static void test1() throws ClassNotFoundException { 5 | Class.forName("Exceptions.Mainn"); 6 | } 7 | 8 | static void test2() throws ClassNotFoundException { 9 | test1(); 10 | } 11 | 12 | public static void main(String[] args) { 13 | try { 14 | test2(); 15 | } catch (ClassNotFoundException c) { 16 | c.printStackTrace(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Programs/A21/Employee2.java: -------------------------------------------------------------------------------- 1 | package sorting_ArrayList; 2 | 3 | 4 | public class Employee2 5 | { 6 | String name; 7 | int xp; 8 | float sal; 9 | public Employee2(String name,int xp, float sal) 10 | { 11 | this.name=name; 12 | this.xp=xp; 13 | this.sal=sal; 14 | } 15 | 16 | //Here we are overloading toString function so that when we try to print the object, it will print the below string instead of some hashCode 17 | public String toString() 18 | { 19 | return "Name:"+name+",Exp:"+xp+",Sal:"+sal; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Programs/B9.java: -------------------------------------------------------------------------------- 1 | //wap to read 2 integers a and b, compute a/b and print when b is not zero. raise an exception when b=0 2 | import java.util.*; 3 | class Main { 4 | public static void main(String[] args) 5 | { 6 | int a,b,c; 7 | 8 | Scanner sc=new Scanner(System.in); 9 | a=sc.nextInt(); 10 | b=sc.nextInt(); 11 | try{ 12 | c=a/b; 13 | System.out.println(c); 14 | } 15 | catch(ArithmeticException e) 16 | { 17 | System.out.println("Divide by Zero error"); 18 | e.printStackTrace(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Programs/A25/A25_1/Student.java: -------------------------------------------------------------------------------- 1 | package Priority; 2 | 3 | //implementation concern - Student class implemented 4 | public class Student implements Comparable { 5 | private int usn; 6 | private String name; 7 | 8 | Student(int usn, String name) { 9 | this.usn = usn; 10 | this.name = name; 11 | } 12 | 13 | public int compareTo(Student S) { 14 | return ((int) (this.usn - S.usn)); 15 | } 16 | 17 | public String toString() { 18 | return "|" + name + "\t" + usn + "|\t"; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Programs/A26/A26_2 BankExcep/Main.java: -------------------------------------------------------------------------------- 1 | package Exceptions.BankExcep; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | System.out.println("Enter age, name"); 9 | int age = sc.nextInt(); 10 | String name = sc.next(); 11 | try { 12 | Bank.CreateAcc(age, name); 13 | } catch (AgeExcep | NameExcep | NameAgeExcep e) { 14 | e.printStackTrace(); 15 | } 16 | 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Programs/A24/TV.java: -------------------------------------------------------------------------------- 1 | package facade_design_pattern; 2 | /* 3 | * this is a class that is hidden under the facade class 4 | * 5 | * it is a implements the common interface Device 6 | */ 7 | 8 | public class TV implements Device { 9 | @Override 10 | public void switchON() { 11 | System.out.println("TV is powered"); 12 | 13 | } 14 | 15 | @Override 16 | public void switchOFF() { 17 | System.out.println("TV is not powered"); 18 | 19 | } 20 | 21 | @Override 22 | public void use() { 23 | System.out.println("TV is being used"); 24 | 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Programs/A24/Laptop.java: -------------------------------------------------------------------------------- 1 | package facade_design_pattern; 2 | /* 3 | * this is a class that is hidden under the facade class 4 | * 5 | * it is a implements the common interface Device 6 | */ 7 | public class Laptop implements Device { 8 | @Override 9 | public void switchON() 10 | { 11 | System.out.println("Laptop is charging"); 12 | } 13 | 14 | @Override 15 | public void switchOFF() 16 | { 17 | System.out.println("laptop is discharging"); 18 | } 19 | 20 | @Override 21 | public void use() { 22 | System.out.println("You used the Laptop"); 23 | 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /Programs/A25/A25_1/Main.java: -------------------------------------------------------------------------------- 1 | package Priority; 2 | 3 | import java.util.*; 4 | 5 | //Usage concern 6 | public class Main { 7 | public static void main(String[] args) { 8 | StuArr obj = new StuArr(); 9 | Queue q = obj.getqueue(); 10 | obj.addEle(q); 11 | 12 | // Printing all the elements 13 | System.out.println(q); 14 | System.out.println(q.poll()); 15 | System.out.println(q.poll()); 16 | System.out.println(q.poll()); 17 | System.out.println(q.poll()); 18 | System.out.println(q.poll()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Programs/A15.java: -------------------------------------------------------------------------------- 1 | public class String_operations { 2 | public static void main(String[] args) { 3 | String str = "Java Developer"; 4 | System.out.println(str.length()); 5 | System.out.println(str.charAt(5)); 6 | System.out.println(str.startsWith("Java")); 7 | System.out.println(str.endsWith("per")); 8 | System.out.println(str.indexOf('e')); 9 | System.out.println(str.lastIndexOf('e')); 10 | System.out.println(str.indexOf(' ', 2)); 11 | System.out.println(str.substring(5)); 12 | System.out.println(str.substring(1, 5)); 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /Programs/A17.java: -------------------------------------------------------------------------------- 1 | public class String_palindrome { 2 | public static void main(String[] args) { 3 | String str1 = "ABCBA"; 4 | String str2 = "ABCD"; 5 | String str3 = ""; 6 | int i, n1 = str1.length(), n2 = str2.length(); 7 | for (i = n1 - 1; i >= 0; i--) 8 | str3 = str3 + str1.charAt(i); 9 | 10 | System.out.println("Original String:" + str1); 11 | System.out.println("Reverse String:" + str3); 12 | if (str1.equals(str3)) 13 | System.out.println("Palindrome"); 14 | else 15 | System.out.println("NOT Palindrome"); 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /Programs/A26/A26_2 BankExcep/Bank.java: -------------------------------------------------------------------------------- 1 | package Exceptions.BankExcep; 2 | 3 | //At a time only one exception can be handled even if multiple exceptions exist 4 | public class Bank { 5 | static void CreateAcc(int age, String name) throws AgeExcep, NameExcep, NameAgeExcep { 6 | if (age > 18 && name.length() > 3) { 7 | System.out.println("Account created"); 8 | } else if (age < 18 && name.length() <= 3) { 9 | throw new NameAgeExcep(age, name); 10 | } else if (age < 18) { 11 | throw new AgeExcep(age); 12 | } else { 13 | throw new NameExcep(name); 14 | } 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Programs/A23/Main.java: -------------------------------------------------------------------------------- 1 | package Laptop; 2 | 3 | import java.util.*; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | LapArr obj = new LapArr(); 8 | ArrayList al = obj.getlist(); 9 | obj.append(al); 10 | 11 | // Printing arraylist elements 2 ways 12 | System.out.println(al); 13 | for (Laptop i : al) { 14 | System.out.println(i); 15 | } 16 | // Sorting using comparable 17 | Collections.sort(al); 18 | System.out.println("\n After sorting:"); 19 | for (Laptop i : al) { 20 | System.out.println(i); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Programs/B3.java: -------------------------------------------------------------------------------- 1 | //prime no or not 2 | public class Prime { 3 | void checkPrime(int num) { 4 | int i; 5 | if (num == 1 || num == 0) { 6 | System.out.println("It's not a prime no "); 7 | return; 8 | } 9 | for (i = 2; i <= Math.sqrt(num); i++) { 10 | if (num % i == 0) { 11 | System.out.println("It's not a prime no "); 12 | return; 13 | } 14 | } 15 | System.out.println("It's a prime no "); 16 | } 17 | } 18 | 19 | public class Main { 20 | public static void main(String[] args) { 21 | Prime obj = new Prime(); 22 | obj.checkPrime(65); 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /Programs/A24/Bulb.java: -------------------------------------------------------------------------------- 1 | package facade_design_pattern; 2 | /* 3 | * this is a class that is hidden under the facade class 4 | * 5 | * it is a implements the common interface Device 6 | */ 7 | public class Bulb implements Device { 8 | @Override 9 | public void switchON() { 10 | // TODO Auto-generated method stub 11 | System.out.println("Bulb is powered"); 12 | this.use(); 13 | } 14 | 15 | @Override 16 | public void switchOFF() { 17 | // TODO Auto-generated method stub 18 | System.out.println("Bulb is turned off"); 19 | 20 | } 21 | 22 | @Override 23 | public void use() { 24 | // TODO Auto-generated method stub 25 | System.out.println("Bulb glows"); 26 | 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Programs/A25/A25_2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class DemoQueue { 4 | public static void main(String[] args) { 5 | PriorityQueue queue = new PriorityQueue(); 6 | queue.add(5); 7 | queue.add(1); 8 | queue.add(3); 9 | queue.add(4); 10 | queue.add(2); 11 | 12 | System.out.println(queue); 13 | System.out.println(queue.size()); 14 | System.out.println(queue.poll()); 15 | System.out.println(queue.size()); 16 | System.out.println(queue.peek()); 17 | System.out.println(queue.size()); 18 | System.out.println(queue.poll()); 19 | System.out.println(queue.size()); 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Programs/A24/FacadePatternDemo.java: -------------------------------------------------------------------------------- 1 | package facade_design_pattern; 2 | 3 | /* 4 | * this is the main class where you access the Facade 5 | * From the facade, you can access functions that does the complicated steps for you 6 | */ 7 | 8 | public class FacadePatternDemo { 9 | 10 | public static void main(String[] args) { 11 | Extension_cord E=new Extension_cord(); 12 | //there are3 devices connected to extension cord:bulb, tv, laptop 13 | 14 | E.switchON();// Switched ON all three devices 15 | 16 | E.useTV(); // used only the tv 17 | 18 | 19 | E.useLaptop();//used only the laptop 20 | 21 | 22 | E.switchOFF();//switched off all 3 devices 23 | 24 | 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Programs/A26/A26_1 Finally.java: -------------------------------------------------------------------------------- 1 | package Exceptions; 2 | 3 | public class Finally { 4 | public static void main(String[] args) { 5 | int a = 10; 6 | int b = 20; 7 | try { 8 | System.out.println("Running try block"); 9 | a = 10 / 0; 10 | System.out.println("Exiting try block"); 11 | } catch (ArithmeticException e) { 12 | System.out.println("Running ArithmeticException catch block"); 13 | 14 | } catch (ClassCastException e) { 15 | System.out.println("Running ClassCastException catch block"); 16 | } finally { 17 | System.out.println("The value of a " + (a + b)); 18 | System.out.println("The value of b " + b); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Programs/A25/A25_1/StuArr.java: -------------------------------------------------------------------------------- 1 | package Priority; 2 | 3 | import java.util.*; 4 | 5 | //Object creation concern 6 | public class StuArr { 7 | 8 | // Create priority queue 9 | public Queue getqueue() { 10 | Queue q = new PriorityQueue(); 11 | return q; 12 | } 13 | 14 | // adding elements to queue 15 | public Queue addEle(Queue q) { 16 | Student S[] = new Student[5]; 17 | S[0] = new Student(1, "A"); 18 | S[1] = new Student(2, "B"); 19 | S[2] = new Student(3, "c"); 20 | S[3] = new Student(4, "d"); 21 | S[4] = new Student(5, "e"); 22 | 23 | q.add(S[2]); 24 | q.add(S[3]); 25 | q.add(S[0]); 26 | q.add(S[4]); 27 | q.add(S[1]); 28 | 29 | return q; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Programs/A26/A26_5.java: -------------------------------------------------------------------------------- 1 | package Exceptions; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Main { 6 | public static void main(String[] args) { 7 | Scanner sc = new Scanner(System.in); 8 | int a = 10; 9 | int b = 20; 10 | int c = 0; 11 | try { 12 | a = Integer.parseInt("123a"); 13 | 14 | } catch (ArithmeticException e) { 15 | System.out.println("ArithmeticException block"); 16 | e.printStackTrace(); 17 | } catch (ClassCastException e) { 18 | System.out.println("ClassCastException block"); 19 | e.printStackTrace(); 20 | } catch (Throwable e) { 21 | System.out.println("Throwable block"); 22 | e.printStackTrace(); 23 | System.out.println(e.getMessage()); 24 | } 25 | 26 | System.out.println(a + " " + b); 27 | 28 | } 29 | } -------------------------------------------------------------------------------- /Programs/A3.java: -------------------------------------------------------------------------------- 1 | // object object interaction 2 | class Teacher { 3 | Student s1 = new Student(); 4 | 5 | void givesPen(Pen p1) { 6 | System.out.println("Teacher gives pen"); 7 | s1.recievesPen(p1); 8 | } 9 | 10 | } 11 | 12 | class Student { 13 | void recievesPen(Pen p1) { 14 | System.out.println("Student accepts pen"); 15 | p1.opensCap(); 16 | p1.writing(); 17 | p1.closeCap(); 18 | } 19 | } 20 | 21 | class Pen { 22 | void opensCap() { 23 | System.out.println("Pen cap open"); 24 | } 25 | 26 | void writing() { 27 | System.out.println("Writing"); 28 | } 29 | 30 | void closeCap() { 31 | System.out.println("Pen cap closed"); 32 | } 33 | } 34 | 35 | class Main { 36 | public static void main(String[] args) { 37 | Teacher t1 = new Teacher(); 38 | Pen p1 = new Pen(); 39 | t1.givesPen(p1); 40 | } 41 | } -------------------------------------------------------------------------------- /Programs/A8.java: -------------------------------------------------------------------------------- 1 | public class Singleton { 2 | static Singleton sref = null; 3 | private int i; 4 | 5 | private Singleton(int i) { 6 | this.i = i; 7 | System.out.println("Running constructor"); 8 | } 9 | 10 | public static Singleton myMethod(int i) { 11 | if (sref == null) 12 | sref = new Singleton(i); 13 | else 14 | System.out.println("2nd time onwards\nSent value=" + i); 15 | return sref; 16 | } 17 | 18 | public void display() { 19 | 20 | System.out.println("I=" + i); 21 | } 22 | 23 | } 24 | 25 | public class Main { 26 | public static void main(String[] args) { 27 | Singleton sref1 = Singleton.myMethod(1); 28 | sref1.display(); 29 | Singleton sref2 = Singleton.myMethod(2); 30 | sref2.display(); 31 | Singleton sref3 = Singleton.myMethod(3); 32 | sref3.display(); 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /Programs/A18.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ArraylistDemo { 4 | 5 | 6 | public static void main(String[] args) { 7 | // TODO Auto-generated method stub 8 | ArrayList al=new ArrayList(); 9 | 10 | //adding objects to array 11 | al.add("Hello"); 12 | al.add(new Integer (10)); 13 | al.add(20);// this convert int to Integer Objects automatically(doesn't work on some versions of Java) 14 | al.add(21.5); 15 | System.out.println("Size of List:"+al.size()); 16 | 17 | System.out.println(al.get(0)); 18 | 19 | //al.get(i) returns object at i-th position 20 | for(int i=0;i getlist() { 9 | // creating an arraylist 10 | ArrayList al = new ArrayList(); 11 | return al; 12 | } 13 | 14 | public ArrayList append(ArrayList al) { 15 | Scanner sc = new Scanner(System.in); 16 | System.out.println("Enter number of laptop objects to be created"); 17 | int n = sc.nextInt(); 18 | Laptop L[] = new Laptop[n]; 19 | for (int i = 0; i < n; i++) { 20 | System.out.println("Enter RAM, display"); 21 | int ram = sc.nextInt(); 22 | int display = sc.nextInt(); 23 | L[i] = new Laptop(ram, display); 24 | // Adding Laptop objects to arraylist 25 | al.add(L[i]); 26 | } 27 | return al; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Programs/A24/Extension_cord.java: -------------------------------------------------------------------------------- 1 | package facade_design_pattern; 2 | 3 | /* 4 | * This is the Facade class 5 | * It is the one that implements the uses of the devices 6 | * here, the exact way the functions of each object work is hidden 7 | */ 8 | 9 | 10 | public class Extension_cord { 11 | private Device laptop; 12 | private Device tv; 13 | private Device bulb; 14 | //constructor 15 | public Extension_cord() 16 | { 17 | laptop=new Laptop(); 18 | tv=new TV(); 19 | bulb=new Bulb(); 20 | } 21 | 22 | //to use only laptop 23 | public void useLaptop() 24 | { 25 | laptop.use(); 26 | } 27 | 28 | //to use only tv 29 | public void useTV() 30 | { 31 | tv.use(); 32 | } 33 | 34 | //to switch on all devices 35 | public void switchON() 36 | { 37 | laptop.switchON(); 38 | bulb.switchON(); 39 | tv.switchON(); 40 | 41 | } 42 | 43 | 44 | //to switch off all devices 45 | public void switchOFF() 46 | { 47 | laptop.switchOFF(); 48 | bulb.switchOFF(); 49 | tv.switchOFF(); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Programs/A5.java: -------------------------------------------------------------------------------- 1 | import java.nio.file.Paths; 2 | 3 | interface car{ 4 | public void startEngine(); 5 | public void moveCar(); 6 | public void stopEngine(); 7 | } 8 | class BMW implements car{ 9 | public void startEngine() 10 | { 11 | System.out.println("The engine starts"); 12 | } 13 | 14 | @Override 15 | public void moveCar() { 16 | System.out.println("The car moves"); 17 | } 18 | public void stopEngine() 19 | { 20 | System.out.println("The emgine stops"); 21 | } 22 | } 23 | class MyCar 24 | { 25 | public static car getcar() 26 | { 27 | return new BMW(); //This is the factory method that returns a BMW car object. 28 | } 29 | } 30 | public class Main 31 | { 32 | public static void main(String[] args) 33 | { 34 | car car =MyCar.getcar(); //This is a type of object upcasting 35 | car.startEngine(); 36 | car.moveCar(); 37 | car.stopEngine(); 38 | } 39 | } -------------------------------------------------------------------------------- /Programs/A21/CompareExp.java: -------------------------------------------------------------------------------- 1 | package sorting_ArrayList; 2 | 3 | import java.util.*; 4 | // comparator interface is stored in java.util 5 | 6 | public class CompareExp implements Comparator{ 7 | /* When Collection sorts an ArrayList 8 | * it calls the compare function for the syntax 9 | * Collections.sort(arraylist , helper class) 10 | * here, while sorting, it calls compare function to see which object is greater 11 | * for compare(obj1,obj2) 12 | * if obj1 > obj2 then return positive value,ie, if we are sorting on age, if obj1.age > obj2.age the return any positive number 13 | * if obj1 < obj2 the return a negative value,ie, if we are sorting on age, if obj1.age < obj2.age the return any negative number 14 | * if both objects have same sorting value then return 0,ie, if we are sorting on age, if obj1.age = obj2.age the return 0 15 | */ 16 | public int compare(Employee2 E1,Employee2 E2) 17 | { 18 | if(E1.xp>E2.xp) 19 | return 1; 20 | if(E1.xp{ 8 | 9 | /* When Collection sorts an ArrayList 10 | * it calls the compare function for the syntax 11 | * Collections.sort(arraylist , helper class) 12 | * here, while sorting, it calls compare function to see which object is greater 13 | * for compare(obj1,obj2) 14 | * if obj1 > obj2 then return positive value,ie, if we are sorting on age, if obj1.age > obj2.age the return any positive number 15 | * if obj1 < obj2 the return a negative value,ie, if we are sorting on age, if obj1.age < obj2.age the return any negative number 16 | * if both objects have same sorting value then return 0,ie, if we are sorting on age, if obj1.age = obj2.age the return 0 17 | */ 18 | public int compare(Employee2 E1,Employee2 E2) 19 | { 20 | if(E1.sal>E2.sal) 21 | return 1; 22 | if(E1.sal getList() { 24 | Student s1 = new Student(123, 20); 25 | Student s2 = new Student(1255, 21); 26 | Student s3 = new Student(1235, 22); 27 | ArrayList al = new ArrayList(); 28 | al.add(s1); 29 | al.add(s2); 30 | al.add(s3); 31 | return al; 32 | } 33 | } 34 | 35 | // usage concern 36 | class Main { 37 | public static void main(String args[]) { 38 | ArrayListDemo d1 = new ArrayListDemo(); 39 | ArrayList list = d1.getList(); 40 | for (Student s : list) { 41 | System.out.println(s.usn); 42 | System.out.println(s.age); 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /Programs/B11.java: -------------------------------------------------------------------------------- 1 | package file_oper; 2 | 3 | import java.io.*; 4 | import java.util.Scanner; 5 | 6 | public class FileDemo { 7 | public static void main(String[] args) { 8 | String filename; 9 | Scanner sc=new Scanner(System.in); 10 | System.out.println("Enter the file name"); 11 | filename=sc.next(); 12 | File f=new File (filename); 13 | System.out.println("File exists: "+f.exists()); 14 | System.out.println("File is readable: "+f.canRead()); 15 | System.out.println("File is writeable: "+f.canWrite()); 16 | System.out.println("Is a directory: "+f.isDirectory()); 17 | System.out.println("Lenght of file is: "+f.length()+" bytes"); 18 | try 19 | { 20 | char ch; 21 | FileInputStream fis = new FileInputStream(filename); 22 | System.out.println("\nContents of file are:\n"); 23 | while(fis.available()!=0) 24 | { 25 | System.out.print((char)fis.read()); 26 | } 27 | fis.close(); 28 | 29 | } 30 | catch (FileNotFoundException e) 31 | { 32 | // TODO: handle exception 33 | System.out.println("Cannot find the specific file"); 34 | } 35 | catch (IOException e) 36 | { 37 | System.out.println("Cannot read file"); 38 | // TODO: handle exception 39 | } 40 | 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Programs/A21/E_list.java: -------------------------------------------------------------------------------- 1 | package sorting_ArrayList; 2 | 3 | import java.util.*; 4 | /* 5 | * Array lists are stored in the package java.util.ArrayList 6 | * 7 | * Java Collections Class are also stored in java.util.Collections 8 | * 9 | * Scanner is stored in java.util.Scanner 10 | * 11 | * to reduce code, we import the whole java.util package by typing [java.util.*], here * means all packages in java.util 12 | */ 13 | 14 | public class E_list { 15 | 16 | //Applying loose coupling to create an array list 17 | public ArrayList getList() 18 | { 19 | ArrayList al= new ArrayList(); 20 | return al; 21 | } 22 | 23 | 24 | //Applying loose coupling to add elements to array list 25 | public void append(ArrayList al) 26 | { 27 | char ans; 28 | Scanner sc=new Scanner(System.in); 29 | do//this loop runs as long as you enter 'y' oy 'Y' when asked to continue 30 | { 31 | System.out.println("Enter Employee details"); 32 | String name=sc.next(); 33 | int xp=sc.nextInt(); 34 | float sal=sc.nextFloat(); 35 | Employee E=new Employee(name,xp,sal); 36 | al.add(E); 37 | System.out.println("Do you want to Append more?"); 38 | ans=sc.next().charAt(0); 39 | }while((ans=='Y')||(ans=='y')); 40 | 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Programs/A21/E_list2.java: -------------------------------------------------------------------------------- 1 | package sorting_ArrayList; 2 | 3 | import java.util.*; 4 | 5 | /* 6 | * Array lists are stored in the package java.util.ArrayList 7 | * 8 | * Java Collections Class are also stored in java.util.Collections 9 | * 10 | * Scanner is stored in java.util.Scanner 11 | * 12 | * to reduce code, we import the whole java.util package by typing [java.util.*], here * means all packages in java.util 13 | */ 14 | 15 | public class E_list2 { 16 | 17 | //Applying loose coupling to create an array list 18 | 19 | public ArrayList getList() 20 | { 21 | ArrayList al= new ArrayList(); 22 | return al; 23 | } 24 | 25 | //Applying loose coupling to add elements to array list 26 | 27 | public void append(ArrayList al) 28 | { 29 | char ans; 30 | Scanner sc=new Scanner(System.in); 31 | //this loop runs as long as you enter 'y' oy 'Y' when asked to continue 32 | do 33 | { 34 | System.out.println("Enter Employee details"); 35 | String name=sc.next(); 36 | int xp=sc.nextInt(); 37 | float sal=sc.nextFloat(); 38 | Employee2 E=new Employee2(name,xp,sal); 39 | al.add(E); 40 | System.out.println("Do you want to Append more?"); 41 | ans=sc.next().charAt(0); 42 | }while((ans=='Y')||(ans=='y')); 43 | 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Programs/B1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class QuadraticEquation { 4 | 5 | public static void main(String args[]) { 6 | Scanner sc = new Scanner(System.in); 7 | System.out.println("For ax^2 + bx + c = 0 "); 8 | System.out.print("Enter the value of a: "); 9 | double a = sc.nextDouble(); 10 | System.out.print("Enter the value of b: "); 11 | double b = sc.nextDouble(); 12 | System.out.print("Enter the value of c: "); 13 | double c = sc.nextDouble(); 14 | double d = b * b - 4.0 * a * c; 15 | if (d > 0.0) { 16 | double r1 = (-b + Math.pow(d, 0.5)) / (2.0 * a); 17 | double r2 = (-b - Math.pow(d, 0.5)) / (2.0 * a); 18 | System.out.println("The roots are real and unequal ie r1 = " + r1 + 19 | " and r2 = " + r2); 20 | } else if (d == 0.0) { 21 | double r1 = -b / (2.0 * a); 22 | System.out.println("The roots are real and equal ie r = " + r1); 23 | } else { 24 | System.out.println("Roots are not real ie \n r1 = " + 25 | (-b) / (2.0 * a) + " + i " + 26 | ((Math.pow(Math.abs(d), 0.5)) / (2.0 * a)) + 27 | "\n r1 = " + (-b) / (2.0 * a) + " - i " + 28 | (Math.pow(Math.abs(d), 0.5)) / (2.0 * a)); 29 | 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Programs/A14.java: -------------------------------------------------------------------------------- 1 | // Inheritance usage 2 | class Person { 3 | String name; 4 | int age; 5 | 6 | Person(String name, int age) { 7 | this.name = name; 8 | this.age = age; 9 | } 10 | 11 | void display() { 12 | System.out.println("Name: " + name + "\nAge: " + age); 13 | } 14 | } 15 | 16 | class Teacher extends Person { 17 | String domain; 18 | int exp; 19 | 20 | Teacher(String name, int age, String domain, int exp) { 21 | super(name, age); 22 | this.domain = domain; 23 | this.exp = exp; 24 | } 25 | 26 | void display() { 27 | super.display(); 28 | System.out.println("Domain: " + domain + "\nExperience: " + exp); 29 | 30 | } 31 | } 32 | 33 | class Student extends Person { 34 | String dept; 35 | String usn; 36 | 37 | Student(String name, int age, String dept, String usn) { 38 | super(name, age); 39 | this.dept = dept; 40 | this.usn = usn; 41 | } 42 | 43 | void display() { 44 | super.display(); 45 | System.out.println("Department: " + dept + " \nUsn: " + usn); 46 | 47 | } 48 | } 49 | 50 | public class Main { 51 | public static void main(String[] args) { 52 | Teacher t1 = new Teacher("Ram", 30, "webdev", 10); 53 | t1.display(); 54 | Student s1 = new Student("Aditya", 19, "cse", "1rn21cs015"); 55 | s1.display(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Programs/A11.java: -------------------------------------------------------------------------------- 1 | public class First_occurence_of_a_character { 2 | 3 | public static void main(String[] args) { 4 | String s = "aswiwsc"; 5 | int i; 6 | 7 | // first character that occurs more than once. eg in " adfdgghha" a repeats from 8 | // 1st position 9 | for (i = 0; i < s.length(); i++) { 10 | if (s.indexOf(s.charAt(i), i + 1) != -1) { 11 | System.out.println(s.charAt(i)); 12 | break; 13 | } 14 | } 15 | if (i == s.length()) { 16 | System.out.println("no repetation"); 17 | } 18 | 19 | // character that repeats the earliest. eg in " adfdgghha" d repeats at 4th 20 | // position 21 | for (i = 0; i < s.length(); i++) { 22 | if (s.indexOf(s.charAt(i)) != i) { 23 | System.out.println(s.charAt(i)); 24 | break; 25 | } 26 | } 27 | if (i == s.length()) { 28 | System.out.println("no repetation"); 29 | } 30 | 31 | // list of repeated characters 32 | int chars[] = new int[256]; 33 | for (i = 0; i < s.length(); i++) { 34 | if (s.indexOf(s.charAt(i), i + 1) != -1) { 35 | chars[s.charAt(i)]++; 36 | } 37 | } 38 | for (i = 'a'; i <= 'z'; i++) { 39 | if (chars[i] > 0) 40 | System.out.println((char) i); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /Programs/A19.java: -------------------------------------------------------------------------------- 1 | 2 | //Employee class(hidden) 3 | public class Employee { 4 | 5 | private String name; 6 | private float sal; 7 | private int id; 8 | public Employee(String a, float b, int c) 9 | { 10 | name=a; 11 | sal=b; 12 | id=c; 13 | } 14 | 15 | // this overload to string so that System.out.println() of any objet of employee class prints the below string instead of hashcode 16 | public String toString() 17 | { 18 | return ("Name="+name+"\nSalary:"+sal+"\nID:"+id); 19 | } 20 | } 21 | 22 | 23 | 24 | import java.util.*; 25 | public class EmployeeArrayList { 26 | 27 | 28 | //return an array list that only stores objects of employee class 29 | public ArrayList getlist() 30 | { 31 | ArrayList al = new ArrayList(); 32 | return al; 33 | } 34 | public ArrayList append(ArrayList al ) 35 | { 36 | Scanner sc= new Scanner(System.in); 37 | System.out.println("Enter Employee name, Salary and ID"); 38 | String name=sc.nextLine(); 39 | float sal=sc.nextFloat(); 40 | int id=sc.nextInt(); 41 | Employee E=new Employee(name,sal,id); 42 | al.add(E); 43 | return al; 44 | } 45 | 46 | } 47 | 48 | 49 | 50 | 51 | public class Main { 52 | 53 | /** 54 | * @param args 55 | */ 56 | public static void main(String[] args) { 57 | // TODO Auto-generated method stub 58 | EmployeeArrayList obj= new EmployeeArrayList(); 59 | ArrayList al= obj.getlist(); 60 | obj.append(al);// this adds an Object of Employee class 61 | //put an infinite while loop for a menu driven version 62 | System.out.println(al.get(0)); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /Programs/A7.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | //Interface class 4 | public interface Account { 5 | public void open_Account(); 6 | 7 | public void close_Account(); 8 | } 9 | 10 | // Method implementation 11 | public class HDFC_Bank implements Account { 12 | 13 | public void open_Account() { 14 | System.out.println("Welcome to ICICI Bank dear Customer"); 15 | 16 | } 17 | 18 | public void close_Account() { 19 | System.out.println("We are sorry for the inconvinience"); 20 | 21 | } 22 | 23 | } 24 | 25 | // Method implementation 26 | public class ICICI_Bank implements Account { 27 | 28 | public void open_Account() { 29 | System.out.println("dear customer, Welcome to HDFC Bank"); 30 | 31 | } 32 | 33 | public void close_Account() { 34 | System.out.println("Account Closed"); 35 | 36 | } 37 | 38 | } 39 | 40 | // Helper Class 41 | public class MyAcc { 42 | public static Account getAccount(int i) { 43 | switch (i) { 44 | case 1: 45 | return new HDFC_Bank(); 46 | case 2: 47 | return new ICICI_Bank(); 48 | default: 49 | System.exit(0); 50 | return new ICICI_Bank(); 51 | 52 | } 53 | } 54 | 55 | } 56 | 57 | // Main class 58 | public class Main { 59 | public static void main(String[] args) { 60 | Scanner sc = new Scanner(System.in); 61 | System.out.println("Enter 1 for ICICI and 2 for HDFC"); 62 | int ch; 63 | ch = sc.nextInt(); 64 | Account A1 = MyAcc.getAccount(ch); 65 | A1.open_Account(); 66 | A1.close_Account(); 67 | sc.close(); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /Programs/A1.java: -------------------------------------------------------------------------------- 1 | public class ComplexAddn { 2 | private float real; 3 | private float img; 4 | 5 | public ComplexAddn() // Default contructor 6 | { 7 | real = 0; 8 | img = 0; 9 | } 10 | 11 | public ComplexAddn(float real, float img)// initialize with set value 12 | { 13 | this.real = real; 14 | this.img = img; 15 | } 16 | 17 | public static ComplexAddn add(ComplexAddn A, ComplexAddn B)// add function on class name 18 | { 19 | ComplexAddn C = new ComplexAddn(); 20 | C.real = A.real + B.real; 21 | C.img = A.img + B.img; 22 | return C; 23 | } 24 | 25 | public ComplexAddn add(ComplexAddn B)// add function on object name 26 | { 27 | ComplexAddn C = new ComplexAddn(); 28 | C.real = this.real + B.real; 29 | C.img = this.img + B.img; 30 | return C; 31 | } 32 | 33 | public void display()// display value 34 | { 35 | System.out.print(real + " "); 36 | if (img >= 0) 37 | System.out.print("+"); 38 | System.out.println(img + "i"); 39 | } 40 | 41 | } 42 | 43 | public class Main { 44 | public static void main(String[] args) { 45 | ComplexAddn C1 = new ComplexAddn(7.1f, 8.3f); // initializing 2 complex numbers 46 | ComplexAddn C2 = new ComplexAddn(-13.5f, 2.1f); 47 | ComplexAddn C3 = new ComplexAddn(); // value for adding on class name 48 | ComplexAddn C4 = new ComplexAddn(); // value for adding with class name 49 | C3 = ComplexAddn.add(C1, C2); // static method addition 50 | C4 = C1.add(C2);// normal method addition 51 | C3.display();// display complex number 52 | C4.display(); 53 | } 54 | } -------------------------------------------------------------------------------- /Programs/A21/Employee.java: -------------------------------------------------------------------------------- 1 | package sorting_ArrayList; 2 | 3 | import java.util.*; 4 | /* 5 | * Scanner is stored in java.util.Scanner 6 | * 7 | * Comparable interface is stored in java.util 8 | * 9 | * to reduce code, we import the whole java.util package by typing [java.util.*], here * means all packages in java.util 10 | */ 11 | 12 | public class Employee implements Comparable { 13 | String name; 14 | int xp; 15 | float sal; 16 | public Employee(String name,int xp, float sal) 17 | { 18 | this.name=name; 19 | this.xp=xp; 20 | this.sal=sal; 21 | } 22 | 23 | /* When Collection sorts an ArrayList 24 | * it calls the compare function for the syntax 25 | * Collections.sort(arraylist) 26 | * here, while sorting, it calls compareTo function to see which object is greater 27 | * for obj1.compareTo(obj2) 28 | * if obj1 > obj2 then return positive value,ie, if we are sorting on age, if obj1.age > obj2.age the return any positive number 29 | * if obj1 < obj2 the return a negative value,ie, if we are sorting on age, if obj1.age < obj2.age the return any negative number 30 | * if both objects have same sorting value then return 0,ie, if we are sorting on age, if obj1.age = obj2.age the return 0 31 | */ 32 | public int compareTo(Employee ob) 33 | { 34 | return (this.xp)-(ob.xp); 35 | // if this.xp>ob.xp then it gives +ve value, if it is less than ob.xp, it gives -ve value . 36 | // if both objects have same xp value, it returns 0 37 | } 38 | 39 | 40 | //Here we are overloading toString function so that when we try to print the object, it will print the below string instead of some hashCode 41 | public String toString() 42 | { 43 | return "Name:"+name+",Exp:"+xp+",Sal:"+sal; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Programs/A16.java: -------------------------------------------------------------------------------- 1 | public class Constant_Pool_and_Non_Constant_Pool { 2 | 3 | public static void main(String[] args) { 4 | String str1 = new String("watermelon"); 5 | String str2 = "watermelon"; 6 | String str3 = "water"; 7 | String str4 = "melon"; 8 | String str5 = "water" + "melon"; 9 | String str6 = str3 + "melon"; 10 | System.out.println(str1 == str2); 11 | System.out.println(str2 == str5); 12 | System.out.println(str2 == str6); 13 | System.out.println(str1 + str1.hashCode()); 14 | System.out.println(str2 + str2.hashCode()); 15 | System.out.println(str3 + str3.hashCode()); 16 | System.out.println(str4 + str4.hashCode()); 17 | System.out.println(str5 + str5.hashCode()); 18 | System.out.println(str6 + str6.hashCode()); 19 | /* 20 | * when you create an object of string class it makes the object in String pool 21 | * string pool has 2 different parts:Constant, non-constant pool 22 | * In Constant pool, there can be no Duplicate objects, ie ,no 2 objects can 23 | * have same string data 24 | * duplicate strings here only refer to the same object 25 | * In Non-constant pool there can be duplicate objects 26 | * when you create a string obj using 'new' keyword, it create it in 27 | * non-constant pool 28 | * when you create a string by assigning a string value 29 | * directly,(str2="watermelon") it is made in the constant pool 30 | * so str2 and str5 both are in constant pool and references to the same 31 | * object"watermelon" 32 | * str6 however is in nonconstant pool since creating a string using an object 33 | * reference causes it to be in non constant pool, 34 | * hence it has a different hash value 35 | * in strA==strB, it compares hash value of object not data inside it 36 | */ 37 | 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /Programs/A10.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Items { 4 | Scanner sc = new Scanner(System.in); 5 | int[] itemCode = new int[50]; 6 | float[] price = new float[50]; 7 | int count = 0; 8 | int delCode; 9 | 10 | public void getdata() { 11 | System.out.println("Enter Item Code\n"); 12 | itemCode[count] = sc.nextInt(); 13 | System.out.println("Enter Item Price\n"); 14 | price[count] = sc.nextFloat(); 15 | count++; 16 | 17 | } 18 | 19 | public void display() { 20 | for (int i = 0; i < count; i++) 21 | System.out.println("Code = " + itemCode[i] + "\nPrice = " + price[i]); 22 | } 23 | 24 | public void delete() { 25 | 26 | System.out.println("Enter Item Code to delete\n"); 27 | delCode = sc.nextInt(); 28 | for (int i = 0; i < count; i++) { 29 | if (delCode == itemCode[i]) 30 | price[i] = 0; 31 | } 32 | 33 | } 34 | 35 | public void sum() { 36 | float sum = 0; 37 | for (int i = 0; i < count; i++) 38 | sum = sum + price[i]; 39 | System.out.println("Sum = " + sum); 40 | } 41 | } 42 | 43 | public class Main { 44 | public static void main(String[] args) { 45 | Scanner sc = new Scanner(System.in); 46 | Items n = new Items(); 47 | int ch; 48 | while (true) { 49 | System.out.println("\n Press 1.Add items \n2.Delete Items \n3..Display items \n4.Display Sum \n5.Exit \n"); 50 | ch = sc.nextInt(); 51 | switch (ch) { 52 | case 1: 53 | n.getdata(); 54 | break; 55 | case 2: 56 | n.delete(); 57 | break; 58 | case 3: 59 | n.display(); 60 | break; 61 | case 4: 62 | n.sum(); 63 | break; 64 | default: 65 | System.exit(0); 66 | 67 | } 68 | 69 | } 70 | 71 | } 72 | 73 | } -------------------------------------------------------------------------------- /Programs/B7.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | //creating an interface with method bioData 4 | public interface Resume 5 | { 6 | public void bioData(); 7 | } 8 | 9 | //creating a Teacher class which implements the interface 10 | public class Teacher implements Resume 11 | { 12 | String name; 13 | String qual; 14 | int exp; 15 | int ach; 16 | 17 | //creating a scanner object for Teacher class 18 | Scanner sc= new Scanner (System.in); 19 | 20 | //implementing the method biodata of interface in Teacher class 21 | public void bioData() 22 | { 23 | System.out.println("Enter name, qualification, experience, achievement: "); 24 | name =sc.next(); 25 | qual=sc.next(); 26 | exp=sc.nextInt(); 27 | ach=sc.nextInt(); 28 | } 29 | 30 | //displaying the informations of Teacher's Resume 31 | public void display() 32 | { 33 | System.out.println("Name: "+name+"\n"+"Qualification: "+qual+"\n"+"Experience: "+exp+"\n"+"Achievement: "+ach+"\n"); 34 | } 35 | } 36 | 37 | //creating a Student class which implements the interface 38 | public class Student implements Resume 39 | { 40 | String name; 41 | String dis; 42 | int res; 43 | 44 | //creating a scanner object for Student class 45 | Scanner sc= new Scanner (System.in); 46 | 47 | //implementing the method biodata of interface in Student class 48 | public void bioData() 49 | { 50 | System.out.println("Enter name, discipline, result: "); 51 | name =sc.next(); 52 | dis=sc.next(); 53 | res=sc.nextInt(); 54 | } 55 | 56 | //displaying the informations of Student's Resume 57 | public void display() 58 | { 59 | System.out.println("Name: "+name+"\n"+"Discipline: "+dis+"\n"+"Result: "+res+"\n"); 60 | } 61 | } 62 | 63 | 64 | class Demo { 65 | public static void main(String[] args) { 66 | //creating an object of Teacher class 67 | Teacher obj=new Teacher(); 68 | obj.bioData(); 69 | obj.display(); 70 | 71 | //creating an object of Student class 72 | Student obj1=new Student(); 73 | obj1.bioData(); 74 | obj1.display(); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /Programs/A21/ArrayListSortDemo.java: -------------------------------------------------------------------------------- 1 | package sorting_ArrayList; 2 | 3 | import java.util.*; 4 | /* 5 | * Array lists are stored in the package java.util.ArrayList 6 | * 7 | * Java Collections Class are also stored in java.util.Collections 8 | * 9 | * Scanner is stored in java.util.Scanner 10 | * 11 | * to reduce code, we import the whole java.util package by typing [java.util.*], here * means all packages in java.util 12 | */ 13 | public class ArrayListSortDemo { 14 | public static void main(String[] args) 15 | { 16 | 17 | /* 18 | * This is to demonstrate use of Comparable interface 19 | * E_list and Employee class use Comparable interface 20 | */ 21 | E_list obj=new E_list(); 22 | //making an Array list 23 | ArrayList al=obj.getList(); 24 | 25 | //Adding Objects with user input data into Array List 26 | obj.append(al); 27 | //Prints the order of array list elements before sorting based on Experience(Exp) 28 | System.out.println(al); 29 | 30 | // this sorts the ArrayList using the CompareTo function on employee class 31 | Collections.sort(al); 32 | //Prints the elements in array list after sorting based on Exp 33 | System.out.println(al); 34 | 35 | 36 | /* 37 | * This is to demonstrate use of Comparator interface 38 | * E_list2 and Employee2 class use Comparator interface 39 | */ 40 | E_list2 obj2=new E_list2(); 41 | 42 | //making an Array list 43 | ArrayList al2=obj2.getList(); 44 | 45 | //Adding Objects with user input data into Array List 46 | obj2.append(al2); 47 | 48 | //Prints the order of array list elements before sorting based on Experience(Exp) 49 | System.out.println(al2); 50 | 51 | // this sorts the ArrayList using the Compare function in helper class CompareExp based on Exp 52 | Collections.sort(al2,new CompareExp()); 53 | 54 | //Prints the elements in array list after sorting based on Exp 55 | System.out.println(al2); 56 | 57 | 58 | // this sorts the ArrayList using the Compare function in helper class CompareSal based on Sal 59 | Collections.sort(al2,new CompareSal()); 60 | 61 | //Prints the elements in array list after sorting based on Sal 62 | System.out.println(al2); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Programs/B2.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.*; 3 | 4 | //Student class 5 | public class Student { 6 | 7 | // Data in Student Class 8 | private String name; 9 | private String USN; 10 | private String branch; 11 | private long phone; 12 | 13 | //Default constructor 14 | public Student() {} 15 | 16 | /* Data to be accepted by user 17 | * This has to be a function since private members 18 | * can only be accessed inside the same class 19 | * 20 | * 21 | */ 22 | public void getData() 23 | { 24 | //Scanner object to accept User input 25 | Scanner input=new Scanner(System.in); // this will give a warning 26 | System.out.println("\n\nEnter the following details:"); 27 | 28 | System.out.print("Name : "); 29 | name=input.nextLine(); //this accepts a full Line of text with spaces 30 | 31 | System.out.print("\nUSN : "); 32 | USN=input.next(); //this accepts text until spacebar,tab or enter key is pressed 33 | 34 | System.out.print("\nBranch : "); 35 | branch=input.next(); 36 | 37 | System.out.print("\nPhone Number : "); 38 | phone=input.nextLong(); // this acceps a number of datatype 'long' 39 | 40 | } 41 | 42 | 43 | /* 44 | * This method is to display the data in object 45 | * since private can only be accessed in the class 46 | */ 47 | public void display() 48 | { 49 | System.out.println("\n\nStudent details:\nName : "+name+"\nUSN : "+USN+"\nBranch : "+branch+"\nPhone Number : "+phone); 50 | } 51 | } 52 | 53 | 54 | 55 | public class Main { 56 | 57 | public static void main(String[] args) { 58 | // TODO Auto-generated method stub 59 | int n,i; 60 | Scanner input=new Scanner(System.in); 61 | 62 | System.out.print("Enter no of Students : "); 63 | n = input.nextInt(); //Accepting no of Students 64 | 65 | 66 | Student S[] = new Student[n]; 67 | /* 68 | * Declaring n objects of Student class 69 | */ 70 | 71 | for(i=0;i getList() 26 | { 27 | //object creation concern is taken care here 28 | int i,n; 29 | Scanner sc=new Scanner(System.in); 30 | System.out.println("Enter no of employees"); 31 | n=sc.nextInt(); 32 | Employee e1[]=new Employee[n]; 33 | for(i=0;i al=new ArrayList(); 41 | for(i=0;i al) 48 | { 49 | for(Employee i: al) 50 | System.out.println("Exp: "+i.Exp+" "+"Eid: "+i.Eid+" "+"Ename: "+i.Ename+" "+"Esal: "+i.Esal); 51 | } 52 | 53 | } 54 | 55 | //using Comparator interface to sort on basis of experience 56 | public class ExpComp implements Comparator { 57 | 58 | public int compare(Employee e1, Employee e2) 59 | { 60 | if (e1.Exp == e2.Exp) 61 | return 0; 62 | else if (e1.Exp > e2.Exp) 63 | return 1; 64 | else 65 | return -1; 66 | } 67 | } 68 | 69 | public class Trial { 70 | public static void main(String[] args) { 71 | //usage concern is taken care here 72 | ArrayListDemo data=new ArrayListDemo(); 73 | ArrayList al=data.getList(); 74 | //using builtin sort function with the help of Comparator 75 | Collections.sort(al, new ExpComp()); 76 | 77 | data.display(al); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /Programs/A2.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class Employee { 4 | private String name; 5 | private int ID; 6 | private int xp; 7 | private double sal; 8 | 9 | public Employee() { 10 | } 11 | 12 | public Employee(String name, int ID, int xp, double sal) { 13 | this.name = name; 14 | this.ID = ID; 15 | this.xp = xp; 16 | this.sal = sal; 17 | } 18 | 19 | public void display() { 20 | System.out.println("\nName:" + name + "\nID:" + ID + "\nExperience:" + xp + "\nSalary:" + sal); 21 | } 22 | 23 | public static void sortXP(Employee obj[]) { 24 | Employee temp = new Employee(); 25 | for (int i = 0; i < 5; i++) { 26 | for (int j = 0; j < 4 - i; j++) { 27 | if (obj[j].xp > obj[j + 1].xp) { 28 | temp = obj[j]; 29 | obj[j] = obj[j + 1]; 30 | obj[j + 1] = temp; 31 | } 32 | } 33 | } 34 | } 35 | 36 | public static void sortSAL(Employee obj[]) { 37 | Employee temp = new Employee(); 38 | for (int i = 0; i < 5; i++) { 39 | for (int j = 0; j < 4 - i; j++) { 40 | if (obj[j].sal > obj[j + 1].sal) { 41 | temp = obj[j]; 42 | obj[j] = obj[j + 1]; 43 | obj[j + 1] = temp; 44 | } 45 | } 46 | } 47 | } 48 | 49 | } 50 | 51 | public class Main { 52 | public static void main(String[] args) { 53 | Scanner sc = new Scanner(System.in); 54 | Employee E[] = new Employee[5]; 55 | String name; 56 | int ID, i, xp; 57 | double sal; 58 | for (i = 0; i < 5; i++) { 59 | 60 | System.out.println("\nEnter Name,ID,Experience,Salary"); 61 | name = sc.next(); 62 | ID = sc.nextInt(); 63 | xp = sc.nextInt(); 64 | sal = sc.nextDouble(); 65 | E[i] = new Employee(name, ID, xp, sal); 66 | } 67 | for (i = 0; i < 5; i++) { 68 | E[i].display(); 69 | } 70 | System.out.println("Enter 1. To sort by XP 2. To sort by Salary"); 71 | int ch = sc.nextInt(); 72 | switch (ch) { 73 | case 1: 74 | Employee.sortXP(E); 75 | break; 76 | case 2: 77 | Employee.sortSAL(E); 78 | break; 79 | default: 80 | System.out.println("Invalid choice"); 81 | } 82 | for (i = 0; i < 5; i++) { 83 | E[i].display(); 84 | } 85 | } 86 | 87 | } -------------------------------------------------------------------------------- /Programs/A21_V2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | // in case of comparable the class itself must implement the interface 5 | 6 | // implementation concern is taken care here 7 | public class Employee implements Comparable 8 | { 9 | int Eid; 10 | String Ename; 11 | double Esal; 12 | int Exp; 13 | 14 | Scanner sc=new Scanner(System.in); 15 | public void getData() 16 | { 17 | System.out.println("Enter id,name,sal,exp"); 18 | Eid=sc.nextInt(); 19 | Ename=sc.next(); 20 | Esal=sc.nextInt(); 21 | Exp=sc.nextInt(); 22 | } 23 | //compareTo method to Sort 24 | public int compareTo(Employee e1) 25 | { 26 | //it will compare both object and return 0 1 or -1 27 | return ((Double)this.Esal).compareTo(e1.Esal); 28 | } 29 | 30 | } 31 | 32 | public class ArrayListDemo 33 | { 34 | public ArrayList getList() 35 | { 36 | //object creation concern is taken care here 37 | int i,n; 38 | Scanner sc=new Scanner(System.in); 39 | System.out.println("Enter no of employees"); 40 | n=sc.nextInt(); 41 | Employee e1[]=new Employee[n]; 42 | for(i=0;i al=new ArrayList(); 50 | for(i=0;i al) 57 | { 58 | for(Employee i: al) 59 | System.out.println("Eid: "+i.Eid+" "+"Ename: "+i.Ename+" "+"Esal: "+i.Esal+" "+"Exp: "+i.Exp); 60 | } 61 | 62 | } 63 | 64 | public class ExpComp implements Comparator { 65 | 66 | public int compare(Employee e1, Employee e2) 67 | { 68 | if (e1.Exp == e2.Exp) 69 | return 0; 70 | else if (e1.Exp > e2.Exp) 71 | return 1; 72 | else 73 | return -1; 74 | } 75 | } 76 | 77 | public class Trial { 78 | public static void main(String[] args) { 79 | //usage concern is taken care here 80 | ArrayListDemo data=new ArrayListDemo(); 81 | ArrayList al=data.getList(); 82 | //sorting using comparator 83 | Collections.sort(al, new ExpComp()); 84 | System.out.println("Sorting on Experience"); 85 | data.display(al); 86 | //sorting using comparable 87 | Collections.sort(al); 88 | System.out.println("Sorting on Salary"); 89 | data.display(al); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /Programs/A6.java: -------------------------------------------------------------------------------- 1 | 2 | //Interface that shows methods available to user 3 | public interface Email { 4 | public void delete(); 5 | public void send(); 6 | public void compose(); 7 | public void show_unsent(); 8 | 9 | } 10 | 11 | 12 | 13 | // Hidden class that implements the functions in interface 14 | import java.util.Scanner; 15 | 16 | public class Google implements Email { 17 | 18 | private String draft; 19 | public Scanner input=new Scanner(System.in); 20 | private boolean unsent=false; 21 | public Google() 22 | { 23 | draft=""; 24 | } 25 | @Override 26 | public void delete() { 27 | if(unsent==false) 28 | { 29 | 30 | System.out.println("No draft email to delete"); 31 | } 32 | else 33 | { 34 | System.out.println("draft deleted"); 35 | draft=""; 36 | unsent=false; 37 | } 38 | 39 | // TODO Auto-generated method stub 40 | 41 | } 42 | 43 | @Override 44 | public void send() { 45 | if(unsent) 46 | { 47 | System.out.println("Email Sent"); 48 | unsent=false; 49 | return; 50 | } 51 | System.out.println("No Email to send"); 52 | 53 | } 54 | 55 | @Override 56 | public void compose() { 57 | System.out.println("Enter no of lines of text"); 58 | String temp; 59 | draft=""; 60 | int i,n; 61 | n=input.nextInt(); 62 | 63 | System.out.println("Enter text"); 64 | temp=input.nextLine(); 65 | for(i=0;i