JAVA practical exam pdf - 16 January

practical pdf:


Program 1 :  WAP for Fibonacci series.



import java.util.*;


class Fibonacci{

public static void main(String args[])

{

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of terms for Fibonacci series: ");



int count = scanner.nextInt();


int n1=0,n2=1,n3,i;

System.out.print(n1+" "+n2);


for(i=2;i<count;++i)

{

n3=n1+n2;

System.out.print(" "+n3);

n1=n2;

n2=n3;

}


}

}




OUTPUT: 


Microsoft Windows [Version 10.0.22000.2538]

(c) Microsoft Corporation. All rights reserved.


C:\Users\admin>cd/


C:\>cd chaitanya


C:\chaitanya>javac Fibonacci.java


C:\chaitanya>java Fibonacci

Enter the number of terms for Fibonacci series: 20

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

C:\chaitanya>















Program 2 : WAP with class that has static method that include a logic of even and odd number.


import java.util.Scanner;


public class Prog2 {

    

    public static void EvenOrOdd(int number) {

        if (number % 2 == 0) {

            System.out.println("Number is Even");

        } else {

            System.out.println("Number is Odd");

        }

    }


public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a number: ");

        int number = scanner.nextInt();


        EvenOrOdd(number);

    }

}

OUTPUT:

C:\chaitanya>javac Prog2.java

C:\chaitanya>java Prog2

Enter a number: 9

Number is Odd





Program 3 :WAP with class that has a default constructor to display a message and another constructor with parameters that include logic to reverse number.


import java.util.Scanner;


class {

private int number;


public NumberReverser() {

System.out.println("Welcome to the Number Reverser!");

}


public NumberReverser(int number) {

int reversedNumber = 0;

int remainder;

int originalNumber = number;


while (originalNumber != 0) {

remainder = originalNumber % 10;

reversedNumber = reversedNumber * 10 + remainder;

originalNumber /= 10;

}


System.out.println("The reversed number is: " + reversedNumber);

}



public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


NumberReverser reverser = new NumberReverser();


System.out.print("Enter a number to reverse: ");

int number = scanner.nextInt();


NumberReverser obj = new NumberReverser(number);


    }

}

OUTPUT:


Microsoft Windows [Version 10.0.22000.2538]

(c) Microsoft Corporation. All rights reserved.



C:\Users\admin>cd/


C:\>cd chaitanya


C:\chaitanya>javac  NumberReverser.java


C:\chaitanya>java  NumberReverser

Welcome to the Number Reverser!

Enter a number to reverse: 56789

The reversed number is: 98765


C:\chaitanya>

















Program 4 : WAP with a class that has a default method to display a message and another method with parameters that include logic to palindrome number.


import java.lang.*;

import java.util.*;


class MethodOver{


void Palindrome(){

System.out.println("This is 1st Methode");

}


void Palindrome(int n){


int rev=0;

int fixn = n;


while(n!=0){

int q=n%10;

rev = rev*10 + q;

n = n/10;

}


if(fixn==rev){

System.out.println("Entered number is a palindrome");

}else{

System.out.println("Entered number is not a palindrome");

}

public static void main(String args[]){

Scanner sc = new Scanner(System.in);

System.out.println("Enter any number of more than 2 digits : ");

int n = sc.nextInt();


MethodOver a = new MethodOver();

a.Palindrome();

a.Palindrome(n);


}

}


OUTPUT:


Microsoft Windows [Version 10.0.22000.2538]

(c) Microsoft Corporation. All rights reserved.


C:\Users\admin>cd/


C:\>cd chaitanya


C:\chaitanya>javac MethodOver.java


C:\chaitanya>java MethodOver

Enter any number of more than 2 digits :

56567

This is 1st Methode

Entered number is not a palindrome

C:\chaitanya>


Programm 5 : WAP in JAVA to implement Hierarchical inheritance.  


  

 class Animal{  

void eat(){

System.out.println("eating...");

}

}


class Dog extends Animal{

void bark(){

System.out.println("barking...");

}

}

c

class Cat extends Animal{

void meow(){

System.out.println("meowing...");

}

}


class HierInheritance{

public static void main(String args[]){

Cat c=new Cat();

c.meow();

c.eat();

}

}  


OUTPUT:


Microsoft Windows [Version 10.0.22000.2538]

(c) Microsoft Corporation. All rights reserved.


C:\Users\admin>cd/


C:\>cd chaitanya


C:\chaitanya>javac HierInheritance.java


C:\chaitanya>java HierInheritance

meowing...

eating...


C:\chaitanya>


Programm 6 : WAP to create user-defined Package.



package mypackage;


public class Shape {

public void display() {

System.out.println("This is a shape.");

}

}


Import mypackage.Shape;


public class Main {

public static void main(String[] args) {

Shape shape = new Shape();

shape.display();

}

}


OUTPUT:







Program 7 : WAP that take two no from user and performs division. Implement exception handling to catch and handle the divide by 0 exception. If the user attempt to divide by 0, display a meaningful error message. Otherwise display the result of division.


import java.util.Scanner;


public class ExceptionHandlingEx {

    

  private static int divide(int numerator, int denominator) {

        return numerator / denominator;

    }


public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);


        try {

            System.out.print("Enter the numerator: ");

            int numerator = scanner.nextInt();


            System.out.print("Enter the denominator: ");

            int denominator = scanner.nextInt();


            int result = divide(numerator, denominator);


            System.out.println("Result of division: " + result);


        }  catch (Exception e) {

            System.out.println("An error occurred: Can not divide by zero");


     } 

    }

}




OUTPUT:


Microsoft Windows [Version 10.0.22000.2538]

