├── ButtonExample.java ├── Calculator.java ├── Calculator_interface.java ├── CheckBox.java ├── Copy_objects.java ├── Day10_1_CopyContentInFile.java ├── Day10_1_File.java ├── Day1_1_Arithmetic.java ├── Day1_2_Display.java ├── Day1_3_Square_Root.java ├── Day1_4_Sum_of_two.java ├── Day1_5_Greater_of_two.java ├── Day2_10_Read_display_array.java ├── Day2_11_Bubble_sort.java ├── Day2_12_Linear_search.java ├── Day2_13_Smallest_largest_in_array.java ├── Day2_14_Descending_sort.java ├── Day2_15_Merge_2_array.java ├── Day2_16_Multiplication_table.java ├── Day2_1_Largest_of_2.java ├── Day2_2_Largest_of_3.java ├── Day2_3_Odd_or_even.java ├── Day2_4_Prime_or_not.java ├── Day2_5_Sum_of_n.java ├── Day2_6_Armstrong_or_not.java ├── Day2_7_Palindrome_or_not.java ├── Day2_8_Reverse_of_number.java ├── Day2_9_Fibonacci_series.java ├── Day3_1_Read_display_array.java ├── Day3_2_Read_display_matrix.java ├── Day3_3_Add_Matrix.java ├── Day3_4_Transpose_matrix.java ├── Day3_5_Multiplication_Matrix.java ├── Day4_1_First_last_index.java ├── Day4_2_Frequency_of_character.java ├── Day4_3_String_palindrome_without.java ├── Day5_1_Class_example.java ├── Day5_2_Simple_inheritance.java ├── Day5_3_Multiple_inheritance.java ├── Day5_4_Method_overloading.java ├── Day6_1_Abstract_class_ex1.java ├── Day6_2_Object_as_Parameter.java ├── Day6_3_Package.java ├── Day6_4_Interface.java ├── Day6_5_Shapes_Interface.java ├── Day7_1_Multiple_Inheritance.java ├── Day7_2_Ex5_LMS.java ├── Day8_1_Error_Handling.java ├── Day8_2_Throw_in_JAVA.java ├── Day9_1_Banking.java ├── Dynamic_polymorphism.java ├── Encapsulation_demo.java ├── Exception_demo.java ├── FileReader_demo.java ├── FileWriter_demo.java ├── File_delete.java ├── File_exists_or_not.java ├── File_write_read_write.java ├── Interface_demo.java ├── Lab_Questions ├── Abstract_class_ex1.java ├── Fahrenheit_Celsius_Converter.java ├── Multithreading_Ex1.java ├── README.md └── Shapes_method_overloading.java ├── MouseEvents.java ├── Multithreading.java ├── Multithreading_demo.java ├── Multithreading_demo2_join.java ├── Polymorphism_demo.java ├── Quick_sort.java ├── README.md ├── RadioButton.java ├── Smiley Emoji ├── Images │ └── Mileeeex_SAAAN.png ├── README.md └── Smiley_Emoji.java ├── Swing1.java ├── Swing2.java ├── Swing3.java ├── Swing4.java ├── Swing5.java ├── Swing6.java ├── Testfile.java ├── Thread_demo.java ├── Thread_synchronization.java ├── Traffic.java ├── Traffic_working.java └── inheritance.java /ButtonExample.java: -------------------------------------------------------------------------------- 1 | package javalab; 2 | 3 | import javax.swing.*; 4 | 5 | public class test1 { 6 | public static void main(String[] args) { 7 | JFrame f = new JFrame("Button Example"); 8 | JButton b = new JButton("Click Here"); 9 | b.setBounds(50, 200, 95, 30); 10 | f.add(b); 11 | 12 | JTextField tf = new JTextField(); 13 | tf.setBounds(50, 50, 150, 20); 14 | f.add(tf); 15 | 16 | JTextField tf2 = new JTextField(); 17 | tf2.setBounds(50, 110, 150, 20); 18 | f.add(tf2); 19 | 20 | JLabel l1, l2; 21 | l1 = new JLabel("First Label"); 22 | l1.setBounds(50, 20, 100, 30); 23 | l2 = new JLabel("Second Label"); 24 | l2.setBounds(50, 80, 100, 30); 25 | f.add(l1); 26 | f.add(l2); 27 | f.setSize(400, 400); 28 | f.setLayout(null); 29 | f.setVisible(true); 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Calculator.java: -------------------------------------------------------------------------------- 1 | import java.awt.event.*; 2 | import javax.swing.*; 3 | public class Calculator implements ActionListener { 4 | JTextField t1; 5 | JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16; 6 | int n,i,flag; 7 | Calculator() { 8 | JFrame f=new JFrame("calculator"); 9 | JPanel p=new JPanel(); 10 | t1=new JTextField(); 11 | t1.setBounds(100,100,200,30); 12 | b1=new JButton("1"); 13 | b1.setBounds(100,140,50,30); 14 | b2=new JButton("2"); 15 | b2.setBounds(150,140,50,30); 16 | b3=new JButton("3"); 17 | b3.setBounds(200,140,50,30); 18 | b4=new JButton("+"); 19 | b4.setBounds(250,140,50,30); 20 | b5=new JButton("4"); 21 | b5.setBounds(100,170,50,30); 22 | b6=new JButton("5"); 23 | b6.setBounds(150,170,50,30); 24 | b7=new JButton("6"); 25 | b7.setBounds(200,170,50,30); 26 | b8=new JButton("-"); 27 | b8.setBounds(250,170,50,30); 28 | b9=new JButton("7"); 29 | b9.setBounds(100,200,50,30); 30 | b10=new JButton("8"); 31 | b10.setBounds(150,200,50,30); 32 | b11=new JButton("9"); 33 | b11.setBounds(200,200,50,30); 34 | b12=new JButton("*"); 35 | b12.setBounds(250,200,50,30); 36 | b13=new JButton("/"); 37 | b13.setBounds(100,230,50,30); 38 | b14=new JButton("0"); 39 | b14.setBounds(150,230,50,30); 40 | b15=new JButton("="); 41 | b15.setBounds(200,230,50,30); 42 | b16=new JButton("C"); 43 | b16.setBounds(250,230,50,30); 44 | p.add(t1); 45 | p.add(b1); 46 | p.add(b2); 47 | p.add(b3); 48 | p.add(b4); 49 | p.add(b5); 50 | p.add(b6); 51 | p.add(b7); 52 | p.add(b8); 53 | p.add(b9); 54 | p.add(b10); 55 | p.add(b11); 56 | p.add(b12); 57 | p.add(b13); 58 | p.add(b14); 59 | p.add(b15); 60 | p.add(b16); 61 | b1.addActionListener(this); 62 | b2.addActionListener(this); 63 | b3.addActionListener(this); 64 | b4.addActionListener(this); 65 | b5.addActionListener(this); 66 | b6.addActionListener(this); 67 | b7.addActionListener(this); 68 | b8.addActionListener(this); 69 | b9.addActionListener(this); 70 | b10.addActionListener(this); 71 | b11.addActionListener(this); 72 | b12.addActionListener(this); 73 | b13.addActionListener(this); 74 | b14.addActionListener(this); 75 | b15.addActionListener(this); 76 | b16.addActionListener(this); 77 | f.setContentPane(p); 78 | f.setSize(400,500); 79 | f.setLayout(null); 80 | f.setVisible(true); 81 | } 82 | public void actionPerformed(ActionEvent e) { 83 | if(e.getSource()==b1) 84 | t1.setText(t1.getText()+"1"); 85 | else if(e.getSource()==b2) 86 | t1.setText(t1.getText()+"2"); 87 | else if(e.getSource()==b3) 88 | t1.setText(t1.getText()+"3"); 89 | else if(e.getSource()==b5) 90 | t1.setText(t1.getText()+"4"); 91 | else if(e.getSource()==b6) 92 | t1.setText(t1.getText()+"5"); 93 | else if(e.getSource()==b7) 94 | t1.setText(t1.getText()+"6"); 95 | else if(e.getSource()==b9) 96 | t1.setText(t1.getText()+"7"); 97 | else if(e.getSource()==b10) 98 | t1.setText(t1.getText()+"8"); 99 | else if(e.getSource()==b11) 100 | t1.setText(t1.getText()+"9"); 101 | else if(e.getSource()==b14) 102 | t1.setText(t1.getText()+"0"); 103 | else if(e.getSource()==b4) 104 | { 105 | String s=t1.getText(); 106 | n=Integer.parseInt(s); 107 | t1.setText(""); 108 | i=1; 109 | } 110 | else if(e.getSource()==b8) 111 | { 112 | String s=t1.getText(); 113 | n=Integer.parseInt(s); 114 | t1.setText(""); 115 | i=2; 116 | } 117 | else if(e.getSource()==b12) 118 | { 119 | String s=t1.getText(); 120 | n=Integer.parseInt(s); 121 | t1.setText(""); 122 | i=3; 123 | } 124 | else if(e.getSource()==b13) 125 | { 126 | String s=t1.getText(); 127 | n=Integer.parseInt(s); 128 | t1.setText(""); 129 | i=4; 130 | } 131 | else if(e.getSource()==b15) { 132 | calc(n,i); 133 | } 134 | else { 135 | t1.setText(""); 136 | } 137 | } 138 | void calc(int t,int e) { 139 | int res; 140 | switch(e) { 141 | case 1 : res=t+Integer.parseInt(t1.getText()); 142 | t1.setText(""); 143 | t1.setText(Integer.toString(res)); 144 | break; 145 | case 2 : res=t-Integer.parseInt(t1.getText()); 146 | t1.setText(""); 147 | t1.setText(Integer.toString(res)); 148 | break; 149 | case 3 : res=t*Integer.parseInt(t1.getText()); 150 | t1.setText(""); 151 | t1.setText(Integer.toString(res)); 152 | break; 153 | case 4 : res=t/Integer.parseInt(t1.getText()); 154 | t1.setText(""); 155 | t1.setText(Integer.toString(res)); 156 | break; 157 | default: 158 | break; 159 | } 160 | } 161 | public static void main(String[] args) { 162 | new Calculator(); 163 | } 164 | 165 | } 166 | -------------------------------------------------------------------------------- /Calculator_interface.java: -------------------------------------------------------------------------------- 1 | import java.awt.event.*; 2 | import javax.swing.*; 3 | 4 | 5 | public class GUI_Calculator implements ActionListener { 6 | JTextField t1; 7 | JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16; 8 | int n,i,flag; 9 | 10 | GUI_Calculator() { 11 | JFrame f = new JFrame("CALCULATOR"); 12 | JPanel p = new JPanel(); 13 | 14 | t1 = new JTextField(); 15 | t1.setBounds(100, 100, 200, 30); 16 | b1 = new JButton("1"); 17 | b1.setBounds(100, 140, 50, 30); 18 | b2 = new JButton("2"); 19 | b2.setBounds(150, 140, 50, 30); 20 | b3 = new JButton("3"); 21 | b3.setBounds(200, 140, 50, 30); 22 | b4 = new JButton("+"); 23 | b4.setBounds(250, 140, 50, 30); 24 | b5 = new JButton("4"); 25 | b5.setBounds(100, 170, 50, 30); 26 | b6 = new JButton("5"); 27 | b6.setBounds(150, 170, 50, 30); 28 | b7 = new JButton("6"); 29 | b7.setBounds(200, 170, 50, 30); 30 | b8 = new JButton("-"); 31 | b8.setBounds(250, 170, 50, 30); 32 | b9 = new JButton("7"); 33 | b9.setBounds(100, 200, 50, 30); 34 | b10 = new JButton("8"); 35 | b10.setBounds(150, 200, 50, 30); 36 | b11 = new JButton("9"); 37 | b11.setBounds(200, 200, 50, 30); 38 | b12 = new JButton("*"); 39 | b12.setBounds(250, 200, 50, 30); 40 | b13 = new JButton("0"); 41 | b13.setBounds(100, 230, 50, 30); 42 | b14 = new JButton("Clr"); 43 | b14.setBounds(150, 230, 53, 30); 44 | b15 = new JButton("="); 45 | b15.setBounds(200, 230, 50, 30); 46 | b16 = new JButton("/"); 47 | b16.setBounds(250, 230, 50, 30); 48 | 49 | b1.addActionListener(this); 50 | b2.addActionListener(this); 51 | b3.addActionListener(this); 52 | b4.addActionListener(this); 53 | b5.addActionListener(this); 54 | b6.addActionListener(this); 55 | b7.addActionListener(this); 56 | b8.addActionListener(this); 57 | b9.addActionListener(this); 58 | b10.addActionListener(this); 59 | b11.addActionListener(this); 60 | b12.addActionListener(this); 61 | b13.addActionListener(this); 62 | b14.addActionListener(this); 63 | b15.addActionListener(this); 64 | b16.addActionListener(this); 65 | 66 | p.add(t1); 67 | p.add(b1); 68 | p.add(b2); 69 | p.add(b3); 70 | p.add(b4); 71 | p.add(b5); 72 | p.add(b6); 73 | p.add(b7); 74 | p.add(b8); 75 | p.add(b9); 76 | p.add(b10); 77 | p.add(b11); 78 | p.add(b12); 79 | p.add(b13); 80 | p.add(b14); 81 | p.add(b15); 82 | p.add(b16); 83 | 84 | 85 | f.setContentPane(p); 86 | f.setSize(400,500); 87 | f.setLayout(null); 88 | f.setVisible(true); 89 | } 90 | 91 | public void actionPerformed(ActionEvent e) { 92 | if(e.getSource() == b1) 93 | t1.setText(t1.getText() + "1"); 94 | else if(e.getSource() == b2) 95 | t1.setText(t1.getText() + "2"); 96 | else if(e.getSource() == b3) 97 | t1.setText(t1.getText() + "3"); 98 | else if(e.getSource() == b5) 99 | t1.setText(t1.getText() + "4"); 100 | else if(e.getSource() == b6) 101 | t1.setText(t1.getText() + "5"); 102 | else if(e.getSource() == b7) 103 | t1.setText(t1.getText() + "6"); 104 | else if(e.getSource() == b9) 105 | t1.setText(t1.getText() + "7"); 106 | else if(e.getSource() == b10) 107 | t1.setText(t1.getText() + "8"); 108 | else if(e.getSource() == b11) 109 | t1.setText(t1.getText() + "9"); 110 | else if(e.getSource() == b13) 111 | t1.setText(t1.getText() + "0"); 112 | else if(e.getSource() == b4) { 113 | String s = t1.getText(); 114 | n = Integer.parseInt(s); 115 | t1.setText(""); 116 | i = 1; 117 | } 118 | else if(e.getSource() == b8) { 119 | String s = t1.getText(); 120 | n = Integer.parseInt(s); 121 | t1.setText(""); 122 | i = 2; 123 | } 124 | else if(e.getSource() == b12) { 125 | String s=t1.getText(); 126 | n=Integer.parseInt(s); 127 | t1.setText(""); 128 | i=3; 129 | } 130 | else if(e.getSource()== b16) { 131 | String s=t1.getText(); 132 | n=Integer.parseInt(s); 133 | t1.setText(""); 134 | i=4; 135 | } 136 | else if(e.getSource() == b15) { 137 | calc(n ,i); 138 | } 139 | else { 140 | t1.setText(""); 141 | } 142 | } 143 | 144 | void calc(int t, int e) { 145 | int res; 146 | switch(e) { 147 | case 1: 148 | res = t + Integer.parseInt(t1.getText()); 149 | t1.setText(""); 150 | t1.setText(Integer.toString(res)); 151 | break; 152 | case 2: 153 | res = t - Integer.parseInt(t1.getText()); 154 | t1.setText(""); 155 | t1.setText(Integer.toString(res)); 156 | break; 157 | case 3: 158 | res = t * Integer.parseInt(t1.getText()); 159 | t1.setText(""); 160 | t1.setText(Integer.toString(res)); 161 | break; 162 | case 4: 163 | res = t / Integer.parseInt(t1.getText()); 164 | t1.setText(""); 165 | t1.setText(Integer.toString(res)); 166 | break; 167 | default: 168 | break; 169 | } 170 | } 171 | 172 | public static void main(String[] args) { 173 | new GUI_Calculator(); 174 | } 175 | 176 | } 177 | -------------------------------------------------------------------------------- /CheckBox.java: -------------------------------------------------------------------------------- 1 | package javalab; 2 | 3 | import javax.swing.*; 4 | 5 | public class test4 { 6 | public static void main(String[] args) { 7 | JFrame f= new JFrame("CheckBox Example"); 8 | JCheckBox checkBox1 = new JCheckBox("C++"); 9 | checkBox1.setBounds(100,100, 100,50); 10 | JCheckBox checkBox2 = new JCheckBox("Java"); 11 | checkBox2.setBounds(100,150, 100,50); 12 | JCheckBox checkBox3 = new JCheckBox("Python", true); 13 | checkBox3.setBounds(100,200, 100,50); 14 | f.add(checkBox1); 15 | f.add(checkBox2); 16 | f.add(checkBox3); 17 | f.setSize(400,400); 18 | f.setLayout(null); 19 | f.setVisible(true); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Copy_objects.java: -------------------------------------------------------------------------------- 1 | class Car { 2 | private String name; 3 | private int price; 4 | 5 | public Car() {} 6 | 7 | public Car(Car c) { 8 | this.copy(c); 9 | } 10 | 11 | public Car(String name, int price) { 12 | this.name = name; 13 | this.price = price; 14 | } 15 | 16 | public void copy(Car c) { 17 | this.name = c.name; 18 | this.price = c.price; 19 | } 20 | 21 | public String toString() { 22 | return name + " costs " + price; 23 | } 24 | } 25 | 26 | public class test { 27 | public static void main(String[] args) { 28 | Car c1 = new Car("BMW", 999999); 29 | System.out.println(c1); 30 | 31 | Car c2 = new Car(); 32 | c2.copy(c1); 33 | System.out.println(c2); 34 | 35 | Car c3 = new Car(c1); 36 | System.out.println(c3); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Day10_1_CopyContentInFile.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.Scanner; 3 | public class Test { 4 | public static void main(String[] args) { 5 | 6 | // Creating sample.txt 7 | try { 8 | File f1= new File("sample.txt"); 9 | if(f1.exists()) { 10 | System.out.println("sample.txt already exists"); 11 | } else { 12 | f1.createNewFile(); 13 | System.out.println("created file sample.txt "); 14 | } 15 | 16 | Scanner sc = new Scanner(System.in); 17 | System.out.print("Enter the contents of file : "); 18 | String content = sc.nextLine(); 19 | FileWriter f = new FileWriter("sample.txt"); 20 | f.write(content); 21 | f.close(); 22 | System.out.println("contents saved to file sample.txt"); 23 | 24 | // Creating new_sample.txt 25 | File f2= new File("new_sample.txt"); 26 | 27 | if(f2.exists()) { 28 | System.out.println("new_sample already exists"); 29 | } else { 30 | f2.createNewFile();///////// 31 | System.out.println("created file new_sample.txt "); 32 | } 33 | 34 | //copy content from sample.txt to new_sample.txt 35 | FileInputStream in = new FileInputStream("sample.txt"); 36 | FileOutputStream out = new FileOutputStream("new_sample.txt"); 37 | int c; 38 | while((c=in.read())!=-1) { 39 | out.write(c); 40 | } 41 | System.out.println("File content copied from sample.txt to new_sample.txt"); 42 | System.out.println("File content of new_sample.txt"); 43 | FileReader fr = new FileReader("new_sample.txt"); 44 | BufferedReader br = new BufferedReader(fr); 45 | String content1; 46 | while((content1 = br.readLine())!=null) { 47 | System.out.println(content1); 48 | } 49 | 50 | } catch(IOException e) { 51 | System.out.println(e); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Day10_1_File.java: -------------------------------------------------------------------------------- 1 | package matrixmulti; 2 | import java.util.Scanner; 3 | import java.io.*; 4 | public class FileOddEven { 5 | public static void main(String[] args) throws IOException { 6 | try { 7 | File f=new File("numbers.txt"); 8 | if(f.createNewFile()) { 9 | System.out.println("file has created and file name is "+f.getName()); 10 | } 11 | } 12 | catch(Exception e){ 13 | System.out.println(e.getMessage()); 14 | } 15 | FileOutputStream f11=new FileOutputStream("numbers.txt"); 16 | Scanner sc=new Scanner(System.in); 17 | System.out.println("enter the limit of the numbers you want to write to the file"); 18 | System.out.println(); 19 | int limit=sc.nextInt(); 20 | System.out.println(); 21 | System.out.println("enter the integers that you want to in the file number.txt"); 22 | for(int i=0;i arr[j+1]){ 26 | int temp = arr[j]; 27 | arr[j] = arr[j+1]; 28 | arr[j+1] = temp; 29 | } 30 | } 31 | } 32 | 33 | System.out.println("\n\nAfter Sorting "); 34 | for(int i=0; i largest) 29 | largest = arr[i]; 30 | } 31 | 32 | System.out.println("\n\nLargest is : " + largest); 33 | System.out.println("Smallest is : " + smallest); 34 | } 35 | } 36 | 37 | /* 38 | Enter the number of elements : 5 39 | Enter the value of element 0 : 10 40 | Enter the value of element 1 : 20 41 | Enter the value of element 2 : 5 42 | Enter the value of element 3 : 25 43 | Enter the value of element 4 : 50 44 | 45 | The array is 46 | 10 20 5 25 50 47 | 48 | Largest is : 50 49 | Smallest is : 5 50 | */ -------------------------------------------------------------------------------- /Day2_14_Descending_sort.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Day2_11_Bubble_sort { 4 | public static void main(String[] args){ 5 | Scanner input = new Scanner(System.in); 6 | int arr[], n; 7 | 8 | System.out.print("Enter the number of elements : "); 9 | n = input.nextInt(); 10 | 11 | arr = new int[n]; 12 | 13 | for(int i=0; i arr[j+1]){ 26 | int temp = arr[j]; 27 | arr[j] = arr[j+1]; 28 | arr[j+1] = temp; 29 | } 30 | } 31 | } 32 | 33 | System.out.println("\n\nAfter Ascending Sorting "); 34 | for(int i=0; i num2) 14 | System.out.println("Largest : " + num1); 15 | else 16 | System.out.println("Largest : " + num2); 17 | } 18 | } 19 | 20 | /* 21 | Enter number 1 : 10 22 | Enter number 2 : 20 23 | Largest : 20 24 | */ -------------------------------------------------------------------------------- /Day2_2_Largest_of_3.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Day2_2_Largest_of_3 { 4 | public static void main(String[] args){ 5 | Scanner input = new Scanner(System.in); 6 | int num1, num2, num3; 7 | 8 | System.out.print("Enter number 1 : "); 9 | num1 = input.nextInt(); 10 | System.out.print("Enter number 2 : "); 11 | num2 = input.nextInt(); 12 | System.out.print("Enter number 3 : "); 13 | num3 = input.nextInt(); 14 | 15 | if(num1>num2 && num1>num3) 16 | System.out.println("Largest : " + num1); 17 | else if(num2 > num3) 18 | System.out.println("Largest : " + num2); 19 | else 20 | System.out.println("Largest : " + num3); 21 | } 22 | } 23 | 24 | /* 25 | Enter number 1 : 10 26 | Enter number 2 : 99 27 | Enter number 3 : 11 28 | Largest : 99 29 | */ 30 | -------------------------------------------------------------------------------- /Day2_3_Odd_or_even.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Day2_3_Odd_or_even { 4 | public static void main(String[] args){ 5 | Scanner input = new Scanner(System.in); 6 | int num; 7 | 8 | System.out.print("Enter the number : "); 9 | num = input.nextInt(); 10 | 11 | if(num%2 == 0) 12 | System.out.println(num + " is EVEN"); 13 | else 14 | System.out.println(num + " is ODD"); 15 | } 16 | } 17 | 18 | /* 19 | Enter the number : 10 20 | 10 is EVEN 21 | */ -------------------------------------------------------------------------------- /Day2_4_Prime_or_not.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Day2_4_Prime_or_not { 4 | public static void main(String[] args){ 5 | Scanner input = new Scanner(System.in); 6 | int num, flag=1; 7 | 8 | System.out.print("Enter the number : "); 9 | num = input.nextInt(); 10 | 11 | for(int i=2; i<=(num/2); i++){ 12 | if(num % i == 0) 13 | flag = 0; 14 | } 15 | 16 | if(flag == 1) 17 | System.out.println(num + " is PRIME"); 18 | else 19 | System.out.println(num + " is not PRIME"); 20 | } 21 | } 22 | 23 | /* 24 | Enter the number : 19 25 | 19 is PRIME 26 | */ -------------------------------------------------------------------------------- /Day2_5_Sum_of_n.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Day2_5_Sum_of_n { 4 | public static void main(String[] args){ 5 | Scanner input = new Scanner(System.in); 6 | int num, sum=0, i; 7 | 8 | System.out.print("Enter the number : "); 9 | num = input.nextInt(); 10 | 11 | i = 1; 12 | while(i <= num){ 13 | sum += i; 14 | i++; 15 | } 16 | System.out.println("Sum using while loop : " + sum); 17 | 18 | i = 1; 19 | sum = 0; 20 | do{ 21 | sum += i; 22 | i++; 23 | } while(i <= num); 24 | System.out.println("Sum using do while loop : " + sum); 25 | 26 | sum = 0; 27 | for(i=1; i<=num; i++) 28 | sum += i; 29 | System.out.println("Sum using for loop : " + sum); 30 | } 31 | } 32 | 33 | /* 34 | Enter the number : 5 35 | Sum using while loop : 15 36 | Sum using do while loop : 15 37 | Sum using for loop : 15 38 | */ -------------------------------------------------------------------------------- /Day2_6_Armstrong_or_not.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.lang.Math; 3 | 4 | public class Day2_6_Armstrong_or_not { 5 | public static void main(String[] args){ 6 | Scanner input = new Scanner(System.in); 7 | int num, sum=0, p, len=0; 8 | 9 | System.out.print("Enter the number : "); 10 | num = input.nextInt(); 11 | 12 | p = num; 13 | while(p != 0){ 14 | len++; 15 | p /= 10; 16 | } 17 | 18 | p = num; 19 | while(p != 0){ 20 | sum += Math.pow(p%10, len); 21 | p /= 10; 22 | } 23 | 24 | if(sum == num) 25 | System.out.println(num + " is an ARMSTRONG number"); 26 | else 27 | System.out.println(num + " is not an ARMSTRONG number"); 28 | } 29 | } 30 | 31 | /* 32 | Enter the number : 153 33 | 153 is an ARMSTRONG number 34 | */ -------------------------------------------------------------------------------- /Day2_7_Palindrome_or_not.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Day2_7_Palindrome_or_not { 4 | public static void main(String[] args){ 5 | Scanner input = new Scanner(System.in); 6 | int num, rev=0, p; 7 | 8 | System.out.print("Enter the number : "); 9 | num = input.nextInt(); 10 | 11 | p = num; 12 | while(p != 0){ 13 | rev = (rev*10) + (p%10); 14 | p /= 10; 15 | } 16 | 17 | if(rev == num) 18 | System.out.println(num + " is a PALINDROME"); 19 | else 20 | System.out.println(num + " is not a PALINDROME"); 21 | } 22 | } 23 | 24 | /* 25 | Enter the number : 12121 26 | 12121 is a PALINDROME 27 | */ -------------------------------------------------------------------------------- /Day2_8_Reverse_of_number.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Day2_8_Reverse_of_number { 4 | public static void main(String[] args){ 5 | Scanner input = new Scanner(System.in); 6 | int num, rev=0, p; 7 | 8 | System.out.print("Enter the number : "); 9 | num = input.nextInt(); 10 | 11 | p = num; 12 | while(p != 0){ 13 | rev = (rev*10) + (p%10); 14 | p /= 10; 15 | } 16 | 17 | System.out.println("Reverse of " + num + " is " + rev); 18 | } 19 | } 20 | 21 | /* 22 | Enter the number : 12345 23 | Reverse of 12345 is 54321 24 | */ -------------------------------------------------------------------------------- /Day2_9_Fibonacci_series.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Day2_9_Fibonacci_series { 4 | public static void main(String[] args){ 5 | Scanner input = new Scanner(System.in); 6 | int num, a=0, b=1, c; 7 | 8 | System.out.print("Enter the limit : "); 9 | num = input.nextInt(); 10 | 11 | for(int i=0; i= 60) { 31 | hour++; 32 | minute_sum -= 60; 33 | } 34 | minute = minute_sum; 35 | 36 | 37 | int second_sum = second + t.second; 38 | if(second_sum >= 60) { 39 | minute++; 40 | second_sum -= 60; 41 | } 42 | second = second_sum; 43 | } 44 | 45 | void giveTime() { 46 | System.out.println(hour + " : " + minute + " : " + second); 47 | } 48 | } 49 | 50 | /* 51 | T1 Time - 6 : 50 : 30 52 | T2 Time - 5 : 20 : 30 53 | T1 + T2 - 12 : 11 : 0 54 | */ 55 | -------------------------------------------------------------------------------- /Day6_3_Package.java: -------------------------------------------------------------------------------- 1 | /* 2 | // In MyPackage, create two classes FirstClass.java and SecondClass.java 3 | 4 | // In FirstClass.java 5 | 6 | package MyPackage; 7 | public class FirstClass { 8 | public void printFirst() { 9 | System.out.println("This is first class"); 10 | } 11 | } 12 | 13 | // In SecondClass.java 14 | 15 | package MyPackage; 16 | public class SecondClass { 17 | public void printSecond() { 18 | System.out.println("This is second class"); 19 | } 20 | } 21 | 22 | 23 | // In the default package, create class Demo.java 24 | 25 | // In Demo.java 26 | 27 | import MyPackage.*; 28 | public class Demo { 29 | public static void main(String[] args) { 30 | 31 | FirstClass obj1 = new FirstClass(); 32 | obj1.printFirst(); 33 | 34 | SecondClass obj2 = new SecondClass(); 35 | obj2.printSecond() 36 | 37 | } 38 | } 39 | */ 40 | -------------------------------------------------------------------------------- /Day6_4_Interface.java: -------------------------------------------------------------------------------- 1 | public class Day6_4_Interface { 2 | public static void main(String[] args) { 3 | PrintData obj1 = new PrintData(); 4 | obj1.print_hello(); 5 | } 6 | } 7 | 8 | interface Display { 9 | void print_hello(); 10 | } 11 | 12 | class PrintData implements Display { 13 | public void print_hello() { 14 | System.out.println("Hello"); 15 | } 16 | } 17 | 18 | // Hello -------------------------------------------------------------------------------- /Day6_5_Shapes_Interface.java: -------------------------------------------------------------------------------- 1 | public class Day6_4_Interface { 2 | public static void main(String[] args) { 3 | 4 | Rectangle shape1 = new Rectangle(); 5 | shape1.getData(20, 10); 6 | shape1.findArea(); 7 | 8 | Square shape2 = new Square(); 9 | shape2.getData(10, 10); 10 | shape2.findArea(); 11 | } 12 | } 13 | 14 | class Shapes { 15 | int length, breadth; 16 | void getData(int length, int breadth) { 17 | this.length = length; 18 | this.breadth = breadth; 19 | } 20 | } 21 | 22 | interface Area { 23 | void findArea(); 24 | } 25 | 26 | class Rectangle extends Shapes implements Area { 27 | public void findArea() { 28 | System.out.println("Area of Rectangle : " + length * breadth); 29 | } 30 | } 31 | 32 | class Square extends Shapes implements Area { 33 | public void findArea() { 34 | System.out.println("Area of Square : " + length * breadth); 35 | } 36 | } 37 | 38 | /* 39 | Area of Rectangle : 200 40 | Area of Square : 100 41 | */ 42 | -------------------------------------------------------------------------------- /Day7_1_Multiple_Inheritance.java: -------------------------------------------------------------------------------- 1 | abstract class Animal { 2 | protected int legs; 3 | protected Animal(int legs) { 4 | this.legs = legs; 5 | } 6 | public abstract void eat(); 7 | public void walk () { 8 | System.out.println("This animal walks on " + legs + " legs."); 9 | } 10 | 11 | } 12 | 13 | class Spider extends Animal { 14 | public Spider() { 15 | super(8); 16 | } 17 | public void eat () { 18 | System.out.println("The spider eats a fire fly."); 19 | } 20 | } 21 | 22 | interface Pet { 23 | public String getName(); 24 | public void setName (String n); 25 | public void play (); 26 | } 27 | 28 | class Cat extends Animal implements Pet { 29 | private String name; 30 | public Cat(String n) { 31 | super(4); 32 | name = n; 33 | } 34 | public Cat() { 35 | this(""); 36 | } 37 | public String getName() { 38 | return name; 39 | } 40 | public void setName (String n) { 41 | name = n; 42 | } 43 | public void play() { 44 | System.out.println(name + " likes to play with string."); 45 | } 46 | public void eat() { 47 | System.out.println("Cats like to eat spiders and mice."); 48 | } 49 | } 50 | 51 | class Fish extends Animal implements Pet { 52 | private String name; 53 | public Fish() { 54 | super(0); 55 | } 56 | public void setName (String name) { 57 | this.name = name; 58 | } 59 | public String getName () { 60 | return name; 61 | } 62 | public void play () { 63 | System.out.println("Fish swim in their tanks all day."); 64 | } 65 | public void walk () { 66 | super.walk(); 67 | System.out.println("Fish, of course, can't walk, they swim."); 68 | } 69 | public void eat () { 70 | System.out.println("Fish eat pond scum."); 71 | } 72 | } 73 | 74 | class TestAnimals { 75 | public static void main(String[] args) { 76 | Fish f = new Fish(); 77 | Cat c = new Cat("Fluffy"); 78 | Animal a = new Fish(); 79 | Animal e = new Cat (); 80 | 81 | f.play(); 82 | c.play(); 83 | 84 | e.eat(); 85 | e.walk(); 86 | 87 | a.walk(); 88 | } 89 | } 90 | 91 | /* 92 | Fish swim in their tanks all day. 93 | Fluffy likes to play with string. 94 | Cats like to eat spiders and mice. 95 | This animal walks on 4 legs.This animal walks on 0 legs. 96 | Fish, of course, can't walk, they swim. 97 | */ 98 | -------------------------------------------------------------------------------- /Day7_2_Ex5_LMS.java: -------------------------------------------------------------------------------- 1 | class Employee { 2 | void display() { 3 | System.out.println("Name of class is Employee."); 4 | } 5 | 6 | void calcSalary() { 7 | System.out.println("Salary of Employee is 10000"); 8 | } 9 | } 10 | 11 | class Engineer extends Employee { 12 | void calcSalary(){ 13 | System.out.println("Salary of Engineer is 20000"); 14 | } 15 | 16 | } 17 | 18 | public class Day7_2_Ex5_LMS { 19 | public static void main(String args[]) { 20 | Engineer e = new Engineer(); 21 | e.display(); 22 | e.calcSalary(); 23 | } 24 | 25 | } 26 | 27 | /* 28 | Name of class is Employee. 29 | Salary of Engineer is 20000 30 | */ -------------------------------------------------------------------------------- /Day8_1_Error_Handling.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Day8_Error_handling { 4 | public static void main(String[] args) { 5 | Scanner input = new Scanner(System.in); 6 | int option; 7 | 8 | System.out.print("MENU\n1) Arithmetic Exception \n2) Array Index Out Of Bounds Exception\n3) Exit\n"); 9 | do { 10 | System.out.print("\nEnter the option : "); 11 | option = input.nextInt(); 12 | 13 | switch(option) { 14 | 15 | case 1: 16 | try { 17 | int a, b; 18 | System.out.println("Program to perform division"); 19 | System.out.print("Enter number 1 : "); 20 | a = input.nextInt(); 21 | System.out.print("Enter number 2 : "); 22 | b = input.nextInt(); 23 | if(b==0) 24 | { 25 | throw new ArithmeticException("Division by Zero"); 26 | } 27 | else 28 | { 29 | System.out.println("Result="+a/b); 30 | } 31 | } catch(Exception e) { 32 | System.out.println(e); 33 | } finally { 34 | System.out.print("finally block content\n"); 35 | } 36 | break; 37 | 38 | case 2: 39 | try { 40 | int n, pos; 41 | System.out.print("Enter number of elements to store in array : "); 42 | n = input.nextInt(); 43 | 44 | int[] arr = new int[n]; 45 | System.out.print("Enter the elements : "); 46 | for(int i=0; i= n) 53 | throw new ArrayIndexOutOfBoundsException("Array Index Out Of Bounds"); 54 | else 55 | System.out.println("Element is : " + arr[pos]); 56 | 57 | } catch(Exception e) { 58 | System.out.println(e); 59 | } finally { 60 | System.out.print("finally block content\n"); 61 | } 62 | break; 63 | 64 | case 3: 65 | System.out.println("Exiting"); 66 | break; 67 | default: 68 | System.out.println("Select a valid option (1 or 2) "); 69 | } 70 | } while(option != 3); 71 | } 72 | } 73 | 74 | /* 75 | MENU 76 | 1) Arithmetic Exception 77 | 2) Array Index Out Of Bounds Exception 78 | 3) Exit 79 | 80 | Enter the option : 2 81 | Enter number of elements to store in array : 3 82 | Enter the elements : 10 20 30 83 | Enter the postion to be accessed : 2 84 | Element is : 30 85 | finally block content 86 | 87 | Enter the option : 2 88 | Enter number of elements to store in array : 4 89 | Enter the elements : 1 2 3 4 90 | Enter the postion to be accessed : 10 91 | java.lang.ArrayIndexOutOfBoundsException: Array Index Out Of Bounds 92 | finally block content 93 | 94 | Enter the option : 1 95 | Program to perform division 96 | Enter number 1 : 10 97 | Enter number 2 : 2 98 | Result=5 99 | finally block content 100 | 101 | Enter the option : 1 102 | Program to perform division 103 | Enter number 1 : 10 104 | Enter number 2 : 0 105 | java.lang.ArithmeticException: Division by Zero 106 | finally block content 107 | 108 | Enter the option : 3 109 | Exiting 110 | 111 | */ 112 | -------------------------------------------------------------------------------- /Day8_2_Throw_in_JAVA.java: -------------------------------------------------------------------------------- 1 | 2 | public class Day8_Ex2 { 3 | 4 | static void check(int a,int b) 5 | { 6 | if(b==0) 7 | { 8 | throw new ArithmeticException("Division by Zero"); 9 | } 10 | else 11 | { 12 | System.out.println("Result="+a/b); 13 | } 14 | } 15 | public static void main(String[] args) { 16 | // TODO Auto-generated method stub 17 | int a=1,b=0; 18 | try 19 | { 20 | check(a,b); 21 | } 22 | catch(ArithmeticException e) 23 | { 24 | System.out.println(e); 25 | } 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Day9_1_Banking.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | class InvalidAmountException extends Exception { 4 | InvalidAmountException(String msg) { 5 | super(msg); 6 | } 7 | } 8 | 9 | class InsufficientFundsException extends Exception { 10 | InsufficientFundsException(String msg) { 11 | super(msg); 12 | } 13 | } 14 | 15 | class Bank { 16 | String name; 17 | int accountnum; 18 | float balance; 19 | Scanner sc = new Scanner(System.in); 20 | 21 | public void create(int m) { 22 | accountnum = m; 23 | System.out.print("Enter the name : "); 24 | name = sc.nextLine(); 25 | System.out.print("Enter the balance : "); 26 | balance = sc.nextFloat(); 27 | } 28 | 29 | public void display() { 30 | System.out.println("\nThe details of the user"); 31 | System.out.println("\nAccount number : " + accountnum); 32 | System.out.println("Name of the account user = " + name); 33 | System.out.println("Balance : " + balance); 34 | } 35 | 36 | public void withdraw() throws InsufficientFundsException,InvalidAmountException { 37 | System.out.print("Enter the amount to withdraw : "); 38 | int amt = sc.nextInt(); 39 | if(amt > balance) { 40 | throw new InsufficientFundsException("Insufficient balance"); 41 | } else if(amt <= 0) { 42 | throw new InvalidAmountException("Amount is not valid"); 43 | } else { 44 | balance = balance-amt; 45 | System.out.println("Balance amount=" + balance); 46 | } 47 | } 48 | 49 | public void deposit() throws InvalidAmountException { 50 | System.out.print("Enter the amount to deposit : "); 51 | int amt1 = sc.nextInt(); 52 | if(amt1 <= 0) { 53 | throw new InvalidAmountException("Amount is not valid"); 54 | } else { 55 | balance = balance+amt1; 56 | System.out.println("Balance amount = " + balance); 57 | } 58 | } 59 | } 60 | 61 | public class Banking { 62 | public static void main(String[] args) { 63 | Scanner sc = new Scanner(System.in); 64 | System.out.print("Enter the number of accounts you want to enter : "); 65 | int n = sc.nextInt(); 66 | Bank b[]; 67 | b = new Bank[n]; 68 | for(int i=0; iContains the specific programs asked in LAB EXAM 2 | -------------------------------------------------------------------------------- /Lab_Questions/Shapes_method_overloading.java: -------------------------------------------------------------------------------- 1 | class ShapeArea { 2 | void getArea(float a) { 3 | System.out.println("Area of circle : " + ((22/7.0) * a * a)); 4 | } 5 | 6 | void getArea(int a) { 7 | System.out.println("Area of square : " + a*a); 8 | } 9 | 10 | void getArea(int a, int b) { 11 | System.out.println("Area of rectangle : " + a*b); 12 | } 13 | 14 | void getArea(float a, float b) { 15 | System.out.println("Area of triangle : " + ((1/2.0) * a * b)); 16 | } 17 | 18 | void getArea(int a, int b, int h) { 19 | System.out.println("Area of trapezium : " + ((h/2) * (a+b))); 20 | } 21 | } 22 | 23 | public class test { 24 | public static void main(String[] args) { 25 | ShapeArea shape = new ShapeArea(); 26 | shape.getArea((float) 5); 27 | shape.getArea(5); 28 | shape.getArea(5, 10); 29 | shape.getArea((float) 5, (float) 10); 30 | shape.getArea(5, 10, 5); 31 | } 32 | } 33 | 34 | /* 35 | Area of circle : 78.57142857142857 36 | Area of square : 25 37 | Area of rectangle : 50 38 | Area of triangle : 25.0 39 | Area of trapezium : 30 40 | */ 41 | -------------------------------------------------------------------------------- /MouseEvents.java: -------------------------------------------------------------------------------- 1 | package javalab; 2 | import java.awt.event.*; 3 | import javax.swing.*; 4 | public class MouseEventDemo extends JFrame implements MouseListener { 5 | public MouseEventDemo() { 6 | addMouseListener(this); 7 | setSize(300,300); 8 | setVisible(true); 9 | } 10 | public void mouseClicked(MouseEvent e) { 11 | System.out.println("Mouse clicked"); 12 | } 13 | public void mouseEntered(MouseEvent e) { 14 | System.out.println("Mouse Entered"); 15 | } 16 | public void mouseExited(MouseEvent e) { 17 | System.out.println("Mouse Exited"); 18 | } 19 | public void mousePressed(MouseEvent e) { 20 | System.out.println("Mouse Pressed"); 21 | } 22 | public void mouseReleased(MouseEvent e) { 23 | System.out.println("Mouse Released"); 24 | } 25 | public static void main(String[] args) { 26 | new MouseEventDemo(); 27 | 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Multithreading.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | class Number extends Thread 4 | 5 | { 6 | 7 | public void run() 8 | 9 | { 10 | 11 | Random random=new Random(); 12 | 13 | for(int i =0; i<10; i++) 14 | 15 | { 16 | 17 | int randomInteger = random.nextInt(100); 18 | 19 | System.out.println("Random Integer generated : " + randomInteger); 20 | 21 | if(randomInteger%2==0) 22 | 23 | { 24 | 25 | Square s = new Square(randomInteger); 26 | 27 | s.start(); 28 | 29 | } 30 | 31 | else 32 | 33 | { 34 | 35 | Cube c = new Cube(randomInteger); 36 | 37 | c.start(); 38 | 39 | } 40 | 41 | try 42 | 43 | { 44 | 45 | Thread.sleep(1000); 46 | 47 | } 48 | 49 | catch(Exception e) 50 | 51 | { 52 | 53 | e.printStackTrace(); 54 | 55 | } 56 | 57 | } 58 | 59 | } 60 | 61 | } 62 | 63 | class Square extends Thread 64 | 65 | { 66 | 67 | int x; 68 | 69 | Square(int n) 70 | 71 | { 72 | 73 | x=n; 74 | 75 | 76 | } 77 | 78 | public void run() 79 | 80 | { 81 | 82 | int sq=x*x; 83 | 84 | System.out.println("Square of"+x+"="+sq); 85 | 86 | } 87 | 88 | } 89 | 90 | class Cube extends Thread 91 | 92 | { 93 | 94 | int x; 95 | 96 | Cube(int n) 97 | 98 | { 99 | 100 | x=n; 101 | 102 | } 103 | 104 | public void run() 105 | 106 | { 107 | 108 | int c=x*x*x; 109 | 110 | System.out.println("Cube of"+x+"="+c); 111 | 112 | } 113 | 114 | } 115 | 116 | public class MultiThreading 117 | 118 | { 119 | 120 | public static void main(String args[]) 121 | 122 | { 123 | 124 | Number n = new Number(); 125 | 126 | n.start(); 127 | 128 | } 129 | 130 | } 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | -------------------------------------------------------------------------------- /Multithreading_demo.java: -------------------------------------------------------------------------------- 1 | class MyThread extends Thread { 2 | public void run() { 3 | for(int i=1; i<=10; i++) { 4 | System.out.println("Thread #1 : " + i); 5 | try { 6 | Thread.sleep(1000); 7 | } catch (InterruptedException e) { 8 | e.printStackTrace(); 9 | } 10 | } 11 | System.out.println("Thread #1 is FINISHED :)"); 12 | } 13 | } 14 | 15 | class MyRunnable implements Runnable { 16 | public void run() { 17 | for(int i=1; i<=10; i++) { 18 | System.out.println("Thread #2 : " + i); 19 | try { 20 | Thread.sleep(1000); 21 | } catch (InterruptedException e) { 22 | e.printStackTrace(); 23 | } 24 | } 25 | System.out.println("Thread #2 is FINISHED :)"); 26 | } 27 | } 28 | 29 | public class test { 30 | public static void main(String[] args) { 31 | 32 | MyThread thread1 = new MyThread(); 33 | 34 | MyRunnable runnable = new MyRunnable(); 35 | Thread thread2 = new Thread(runnable); 36 | 37 | thread1.start(); 38 | thread2.start(); 39 | } 40 | } 41 | 42 | /* 43 | Thread #2 : 1 44 | Thread #1 : 1 45 | Thread #1 : 2 46 | Thread #2 : 2 47 | Thread #2 : 3 48 | Thread #1 : 3 49 | Thread #2 : 4 50 | Thread #1 : 4 51 | Thread #2 : 5 52 | Thread #1 : 5 53 | Thread #1 : 6 54 | Thread #2 : 6 55 | Thread #2 : 7 56 | Thread #1 : 7 57 | Thread #1 : 8 58 | Thread #2 : 8 59 | Thread #2 : 9 60 | Thread #1 : 9 61 | Thread #1 : 10 62 | Thread #2 : 10 63 | Thread #1 is FINISHED :) 64 | Thread #2 is FINISHED :) 65 | */ 66 | -------------------------------------------------------------------------------- /Multithreading_demo2_join.java: -------------------------------------------------------------------------------- 1 | class MyThread extends Thread { 2 | public void run() { 3 | for(int i=1; i<=10; i++) { 4 | System.out.println("Thread #1 : " + i); 5 | try { 6 | Thread.sleep(1000); 7 | } catch (InterruptedException e) { 8 | e.printStackTrace(); 9 | } 10 | } 11 | System.out.println("Thread #1 is FINISHED :)"); 12 | } 13 | } 14 | 15 | class MyRunnable implements Runnable { 16 | public void run() { 17 | for(int i=1; i<=10; i++) { 18 | System.out.println("Thread #2 : " + i); 19 | try { 20 | Thread.sleep(1000); 21 | } catch (InterruptedException e) { 22 | e.printStackTrace(); 23 | } 24 | } 25 | System.out.println("Thread #2 is FINISHED :)"); 26 | } 27 | } 28 | 29 | public class test { 30 | public static void main(String[] args) { 31 | 32 | MyThread thread1 = new MyThread(); 33 | 34 | MyRunnable runnable = new MyRunnable(); 35 | Thread thread2 = new Thread(runnable); 36 | 37 | thread1.start(); 38 | 39 | try { 40 | thread1.join(); 41 | } catch (InterruptedException e) { 42 | e.printStackTrace(); 43 | } 44 | 45 | thread2.start(); 46 | } 47 | } 48 | 49 | /* 50 | Thread #1 : 1 51 | Thread #1 : 2 52 | Thread #1 : 3 53 | Thread #1 : 4 54 | Thread #1 : 5 55 | Thread #1 : 6 56 | Thread #1 : 7 57 | Thread #1 : 8 58 | Thread #1 : 9 59 | Thread #1 : 10 60 | Thread #1 is FINISHED :) 61 | Thread #2 : 1 62 | Thread #2 : 2 63 | Thread #2 : 3 64 | Thread #2 : 4 65 | Thread #2 : 5 66 | Thread #2 : 6 67 | Thread #2 : 7 68 | Thread #2 : 8 69 | Thread #2 : 9 70 | Thread #2 : 10 71 | Thread #2 is FINISHED :) 72 | */ 73 | -------------------------------------------------------------------------------- /Polymorphism_demo.java: -------------------------------------------------------------------------------- 1 | class Vehicle { 2 | public void go() { 3 | System.out.println("VEHICLE started"); 4 | } 5 | } 6 | 7 | class Car extends Vehicle { 8 | public void go() { 9 | System.out.println("CAR started"); 10 | } 11 | } 12 | 13 | class Bike extends Vehicle { 14 | public void go() { 15 | System.out.println("BIKE started"); 16 | } 17 | } 18 | 19 | class Truck extends Vehicle { 20 | public void go() { 21 | System.out.println("TRUCK started"); 22 | } 23 | } 24 | 25 | public class test { 26 | public static void main(String[] args) { 27 | Car c = new Car(); 28 | Bike b = new Bike(); 29 | Truck t = new Truck(); 30 | Vehicle[] v = {c, b, t}; 31 | for(Vehicle i : v) { 32 | i.go(); 33 | } 34 | } 35 | } 36 | 37 | /* 38 | CAR started 39 | BIKE started 40 | TRUCK started 41 | */ 42 | -------------------------------------------------------------------------------- /Quick_sort.java: -------------------------------------------------------------------------------- 1 | package java_test_project; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Quick_sort { 6 | 7 | static int Partition(int A[],int lb,int ub) { 8 | 9 | int Pivot=A[ub]; 10 | 11 | int i=lb-1; 12 | 13 | for(int j=lb;jPrograms done in JAVA lab during S3 B-tech 2 | -------------------------------------------------------------------------------- /RadioButton.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.*; 3 | 4 | public class test2 { 5 | public static void main(String[] args) { 6 | JFrame frame = new JFrame("Radio Button Demo"); 7 | ButtonGroup radioGroup = new ButtonGroup(); 8 | JRadioButton rb1 = new JRadioButton("Male", true); 9 | JRadioButton rb2 = new JRadioButton("Female"); 10 | radioGroup.add(rb1); 11 | radioGroup.add(rb2); 12 | frame.add(rb1); 13 | frame.add(rb2); 14 | frame.setLayout(new FlowLayout()); 15 | frame.setSize(500, 500); 16 | frame.setVisible(true); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Smiley Emoji/Images/Mileeeex_SAAAN.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/joswinemmanuel/JAVA-LAB-S3/a67615fc26dd0957bd0dc0eab3dbf04626da91be/Smiley Emoji/Images/Mileeeex_SAAAN.png -------------------------------------------------------------------------------- /Smiley Emoji/README.md: -------------------------------------------------------------------------------- 1 |

Smiley Emoji using 2D Graphics in Java

2 | 3 | -------------------------------------------------------------------------------- /Smiley Emoji/Smiley_Emoji.java: -------------------------------------------------------------------------------- 1 | import java.awt.BasicStroke; 2 | import java.awt.Color; 3 | import java.awt.Font; 4 | import java.awt.Graphics; 5 | import java.awt.Graphics2D; 6 | import javax.swing.*; 7 | 8 | class demo1 extends JFrame { 9 | private static final long serialVersionUID = 1L; 10 | public demo1(){ 11 | this.setTitle("Test"); 12 | this.setSize(700, 600); 13 | this.setVisible(true); 14 | } 15 | 16 | public void paint(Graphics g) { 17 | Graphics2D g2d = (Graphics2D) g; 18 | g2d.setPaint(Color.yellow); 19 | g2d.fillOval(100, 50, 400, 400); 20 | g2d.setPaint(Color.black); 21 | g2d.setStroke(new BasicStroke(5)); 22 | g2d.fillOval(150, 150, 100, 100); 23 | g2d.fillOval(350, 150, 100, 100); 24 | g2d.drawOval(100, 50, 400, 400); 25 | g2d.drawArc(200, 210, 200, 170, 180, 180); 26 | 27 | g2d.setFont(new Font("Ink Free", Font.BOLD, 45)); 28 | g2d.drawString("Hello Mileex SAAAN", 125, 520); 29 | } 30 | } 31 | 32 | public class graphtut { 33 | public static void main(String[] args) { 34 | new demo1(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Swing1.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import javax.swing.*; 3 | 4 | public class Main { 5 | public static void main(String[] args) { 6 | 7 | JLabel label = new JLabel("You bruv"); 8 | label.setBounds(500, 100, 100, 100); 9 | 10 | JPanel panel = new JPanel(); 11 | panel.setBackground(Color.red); 12 | panel.setBounds(100, 100, 100, 100); 13 | 14 | 15 | JFrame frame = new JFrame("Frame dawg"); 16 | frame.add(panel); 17 | frame.add(label); 18 | frame.setLayout(null); 19 | frame.setSize(600, 300); 20 | frame.setVisible(true); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Swing2.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.event.ActionEvent; 3 | import java.awt.event.ActionListener; 4 | import javax.swing.*; 5 | 6 | class test2 extends JFrame implements ActionListener { 7 | JButton button; 8 | int i = 1; 9 | 10 | test2() { 11 | button = new JButton(); 12 | button.setText("Powwww"); 13 | button.setBounds(100, 100, 100, 35); 14 | button.setForeground(Color.RED); 15 | button.setBackground(Color.BLACK); 16 | button.addActionListener(this); 17 | 18 | this.add(button); 19 | this.setTitle("Joswin Woo"); 20 | this.getContentPane().setBackground(Color.GRAY); 21 | this.setLayout(null); 22 | this.setVisible(true); 23 | this.setSize(500, 400); 24 | } 25 | 26 | public void actionPerformed(ActionEvent e) { 27 | if (e.getSource() == button) { 28 | System.out.println("pow " + i); 29 | i++; 30 | } 31 | } 32 | } 33 | 34 | class test { 35 | public static void main(String[] args) { 36 | new test2(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Swing3.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.Shape; 3 | import java.awt.event.ActionEvent; 4 | import java.awt.event.ActionListener; 5 | 6 | import javax.swing.*; 7 | 8 | class demo extends JFrame implements ActionListener{ 9 | JRadioButton radio1; 10 | JRadioButton radio2; 11 | JRadioButton radio3; 12 | JPanel panel1; 13 | JPanel panel2; 14 | JPanel panel3; 15 | public demo() { 16 | 17 | radio1 = new JRadioButton("Red"); 18 | radio1.setBounds(250, 50, 75, 10); 19 | radio1.addActionListener(this); 20 | radio2 = new JRadioButton("Yellow"); 21 | radio2.addActionListener(this); 22 | radio2.setBounds(350, 50, 75, 10); 23 | radio3 = new JRadioButton("Green"); 24 | radio3.addActionListener(this); 25 | radio3.setBounds(450, 50, 75, 10); 26 | 27 | ButtonGroup group = new ButtonGroup(); 28 | group.add(radio1); 29 | group.add(radio2); 30 | group.add(radio3); 31 | 32 | panel1 = new JPanel(); 33 | panel1.setBounds(350, 100, 100, 100); 34 | panel2 = new JPanel(); 35 | panel2.setBounds(350, 250, 100, 100); 36 | panel3 = new JPanel(); 37 | panel3.setBounds(350, 400, 100, 100); 38 | 39 | this.add(radio1); 40 | this.add(radio2); 41 | this.add(radio3); 42 | this.add(panel1); 43 | this.add(panel2); 44 | this.add(panel3); 45 | this.setLayout(null); 46 | this.getContentPane().setBackground(Color.gray); 47 | this.setTitle("Traffic Light"); 48 | this.setSize(1000, 800); 49 | this.setVisible(true); 50 | } 51 | 52 | public void actionPerformed(ActionEvent e) { 53 | if(e.getSource() == radio1) { 54 | panel1.setBackground(Color.red); 55 | panel2.setBackground(Color.white); 56 | panel3.setBackground(Color.white); 57 | } else if(e.getSource() == radio2) { 58 | panel1.setBackground(Color.white); 59 | panel2.setBackground(Color.yellow); 60 | panel3.setBackground(Color.white); 61 | } else if(e.getSource() == radio3) { 62 | panel1.setBackground(Color.white); 63 | panel2.setBackground(Color.white); 64 | panel3.setBackground(Color.green); 65 | } 66 | } 67 | } 68 | 69 | public class EXAM { 70 | public static void main(String[] args) { 71 | new demo(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /Swing4.java: -------------------------------------------------------------------------------- 1 | import java.awt.BasicStroke; 2 | import java.awt.Color; 3 | import java.awt.Font; 4 | import java.awt.Graphics; 5 | import java.awt.Graphics2D; 6 | import javax.swing.*; 7 | 8 | class demo1 extends JFrame { 9 | private static final long serialVersionUID = 1L; 10 | public demo1(){ 11 | this.setTitle("Test"); 12 | this.setSize(700, 600); 13 | this.setVisible(true); 14 | } 15 | 16 | public void paint(Graphics g) { 17 | Graphics2D g2d = (Graphics2D) g; 18 | g2d.setPaint(Color.yellow); 19 | g2d.fillOval(100, 50, 400, 400); 20 | g2d.setPaint(Color.black); 21 | g2d.setStroke(new BasicStroke(5)); 22 | g2d.fillOval(150, 150, 100, 100); 23 | g2d.fillOval(350, 150, 100, 100); 24 | g2d.drawOval(100, 50, 400, 400); 25 | g2d.drawArc(200, 210, 200, 170, 180, 180); 26 | 27 | g2d.setFont(new Font("Ink Free", Font.BOLD, 45)); 28 | g2d.drawString("Hello Mileex SAAAN", 125, 520); 29 | } 30 | } 31 | 32 | public class graphtut { 33 | public static void main(String[] args) { 34 | new demo1(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Swing5.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.event.ActionEvent; 3 | import java.awt.event.ActionListener; 4 | import javax.swing.*; 5 | 6 | class test2 extends JFrame implements ActionListener { 7 | JButton button; 8 | JTextField field; 9 | int i = 1; 10 | 11 | test2() { 12 | field = new JTextField(); 13 | field.setBounds(300, 100, 100, 35); 14 | field.setText(" POW 0"); 15 | field.setForeground(Color.green); 16 | field.setBackground(Color.black); 17 | button = new JButton(); 18 | button.setText("Powwww"); 19 | button.setBounds(100, 100, 100, 35); 20 | button.setForeground(Color.RED); 21 | button.setBackground(Color.BLACK); 22 | button.addActionListener(this); 23 | 24 | this.add(field); 25 | this.add(button); 26 | this.setTitle("Joswin Woo"); 27 | this.getContentPane().setBackground(Color.GRAY); 28 | this.setLayout(null); 29 | this.setVisible(true); 30 | this.setSize(500, 400); 31 | } 32 | 33 | public void actionPerformed(ActionEvent e) { 34 | if (e.getSource() == button) { 35 | field.setText(" POW "+i); 36 | System.out.println("pow " + i); 37 | i++; 38 | } 39 | } 40 | } 41 | 42 | class test { 43 | public static void main(String[] args) { 44 | new test2(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Swing6.java: -------------------------------------------------------------------------------- 1 | import java.awt.Color; 2 | import java.awt.event.ActionEvent; 3 | import java.awt.event.ActionListener; 4 | 5 | import javax.swing.JButton; 6 | import javax.swing.JFrame; 7 | import javax.swing.JTextField; 8 | 9 | class demon implements ActionListener{ 10 | JButton button; 11 | JTextField field1, field2; 12 | 13 | demon() { 14 | JFrame frame = new JFrame("JOSWIN Woo"); 15 | 16 | field1 = new JTextField("Hello"); 17 | field1.setBounds(400, 300, 80, 30); 18 | field1.setBackground(Color.black); 19 | field1.setForeground(Color.green); 20 | 21 | field2 = new JTextField(); 22 | field2.setBounds(250, 300, 80, 30); 23 | field2.setBackground(Color.black); 24 | field2.setForeground(Color.red); 25 | 26 | button = new JButton("Get Data"); 27 | button.setBounds(100, 300, 100, 30); 28 | button.addActionListener(this); 29 | button.setBackground(Color.black); 30 | button.setForeground(Color.green); 31 | 32 | frame.add(field1); 33 | frame.add(field2); 34 | frame.add(button); 35 | frame.getContentPane().setBackground(Color.DARK_GRAY); 36 | frame.setLayout(null); 37 | frame.setSize(800, 700); 38 | frame.setVisible(true); 39 | } 40 | 41 | public void actionPerformed(ActionEvent e) { 42 | if(e.getSource() == button) { 43 | String data = field1.getText(); 44 | System.out.println(data); 45 | field2.setText(data); 46 | } 47 | } 48 | } 49 | 50 | 51 | public class test { 52 | public static void main(String[] args) { 53 | new demon(); 54 | } 55 | } 56 | 57 | -------------------------------------------------------------------------------- /Testfile.java: -------------------------------------------------------------------------------- 1 | package matrixmulti; 2 | import java.io.*; 3 | import java.util.Scanner; 4 | public class FileDemo{ 5 | public static void main(String[] args) { 6 | try 7 | { 8 | File f1= new File ("sample.txt"); 9 | if(f1.exists()) { 10 | System.out.println("file1 exists"); 11 | } 12 | else 13 | { 14 | f1.createNewFile(); 15 | System.out.println("created file sample.txt "); 16 | } 17 | Scanner sc= new Scanner(System.in); 18 | System.out.println("enter the contents of file"); 19 | String content=sc.nextLine(); 20 | FileWriter f= new FileWriter("sample.txt"); 21 | f.write(content); 22 | f.close(); 23 | System.out.println("contents saved to file sample.txt"); 24 | //create new file 25 | File f2= new File ("new_sample.txt"); 26 | 27 | if(f2.exists()) { 28 | System.out.println("file2 exists"); 29 | } 30 | else 31 | { 32 | f1.createNewFile(); 33 | System.out.println("created file new_sample.txt "); 34 | } 35 | //copy content from one file to another 36 | FileInputStream in= new FileInputStream("sample.txt"); 37 | FileOutputStream out= new FileOutputStream("new_sample.txt"); 38 | int c; 39 | while((c=in.read())!=-1) 40 | { 41 | out.write(c); 42 | } 43 | System.out.println("file contents copied from sample to sample 2"); 44 | System.out.println("file contents from sample 2"); 45 | FileReader fr=new FileReader("new_sample.txt"); 46 | BufferedReader br= new BufferedReader (fr); 47 | String content1; 48 | while ((content1= br.readLine())!=null) 49 | { 50 | System.out.println(content1); 51 | } 52 | }catch(IOException e) 53 | 54 | { 55 | System.out.println(e); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Thread_demo.java: -------------------------------------------------------------------------------- 1 | class T1 extends Thread { 2 | public void run() { 3 | System.out.println("T1 is running"); 4 | } 5 | } 6 | 7 | public class test { 8 | public static void main(String[] args) { 9 | 10 | //System.out.println(Thread.currentThread().getName()); 11 | //Thread.currentThread().setName("MAAAAIN"); 12 | //System.out.println(Thread.activeCount()); 13 | //Thread.currentThread().setPriority(6); 14 | //System.out.println(Thread.currentThread().getPriority()); 15 | //System.out.println(Thread.currentThread().isAlive()); 16 | 17 | /*for(int i=0; i<10; i++) { 18 | try { 19 | Thread.sleep(1000); // sleep for 1 second 20 | } catch (InterruptedException e) { 21 | e.printStackTrace(); 22 | } 23 | System.out.println(i); 24 | }*/ 25 | 26 | 27 | T1 thread1 = new T1(); 28 | thread1.start(); 29 | System.out.println(thread1.isAlive()); 30 | 31 | //System.out.println(thread1.isDaemon()); 32 | //thread1.setDaemon(true); 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Thread_synchronization.java: -------------------------------------------------------------------------------- 1 | class PrintTest extends Thread { 2 | synchronized public void printThread(int n) { 3 | for (int i = 1; i <= 5; i++) { 4 | System.out.println("Thread " + n + " is working..."); 5 | 6 | try { 7 | Thread.sleep(600); 8 | } catch (Exception e) { 9 | e.printStackTrace(); 10 | } 11 | 12 | } 13 | 14 | System.out.println("--------------------------"); 15 | } 16 | } 17 | 18 | class Thread1 extends Thread { 19 | PrintTest test; 20 | Thread1(PrintTest p) { 21 | test = p; 22 | } 23 | 24 | public void run() { 25 | test.printThread(1); 26 | } 27 | } 28 | 29 | class Thread2 extends Thread { 30 | PrintTest test; 31 | Thread2(PrintTest p) { 32 | test = p; 33 | } 34 | 35 | public void run() { 36 | test.printThread(2); 37 | } 38 | } 39 | 40 | public class thread { 41 | public static void main(String[] args) { 42 | PrintTest p = new PrintTest(); 43 | Thread1 t1 = new Thread1(p); 44 | Thread2 t2 = new Thread2(p); 45 | 46 | t1.start(); 47 | t2.start(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Traffic.java: -------------------------------------------------------------------------------- 1 | package trafficdemo; 2 | //Java Program to Implement Traffic signal 3 | //Using Java Swing Components 4 | //Importing required classes 5 | import java.awt.*; 6 | import java.awt.event.*; 7 | //import java.util.*; 8 | import javax.swing.*; 9 | //Main class 10 | //Extending JFrame class and 11 | //Implementing ActionListener interface 12 | public class Traffic_Signal extends JFrame implements ActionListener { 13 | // Setting the buttons for the layout 14 | JRadioButton jr1; 15 | JRadioButton jr2; 16 | JRadioButton jr3; 17 | ButtonGroup b = new ButtonGroup(); 18 | private Color red_c; 19 | private Color yellow_c; 20 | private Color green_c; 21 | public Traffic_Signal(String msg) 22 | { 23 | setLayout(new FlowLayout()); 24 | // Assigning name to the button declared above 25 | // with help of JRadioButton class 26 | jr1 = new JRadioButton("Red"); 27 | jr2 = new JRadioButton("Yellow"); 28 | jr3 = new JRadioButton("Green"); 29 | jr1.addActionListener(this); 30 | jr2.addActionListener(this); 31 | jr3.addActionListener(this); 32 | add(jr1); 33 | add(jr2); 34 | add(jr3); 35 | b.add(jr1); 36 | b.add(jr2); 37 | b.add(jr3); 38 | } 39 | // Method 1 40 | 41 | // To change colors of traffic signal 42 | public void actionPerformed(ActionEvent ae) 43 | { 44 | if (jr1.isSelected()==true) { 45 | red_c=Color.red; 46 | green_c=getBackground(); 47 | yellow_c=getBackground(); 48 | } 49 | if (jr2.isSelected()==true) { 50 | red_c=getBackground(); 51 | yellow_c=Color.YELLOW; 52 | green_c=getBackground(); 53 | } 54 | if (jr3.isSelected()==true) { 55 | red_c=getBackground(); 56 | yellow_c=getBackground(); 57 | green_c=Color.GREEN; 58 | } 59 | repaint(); 60 | } 61 | // Method 2 62 | // handling the paint graphics and 63 | // dimensions of the buttons via 64 | // setting co-ordinates 65 | public void paint(Graphics g) 66 | { 67 | g.drawRect(100, 105, 110, 270); 68 | g.drawOval(120, 150, 60, 60); 69 | g.drawOval(120, 230, 60, 60); 70 | g.drawOval(120, 300, 60, 60); 71 | g.setColor(red_c); 72 | g.fillOval(120, 150, 60, 60); 73 | g.setColor(yellow_c); 74 | g.fillOval(120, 230, 60, 60); 75 | g.setColor(green_c); 76 | g.fillOval(120, 300, 60, 60); 77 | } 78 | // Method 3 79 | // Main driver method 80 | public static void main(String args[]) 81 | 82 | { 83 | // Creating object of Jframe class inside main() 84 | // method 85 | JFrame jf = new Traffic_Signal("Traffic Light"); 86 | // Setting size and making traffic signal 87 | // operational using setVisible() method 88 | jf.setSize(500, 500); 89 | jf.setVisible(true); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /Traffic_working.java: -------------------------------------------------------------------------------- 1 | import java.awt.*; 2 | import java.awt.event.ActionEvent; 3 | import java.awt.event.ActionListener; 4 | 5 | import javax.swing.*; 6 | public class Trafficlight extends JPanel implements ActionListener{ 7 | JRadioButton r1,r2,r3; 8 | Color red_c,green_c,yellow_c; 9 | Trafficlight(){ 10 | setBounds(0,0,640,480); 11 | r1=new JRadioButton("Red"); 12 | r2=new JRadioButton("Yellow"); 13 | r3=new JRadioButton("Green"); 14 | r1.setSelected(true); 15 | red_c=Color.red; 16 | yellow_c=getBackground(); 17 | green_c=getBackground(); 18 | ButtonGroup b=new ButtonGroup(); 19 | b.add(r1); 20 | b.add(r2); 21 | b.add(r3); 22 | add(r1); 23 | add(r2); 24 | add(r3); 25 | r1.addActionListener(this); 26 | r2.addActionListener(this); 27 | r3.addActionListener(this); 28 | } 29 | public void paintComponent(Graphics g) { 30 | super.paintComponent(g); 31 | g.drawOval(50,50,50,50); 32 | g.drawOval(50,110,50,50); 33 | g.drawOval(50,170,50,50); 34 | g.setColor(red_c); 35 | g.fillOval(50,50,50,50); 36 | g.setColor(yellow_c); 37 | g.fillOval(50,110,50,50); 38 | g.setColor(green_c); 39 | g.fillOval(50,170,50,50); 40 | } 41 | public void actionPerformed(ActionEvent e) { 42 | if(r1.isSelected()==true) { 43 | red_c=Color.red; 44 | yellow_c=getBackground(); 45 | green_c=getBackground(); 46 | 47 | } 48 | else if(r2.isSelected()==true) { 49 | 50 | red_c=Color.white; 51 | yellow_c=Color.yellow; 52 | green_c=Color.white; 53 | } 54 | else if(r3.isSelected()==true) { 55 | 56 | red_c=Color.white; 57 | yellow_c=Color.white; 58 | green_c=Color.green; 59 | } 60 | repaint(); 61 | } 62 | 63 | public static void main(String[] args) { 64 | // TODO Auto-generated method stub 65 | JFrame f=new JFrame("Traffic light"); 66 | f.setSize(500,500); 67 | f.setLayout(null); 68 | f.setVisible(true); 69 | Trafficlight tl=new Trafficlight(); 70 | f.add(tl); 71 | } 72 | 73 | } 74 | 75 | -------------------------------------------------------------------------------- /inheritance.java: -------------------------------------------------------------------------------- 1 | class Parent { 2 | String name; 3 | int age; 4 | 5 | Parent(String name, int age) { 6 | this.name = name; 7 | this.age = age; 8 | } 9 | 10 | void display() { 11 | System.out.println("Hello " + name + ". You are " + age + " years old"); 12 | } 13 | } 14 | 15 | class Child extends Parent{ 16 | int grade; 17 | 18 | Child(String name, int age, int grade) { 19 | super(name, age); 20 | this.grade = grade; 21 | } 22 | 23 | void display() { 24 | super.display(); 25 | System.out.println(name + " is in grade " + grade); 26 | } 27 | } 28 | 29 | 30 | public class test { 31 | public static void main(String[] args) { 32 | Parent p1 = new Parent("Karen", 42); 33 | p1.display(); 34 | 35 | Child c1 = new Child("Ron", 12, 5); 36 | c1.display(); 37 | } 38 | } 39 | --------------------------------------------------------------------------------