You are on page 1of 39

INDEX

No. Title Date


1 Programs to demonstrate the use of various Data types and Variables by creating
CUI Application.
2 Program demonstration on Strings: String Comparisons, String Concatenation,
Check the equality of two strings, Reverse a string, Finding length of string.
3 Create Java Application by using Array:
Program to print the largest and smallest element in an array, Program to print the
sum of all the items of the array,
Program to add two Matrices, Program to multiply two Matrices, Program to sort the
elements of an array in ascending order.
4 Write an appropriate Java program to demonstrate the use of different types of
operators.
5 Write a suitable Java Program using following selection statements:
i. if and if-else
ii. nested-if
iii. if-else-if
iv. switch-case
v. Jump Statements: break, continue, return.
6 Implement Java programs using following loops:
i. for loop
ii. for-each loop
iii. while loop
iv. do-while loop
7 Write a suitable Java Program to demonstrate following types of Inheritance:
i. Single Inheritance
ii. Multilevel Inheritance
iii. Hierarchical Inheritance
iv. Multiple Inheritance
v. Hybrid Inheritance
8 Programs to demonstrate polymorphism:
i. Method over-riding
ii. Method overloading
9 Program to demonstrate all Math class functions.
10 Program on files:
i. Reading and Writing file using Byte Stream.
ii. Reading and Writing file using Character Stream.
iii. Program to copy a file into another file using classes in java.io package
11 Write a program to demonstrate the use of Date and Time API.
12 Write a program to implement the concepts of Thread and achieve Multi-threaded
programming.
13 Demonstrate the working of various AWT Layout Managers.
14 Write a Java program to demonstrate the use of AWT Controls.
15 Create a Real-World CUI/GUI Application.
Practical 1
Aim: Programs to demonstrate the use of various Data types and Variables by creating CUI Application.

Code
class baka {
    public static void main(String args[])
  {
    char grade;
    byte myByte1;
    int myNum1, myNum2, result; //integer has other variant 'long'
    float myFloat1,myFloat2,fresult; //float has other variant 'double'
    boolean myBool = true;
    grade = 'B';
    char myChar1 = 'A';
    myByte1 = 127;
    short myShort = 6000;
    myNum1 = -7000;
    myNum2 = 90000;
    result = myNum1 + myNum2;
    myFloat1=1000.666f;
    myFloat2=110.77f;
    fresult =myFloat1-myFloat2;
    System.out.println("Grade: "+grade);
    System.out.println("myChar1: " +myChar1);
    System.out.println("Byte 1: " +myByte1);
    System.out.println("myShort: " + myShort);
    System.out.println("Number 1: " +myNum1);
    System.out.println("Number 2: " +myNum2);
    System.out.println("Number 1 + Number 2: " +result);     
    System.out.println("Number1: "+myFloat1);
    System.out.println("Number2: "+myFloat2);
    System.out.println("Number1-Number2: "+fresult);
    if(myBool == true)
      System.out.println("I am using a Boolean data type");
      System.out.println(myBool);
  }
}

Output
Practical 2
Aim: Program demonstration on Strings:
i. String Comparisons,
ii. String Concatenation,
iii. Check the equality of two strings,
iv. Reverse a string,
v. Finding length of string.

