└── java.txt /java.txt: -------------------------------------------------------------------------------- 1 | java programs. 2 | 3 | 4 | 1. Write a Java program that implements Bubble sort algorithm for sorting in descending 5 | order and also shows the number of interchanges occurred for the given set of integers 6 | 7 | 8 | public class BubbleSort { 9 | 10 | void bubbleSort(int arr[]) 11 | { 12 | int count=0; 13 | int n=arr.length; 14 | for(int i=0;i 0) { 132 | j--; 133 | } 134 | 135 | if (i <= j) { 136 | exchangeNames(i, j); 137 | i++; 138 | j--; 139 | } 140 | } 141 | if (lowerIndex < j) { 142 | quickSort (lowerIndex, j); 143 | } 144 | if(i < higherIndex) { 145 | quickSort(i, higherIndex); 146 | } 147 | } 148 | void exchangeNames (int i, int j) { 149 | String temp = this. names [i]; 150 | this.names [i] = this . names[j]; 151 | this.names [j] = temp; 152 | } 153 | } 154 | 155 | 4.Write a Java program that implements a multi-thread application that has three threads. 156 | First thread generates random integer every 1 second and if the value is even, second 157 | thread computes the square of the number and prints. If the value is odd, the third thread 158 | 159 | import java.util.Random; 160 | 161 | 162 | class RandomNumberThread extends Thread { 163 | public void run() { 164 | Random random = new Random(); 165 | for(int i=0;i<10;i++) { 166 | int randomInteger = random.nextInt(100); 167 | System.out.println("Random Integar generated:"+ randomInteger); 168 | if((randomInteger%2)==0) { 169 | SquareThread sThread =new SquareThread(randomInteger); 170 | sThread.start(); 171 | } 172 | else { 173 | CubeThread cThread = new CubeThread(randomInteger); 174 | cThread.start(); 175 | } 176 | try { 177 | Thread.sleep(1000); 178 | } 179 | catch (InterruptedException ex) { 180 | System.out.println(ex); 181 | } 182 | } 183 | } 184 | } 185 | 186 | class SquareThread extends Thread { 187 | int number; 188 | 189 | SquareThread(int randomNumbern){ 190 | number=randomNumbern; 191 | } 192 | public void run() { 193 | System.out.println("Square of "+number+"="+(number*number)); 194 | } 195 | } 196 | 197 | class CubeThread extends Thread { 198 | int number; 199 | 200 | CubeThread(int randomNumber) { 201 | number = randomNumber; 202 | } 203 | public void run() { 204 | System.out.println("Cube of "+number+"="+number*number*number); 205 | } 206 | } 207 | public class MultiThreadingTest{ 208 | public static void main(String args[]) { 209 | RandomNumberThread rnThread =new RandomNumberThread(); 210 | rnThread.start(); 211 | } 212 | 213 | } 214 | 215 | 5. Write a Java program to list all the files in a directory including the files present in all its 216 | subdirectories. 217 | 218 | import java.util.Scanner; 219 | import java.io.*; 220 | 221 | public class ListingFiles { 222 | 223 | public static void main(String[] args) { 224 | String path =null; 225 | Scanner sc =new Scanner(System.in); 226 | System.out.print("Enter the root directory name:"); 227 | path=sc.next()+":\\"; 228 | File f_ref =new File(path); 229 | if(!f_ref.exists()) { 230 | //printLine(); 231 | System.out.println("Root direcory does not exists!"); 232 | //println(); 233 | } 234 | else { 235 | String ch="y"; 236 | while(ch.equalsIgnoreCase("y")) { 237 | printFiles(path); 238 | System.out.print("Do you want to open any sub-directory(Y/N): "); 239 | ch=sc.next().toLowerCase(); 240 | if(ch.equalsIgnoreCase("y")) { 241 | System.out.print("Enter the sub-directory name:"); 242 | path=path+"\\\\"+sc.next(); 243 | File f_ref_2=new File(path); 244 | if(!f_ref_2.exists()){ 245 | //printLine(); 246 | System.out.print("The Sub-directory does not exists!"); 247 | //printLine(); 248 | int lastIndex =path.lastIndexOf("\\"); 249 | path = path.substring(0,lastIndex); 250 | } 251 | } 252 | } 253 | } 254 | System.out.println("****** Program Closed *******"); 255 | } 256 | // TODO Auto-generated method stub 257 | public static void printFiles(String path) { 258 | System.out.println("Current Location"+path); 259 | File f_ref = new File(path); 260 | File[] fileslist = f_ref.listFiles( ); 261 | for (File file : fileslist) { 262 | if (file. isFile( )) 263 | System.out.println("-" + file.getName( ) ); 264 | else 265 | System.out.println(">" + file.getName( )); 266 | } 267 | } 268 | public static void printline() { 269 | 270 | System.out.println("---------------------------"); 271 | } 272 | } 273 | 274 | 275 | 276 | 6 . Write a Java program for the following: Create a doubly linked list of elements. Delete a 277 | given element from the above list. Display the contents of the list after deletion. 278 | 279 | public class DLL{ 280 | Node head; 281 | class Node{ 282 | int data; 283 | Node prev; 284 | Node next; 285 | Node(int d) { data= d;} 286 | } 287 | 288 | public void push(int new_data) 289 | { 290 | Node new_Node=new Node(new_data); 291 | new_Node.next=head; 292 | new_Node.prev=null; 293 | if(head!=null) 294 | head.prev=new_Node; 295 | head=new_Node; 296 | } 297 | 298 | public void printlist(Node node) 299 | { 300 | Node last = null; 301 | while(node!=null) { 302 | System.out.print(node.data+" "); 303 | last=node; 304 | node=node.next; 305 | } 306 | System.out.println(); 307 | } 308 | void deleteNode(Node del) 309 | { 310 | if(head==null || del==null){ 311 | return; 312 | } 313 | 314 | if(head == del) { 315 | head=del.next; 316 | } 317 | 318 | if(del.next !=null){ 319 | del.next.prev=del.prev; 320 | } 321 | 322 | if(del.prev !=null) { 323 | del.prev.next=del.next; 324 | } 325 | 326 | return; 327 | 328 | } 329 | 330 | public static void main(String[] args) 331 | { 332 | DLL dll=new DLL(); 333 | dll.push(2); 334 | dll.push(4); 335 | dll.push(8); 336 | dll.push(10); 337 | System.out.print("Created DLL is: "); 338 | dll.printlist(dll.head); 339 | dll.deleteNode(dll.head); 340 | System.out.print("\nList after deleting first node:"); 341 | dll.printlist(dll.head); 342 | dll.deleteNode(dll.head.next); 343 | System.out.println("\nList after Deleting Midddle Node:"); 344 | dll.printlist(dll.head); 345 | } 346 | } 347 | 348 | 349 | 7. Write a Java program that loads names and phone numbers from a text file where the data 350 | is organized as one line per record and each field in a record are separated by a tab (\t). It 351 | takes a name or phone number as input and prints the corresponding other value from the 352 | hash table (hint: use hash tables). 353 | 354 | import java.io.BufferedReader; 355 | import java.io.File; 356 | import java.io.FileNotFoundException; 357 | import java.io.FileReader; 358 | import java.io.IOException; 359 | import java.util.Hashtable; 360 | import java.util.Iterator; 361 | import java.util.Set; 362 | public class HashTab 363 | { 364 | public static void main(String[] args) 365 | { 366 | HashTab prog11 = new HashTab(); 367 | HashtablehashData = prog11.readFromFile("HashTab.txt"); 368 | System.out.println("File data into Hashtable:\n"+hashData); 369 | prog11.printTheData(hashData, "vbit"); 370 | prog11.printTheData(hashData, "123"); 371 | prog11.printTheData(hashData, "----"); 372 | } 373 | private void printTheData(HashtablehashData, String input) 374 | { 375 | String output = null; 376 | if(hashData != null) 377 | { 378 | Set keys = hashData.keySet(); 379 | if(keys.contains(input)) 380 | 381 | { 382 | output = hashData.get(input); 383 | } 384 | else 385 | { 386 | Iterator iterator = keys.iterator(); 387 | while(iterator.hasNext()) { 388 | String key = iterator.next(); 389 | String value = hashData.get(key); 390 | if(value.equals(input)) 391 | { 392 | output = key; 393 | break; 394 | } } } } 395 | System.out.println("Input given:"+input); 396 | if(output != null) 397 | { 398 | System.out.println("Data found in HashTable:"+output); 399 | } 400 | else { 401 | System.out.println("Data not found in HashTable"); 402 | } } 403 | private HashtablereadFromFile(String fileName) { 404 | Hashtable hashData = new Hashtable(); 405 | try { 406 | File f = new File("c:\\java\\"+fileName); 407 | BufferedReader br = new BufferedReader(new FileReader(f)); 408 | String line = null; 409 | while((line = br.readLine()) != null) { 410 | String[] details = line.split("\t"); 411 | hashData.put(details[0], details[0]); 412 | } 413 | } catch (FileNotFoundException e) { 414 | 415 | e.printStackTrace(); 416 | } catch (IOException e) { 417 | e.printStackTrace(); } 418 | return hashData; } } 419 | 420 | 8. Suppose that a table named Table.txt is stored in a text file. The first line in the file is the 421 | header, and the remaining lines correspond to rows in the table. The elements are 422 | separated by commas. 423 | Write a java program to display the table using Labels in Grid 424 | Layout 425 | 426 | import java.io.*; 427 | import java.util.*; 428 | import java.awt.*; 429 | import java.swing.*; 430 | 431 | class A extends JFrame { 432 | public A() { 433 | setSize(400, 400); 434 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 435 | GridLayout g = new GridLayout(0, 3); 436 | setLayout(g); 437 | try { 438 | FileInputStream fin = new FileInputStream("D:\\java\\Table.txt"); 439 | Scanner sc = new Scanner(fin).useDelimiter(","); 440 | String[] arrayList; 441 | String a; 442 | while (sc.hasNextLine()) { 443 | a = sc.nextLine(); 444 | arrayList = a.split(","); 445 | for (String i : arrayList) { 446 | add(new JLabel(i)); 447 | } 448 | } 449 | } catch (Exception ex) { 450 | } 451 | setDefaultLookAndFeelDecorated(true); 452 | //pack(); 453 | setVisible(true); 454 | } 455 | } 456 | public class TableTest { 457 | public static void main(String[] args) { 458 | A a = new A(); 459 | } 460 | } 461 | 462 | 9.Write a Java program that handles all mouse events and shows the event name at the 463 | center of the window when a mouse event is fired (Use Adapter classes). 464 | 465 | import javax.swing.*; 466 | import java.awt.*; 467 | import javax.swing.event.*; 468 | import java.awt.event.*; 469 | class MouseEventDemo extends JFrame implements MouseListener,MouseMotionListener 470 | { 471 | // var 472 | String str=" "; 473 | JTextArea ta; 474 | Container c; 475 | int x,y; 476 | 477 | public MouseEventDemo() 478 | { 479 | // create conntent pane 480 | c=getContentPane(); 481 | c.setLayout(new FlowLayout()); 482 | // create a textarea and set font to it 483 | ta=new JTextArea("click the mouse or move it 5, 20"); 484 | ta.setFont(new Font("Arial",Font.BOLD,30)); 485 | // add textarea to contentpane 486 | c.add(ta); 487 | // add mouse listiner,mouse motion listener to textarea 488 | ta.addMouseListener(this); 489 | ta.addMouseMotionListener(this); 490 | 491 | } 492 | public void mouseClicked(MouseEvent me) 493 | { 494 | // know which button of mouse is cliked 495 | int i=me.getButton(); 496 | if(i==1) 497 | str+="Clicked Button:Left"; 498 | else if(i==2) 499 | str+="clicked Button:Middle"; 500 | else if(i==3) 501 | str+="cliked button:Right"; 502 | this.display(); 503 | } 504 | public void mouseEntered(MouseEvent me) 505 | { 506 | str+=" Mouse entered"; 507 | this.display(); 508 | } 509 | public void mouseExited(MouseEvent me) 510 | { 511 | str+=" Mouse Exited"; 512 | this.display(); 513 | } 514 | public void mousePressed(MouseEvent me) 515 | { 516 | x=me.getX(); 517 | y=me.getY(); 518 | str+= "Mouse pressed at:" +x+"\t"+y; 519 | this.display(); 520 | } 521 | public void mouseDragged(MouseEvent me) 522 | { 523 | x=me.getX(); 524 | y=me.getY(); 525 | str+= "Mouse Dragged at:" +x+"\t"+y; 526 | this.display(); 527 | } 528 | public void mouseReleased(MouseEvent me) 529 | { 530 | x=me.getX(); 531 | y=me.getY(); 532 | str+= "Mouse Released at:" +x+"\t"+y; 533 | this.display(); 534 | } 535 | 536 | public void mouseMoved(MouseEvent me) 537 | { 538 | x=me.getX(); 539 | y=me.getY(); 540 | str+= "Mouse Moved at:" +x+"\t"+y; 541 | this.display(); 542 | } 543 | public void display() 544 | { 545 | ta.setText(str); 546 | str=""; 547 | } 548 | public static void main(String args[]) 549 | { 550 | // create the frame 551 | MouseEventDemo mes=new MouseEventDemo(); 552 | mes.setSize(400,400); 553 | mes.setVisible(true); 554 | mes.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 555 | }} 556 | 557 | 10. Write a Java program that works as a simple calculator. Use a grid layout to arrange 558 | buttons for the digits and for the +, -,*, % operations. Add a text field to display the 559 | result. Handle any possible exceptions like divided by zero. 560 | 561 | import java.awt.*; 562 | import java.awt.event.*; 563 | import javax.swing.*; 564 | public class Calculator extends JFrame implements ActionListener 565 | { 566 | JTextField t1; 567 | JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10, b11,b12,b13,b14,b15,b16; 568 | int x,y,z,i; 569 | String op; 570 | public Calculator() 571 | { 572 | GridLayout gl=new GridLayout(4,5); 573 | setLayout(gl); 574 | t1 = new JTextField(10); 575 | b1=new JButton("1"); 576 | b2=new JButton("2"); 577 | b3=new JButton("3"); 578 | b4=new JButton("4"); 579 | b5=new JButton("5"); 580 | b6=new JButton("6"); 581 | b7=new JButton("7"); 582 | b8=new JButton("8"); 583 | b9=new JButton("9"); 584 | b10=new JButton("0"); 585 | b11=new JButton("+"); 586 | b12=new JButton("-"); 587 | b13=new JButton("*"); 588 | b14=new JButton("/"); 589 | b15=new JButton("="); 590 | b16=new JButton("C"); 591 | add(b1); 592 | add(b2); 593 | add(b3); 594 | add(b4); 595 | add(b5); 596 | add(b6); 597 | add(b7); 598 | add(b8); 599 | add(b9); 600 | add(b10); 601 | add(b11); 602 | add(b12); 603 | add(b13); 604 | add(b14); 605 | add(b15); 606 | add(b16); 607 | add(t1); 608 | b1.addActionListener(this); 609 | b2.addActionListener(this); 610 | b3.addActionListener(this); 611 | b4.addActionListener(this); 612 | b5.addActionListener(this); 613 | b6.addActionListener(this); 614 | b7.addActionListener(this); 615 | b8.addActionListener(this); 616 | b9.addActionListener(this); 617 | b10.addActionListener(this); 618 | b11.addActionListener(this); 619 | b12.addActionListener(this); 620 | b13.addActionListener(this); 621 | b14.addActionListener(this); 622 | b15.addActionListener(this); 623 | b16.addActionListener(this); 624 | setSize(300,300); 625 | setVisible(true); 626 | setTitle("Calculator"); 627 | } 628 | public void actionPerformed(ActionEvent ae) 629 | { 630 | if(ae.getActionCommand()=="1") 631 | t1.setText(t1.getText()+"1"); 632 | if(ae.getActionCommand()=="2") 633 | t1.setText(t1.getText()+"2"); 634 | if(ae.getActionCommand()=="3") 635 | t1.setText(t1.getText()+"3"); 636 | if(ae.getActionCommand()=="4") 637 | t1.setText(t1.getText()+"4"); 638 | if(ae.getActionCommand()=="5") 639 | t1.setText(t1.getText()+"5"); 640 | if(ae.getActionCommand()=="6") 641 | t1.setText(t1.getText()+"6"); 642 | if(ae.getActionCommand()=="7") 643 | t1.setText(t1.getText()+"7"); 644 | if(ae.getActionCommand()=="8") 645 | t1.setText(t1.getText()+"8"); 646 | if(ae.getActionCommand()=="9") 647 | t1.setText(t1.getText()+"9"); 648 | if(ae.getActionCommand()=="0") 649 | t1.setText(t1.getText()+"0"); 650 | if(ae.getActionCommand()=="+") 651 | { 652 | x=Integer.parseInt(t1.getText()); 653 | t1.setText(""); 654 | op="+"; 655 | } 656 | if(ae.getActionCommand()=="-") 657 | { 658 | x=Integer.parseInt(t1.getText()); 659 | t1.setText(""); 660 | op="-"; 661 | } 662 | if(ae.getActionCommand()=="*") 663 | { 664 | x=Integer.parseInt(t1.getText()); 665 | t1.setText(""); 666 | op="*"; 667 | } 668 | if(ae.getActionCommand()=="/") 669 | { 670 | x=Integer.parseInt(t1.getText()); 671 | t1.setText(""); 672 | op="/"; 673 | } 674 | if(ae.getActionCommand()=="=") 675 | { 676 | y=Integer.parseInt(t1.getText()); 677 | if(op=="+") 678 | z=x+y; 679 | if(op=="-") 680 | z=x-y; 681 | if(op=="*") 682 | z=x*y; 683 | if(op=="/") 684 | z=x/y; 685 | t1.setText(String.valueOf(z)); 686 | } 687 | if(ae.getActionCommand()=="C") 688 | { 689 | t1.setText(""); 690 | } 691 | } 692 | public static void main(String args[]) 693 | { 694 | Calculator cal=new Calculator(); 695 | 696 | } 697 | } 698 | 699 | 700 | 701 | 702 | 703 | //week12 704 | // Write a Java program that correctly implements the producer – consumer problem using the 705 | //concept of interthread communication. 706 | 707 | 708 | //sourse code: 709 | 710 | class ItemQueue { 711 | int item; 712 | boolean valueSet = false; 713 | 714 | synchronized int getItem() 715 | 716 | { 717 | while (!valueSet) 718 | try { 719 | wait(); 720 | } catch (InterruptedException e) { 721 | System.out.println("InterruptedException caught"); 722 | } 723 | System.out.println("Consummed:" + item); 724 | valueSet = false; 725 | try { 726 | Thread.sleep(1000); 727 | } catch (InterruptedException e) { 728 | System.out.println("InterruptedException caught"); 729 | } 730 | notify(); 731 | return item; 732 | } 733 | 734 | synchronized void putItem(int item) { 735 | while (valueSet) 736 | try { 737 | wait(); 738 | } catch (InterruptedException e) { 739 | System.out.println("InterruptedException caught"); 740 | } 741 | this.item = item; 742 | valueSet = true; 743 | System.out.println("Produced: " + item); 744 | try { 745 | Thread.sleep(1000); 746 | } catch (InterruptedException e) { 747 | System.out.println("InterruptedException caught"); 748 | } 749 | notify(); 750 | } 751 | } 752 | 753 | class Producer implements Runnable{ 754 | ItemQueue itemQueue; 755 | Producer(ItemQueue itemQueue){ 756 | this.itemQueue = itemQueue; 757 | new Thread(this, "Producer").start(); 758 | } 759 | public void run() { 760 | int i = 0; 761 | while(true) { 762 | itemQueue.putItem(i++); 763 | } 764 | } 765 | } 766 | class Consumer implements Runnable{ 767 | 768 | ItemQueue itemQueue; 769 | Consumer(ItemQueue itemQueue){ 770 | this.itemQueue = itemQueue; 771 | new Thread(this, "Consumer").start(); 772 | } 773 | public void run() { 774 | while(true) { 775 | itemQueue.getItem(); 776 | } 777 | } 778 | } 779 | 780 | class ProducerConsumer{ 781 | public static void main(String args[]) { 782 | ItemQueue itemQueue = new ItemQueue(); 783 | new Producer(itemQueue); 784 | new Consumer(itemQueue); 785 | System.out.println("Press Control+c to exit"); 786 | } 787 | } 788 | 789 | 790 | 791 | //output: 792 | 793 | /*Press Control+c to exit 794 | Produced: 0 795 | Consummed:0 796 | Produced: 1 797 | Consummed:1 798 | Produced: 2 799 | Consummed:2 800 | Produced: 3 801 | Consummed:3 802 | Produced: 4 803 | Consummed:4 804 | Produced: 5 805 | Consummed:5 806 | Produced: 6 807 | Consummed:6 808 | Produced: 7 */ 809 | --------------------------------------------------------------------------------