(c) Microsoft Corporation. All rights reserved.


C:\Users\admin>cd/


C:\>cd chaitanya


C:\chaitanya>javac ExceptionHandlingEx.java


C:\chaitanya>java ExceptionHandlingEx

Enter the numerator: 20

Enter the denominator: 4

Result of division: 5


C:\chaitanya>






Program 8 : WAP to create user-defined Exception.


import java.util.*;

class MyException extends Exception

{

MyException(String msg)

{

super(msg);

}

}

class ExampleExc

{

public static void main(String args[])throws MyException

{

System.out.println("enter age");

Scanner sc=new Scanner(System.in);

int a=sc.nextInt();

if(a<18)

{

throw new MyException("please enter valid age");

}

else

{

System.out.println("u are eligible");

}

}

}



OUTPUT:


Microsoft Windows [Version 10.0.22000.2538]

(c) Microsoft Corporation. All rights reserved.


C:\Users\admin>cd/


C:\>cd chaitanya

C:\chaitanya>javac ExampleExc.java


C:\chaitanya>java ExampleExc

enter age

30

u are eligible


C:\chaitanya>java ExampleExc

enter age

16

Exception in thread "main" MyException: please enter valid age

        at ExampleExc.main(ExampleExc.java:20)


C:\chaitanya>








Program 9 : WAP to create Frame containing all the awt components.


import java.awt.*;


public class AWTComponents {

    public static void main(String[] args) {

        Frame frame = new Frame("Simple AWT Components Frame");


        Label label = new Label("Enter Text:");

        TextField textField = new TextField(20);

        Button button = new Button("Click Me");

        Checkbox checkbox = new Checkbox("Check Me");

        Choice choice = new Choice();

        choice.add("Option 1");

        choice.add("Option 2");

        choice.add("Option 3");

        List list = new List();

        list.add("Item 1");

        list.add("Item 2");

        list.add("Item 3");


        frame.setLayout(new FlowLayout());


        frame.add(label);

        frame.add(textField);

        frame.add(button);

        frame.add(checkbox);

        frame.add(choice);

        frame.add(list);


        frame.setSize(300, 200);

        frame.setVisible(true);


    }

}


OUTPUT:










Program: 10

import java.awt.*;


public class LoginWindow {

    public static void main(String[] args) {

        Frame frame = new Frame("Login Window");


        Label usernameLabel = new Label("Username:");

        Label passwordLabel = new Label("Password:");

        TextField usernameField = new TextField();

        TextField passwordField = new TextField();

        Button loginButton = new Button("Login");


        passwordField.setEchoChar('*');


        usernameLabel.setBounds(50, 50, 80, 25);

        passwordLabel.setBounds(50, 80, 80, 25);

        usernameField.setBounds(150, 50, 150, 25);

        passwordField.setBounds(150, 80, 150, 25);

        loginButton.setBounds(120, 120, 80, 25);


        frame.add(usernameLabel);

        frame.add(passwordLabel);

        frame.add(usernameField);

        frame.add(passwordField);

        frame.add(loginButton);


        frame.setLayout(null);


        frame.setSize(350, 200);

        frame.setVisible(true);

    }

}



OUTPUT:










Program 11 : WAP to implement Mouse Event.


import java.awt.*;

import java.awt.event.*;


public class MouseEventEx extends Frame implements MouseListener {

    private Label label;


    public MouseEventEx() {

        label = new Label("Click here");

        label.setBounds(100, 100, 100, 30);

        label.addMouseListener(this);


        add(label);

        setSize(300, 200);

        setLayout(null);

        setVisible(true);

    }


    public void mouseClicked(MouseEvent e) {

        label.setText("Clicked");

    }


    public void mouseEntered(MouseEvent e) {

        label.setText("Entered");

    }


    public void mouseExited(MouseEvent e) {

        label.setText("Exited");

    }


    public void mousePressed(MouseEvent e) {

        label.setText("Pressed");

    }


    public void mouseReleased(MouseEvent e) {

        label.setText("Released");

    }


    public static void main(String[] args) {

        new MouseEventEx();

    }

}


OUTPUT:


Program 12 : WAP to implement Key Event.


import java.awt.*;

import java.awt.event.*;


public class KeyEventEx extends Frame implements KeyListener {

private TextField textField;


public KeyEventEx() {

textField = new TextField();

textField.setBounds(20, 40, 200, 30);

textField.addKeyListener(this);


add(textField);

setSize(300, 200);

setLayout(null);

setVisible(true);

}


public void keyTyped(KeyEvent e) {

char c = e.getKeyChar();

textField.setText("Key Typed: " + c);

}


public void keyPressed(KeyEvent e) {

int keyCode = e.getKeyCode();

textField.setText("Key Pressed: " + KeyEvent.getKeyText(keyCode));

}


public void keyReleased(KeyEvent e) {

textField.setText("Key Released");

}


public static void main(String[] args) {

new KeyEventEx();

}

}



OUTPUT:








Program 13 : WAP to implement Window Event using Adapter class.


import java.awt.*;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;


public class WindowEventEx extends Frame {

public WindowEventEx() {

setSize(300, 200);

setLayout(null);

setVisible(true);


addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent we) {

System.exit(0);

}


public void windowOpened(WindowEvent we) {

System.out.println("Window Opened");

}


public void windowActivated(WindowEvent we) {

System.out.println("Window Activated");

}


public void windowDeactivated(WindowEvent we) {

System.out.println("Window Deactivated");

}

});

}


public static void main(String[] args) {

new WindowEventEx();

}

}





OUTPUT:










Comments

Popular posts from this blog

DSA Practical Exam pdf - 17 jan 2024