Code
class YO {  
    public static void main(String args[]){  
      String s1="Jhon";  
      String s2="Jhon";  
      String s3="Jhon";  
      String s4="Jane";  
      System.out.println("\nString Comparison:");//--->
      System.out.println(s1.equals(s2));//true  
      System.out.println(s1.equals(s3));//true  
      System.out.println(s1.equals(s4));//false  

      System.out.println("\nString Concatenation:");//--->
      String s="Jhon"+" doe";  
      System.out.println(s); 

      System.out.println("\nString Comparison:"); //--->
      String str1 = "JhonDoe";
      String str2 = "JhonDoe";
      String str3 = "Hi";
      // checking for equality
      boolean retval1 = str2.equals(str1);
      boolean retval2 = str2.equals(str3);
      // prints the return value
      System.out.println("str2 is equal to str1 = " + retval1);
      System.out.println("str2 is equal to str3 = " + retval2);

      System.out.println("\nReverse string:"); //--->
      String input = "JhonDoeJhon";
      byte[] strAsByteArray = input.getBytes();
      byte[] result = new byte[strAsByteArray.length];
      for (int i = 0; i < strAsByteArray.length; i++){
        result[i] = strAsByteArray[strAsByteArray.length - i - 1];
      }
      System.out.println(new String(result));

      System.out.println("\nLength of String :"); //--->
      String name = "Whatsup_dude";  
      int length = name.length();  
      System.out.println("The length of the String \""+name+"\" is: " +length); 
    }  

Output

Practical 3
Aim: Create Java Application by using Array:
i. Program to print the largest and smallest element in an array.
ii. Program to print the sum of all the items of the array.
iii. Program to add two Matrices.
iv. Program to multiply two Matrices.
v. Program to sort the elements of an array in ascending order.

Code
class YO {
  public static void main(String args[]) {
    //array of 10 numbers
    int arr[] = new int[] { 12, 56, 76, 89, 100, 343, 21, 234 };

    int smallest = arr[0];
    int largest = arr[0];

    for (int i = 1; i < arr.length; i++) {
      if (arr[i] > largest) largest = arr[i]; else if (
        arr[i] < smallest
      ) smallest = arr[i];
    }
    System.out.println("\nSmallest & largest from an Array: "); //---->
    System.out.println("Smallest Number is : " + smallest);
    System.out.println("Largest Number is : " + largest);

    System.out.println("\nSum of all the items of an array: ");//---->
    int sum = 0;  
    for (int i = 0; i < arr.length; i++) {  
       sum = sum + arr[i];  
    }  
    System.out.println("Sum of all the elements of an array: " + sum);  

    
    System.out.println("\nAdd two matrices: ");//---->
    int a[][]={{1,3,4},{2,4,3},{3,4,5}};    
    int b[][]={{1,3,4},{2,4,3},{1,2,4}};       
    //creating another matrix to store the sum of two matrices    
    int c[][]=new int[3][3]; 
        
    //adding and printing addition of 2 matrices    
    for(int i=0;i<3;i++){    
        for(int j=0;j<3;j++){    
            c[i][j]=a[i][j]+b[i][j];  
            System.out.print(c[i][j]+" ");    
        }    
        System.out.println();  
    }    

    System.out.println("\nMultiply two matrices: ");//---->
    //creating another matrix to store the multiplication of two matrices    
    for(int i=0;i<3;i++){    
        for(int j=0;j<3;j++){    
            c[i][j]=0;      
            for(int k=0;k<3;k++)      
            {      
                c[i][j]+=a[i][k]*b[k][j];      
            }//end of k loop  
            System.out.print(c[i][j]+" ");    
        }//end of j loop  
        System.out.println(); 
    }    

    System.out.println("\nSorting an Array (Ascending): ");//---->
    int temp = 0;    
            
    System.out.println("Elements of original array: ");    
    for (int i = 0; i < arr.length; i++) {     
        System.out.print(arr[i] + " ");    
    }    
          
    for (int i = 0; i < arr.length; i++) {     
        for (int j = i+1; j < arr.length; j++) {     
           if(arr[i] > arr[j]) {    
               temp = arr[i];    
               arr[i] = arr[j];    
               arr[j] = temp;    
           }     
        }     
    }    
    System.out.println();    
        
    System.out.println("Elements of array sorted in ascending order: ");    
    for (int i = 0; i < arr.length; i++) {     
        System.out.print(arr[i] + " ");    
    }    
    System.out.println("\n\n");  
  }
}

Output
Practical 4
Aim: Write an appropriate Java program to demonstrate the use of different types of operators.
Code
public class YO{  
    public static void main(String args[]){  
        System.out.println("\nUnary Operator");// --->
        int x=10;  
        System.out.println(x++);//10 (11)  
        System.out.println(++x);//12  
        System.out.println(x--);//12 (11)  
        System.out.println(--x);//10 
        

        System.out.println("\nArithmetic Operator");// --->
        int a=10;  
        int b=5;  
        System.out.println(a+b);//15  
        System.out.println(a-b);//5  
        System.out.println(a*b);//50  
        System.out.println(a/b);//2  
        System.out.println(a%b);//0  

        System.out.println("\nLeft Shift Operator");// --->
        System.out.println(10<<2);//10*2^2=10*4=40  
        System.out.println(10<<3);//10*2^3=10*8=80 
        

        System.out.println("\nRight Shift Operator");// --->
        System.out.println(10>>2);//10/2^2=10/4=2  
        System.out.println(20>>2);//20/2^2=20/4=5  

        System.out.println("\nJava AND Operator Example: Logical && vs Bitwise &");// --->
        a=10;  
        b=5;  
        int c=20;  
        System.out.println(a<b&&a++<c);//false && true = false  
        System.out.println(a);//10 because second condition is not checked  
        System.out.println(a<b&a++<c);//false && true = false  
        System.out.println(a);//11 because second condition is checked  

        System.out.println("\nJava OR Operator Example: Logical || and Bitwise |");// --->
        System.out.println(a>b||a<c);//true || true = true  
        System.out.println(a>b|a<c);//true | true = true  
        //|| vs |  
        System.out.println(a>b||a++<c);//true || true = true  
        System.out.println(a);//10 because second condition is not checked  
        System.out.println(a>b|a++<c);//true | true = true  
        System.out.println(a);//11 because second condition is checked  

        System.out.println("\nJava Ternary Operator");// --->
        int min=(a<b)?a:b;  
        System.out.println(min);  

        System.out.println("\nJava Assignment Operator");// --->
        a+=4;//a=a+4 (a=10+4)  
        b-=4;//b=b-4 (b=20-4)  
        System.out.println(a);  
        System.out.println(b);  

        System.out.println("\n\n\n");// --->
    }
}  

Output
Practical 5
Aim: Write a suitable Java Program using following selection statements:
i. if and if-else
ii. nested-if
iii. if-else-if
iv. switch-case
v. Jump Statements: break, continue, return.

Code
public class YO{  
    public static void main(String args[]){  
        System.out.println("\nif & if-else -->");// --->
        if (20 > 18) {
            System.out.println("20 is greater than 18");
        }
        int time = 20;
        if (time < 18) {
        System.out.println("Good day.");
        } else {
        System.out.println("Good evening.");
        }

        System.out.println("\nNested if -->");// --->
        int i = 10;
        if (i == 10){
            if (i < 15)
                System.out.println("i is smaller than 15");
            if (i < 12)
                System.out.println("i is smaller than 12 too");
            else
                System.out.println("i is greater than 15");
        }

        System.out.println("\nIf-else-if -->");// --->
        i=20;
        if (i == 10)
            System.out.println("i is 10");
        else if (i == 15)
            System.out.println("i is 15");
        else if (i == 20)
            System.out.println("i is 20");
        else
            System.out.println("i is not present");
        

        System.out.println("\nSwitch-case -->");// --->
        int number=30;  
        switch(number){  
            case 10: System.out.println("Number is 10");  
            break;  
            case 20: System.out.println("Number is 20");  
            break;  
            case 30: System.out.println("Number is 30");  
            break;  
            //Default case statement  
            default:System.out.println("Not in 10, 20 or 30");  
        }  

        System.out.println("\nJump Statements: break, continue, return.-->");// --->

        //break
        int num=10;  
        switch(num){  
            case 10: System.out.println("Break Number is 10");  
            break;  
            case 20: System.out.println("Break Number is 20");  
            break;  
            case 30: System.out.println("Break Number is 30");  
            break;  
            //Default case statement  
            default:System.out.println("Not in 10, 20 or 30");  
        }  

        //continue
        for (int v = 0; v < 10; v++){
            if (v%2 == 0)
                continue;
            System.out.print(v + " ");
        }

        //return
        boolean t = true;
        System.out.println("\nBefore the return.\n");
      
        if (t)
            return;
  
        // Compiler will bypass every statement after return
        System.out.println("This won't execute.");

        System.out.println("\n\n\n");// --->
    }
}  

Output
Practical 6
Aim: Implement Java programs using following loops:
i. for loop
ii. for-each loop
iii. while loop
iv. do-while loop

Code
class baka {
    public static void main(String args[])
  {
    //Code of Java for loop 
    System.out.println("for loop:");
    for(int i=1;i<=10;i++){  
        System.out.println(i);  
    }
    //Declaring an array  
    int arr[]={12,23,44,56,78};  
    //Printing array using for-each loop  
    System.out.println("\nfor each loop:");
    for(int i:arr){  
        System.out.println(i);  
    }
    //Code for while loop
    System.out.println("\nwhile loop:");
    int i=1;  
    while(i<=10){  
        System.out.println(i);  
    i++;  
    }
    //Code for do-while loop
    System.out.println("\ndo-while loop:");
    int j=1;    
    do{    
        System.out.println(i);    
        j++;    
    }while(j<=10);
  }
}

Output
Practical 7
Aim: Write a suitable Java Program to demonstrate following types of Inheritance:
i. Single Inheritance
ii. Multilevel Inheritance
iii. Hierarchical Inheritance
iv. Multiple Inheritance
v. Hybrid Inheritance

Code
class Animal{  
    void eat(){System.out.println("Dog Eating :)");}  

class Dog extends Animal{  
    void bark(){System.out.println("Dog Barking :)");}  
}    

class BabyDog extends Dog{  
    void weep(){System.out.println("BabyDog Weeping :)");}  
}  

class Cat extends Animal{  
    void meow(){System.out.println("meowing...");}  

 
interface one {
    public void print_geek();
}
 
interface two {
    public void print_for();
}
 
interface three extends one, two {
    public void print_geek();
}

class child implements three {
    @Override public void print_geek(){
        System.out.println("Nerds");
    }
 
