0% found this document useful (0 votes)
0 views33 pages

Java-PF

Uploaded by

raaz ftb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views33 pages

Java-PF

Uploaded by

raaz ftb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Q1.

Write a program o create a calculator that perform all the


arithmetic and bitwise operation on the two numbers using switch
case.

import java.util.Scanner;

public class Calculator {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

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


int a = sc.nextInt();

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


int b = sc.nextInt();

System.out.println("\nChoose operation:");
System.out.println("1 : Addition (+)");
System.out.println("2 : Subtraction (-)");
System.out.println("3 : Multiplication (*)");
System.out.println("4 : Division (/)");
System.out.println("5 : Modulus (%)");
System.out.println("6 : Bitwise AND (&)");
System.out.println("7 : Bitwise OR (|)");
System.out.println("8 : Bitwise XOR (^)");
System.out.println("9 : Left Shift (a << b)");
System.out.println("10: Right Shift (a >> b)");

System.out.print("Enter your choice (1-10): ");


int choice = sc.nextInt();

switch (choice) {
case 1: System.out.println("Result: " + (a + b)); break;
case 2: System.out.println("Result: " + (a - b)); break;
case 3: System.out.println("Result: " + (a * b)); break;
case 4:
if (b != 0) System.out.println("Result: " + (a / b));
else System.out.println("Division by zero error!");
break;
case 5:
if (b != 0) System.out.println("Result: " + (a % b));
else System.out.println("Modulus by zero error!");
break;
case 6: System.out.println("Result: " + (a & b)); break;
case 7: System.out.println("Result: " + (a | b)); break;
case 8: System.out.println("Result: " + (a ^ b)); break;
case 9: System.out.println("Result: " + (a << b)); break;
case 10:System.out.println("Result: " + (a >> b)); break;
default: System.out.println("Invalid choice!");
}

sc.close();
}
}
Q2. Write a program to arrange three numbers in descending order.

import java.util.Scanner;

public class DescendingOrder {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

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


int a = sc.nextInt();

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


int b = sc.nextInt();

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


int c = sc.nextInt();

int temp;

if (a < b) { temp = a; a = b; b = temp; }


if (a < c) { temp = a; a = c; c = temp; }
if (b < c) { temp = b; b = c; c = temp; }

System.out.println("Numbers in descending order:");


System.out.println(a + " " + b + " " + c);

sc.close();
}
}
Q3. Write a program to find prime numbers between 1 and 100.

public class Primes {


public static void main(String[] args) {
for (int n = 2; n <= 100; n++) {
int count = 0;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
count++;
break;
}
}
if (count == 0) {
System.out.print(n + " ");
}
}
}
}
Q4. Write a program to create static member and static method in class.

class Demo {
static int count = 0;

static void showCount() {


System.out.println("Current count: " + count);
}

void increment() {
count++;
}
}

public class StaticExample {


public static void main(String[] args) {
Demo.showCount();

Demo obj1 = new Demo();


Demo obj2 = new Demo();

obj1.increment();
obj2.increment();

Demo.showCount();
}
}
Q5. Write a program for overriding of methods in classes.

class Parent {
void display() {
System.out.println("This is the parent class method");
}
}

class Child extends Parent {


@Override
void display() {
System.out.println("This is the child class method");
}
}

public class OverridingExample {


public static void main(String[] args) {
Parent p = new Parent();
p.display();

Child c = new Child();


c.display();

Parent pc = new Child();


pc.display();
}
}
Q6. Write a program to create matrix multiplication using array.

public class MatrixMultiplication {


public static void main(String[] args) {
int[][] mat1 = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int[][] mat2 = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};
int[][] result = new int[3][3];
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
for(int k=0; k<3; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
System.out.println("Resultant matrix:");
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
}
Q7. Write a program to find string palindrome or not.

import java.util.Scanner;

public class PalindromeCheck {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

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


String str = sc.nextLine();

String reversed = "";


for (int i = str.length() - 1; i >= 0; i--) {
reversed += str.charAt(i);
}

if (str.equals(reversed)) {
System.out.println("The string is a palindrome.");
} else {
System.out.println("The string is not a palindrome.");
}

sc.close();
}
}
Q8. Write a program to implement interface in java.

