├── CSL203Syllabus.pdf ├── Experiment_List.txt ├── Java Lab Record ├── Readme.md ├── prgm1.pdf ├── prgm10.pdf ├── prgm11.pdf ├── prgm12.pdf ├── prgm13.pdf ├── prgm2.pdf ├── prgm3.pdf ├── prgm4.pdf ├── prgm5.pdf ├── prgm6.pdf ├── prgm7.pdf ├── prgm8.pdf └── prgm9.pdf ├── Linear_search.java ├── README.md ├── _1.palindrome.java ├── _1.practice_question_reverse_string.java ├── _10.calculator.java ├── _11.trafficlight.java ├── _12.BinarySearch.java ├── _15.jdbc.java ├── _16.DoublyLinkedList.java ├── _17.QuickSort.java ├── _2.frequency.java ├── _2.practice_question_display_transpose_of_a_matrix.java ├── _3.KeyEvents.java ├── _3.practice_question_second_smallest.java ├── _3a.MatrixMultiplication.java ├── _4.inheritance_array-of-objects-method.java ├── _4.prime.java ├── _5.Abstraction_(sides+areas_constructor method).java ├── _5.ExceptionHandling.java ├── _5.polymorphism.java ├── _6.EvenOddThread.java ├── _7.BinarySort.java ├── _7.FileCount.java ├── _8.File_Write&Read_and_Count_Lines,words,characters.java ├── _8.Multithread.java ├── _8.PriorityThread.java ├── _9.Interface.java ├── _9.StringTokenizerClass.java ├── amstrong.java ├── anagram.java ├── insertion sort.java ├── second smallest element ├── string reverse └── transpose /CSL203Syllabus.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CSI-SCT-SB/CSL203-Object-Oriented-Programming-Lab-In-Java/7abd9dfdf72b7f3fbd6d15b16344f8adacbf28a3/CSL203Syllabus.pdf -------------------------------------------------------------------------------- /Experiment_List.txt: -------------------------------------------------------------------------------- 1 | The syllabus contains six sessions (A, B, C, D, E, F). Each session consists of three concrete 2 | Java exercises, out of which at least two questions are mandatory. 3 | 4 | (A) Basic programs using datatypes, operators, and control statements in Java. 5 | 6 | 1) Write a Java program that checks whether a given string is a palindrome or not. 7 | Ex: MALAYALAM is palindrome. 8 | 9 | 2) Write a Java Program to find the frequency of a given character in a string. ** 10 | 11 | 3) Write a Java program to multiply two given matrices. ** 12 | 13 | (B) Object Oriented Programming Concepts: Problem on the use of constructors, inheritance, 14 | method overloading & overriding, polymorphism and garbage collection: 15 | 16 | 4) Write a Java program which creates a class named 'Employee' having the following 17 | members: Name, Age, Phone number, Address, Salary. It also has a method named 'printSalary( )' which prints the salary of the Employee. Two classes 'Officer' and 'Manager' 18 | inherits the 'Employee' class. The 'Officer' and 'Manager' classes have data members 'specialization' and 'department' respectively. Now, assign name, age, phone number, address 19 | and salary to an officer and a manager by making an object of both of these classes and 20 | print the same. (Exercise to understand inheritance). ** 21 | 22 | 5) Write a java program to create an abstract class named Shape that contains an empty 23 | method named numberOfSides( ). Provide three classes named Rectangle, Triangle and 24 | Hexagon such that each one of the classes extends the class Shape. Each one of the classes contains only the method numberOfSides( ) that shows the number of sides in the given geometrical structures. (Exercise to understand polymorphism). ** 25 | 26 | 6) Write a Java program to demonstrate the use of garbage collector. 27 | 28 | (C) Handling different types of files as well as input and output management methods: 29 | 7) Write a file handling program in Java with reader/writer. 30 | 31 | 8) Write a Java program that read from a file and write to file by handling all file related exceptions. ** 32 | 33 | 9) Write a Java program that reads a line of integers, and then displays each integer, and the 34 | sum of all the integers (Use String Tokenizer class of java.util). ** 35 | 36 | (D) Exception handling and multi-threading applications: 37 | 38 | 10) Write a Java program that shows the usage of try, catch, throws and finally. ** 39 | 40 | 11) Write a Java program that implements a multi-threaded program which has three threads. 41 | First thread generates a random integer every 1 second. If the value is even, second 42 | thread computes the square of the number and prints. If the value is odd the third thread 43 | will print the value of cube of the number. 44 | 45 | 12) Write a Java program that shows thread synchronization. ** 46 | 47 | (E) Graphics Programming: 48 | 49 | 13) Write a Java program that works as a simple calculator. Arrange Buttons for digits and 50 | the + - * % operations properly. Add a text field to display the result. Handle any possible 51 | exceptions like divide by zero. Use Java Swing. ** 52 | 53 | 14) Write a Java program that simulates a traffic light. The program lets the user select one of 54 | three lights: red, yellow, or green. When a radio button is selected, the light is turned on, 55 | and only one light can be on at a time. No light is on when the program starts. ** 56 | 57 | 15) Write a Java program to display all records from a table using Java Database Connectivity (JDBC). 58 | (F) Standard Searching and Sorting Algorithms using data structures and algorithms learned 59 | from course Data Structures (CST 201): 60 | 61 | 16) Write a Java program for the following: ** 62 | 63 | 1) Create a doubly linked list of elements. 64 | 2) Delete a given element from the above list. 65 | 3) Display the contents of the list after deletion. 66 | 67 | 17) Write a Java program that implements Quick sort algorithm for sorting a list of names in 68 | ascending order. ** 69 | 18) Write a Java program that implements the binary search algorithm. -------------------------------------------------------------------------------- /Java Lab Record/Readme.md: -------------------------------------------------------------------------------- 1 | ## THIS IS THE JAVA LAB RECORD OF 2 | 3 | [DEVIKA BIJU](https://github.com/devx19) 4 | -------------------------------------------------------------------------------- /Java Lab Record/prgm1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CSI-SCT-SB/CSL203-Object-Oriented-Programming-Lab-In-Java/7abd9dfdf72b7f3fbd6d15b16344f8adacbf28a3/Java Lab Record/prgm1.pdf -------------------------------------------------------------------------------- /Java Lab Record/prgm10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CSI-SCT-SB/CSL203-Object-Oriented-Programming-Lab-In-Java/7abd9dfdf72b7f3fbd6d15b16344f8adacbf28a3/Java Lab Record/prgm10.pdf -------------------------------------------------------------------------------- /Java Lab Record/prgm11.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CSI-SCT-SB/CSL203-Object-Oriented-Programming-Lab-In-Java/7abd9dfdf72b7f3fbd6d15b16344f8adacbf28a3/Java Lab Record/prgm11.pdf -------------------------------------------------------------------------------- /Java Lab Record/prgm12.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CSI-SCT-SB/CSL203-Object-Oriented-Programming-Lab-In-Java/7abd9dfdf72b7f3fbd6d15b16344f8adacbf28a3/Java Lab Record/prgm12.pdf -------------------------------------------------------------------------------- /Java Lab Record/prgm13.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CSI-SCT-SB/CSL203-Object-Oriented-Programming-Lab-In-Java/7abd9dfdf72b7f3fbd6d15b16344f8adacbf28a3/Java Lab Record/prgm13.pdf -------------------------------------------------------------------------------- /Java Lab Record/prgm2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CSI-SCT-SB/CSL203-Object-Oriented-Programming-Lab-In-Java/7abd9dfdf72b7f3fbd6d15b16344f8adacbf28a3/Java Lab Record/prgm2.pdf -------------------------------------------------------------------------------- /Java Lab Record/prgm3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CSI-SCT-SB/CSL203-Object-Oriented-Programming-Lab-In-Java/7abd9dfdf72b7f3fbd6d15b16344f8adacbf28a3/Java Lab Record/prgm3.pdf -------------------------------------------------------------------------------- /Java Lab Record/prgm4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CSI-SCT-SB/CSL203-Object-Oriented-Programming-Lab-In-Java/7abd9dfdf72b7f3fbd6d15b16344f8adacbf28a3/Java Lab Record/prgm4.pdf -------------------------------------------------------------------------------- /Java Lab Record/prgm5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CSI-SCT-SB/CSL203-Object-Oriented-Programming-Lab-In-Java/7abd9dfdf72b7f3fbd6d15b16344f8adacbf28a3/Java Lab Record/prgm5.pdf -------------------------------------------------------------------------------- /Java Lab Record/prgm6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CSI-SCT-SB/CSL203-Object-Oriented-Programming-Lab-In-Java/7abd9dfdf72b7f3fbd6d15b16344f8adacbf28a3/Java Lab Record/prgm6.pdf -------------------------------------------------------------------------------- /Java Lab Record/prgm7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CSI-SCT-SB/CSL203-Object-Oriented-Programming-Lab-In-Java/7abd9dfdf72b7f3fbd6d15b16344f8adacbf28a3/Java Lab Record/prgm7.pdf -------------------------------------------------------------------------------- /Java Lab Record/prgm8.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CSI-SCT-SB/CSL203-Object-Oriented-Programming-Lab-In-Java/7abd9dfdf72b7f3fbd6d15b16344f8adacbf28a3/Java Lab Record/prgm8.pdf -------------------------------------------------------------------------------- /Java Lab Record/prgm9.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CSI-SCT-SB/CSL203-Object-Oriented-Programming-Lab-In-Java/7abd9dfdf72b7f3fbd6d15b16344f8adacbf28a3/Java Lab Record/prgm9.pdf -------------------------------------------------------------------------------- /Linear_search.java: -------------------------------------------------------------------------------- 1 | /* 2 | Program Name : Implementation of linear search. 3 | Author Name : Aryan Sajan Kulathinal 4 | Branch : R3A , Roll.No: 218 5 | */ 6 | 7 | import java.util.Scanner; 8 | public class Linear_search { 9 | public static void main(String[] args){ 10 | int n,i,x,flag=0; 11 | Scanner sc= new Scanner(System.in); 12 | System.out.println("\nEnter the number of elements in the array : "); 13 | n=sc.nextInt(); 14 | int arr[]= new int[n]; 15 | System.out.println("Enter the required array : "); 16 | for(i=0;iarr[mid]) 29 | beg = mid+1; 30 | 31 | else 32 | end = mid-1; 33 | } 34 | if(flag==0) 35 | System.out.println("Element Not Found !"); 36 | System.out.println("*************************************************"); 37 | System.out.println("Want to search for another element? (1.Yes/2.No)"); 38 | choice = sc.nextInt(); 39 | }while(choice==1); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /_15.jdbc.java: -------------------------------------------------------------------------------- 1 | import java.sql.*; 2 | 3 | public class GFG { 4 | public static void main(String[] args) 5 | { 6 | Connection con = null; 7 | PreparedStatement p = null; 8 | ResultSet rs = null; 9 | 10 | con = connection.connectDB(); 11 | 12 | // Try block to catch exception/s 13 | try { 14 | String sql = "select * from cuslogin"; 15 | p = con.prepareStatement(sql); 16 | rs = p.executeQuery(); 17 | System.out.println("id\t\tname\t\temail"); 18 | while (rs.next()) { 19 | 20 | int id = rs.getInt("id"); 21 | String name = rs.getString("name"); 22 | String email = rs.getString("email"); 23 | System.out.println(id + "\t\t" + name 24 | + "\t\t" + email); 25 | } 26 | } 27 | catch (SQLException e) { 28 | System.out.println(e); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /_16.DoublyLinkedList.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class Node 4 | { 5 | protected int data; 6 | protected Node next,prev; 7 | 8 | public Node() 9 | { 10 | next = null; 11 | prev = null; 12 | data = 0; 13 | } 14 | 15 | public Node(int d, Node n, Node p) 16 | { 17 | next = n; 18 | prev = p; 19 | data = d; 20 | } 21 | 22 | public void setLinkNext(Node n) 23 | { 24 | next = n; 25 | } 26 | 27 | public void setLinkPrev(Node p) 28 | { 29 | prev = p; 30 | } 31 | 32 | public Node getLinkNext() 33 | { 34 | return next; 35 | } 36 | 37 | public Node getLinkPrev() 38 | { 39 | return prev; 40 | } 41 | 42 | public void setData(int d) 43 | { 44 | data = d; 45 | } 46 | 47 | public int getData() 48 | { 49 | return data; 50 | } 51 | } 52 | 53 | class linkedList 54 | { 55 | protected Node start; 56 | protected Node end; 57 | public int size; 58 | 59 | linkedList() 60 | { 61 | start = null; 62 | end = null; 63 | size = 0; 64 | } 65 | 66 | public boolean isEmpty() 67 | { 68 | return start==null; 69 | } 70 | 71 | public int getSize() 72 | { 73 | return size; 74 | } 75 | 76 | public void insertAtStart(int val) 77 | { 78 | Node nptr = new Node(val, null, null); 79 | if(start == null) 80 | { 81 | start = nptr; 82 | end = start; 83 | } 84 | else 85 | { 86 | start.setLinkPrev(nptr); 87 | nptr.setLinkNext(start); 88 | start = nptr; 89 | } 90 | size++; 91 | } 92 | 93 | public void insertAtEnd(int val) 94 | { 95 | Node nptr = new Node(val, null, null); 96 | if(start == null) 97 | { 98 | start = nptr; 99 | end = start; 100 | } 101 | else 102 | { 103 | nptr.setLinkPrev(end); 104 | end.setLinkNext(nptr); 105 | end = nptr; 106 | } 107 | size++; 108 | } 109 | 110 | public void insertAtPos(int val, int pos) 111 | { 112 | Node nptr = new Node(val, null, null); 113 | if(pos == 1) 114 | { 115 | insertAtStart(val); 116 | return; 117 | } 118 | Node ptr = start; 119 | for(int i=2; i<=size; i++) 120 | { 121 | if(i==pos) 122 | { 123 | Node tmp = ptr.getLinkNext(); 124 | ptr.setLinkNext(nptr); 125 | nptr.setLinkPrev(ptr); 126 | nptr.setLinkNext(tmp); 127 | tmp.setLinkPrev(nptr); 128 | } 129 | } 130 | size++; 131 | } 132 | 133 | public void deleteAtFront() 134 | { 135 | if(start == null) 136 | { 137 | System.out.println("Cannot perform delete operation."); 138 | } 139 | else if(size == 1) 140 | { 141 | start = null; 142 | end = null; 143 | size = 0; 144 | return; 145 | } 146 | else 147 | { 148 | start = start.getLinkNext(); 149 | start.setLinkPrev(null); 150 | size--; 151 | } 152 | return; 153 | } 154 | 155 | public void deleteAtEnd() 156 | { 157 | if(start == null) 158 | { 159 | System.out.println("Cannot perform delete operation."); 160 | } 161 | else if(size == 1) 162 | { 163 | start = null; 164 | end = null; 165 | size = 0; 166 | return; 167 | } 168 | else 169 | { 170 | end = end.getLinkPrev(); 171 | end.setLinkNext(null); 172 | size--; 173 | } 174 | } 175 | 176 | public void deleteAtPos(int pos) 177 | { 178 | if(start == null) 179 | { 180 | System.out.println("Cannot perform delete operation."); 181 | } 182 | else 183 | { 184 | Node ptr = start.getLinkNext(); 185 | for(int i=2; i<=size;i++) 186 | { 187 | if(i == pos) 188 | { 189 | Node p = ptr.getLinkPrev(); 190 | Node n = ptr.getLinkNext(); 191 | p.setLinkNext(n); 192 | n.setLinkPrev(p); 193 | size--; 194 | return; 195 | } 196 | ptr = ptr.getLinkNext(); 197 | } 198 | } 199 | } 200 | 201 | public void display() 202 | { 203 | System.out.print("\nDoubly Linked List is "); 204 | if(size == 0) 205 | { 206 | System.out.print("Empty\n"); 207 | return; 208 | } 209 | if(start.getLinkNext() == null) 210 | { 211 | System.out.println(start.getData()); 212 | return; 213 | } 214 | Node ptr = start; 215 | System.out.print(start.getData()+"<->"); 216 | ptr = start.getLinkNext(); 217 | while(ptr.getLinkNext() != null) 218 | { 219 | System.out.print(ptr.getData()+"<->"); 220 | ptr = ptr.getLinkNext(); 221 | } 222 | System.out.print(ptr.getData()+"\n"); 223 | } 224 | } 225 | 226 | public class doublylinkedlist 227 | { 228 | public static void main(String[] args) 229 | { 230 | Scanner scan = new Scanner(System.in); 231 | linkedList list = new linkedList(); 232 | System.out.println("Doubly Linked List\n"); 233 | char ch; 234 | do 235 | { 236 | System.out.println("\n___Doubly Linked List Operations___"); 237 | System.out.println("1.Insert at beginning"); 238 | System.out.println("2.Insert at end"); 239 | System.out.println("3.Insert at position"); 240 | System.out.println("4.Delete at front"); 241 | System.out.println("5.Delete at end"); 242 | System.out.println("6.Delete at position"); 243 | System.out.println("\nEnter your choice: "); 244 | int choice = scan.nextInt(); 245 | switch(choice) 246 | { 247 | case 1: 248 | System.out.println("\nEnter integer element to insert:"); 249 | list.insertAtStart(scan.nextInt()); 250 | break; 251 | case 2: 252 | System.out.println("\nEnter integer element to insert:"); 253 | list.insertAtEnd(scan.nextInt()); 254 | break; 255 | case 3: 256 | System.out.println("\nEnter integer element to insert:"); 257 | int num = scan.nextInt(); 258 | System.out.println("\nEnter position:"); 259 | int pos = scan.nextInt(); 260 | if( pos<1 || pos>list.getSize() ) 261 | System.out.println("Invalid position\n"); 262 | else 263 | list.insertAtPos(num,pos); 264 | break; 265 | case 4: 266 | list.deleteAtFront(); 267 | break; 268 | case 5: 269 | list.deleteAtEnd(); 270 | break; 271 | case 6: 272 | System.out.println("\nEnter position"); 273 | int p = scan.nextInt(); 274 | if( p<1 || p>list.getSize() ) 275 | System.out.println("Invalid position\n"); 276 | else 277 | list.deleteAtPos(p); 278 | break; 279 | default : 280 | System.out.println("Enter correct choice\n"); 281 | break; 282 | } 283 | //Displaying the Linked List 284 | list.display(); 285 | System.out.println("\nDo you want to continue(Type y for Yes OR n for No)"); 286 | ch = scan.next().charAt(0); 287 | }while(ch=='y' || ch=='Y'); 288 | } 289 | } 290 | -------------------------------------------------------------------------------- /_17.QuickSort.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class quicksort 3 | { 4 | public static void quicksort(String A[],int p,int r) 5 | { 6 | if(parr[y+1]){ 17 | temp=arr[y+1]; 18 | arr[y+1]=arr[j]; 19 | arr[j]=temp; 20 | } 21 | } 22 | } 23 | System.out.println("Second largest :"+arr[arr.length-2]); 24 | } 25 | } -------------------------------------------------------------------------------- /_3a.MatrixMultiplication.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | 4 | class Matrixmulti 5 | { 6 | public static void main(String[] args) 7 | { 8 | int A[][] = new int[10][10]; 9 | int B[][] = new int[10][10]; 10 | int C[][] = new int[10][10]; 11 | int i,j,k,r1,c1,r2,c2; 12 | Scanner ob= new Scanner(System.in); 13 | System.out.println(" Enter the no. of rows and columns of 1st matrix "); 14 | r1 = ob.nextInt(); 15 | c1 = ob.nextInt(); 16 | System.out.println(" Enter the no. of rows and columns of 2nd matrix "); 17 | r2 = ob.nextInt(); 18 | c2 = ob.nextInt(); 19 | if(c1 == r2){ 20 | System.out.println(" Enter the elements of 1st matrix: "); 21 | for(i=0; i2000) 30 | { 31 | throw new InsufficientBalanceError("No sufficient balance in your account!"); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /_5.polymorphism.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | abstract class Shape{ 3 | public abstract void numberofSides(); 4 | } 5 | 6 | class Triangle extends Shape{ 7 | public void numberofSides(){ 8 | System.out.println("Number of sides of rectangle is :3"); 9 | } 10 | } 11 | class Rectangle extends Shape{ 12 | public void numberofSides(){ 13 | System.out.println("Number of sides of rectangle is :4"); 14 | } 15 | } 16 | class Hexagon extends Shape{ 17 | public void numberofSides(){ 18 | System.out.println("Number of sides of rectangle is :6"); 19 | } 20 | } 21 | class Main{ 22 | public static void main(String args []){ 23 | Triangle t =new Triangle(); 24 | Rectangle r = new Rectangle(); 25 | Hexagon h = new Hexagon(); 26 | t.numberofSides(); 27 | r.numberofSides(); 28 | h.numberofSides(); 29 | } 30 | } -------------------------------------------------------------------------------- /_6.EvenOddThread.java: -------------------------------------------------------------------------------- 1 | class Even extends Thread //thread to display even numbers 2 | { 3 | public void run() 4 | { 5 | System.out.println("Even Numbers :"); 6 | for(int i=2;i<=100;i+=2) 7 | System.out.println(i); 8 | } 9 | } 10 | class Odd extends Thread //thread to display odd numbers 11 | { 12 | public void run() 13 | { 14 | System.out.println("Odd Numbers :"); 15 | for(int i=1;i<=100;i+=2) 16 | System.out.println(i); 17 | } 18 | } 19 | 20 | 21 | public class Main 22 | { 23 | public static void main(String[] args) 24 | { 25 | Even ob1 = new Even(); 26 | ob1.run(); 27 | System.out.println(); 28 | Odd ob2 = new Odd(); 29 | ob2.run(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /_7.BinarySort.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class BinarySort { 4 | 5 | public static void main(String args[]) { 6 | 7 | int num, i = 0; 8 | 9 | System.out.println("Enter The Number Of Elements: "); 10 | Scanner sc = new Scanner(System.in); 11 | 12 | num = sc.nextInt(); 13 | 14 | System.out.println("Enter The Elements (Ascending order): "); 15 | 16 | int arr[] = new int[num]; 17 | 18 | while (i < num) { 19 | 20 | arr[i] = sc.nextInt(); 21 | i++; 22 | 23 | } 24 | System.out.println("Enter The element To Search: "); 25 | 26 | int sr = sc.nextInt(); 27 | 28 | int l = 0, r = num - 1; 29 | int mid = 0; 30 | 31 | while (l <= r) { 32 | 33 | mid = (l + r) / 2; 34 | 35 | if (arr[mid] == sr) { 36 | break; 37 | } else if (arr[mid] > sr) { 38 | 39 | r = mid - 1; 40 | continue; 41 | 42 | } else { 43 | l = mid + 1; 44 | continue; 45 | } 46 | } 47 | 48 | if (l > r) { 49 | System.out.println("Elements Not Found ! "); 50 | } else if (arr[mid] == sr) { 51 | System.out.println(arr[mid] + " Found At Position " + mid); 52 | } 53 | 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /_7.FileCount.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.io.File; 3 | import java.io.FileWriter; 4 | import java.io.IOException; 5 | 6 | public class File_readWrite 7 | { 8 | public static void main(String[] args) 9 | { 10 | try 11 | { 12 | /*creating a new file*/ 13 | File f1 = new File("test.txt"); 14 | if(f1.createNewFile()) 15 | { 16 | System.out.println("**File Created Successfully**"); 17 | System.out.println(" Filename : "+f1.getName()); 18 | System.out.println(" Readable : "+f1.canRead()); 19 | System.out.println(" Writeable : "+f1.canWrite()); 20 | 21 | } 22 | else 23 | { 24 | System.out.println("An error occurred!"); 25 | } 26 | /*writing to the file using object of FileWriter*/ 27 | FileWriter writer = new FileWriter("test.txt"); 28 | writer.write("This is a test file for demonstrating File Handling in Java!"); 29 | writer.close(); 30 | 31 | System.out.println("-------------------------------------"); 32 | System.out.println("Successfully wrote to the file.."); 33 | System.out.println("-------------------------------------"); 34 | 35 | /*reading the file using object of scanner class*/ 36 | Scanner sc = new Scanner(f1); 37 | System.out.println("**FILE CONTENT**"); 38 | System.out.println(); 39 | int character=0, word=0, line=0; 40 | while(sc.hasNextLine()) 41 | { 42 | //character++; //counts whitespace also 43 | line++; 44 | word++; 45 | String str = sc.nextLine(); 46 | for(int i=0; i=65 && i<=90 || i>=97 && i<=122){ 21 | noAlpha++; 22 | } 23 | if(i==46){ 24 | noLine++; 25 | noWords++; 26 | } 27 | if(i==32 || i==40 || i==44 || i==47 || i==59){ 28 | noWords++; 29 | } 30 | } 31 | System.out.println("Character Count : "+noChar+"\nAlphabet Count : "+noAlpha+"\nLine Count : "+noLine+"\nWord Count : "+noWords); 32 | }catch(Exception e) { 33 | System.out.println("Thrown an exception : "+e); 34 | } 35 | }catch(Exception e){System.out.println(e);} 36 | } 37 | } -------------------------------------------------------------------------------- /_8.Multithread.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | class X implements Runnable { 4 | static int random; 5 | Random rand = new Random(); 6 | 7 | public void run() { 8 | random = rand.nextInt(25); 9 | System.out.println(random); 10 | } 11 | } 12 | 13 | class Y implements Runnable { 14 | public void run() { 15 | if (X.random % 2 == 0) 16 | System.out.println((int) Math.pow(X.random, 2) + "\n"); 17 | } 18 | } 19 | 20 | class Z implements Runnable { 21 | public void run() { 22 | if (X.random % 2 != 0) 23 | System.out.println((int) Math.pow(X.random, 3) + "\n"); 24 | } 25 | } 26 | 27 | class Multithread { 28 | public static void main(String args[]) { 29 | for (int i = 0; i < 10; i++) { 30 | Thread objX = new Thread(new X()); 31 | Thread objY = new Thread(new Y()); 32 | Thread objZ = new Thread(new Z()); 33 | try { 34 | Thread.sleep(1000); 35 | } catch (Exception e) { 36 | } 37 | objX.start(); 38 | objY.start(); 39 | objZ.start(); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /_8.PriorityThread.java: -------------------------------------------------------------------------------- 1 | class MyThread extends Thread 2 | { 3 | public void run() 4 | { 5 | System.out.println("***Priority of threads being checked***"); 6 | } 7 | } 8 | 9 | public class Main 10 | { 11 | public static void main(String[] args) 12 | { 13 | MyThread t1 = new MyThread(); 14 | t1.run(); 15 | System.out.println("t1 Priority :"+t1.getPriority()); //displaying predefined priorities 16 | MyThread t2 = new MyThread(); 17 | System.out.println("t2 Priority :"+t2.getPriority()); 18 | MyThread t3 = new MyThread(); 19 | System.out.println("t3 Priority :"+t3.getPriority()); 20 | System.out.println(); 21 | t1.setPriority(3); //assigning priorities by changing predefined priorities 22 | t2.setPriority(5); 23 | t3.setPriority(7); 24 | System.out.println("t1 Priority :"+t1.getPriority()); 25 | System.out.println("t2 Priority :"+t2.getPriority()); 26 | System.out.println("t3 Priority :"+t3.getPriority()); 27 | 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /_9.Interface.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | interface officer_int 3 | { 4 | //by default the visibility of the methods in interface in java is public void 5 | void printSalary(); 6 | void printdetails(); 7 | } 8 | interface Manager extends officer_int{ 9 | 10 | } 11 | 12 | public class InterfaceInheritance{ 13 | public static void main(String ar[]){ 14 | Scanner sc = new Scanner(System.in); 15 | officer o1 = new officer(); 16 | manager m1 = new manager(); 17 | //officer section 18 | System.out.println("Enter the name of the officer:"); 19 | o1.name=sc.nextLine(); 20 | System.out.println("Enter the age of the officer:"); 21 | o1.age=sc.nextInt(); 22 | System.out.println("Enter the phhone number of the officer:"); 23 | o1.phn_number=sc.nextLong(); 24 | String c = sc.nextLine(); 25 | System.out.println("Enter the address of the officer:"); 26 | o1.address=sc.nextLine(); 27 | System.out.println("Enter the salary of the officer:"); 28 | o1.salary=sc.nextFloat(); 29 | c = sc.nextLine(); 30 | System.out.println("Enter the specialisation of the officer:"); 31 | o1.specialisation=sc.nextLine(); 32 | //manager section 33 | System.out.println("Enter the name of the manager:"); 34 | m1.name=sc.nextLine(); 35 | System.out.println("Enter the age of the manager:"); 36 | m1.age=sc.nextInt(); 37 | System.out.println("Enter the phhone number of the manager:"); 38 | m1.phn_number=sc.nextLong(); 39 | c = sc.nextLine(); 40 | System.out.println("Enter the address of the manager:"); 41 | m1.address=sc.nextLine(); 42 | System.out.println("Enter the salary of the manager:"); 43 | m1.salary=sc.nextFloat(); 44 | c = sc.nextLine(); 45 | System.out.println("Enter the department of the manager:"); 46 | m1.department=sc.nextLine(); 47 | System.out.println(); 48 | System.out.println(); 49 | System.out.println("///////////////////////////////////////////////////////////////////////"); 50 | System.out.println("OFFICER DETAILS:"); 51 | o1.printSalary(); 52 | o1.printdetails(); 53 | System.out.println(); 54 | System.out.println(); 55 | System.out.println("///////////////////////////////////////////////////////////////////////"); 56 | System.out.println("MANAGER DETAILS:"); 57 | m1.printSalary(); 58 | m1.printdetails(); 59 | System.out.println("//////////////////////////////////END//////////////////////////////////"); 60 | } 61 | } 62 | 63 | 64 | class officer implements officer_int{ 65 | String name; 66 | int age; 67 | long phn_number; 68 | String address; 69 | float salary; 70 | String specialisation; 71 | //String department; 72 | public void printSalary(){ 73 | System.out.println("Salary: "+salary); 74 | } 75 | public void printdetails(){ 76 | System.out.println("Name: "+name); 77 | System.out.println("Age: "+age); 78 | System.out.println("Ph No: "+phn_number); 79 | System.out.println("Address: "+address); 80 | System.out.println("Department: "+specialisation); 81 | } 82 | } 83 | 84 | class manager implements Manager{ 85 | //String specialisation; 86 | String name; 87 | int age; 88 | long phn_number; 89 | String address; 90 | float salary; 91 | String department; 92 | public void printSalary(){ 93 | System.out.println("Salary: "+salary); 94 | } 95 | public void printdetails(){ 96 | System.out.println("Name: "+name); 97 | System.out.println("Age: "+age); 98 | System.out.println("Ph No: "+phn_number); 99 | System.out.println("Address: "+address); 100 | System.out.println("Department: "+department); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /_9.StringTokenizerClass.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class StringTokenizer 3 | { 4 | public static void main(String args[]) 5 | { 6 | int dig=0,sum=0; 7 | Scanner scan = new Scanner(System.in); 8 | System.out.println("Enter a sequence of integers with space between them : "); 9 | String digit = scan.nextLine(); 10 | StringTokenizer token = new StringTokenizer(digit); 11 | System.out.println("The integers in the sequence are : "); 12 | while(token.hasMoreTokens()) 13 | { 14 | String s = token.nextToken(); 15 | dig = Integer.parseInt(s); 16 | System.out.println(dig+" "); 17 | sum = sum + dig; 18 | } 19 | System.out.println(); 20 | System.out.println("Sum of the integers : "+sum); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /amstrong.java: -------------------------------------------------------------------------------- 1 | public class Armstrong { 2 | int power(int x, long y) 3 | { 4 | if (y == 0) 5 | return 1; 6 | if (y % 2 == 0) 7 | return power(x, y / 2) * power(x, y / 2); 8 | return x * power(x, y / 2) * power(x, y / 2); 9 | } 10 | int order(int x) 11 | { 12 | int n = 0; 13 | while (x != 0) { 14 | n++; 15 | x = x / 10; 16 | } 17 | return n; 18 | } 19 | 20 | boolean isArmstrong(int x) 21 | { 22 | int n = order(x); 23 | int temp = x, sum = 0; 24 | while (temp != 0) { 25 | int r = temp % 10; 26 | sum = sum + power(r, n); 27 | temp = temp / 10; 28 | } 29 | 30 | return (sum == x); 31 | } 32 | 33 | public static void main(String[] args) 34 | { 35 | Armstrong ob = new Armstrong(); 36 | int x = 153; 37 | System.out.println(ob.isArmstrong(x)); 38 | x = 1253; 39 | System.out.println(ob.isArmstrong(x)); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /anagram.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | class Main { 4 | public static void main(String[] args) { 5 | String str1 = "Race"; 6 | String str2 = "Care"; 7 | 8 | str1 = str1.toLowerCase(); 9 | str2 = str2.toLowerCase(); 10 | 11 | // check if length is same 12 | if(str1.length() == str2.length()) { 13 | 14 | // convert strings to char array 15 | char[] charArray1 = str1.toCharArray(); 16 | char[] charArray2 = str2.toCharArray(); 17 | 18 | // sort the char array 19 | Arrays.sort(charArray1); 20 | Arrays.sort(charArray2); 21 | 22 | // if sorted char arrays are same 23 | // then the string is anagram 24 | boolean result = Arrays.equals(charArray1, charArray2); 25 | 26 | if(result) { 27 | System.out.println(str1 + " and " + str2 + " are anagram."); 28 | } 29 | else { 30 | System.out.println(str1 + " and " + str2 + " are not anagram."); 31 | } 32 | } 33 | else { 34 | System.out.println(str1 + " and " + str2 + " are not anagram."); 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /insertion sort.java: -------------------------------------------------------------------------------- 1 | 2 | public class InsertionSort { 3 | void sort(int arr[]) 4 | { 5 | int n = arr.length; 6 | for (int i = 1; i < n; ++i) { 7 | int key = arr[i]; 8 | int j = i - 1; 9 | 10 | /* Move elements of arr[0..i-1], that are 11 | greater than key, to one position ahead 12 | of their current position */ 13 | while (j >= 0 && arr[j] > key) { 14 | arr[j + 1] = arr[j]; 15 | j = j - 1; 16 | } 17 | arr[j + 1] = key; 18 | } 19 | } 20 | 21 | 22 | static void printArray(int arr[]) 23 | { 24 | int n = arr.length; 25 | for (int i = 0; i < n; ++i) 26 | System.out.print(arr[i] + " "); 27 | 28 | System.out.println(); 29 | } 30 | 31 | 32 | public static void main(String args[]) 33 | { 34 | int arr[] = { 12, 11, 13, 5, 6 }; 35 | 36 | InsertionSort ob = new InsertionSort(); 37 | ob.sort(arr); 38 | 39 | printArray(arr); 40 | } 41 | }; 42 | 43 | 44 | -------------------------------------------------------------------------------- /second smallest element: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Secondsmallest{ 3 | public static void main(String args []){ 4 | Scanner s = new Scanner(System.in); 5 | int n,temp; 6 | System.out.print("enter the number of elements of array :"); 7 | n = s.nextInt(); 8 | int a[] = new int[n]; 9 | for(int i=0; i a[j + 1]) { 16 | // swap arr[j+1] and arr[j] 17 | temp = a[j]; 18 | a[j] = a[j + 1]; 19 | a[j + 1] = temp; 20 | } 21 | } 22 | } 23 | System.out.print("second smallest element is :"+a[1]); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /string reverse: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | // 3 | class Reverse{ 4 | public static void main(String args []){ 5 | Scanner s = new Scanner(System.in); 6 | String str; 7 | String rev = ""; 8 | System.out.print("enter the string"); 9 | str = s.next(); 10 | for(int i = 0; i