    public void print_for() { System.out.println("for"); }
}

class SolarSystem {
}
class Earth extends SolarSystem {
}
class Mars extends SolarSystem {
}

public class YO{  
    public static void main(String args[]){  
        System.out.println("\nSingle Inheritance --->");
        Dog d=new Dog();  
        d.bark();  
        d.eat();  

        System.out.println("\nMultilevel Inheritance --->");
        BabyDog d2=new BabyDog();  
        d2.weep();  
        d2.eat(); 

        System.out.println("\nHierarchical Inheritance --->");
        Cat cat=new Cat();  
        cat.meow();  
        cat.eat();  

        System.out.println("\nMultiple Inheritance --->");
        child c = new child();
        c.print_geek();
        c.print_for();
        c.print_geek();

        System.out.println("\nHybrid Inheritance --->");
        SolarSystem s = new SolarSystem();
        Earth e = new Earth();
        Mars m = new Mars();
 
        System.out.println(s instanceof SolarSystem);
        System.out.println(e instanceof Earth);
        System.out.println(m instanceof SolarSystem);

        System.out.println("\n\n\n");
    }
}  

Output
Practical 8
Aim: Programs to demonstrate polymorphism:
i. Method over-riding
ii. Method overloading

Code
Method Over-riding
class Vehicle{  
    //defining a method  
    void run(){System.out.println("Vehicle is running");}  
}  
public class YO extends Vehicle{
    void run(){System.out.println("Bike is running safely");}    
    public static void main(String args[]){  
        System.out.println("Method Over-riding :");
        YO obj = new YO();//creating object  
        obj.run();//calling method  
        System.out.println("\n\n\n");
    }
}  

Method Over-loading

class Adder {