interface Vehicle {
void start();
void stop();
}

class Car implements Vehicle {


@Override
public void start() {
System.out.println("Car starts");
}

@Override
public void stop() {
System.out.println("Car stops");
}
}

public class InterfaceExample {


public static void main(String[] args) {
Car myCar = new Car();
myCar.start();
myCar.stop();
}
}
Q9. Write a program to create a package and import the classes from
package in java.

--Package file~ Prime.java--

package mypack;

public class Prime {


public static boolean check(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i < n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}

--Main file~ Main.java--

import mypack.Prime;

public class Main {


public static void main(String[] args) {
System.out.println("Is 2 prime? " + Prime.check(2));
System.out.println("Is 9 prime? " + Prime.check(9));
System.out.println("Is 13 prime? " + Prime.check(13));
}
}
Q10. Write a program to create custom exception by extending the
built-in exception class.

import java.util.Scanner;

class MyException extends Exception {


MyException(String message) {
super(message);
}
}

public class CustomExceptionExample {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a positive number: ");
int num = sc.nextInt();

try {
if (num < 0) {
throw new MyException("Negative numbers are not allowed!");
} else {
System.out.println("You entered: " + num);
}
} catch (MyException e) {
System.out.println("Caught exception: " + e.getMessage());
}

sc.close();
}
}
Q11. Write a program to copy bytes from one file to another file.

import java.io.*;

public class CopyBytes {


public static void main(String[] args) {
try {
FileInputStream in = new FileInputStream("source.txt");
FileOutputStream out = new FileOutputStream("destination.txt");

int b;
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
out.close();
System.out.println("File copied successfully!");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Q12. Write a program to create threads using thread class.

class MyThread extends Thread {


public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Thread running: " + i);
}
}
}

public class ThreadExample {


public static void main(String[] args) {
MyThread t1 = new MyThread();
t1.start();

for (int i = 1; i <= 5; i++) {


System.out.println("Main thread: " + i);
}
}
}
Q13. Write a program to use priority in threads.

class MyThread extends Thread {


public MyThread(String name) {
super(name);
}

public void run() {


for(int i = 1; i <= 5; i++) {
System.out.println(getName() + " is running, iteration: " + i);
}
}
}

public class ThreadPriorityDemo {


public static void main(String[] args) {
MyThread t1 = new MyThread("Thread1");
MyThread t2 = new MyThread("Thread2");
MyThread t3 = new MyThread("Thread3");

t1.setPriority(1);
t2.setPriority(5);
t3.setPriority(10);

t1.start();
t2.start();
t3.start();
}
}
Q14. Write a program to create login form.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class loginform {


JFrame frame;
JTextField usernameField;
JPasswordField passwordField;
public loginform() {
frame = new JFrame("Login Form");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(3, 2, 5, 5));
panel.add(new JLabel("Username:"));
usernameField = new JTextField(15);
panel.add(usernameField);
panel.add(new JLabel("Password:"));
passwordField = new JPasswordField(15);
panel.add(passwordField);
JButton loginButton = new JButton("Login");
panel.add(new JLabel());
panel.add(loginButton);
frame.add(panel, BorderLayout.CENTER);
loginButton.addActionListener(e -> {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
JOptionPane.showMessageDialog(frame, "Username: " + username +
"\nPassword: " + password);
});
frame.setVisible(true);
}
public static void main(String[] args) {
new loginform();
}}
Q15. Write a program to create a notepad text editor.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Notepad {


JFrame frame;
JTextArea textArea;

public Notepad() {
frame = new JFrame("Notepad");
textArea = new JTextArea(20, 40);
frame.add(new JScrollPane(textArea), BorderLayout.CENTER);

JMenuBar menuBar = new JMenuBar();


JMenu fileMenu = new JMenu("File");

JMenuItem saveItem = new JMenuItem("Save");


saveItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
fileMenu.add(saveItem);
menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);

frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new notepad();
}
}

You might also like