├── LICENSE ├── Programs ├── A1.java ├── A10.java ├── A11.java ├── A12.java ├── A13.java ├── A14.java ├── A15.java ├── A16.java ├── A17.java ├── A18.java ├── A19.java ├── A2.java ├── A20.java ├── A21 │ ├── ArrayListSortDemo.java │ ├── CompareExp.java │ ├── CompareSal.java │ ├── E_list.java │ ├── E_list2.java │ ├── Employee.java │ └── Employee2.java ├── A21_V2.java ├── A22.java ├── A23 │ ├── LapArr.java │ ├── Laptop.java │ └── Main.java ├── A24 │ ├── Bulb.java │ ├── Device.java │ ├── Extension_cord.java │ ├── FacadePatternDemo.java │ ├── Laptop.java │ └── TV.java ├── A25 │ ├── A25_1 │ │ ├── Main.java │ │ ├── StuArr.java │ │ └── Student.java │ └── A25_2.java ├── A26 │ ├── A26_1 Finally.java │ ├── A26_2 BankExcep │ │ ├── AgeExcep.java │ │ ├── Bank.java │ │ ├── Main.java │ │ ├── NameAgeExcep.java │ │ └── NameExcep.java │ ├── A26_3 MakeExcep │ │ ├── CreateExcep.java │ │ ├── Main.java │ │ └── MyClass.java │ ├── A26_4.java │ └── A26_5.java ├── A3.java ├── A4.java ├── A5.java ├── A6.java ├── A7.java ├── A8.java ├── A9.java ├── B1.java ├── B10.java ├── B11.java ├── B12 │ ├── Calci.java │ ├── MsgBox.java │ └── note.txt ├── B12_v2.java ├── B2.java ├── B3.java ├── B4.java ├── B5.java ├── B6.java ├── B7.java ├── B8.java └── B9.java ├── Question_recommendations.txt └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Adhvith 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /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/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/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/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/A13.java: -------------------------------------------------------------------------------- 1 | //Program to demo use of toString function 2 | public class Student1 { 3 | public int age; 4 | public String name; 5 | public int id; 6 | 7 | public Student1(int id, String name, int age) { 8 | this.id = id; 9 | this.age = age; 10 | this.name = name; 11 | } 12 | 13 | public String toString() { 14 | return id + " " + name + " " + age; 15 | } 16 | } 17 | 18 | public class Student { 19 | public int age; 20 | public String name; 21 | public int id; 22 | 23 | public Student(int id, String name, int age) { 24 | this.id = id; 25 | this.age = age; 26 | this.name = name; 27 | } 28 | 29 | } 30 | 31 | public class Main { 32 | public static void main(String[] args) { 33 | Student S1 = new Student(1, "Ram", 20); 34 | Student S2 = new Student(2, "raj", 21); 35 | System.out.println(S1); 36 | System.out.println(S2); 37 | Student1 S3 = new Student1(1, "Ram", 20); 38 | Student1 S4 = new Student1(2, "raj", 21); 39 | System.out.println(S3); 40 | System.out.println(S4); 41 | } 42 | 43 | } -------------------------------------------------------------------------------- /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/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/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/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/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() 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/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/A20.java: -------------------------------------------------------------------------------- 1 | 2 | // Student class implementation using ArrayList and generics 3 | /*Loose coupling is achieved in this program 4 | * implementation concern 5 | * object concern 6 | * usage concern 7 | */ 8 | // implementation concern 9 | import java.util.*; 10 | 11 | class Student { 12 | int usn; 13 | int age; 14 | 15 | Student(int usn, int age) { 16 | this.usn = usn; 17 | this.age = age; 18 | } 19 | } 20 | 21 | // object concern 22 | class ArrayListDemo { 23 | public ArrayList 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/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/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() 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/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/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/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/A22.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | // implementation concern is taken care here 4 | public class Employee 5 | { 6 | int Eid; 7 | String Ename; 8 | double Esal; 9 | int Exp; 10 | 11 | Scanner sc=new Scanner(System.in); 12 | public void getData() 13 | { 14 | System.out.println("Enter id,name,sal,exp"); 15 | Eid=sc.nextInt(); 16 | Ename=sc.next(); 17 | Esal=sc.nextDouble(); 18 | Exp=sc.nextInt(); 19 | } 20 | 21 | } 22 | 23 | public class ArrayListDemo 24 | { 25 | public ArrayList 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/A23/LapArr.java: -------------------------------------------------------------------------------- 1 | package Laptop; 2 | 3 | import java.util.*; 4 | 5 | // import javax.sound.midi.SysexMessage; 6 | 7 | public class LapArr { 8 | public ArrayList 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/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/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/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/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/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/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/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/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/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/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/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/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/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/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_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/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/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/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/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/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/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/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/A4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Currency { 4 | 5 | double inr, usd; 6 | double euro, yen; 7 | Scanner in = new Scanner(System.in); 8 | 9 | public void dollartorupee() { 10 | System.out.println("Enter dollars to convert into Rupees:"); 11 | usd = in.nextInt(); 12 | inr = usd * 67; 13 | System.out.println("Dollar =" + usd + "equal to INR=" + inr); 14 | } 15 | 16 | public void rupeetodollar() { 17 | System.out.println("Enter Rupee to convert into Dollars:"); 18 | inr = in.nextInt(); 19 | usd = inr / 67; 20 | System.out.println("Rupee =" + inr + "equal to Dollars=" + usd); 21 | } 22 | 23 | public void eurotorupee() { 24 | System.out.println("Enter euro to convert into Rupees:"); 25 | euro = in.nextInt(); 26 | inr = euro * 79.50; 27 | System.out.println("Euro =" + euro + "equal to INR=" + inr); 28 | } 29 | 30 | public void rupeetoeuro() { 31 | System.out.println("Enter Rupees to convert into Euro:"); 32 | inr = in.nextInt(); 33 | euro = (inr / 79.50); 34 | System.out.println("Rupee =" + inr + "equal to Euro=" + euro); 35 | } 36 | 37 | public void yentorupee() { 38 | System.out.println("Enter yen to convert into Rupees:"); 39 | yen = in.nextInt(); 40 | inr = yen * 0.61; 41 | System.out.println("YEN=" + yen + "equal to INR=" + inr); 42 | } 43 | 44 | public void rupeetoyen() { 45 | System.out.println("Enter Rupees to convert into Yen:"); 46 | inr = in.nextInt(); 47 | yen = (inr / 0.61); 48 | System.out.println("INR=" + inr + "equal to YEN" + yen); 49 | } 50 | } 51 | 52 | public class Distance { 53 | double km, m, miles; 54 | Scanner sc = new Scanner(System.in); 55 | 56 | public void kmtom() { 57 | System.out.print("Enter in km "); 58 | km = sc.nextDouble(); 59 | m = (km * 1000); 60 | System.out.println(km + "km" + "equal to" + m + "metres"); 61 | } 62 | 63 | public void mtokm() { 64 | System.out.print("Enter in meter "); 65 | m = sc.nextDouble(); 66 | km = (m / 1000); 67 | System.out.println(m + "m" + "equal to" + km + "kilometres"); 68 | } 69 | 70 | public void milestokm() { 71 | System.out.print("Enter in miles"); 72 | miles = sc.nextDouble(); 73 | km = (miles * 1.60934); 74 | System.out.println(miles + "miles" + "equal to" + km + "kilometres"); 75 | } 76 | 77 | public void kmtomiles() { 78 | System.out.print("Enter in km"); 79 | km = sc.nextDouble(); 80 | miles = (km * 0.621371); 81 | System.out.println(km + "km" + "equal to" + miles + "miles"); 82 | } 83 | 84 | } 85 | 86 | public class Timer { 87 | int hours, seconds, minutes; 88 | int input; 89 | Scanner sc = new Scanner(System.in); 90 | 91 | public void secondstohours() { 92 | System.out.print("Enter the number of seconds: "); 93 | input = sc.nextInt(); 94 | hours = input / 3600; 95 | minutes = (input % 3600) / 60; 96 | seconds = (input % 3600) % 60; 97 | System.out.println("Hours: " + hours); 98 | System.out.println("Minutes: " + minutes); 99 | System.out.println("Seconds: " + seconds); 100 | } 101 | 102 | public void minutestohours() { 103 | System.out.print("Enter the number of minutes: "); 104 | minutes = sc.nextInt(); 105 | hours = minutes / 60; 106 | minutes = minutes % 60; 107 | System.out.println("Hours: " + hours); 108 | System.out.println("Minutes: " + minutes); 109 | } 110 | 111 | public void hourstominutes() { 112 | System.out.println("enter the no of hours"); 113 | hours = sc.nextInt(); 114 | minutes = (hours * 60); 115 | System.out.println("Minutes: " + minutes); 116 | } 117 | 118 | public void hourstoseconds() { 119 | System.out.println("enter the no of hours"); 120 | hours = sc.nextInt(); 121 | seconds = (hours * 3600); 122 | System.out.println("Minutes: " + seconds); 123 | } 124 | 125 | } 126 | 127 | public class Main { 128 | 129 | public static void main(String args[]) { 130 | Scanner s = new Scanner(System.in); 131 | int choice, ch; 132 | Currency c = new Currency(); 133 | Distance d = new Distance(); 134 | Timer t = new Timer(); 135 | do { 136 | System.out.println("1.dollar to rupee "); 137 | System.out.println("2.rupee to dollar "); 138 | System.out.println("3.Euro to rupee "); 139 | System.out.println("4..rupee to Euro "); 140 | System.out.println("5.Yen to rupee "); 141 | System.out.println("6.Rupee to Yen "); 142 | System.out.println("7.Meter to kilometer "); 143 | System.out.println("8.kilometer to meter "); 144 | System.out.println("9.Miles to kilometer "); 145 | System.out.println("10.kilometer to miles"); 146 | System.out.println("11.Hours to Minutes"); 147 | System.out.println("12.Hours to Seconds"); 148 | System.out.println("13.Seconds to Hours"); 149 | System.out.println("14.Minutes to Hours"); 150 | System.out.println("Enter ur choice"); 151 | choice = s.nextInt(); 152 | switch (choice) { 153 | case 1: 154 | c.dollartorupee(); 155 | break; 156 | 157 | case 2: 158 | 159 | c.rupeetodollar(); 160 | break; 161 | 162 | case 3: 163 | 164 | c.eurotorupee(); 165 | break; 166 | 167 | case 4: 168 | 169 | c.rupeetoeuro(); 170 | break; 171 | 172 | case 5: 173 | c.yentorupee(); 174 | break; 175 | case 6: 176 | 177 | c.rupeetoyen(); 178 | break; 179 | 180 | case 7: 181 | 182 | d.mtokm(); 183 | break; 184 | 185 | case 8: 186 | 187 | d.kmtom(); 188 | break; 189 | 190 | case 9: 191 | 192 | d.milestokm(); 193 | break; 194 | 195 | case 10: 196 | 197 | d.kmtomiles(); 198 | break; 199 | 200 | case 11: 201 | 202 | t.hourstominutes(); 203 | break; 204 | 205 | case 12: 206 | 207 | t.hourstoseconds(); 208 | break; 209 | 210 | case 13: 211 | 212 | t.secondstohours(); 213 | break; 214 | 215 | case 14: 216 | 217 | t.minutestohours(); 218 | break; 219 | } 220 | System.out.println("Enter 0 to quit and 1 to continue "); 221 | ch = s.nextInt(); 222 | } while (ch == 1); 223 | } 224 | } -------------------------------------------------------------------------------- /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/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