  static int add(int a, int b) {
    return a + b;
  }

  static double add(double a, double b) {
    return a + b;
  }
}

class YO {
  public static void main(String[] args) {
    System.out.println("\nMethod Over-loading:");
    System.out.println(Adder.add(11, 11));
    System.out.println(Adder.add(12.3, 12.6));
    System.out.println("\n\n");

  }
}

Output
Practical 9
Aim: Program to demonstrate all Math class functions.

Code
class baka {
    public static void main(String args[])
  {
    double x = 28;
    double y = 4;
    double a = 69.420;
    // return the maximum of two numbers  
    System.out.println("Maximum number of x and y is: " +Math.max(x, y));
          
    // return the square root of y   
    System.out.println("Square root of y is: " + Math.sqrt(y));
          
    // returns 28 power of 4 i.e. 28*28*28*28    
    System.out.println("Power of x and y is: " + Math.pow(x, y));
  
    // return the logarithm of given value       
    System.out.println("Logarithm of x is: " + Math.log(x));
    System.out.println("Logarithm of y is: " + Math.log(y));
  
    // return a power of 2    
    System.out.println("exp of x is: " +Math.exp(x));

    //return round of a
    System.out.println("round of a is: " +Math.round(a));

    //return ceil of a
    System.out.println("ceil of a is: " +Math.ceil(a));
    
    //return floor of a
    System.out.println("floor of a is: " +Math.floor(a));
  }
}

Output

Practical 10
Aim: Program on files:
i. Reading and Writing file using Byte Stream.
ii. Reading and Writing file using Character Stream.
iii. Program to copy a file into another file using classes in java.io package

Code
import java.io.FileOutputStream;
import java.io.IOException;

class YO {
  public static void main(String[] args) {
    System.out.println("\n");
        try {
            FileOutputStream fout= new FileOutputStream("demo.txt");
            String s= "This is an example of Java program to write Bytes using ByteStream.
";
            byte b[] = s.getBytes();
            fout.write(b);
            fout.close();
        }
        catch (IOException e) {
            System.out.println(e);
        }
    System.out.println("\n\n");

  }
}

Reading and Writing file using Character Stream :


import java.io.FileReader;
import java.io.IOException;

class YO {

  public static void main(String[] args) {
      System.out.print("\n\n\n\n");
    try {
      FileReader reader = new FileReader("test.txt");
      int character;

      while ((character = reader.read()) != -1) {
        System.out.print((char) character);
      }
      reader.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Program to copy a file into another file using classes in java.io package :
import java.io.*;
import java.util.*;

class YO {

  public static void main(String arg[]) throws Exception {
    Scanner sc = new Scanner(System.in);
    System.out.print("Provide source file name :");
    String sfile = sc.next();
    System.out.print("Provide destination file name :");
    String dfile = sc.next();
    FileReader fin = new FileReader(sfile);
    FileWriter fout = new FileWriter(dfile, true);
    int c;
    while ((c = fin.read()) != -1) {
      fout.write(c);
    }
    System.out.println("Copy finish...");
    fin.close();
    fout.close();
  }
}

Output
Practical 11
Aim: Write a program to demonstrate the use of Date and Time API.

Code
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
 
public class baka {
 
// Function to get Zoned Date and Time
public static void ZonedTimeAndDate()
{
    LocalDateTime date = LocalDateTime.now();
    DateTimeFormatter format1 = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
     
    String formattedCurrentDate = date.format(format1);
     
    System.out.println("formatted current Date and" + " Time : "+formattedCurrentDate);
    
    ZonedDateTime currentZone = ZonedDateTime.now();
    System.out.println("the current zone is " + currentZone.getZone());

    ZoneId tokyo = ZoneId.of("Asia/Tokyo");
 
    ZonedDateTime tokyoZone = currentZone.withZoneSameInstant(tokyo);
                   
    System.out.println("tokyo time zone is " + tokyoZone);
 
    DateTimeFormatter format =
        DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
     
    String formatedDateTime = tokyoZone.format(format);
 
    System.out.println("formatted tokyo time zone "+ formatedDateTime);
     
}   
    public static void main(String[] args)
    {
         
        ZonedTimeAndDate();
         
    }
}

Output
Practical 12
Aim: Write a program to implement the concepts of Thread and achieve Multi-threaded programming.

Code
class MultithreadingDemo extends Thread {
  public void run()
  {
      try {
          // Displaying the thread that is running
          System.out.println(
              "Thread " + Thread.currentThread().getId() + " is running");
      }
      catch (Exception e) {
          // Throwing an exception
          System.out.println("Exception is caught");
      }
  }
}

// Main Class
public class Multithread {
  public static void main(String[] args)
  {
      int n = 8; // Number of threads
      for (int i = 0; i < n; i++) {
          MultithreadingDemo object = new MultithreadingDemo();
          object.start();
      }
  }
}

Output
Practical 13
Aim: Demonstrate the working of various AWT Layout Managers.

Code
1. Flow Layout:
import java.awt.*;  
import java.awt.event.*;  
public class YO  
{  
  public static void main(String[] args)  
  {  
   Frame frame= new Frame("FlowLayout Frame");  
   Panel pa= new Panel();  
   Button ba1= new Button();  
   Button ba2=new Button();  
   Button ba3=new Button();  
   Button ba4=new Button();  
   Button ba5=new Button();  
   frame.add(pa);  
   pa.setLayout(new FlowLayout());  
   pa.add(new Button("India"));  
   pa.add(new Button("Pakistan"));  
   pa.add(new Button("Japan"));  
   pa.add(new Button("China"));  
   pa.add(new Button("Countries"));  
   frame.setSize(300,300);  
   frame.setVisible(true);  
   frame.addWindowListener(new WindowAdapter()  
    {  
     public void windowClosing(WindowEvent e)  
      {  
       System.exit(0);  
      }  
    });  
  }  
}
2. Grid Layout:
import java.awt.*;
import java.awt.event.*;

public class YO {

  public static void main(String[] args) {
    Frame frame = new Frame("GridLayout Frame");
    Panel pa = new Panel();
    Button ba1 = new Button();
    Button ba2 = new Button();
    Button ba3 = new Button();
    Button ba4 = new Button();
    Button ba5 = new Button();
    frame.add(pa);
    pa.setLayout(new GridLayout());
    pa.add(new Button("India"));
    pa.add(new Button("Pakistan"));
    pa.add(new Button("Japan"));
    pa.add(new Button("China"));
    pa.add(new Button("Countries"));
    frame.setSize(300, 300);
    frame.setVisible(true);
    frame.addWindowListener(
      new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
        }
      }
    );
  }
}

3. Card Layout:
import java.awt.*;
import java.awt.event.*;

public class YO {

  public static void main(String[] args) {
    Frame frame= new Frame("CardLayout Frame");
    Panel pa = new Panel();
    Button ba1 = new Button();
    Button ba2 = new Button();
    Button ba3 = new Button();
    Button ba4 = new Button();
    Button ba5 = new Button();
    frame.add(pa);
    pa.setLayout(new CardLayout());
    pa.add(new Button("India"));
    pa.add(new Button("Pakistan"));
    pa.add(new Button("Japan"));
    pa.add(new Button("China"));
    pa.add(new Button("Countries"));
    frame.setSize(300, 300);
    frame.setVisible(true);
    frame.addWindowListener(
      new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
        }
      }
    );
  }
}

4. Border Layout:
import java.awt.*;
import java.awt.event.*;

public class YO {

  public static void main(String[] args) {
    Frame frame = new Frame("BorderLayout Frame");
    Panel pa = new Panel();
    Button ba1 = new Button();
    Button ba2 = new Button();
    Button ba3 = new Button();
    Button ba4 = new Button();
    Button ba5 = new Button();
    frame.add(pa);
    pa.setLayout(new BorderLayout());
    pa.add(new Button("India"), BorderLayout.NORTH);
    pa.add(new Button("Pakistan"), BorderLayout.SOUTH);
    pa.add(new Button("Japan"), BorderLayout.EAST);
    pa.add(new Button("China"), BorderLayout.WEST);
    pa.add(new Button("Countries"), BorderLayout.CENTER);
    frame.setSize(300, 300);
    frame.setVisible(true);
    frame.addWindowListener(
      new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
        }
      }
    );
  }
}

5. GridBag Layout:
import java.awt.*;
import java.awt.event.*;

public class YO {

  public static void main(String[] args) {
    Frame frame = new Frame("GridBagLayout Frame");
    Panel pa = new Panel();
    Button ba1 = new Button();
    Button ba2 = new Button();
    Button ba3 = new Button();
    Button ba4 = new Button();
    Button ba5 = new Button();
    frame.add(pa);
    pa.setLayout(new GridBagLayout());
    pa.add(new Button("India"));
    pa.add(new Button("Pakistan"));
    pa.add(new Button("Japan"));
    pa.add(new Button("China"));
    pa.add(new Button("Countries"));
    frame.setSize(300, 300);
    frame.setVisible(true);
    frame.addWindowListener(
      new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
        }
      }
    );
  }
}

Output
Practical 14
Aim: Write a Java program to demonstrate the use of AWT Controls.
Code
import java.awt.*;  
public class AwtApp extends Frame {  
  
AwtApp(){  
Label firstName = new Label("First Name");  
firstName.setBounds(20, 50, 80, 20);  
  
Label lastName = new Label("Last Name");  
lastName.setBounds(20, 80, 80, 20);  
  
Label dob = new Label("Date of Birth");  
dob.setBounds(20, 110, 80, 20);  
  
TextField firstNameTF = new TextField();  
firstNameTF.setBounds(120, 50, 100, 20);  
  
TextField lastNameTF = new TextField();  
lastNameTF.setBounds(120, 80, 100, 20);  
  
TextField dobTF = new TextField();  
dobTF.setBounds(120, 110, 100, 20);  
  
Button sbmt = new Button("Submit");  
sbmt.setBounds(20, 160, 100, 30);  
  
Button reset = new Button("Reset");  
reset.setBounds(120,160,100,30);  
  
add(firstName);  
add(lastName);  
add(dob);  
add(firstNameTF);  
add(lastNameTF);  
add(dobTF);  
add(sbmt);  
add(reset);  
  
setSize(300,300);  
setLayout(null);  
setVisible(true);  
}  
public static void main(String[] args) {  
// TODO Auto-generated method stub  
AwtApp awt = new AwtApp();    
}  

Output
Practical 15
Aim: Create a Real-World CUI/GUI Application.

Code
import java.util.ArrayList;
import java.util.Scanner;
import java.util.ArrayList;
 
class GroceryList {
  private ArrayList<String> groceryList = new ArrayList<String>();
  public ArrayList<String> getGroceryList() {
    return groceryList;
  }
 
  public void setGroceryList(ArrayList<String> groceryList) {
    this.groceryList = groceryList;
  }
 
  public void addGroceryItem(String item) {
    groceryList.add(item);
  }
  
  public void printGroceryList() {
    System.out.println("You Have "+ groceryList.size() + " item in your grocery list");
    for(int i = 0; i < groceryList.size(); i ++) {
      System.out.println((i+1) +" . "+groceryList.get(i));
    }
  }
  
  public void modifyGroceryItem(String currentItem, String newItem) {
    int position = findItem(currentItem);
    if(position >= 0) {
      modifyGroceryItem(position , newItem);
    }
  }
  
  private void modifyGroceryItem(int position , String item) {
    groceryList.set(position, item);
    System.out.println("Grocery Item " + (item) + " has been modified");
  }
  
  public void removeItem(String item) {
    int position = findItem(item);
    if(position >= 0) {
      removeItem(position);
    }
  }
  
  private void removeItem(int position) {
    groceryList.remove(position);
  }
   
  private int findItem(String searchItem) {
    
    return groceryList.indexOf(searchItem);
    
  }
  
  public boolean onFile(String item) {
    int position = findItem(item);
    if(position >= 0) {
      return true;
    }
    return false;
  }
 
}

public class YO {
 
  private static Scanner scanner = new Scanner(System.in);
  private static GroceryList groceryList = new GroceryList();
  
  
  public static void main(String[] args) {
 
 
    boolean quit = false;
    int choice = 0;
    printInstructions();
    
    while(!quit) {
      System.out.println("Enter Your Choice");
      choice = scanner.nextInt();
      scanner.nextLine();
      
      
      switch (choice) {
      case 0:
        printInstructions();
        break;
        
      case 1:
        groceryList.printGroceryList();
        
        break;
        
      case 2:
        addItem();
        break;
        
      case 3 :
        modifyItem();
        break;
        
      case 4:
        removeItem();
        break;
        
      case 5:
        searchForItem();
        break;
        
      case 6:
        processArrayList();
        break;
        
      case 7:
        quit= true;
        break;
      }
    }
    
    System.out.println("*********************************************************\r");  
    ArrayList<String> myToDoList = new ArrayList<String>();
    
    myToDoList.add("Write an articel");
    myToDoList.add("Make a Youtube Video");
    myToDoList.add("Make a salad for dinner");
    
    System.out.println(myToDoList.size());
    
    myToDoList.set(0, "Dont forget things");
    myToDoList.set(1, "Buy milk and orange juice");
    
    myToDoList.remove(0);
    
    
    for(int i = 0; i < myToDoList.size(); i++) {
      System.out.println("Item No " + (i) + " - "+ myToDoList.get(i));
    }
    
  }
 
  public static void printInstructions() {
    System.out.println("\nPress");
    System.out.println("\t 0 - To Print The Instructions ");
    System.out.println("\t 1 - To Print GroceryList ");
    System.out.println("\t 2 - To Add Item ");
    System.out.println("\t 3 - To Modify item ");
    System.out.println("\t 4 - To Remove Item ");
    System.out.println("\t 5 - To Search for an Item ");
    System.out.println("\t 6 - To Process Array List. ");
    System.out.println("\t 7 - To Not Continue");
  }
  
  public static void addItem() {
    System.out.print("Add : Please enter the grocery Item : ");
    groceryList.addGroceryItem(scanner.nextLine());
  }
  
  public static void modifyItem() {
    System.out.print("Modify : Enter the Item: ");
    String curItem = scanner.nextLine();    
    
    System.out.print("Modify : Enter the Replacement Item");
    String newItem = scanner.nextLine();
    
    groceryList.modifyGroceryItem(curItem, newItem);
  }
  
  public static void removeItem() {
    System.out.print("Remove : Enter the Item: ");
    String itemNo = scanner.nextLine();
    groceryList.removeItem(itemNo);
    System.out.print("msg: The Item has been removed");
    groceryList.printGroceryList();
  }
  
   public static void searchForItem() {
          System.out.print("Item to search for: ");
          String searchItem = scanner.nextLine();
          if(groceryList.onFile(searchItem)) {
            System.out.println("Item " + searchItem + " is on the list");
          }else{
            System.out.println(searchItem + " is not in the list");
          }
      }
   
   public static void processArrayList() {
     
     ArrayList<String> newArrayList = new ArrayList<String>();
     newArrayList.addAll(groceryList.getGroceryList());
     
     
     ArrayList<String> anotherArrayList = new ArrayList<>(groceryList.getGroceryList());
     
     String[] myArray = new String[groceryList.getGroceryList().size()];
     myArray = groceryList.getGroceryList().toArray(myArray);
   }
  
}

Output

You might also like