Q1.
Write a program o create a calculator that perform all the
arithmetic and bitwise operation on the two numbers using switch
case.
import [Link];
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
[Link]("\nChoose operation:");
[Link]("1 : Addition (+)");
[Link]("2 : Subtraction (-)");
[Link]("3 : Multiplication (*)");
[Link]("4 : Division (/)");
[Link]("5 : Modulus (%)");
[Link]("6 : Bitwise AND (&)");
[Link]("7 : Bitwise OR (|)");
[Link]("8 : Bitwise XOR (^)");
[Link]("9 : Left Shift (a << b)");
[Link]("10: Right Shift (a >> b)");
[Link]("Enter your choice (1-10): ");
int choice = [Link]();
switch (choice) {
case 1: [Link]("Result: " + (a + b)); break;
case 2: [Link]("Result: " + (a - b)); break;
case 3: [Link]("Result: " + (a * b)); break;
case 4:
if (b != 0) [Link]("Result: " + (a / b));
else [Link]("Division by zero error!");
break;
case 5:
if (b != 0) [Link]("Result: " + (a % b));
else [Link]("Modulus by zero error!");
break;
case 6: [Link]("Result: " + (a & b)); break;
case 7: [Link]("Result: " + (a | b)); break;
case 8: [Link]("Result: " + (a ^ b)); break;
case 9: [Link]("Result: " + (a << b)); break;
case 10:[Link]("Result: " + (a >> b)); break;
default: [Link]("Invalid choice!");
}
[Link]();
}
}
Q2. Write a program to arrange three numbers in descending order.
import [Link];
public class DescendingOrder {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
[Link]("Enter third number: ");
int c = [Link]();
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; }
[Link]("Numbers in descending order:");
[Link](a + " " + b + " " + c);
[Link]();
}
}
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) {
[Link](n + " ");
}
}
}
}
Q4. Write a program to create static member and static method in class.
class Demo {
static int count = 0;
static void showCount() {
[Link]("Current count: " + count);
}
void increment() {
count++;
}
}
public class StaticExample {
public static void main(String[] args) {
[Link]();
Demo obj1 = new Demo();
Demo obj2 = new Demo();
[Link]();
[Link]();
[Link]();
}
}
Q5. Write a program for overriding of methods in classes.
class Parent {
void display() {
[Link]("This is the parent class method");
}
}
class Child extends Parent {
@Override
void display() {
[Link]("This is the child class method");
}
}
public class OverridingExample {
public static void main(String[] args) {
Parent p = new Parent();
[Link]();
Child c = new Child();
[Link]();
Parent pc = new Child();
[Link]();
}
}
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];
}
}
}
[Link]("Resultant matrix:");
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
[Link](result[i][j] + " ");
}
[Link]();
}
}
}
Q7. Write a program to find string palindrome or not.
import [Link];
public class PalindromeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String reversed = "";
for (int i = [Link]() - 1; i >= 0; i--) {
reversed += [Link](i);
}
if ([Link](reversed)) {
[Link]("The string is a palindrome.");
} else {
[Link]("The string is not a palindrome.");
}
[Link]();
}
}
Q8. Write a program to implement interface in java.
interface Vehicle {
void start();
void stop();
}
class Car implements Vehicle {
@Override
public void start() {
[Link]("Car starts");
}
@Override
public void stop() {
[Link]("Car stops");
}
}
public class InterfaceExample {
public static void main(String[] args) {
Car myCar = new Car();
[Link]();
[Link]();
}
}
Q9. Write a program to create a package and import the classes from
package in java.
--Package file~ [Link]--
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~ [Link]--
import [Link];
public class Main {
public static void main(String[] args) {
[Link]("Is 2 prime? " + [Link](2));
[Link]("Is 9 prime? " + [Link](9));
[Link]("Is 13 prime? " + [Link](13));
}
}
Q10. Write a program to create custom exception by extending the
built-in exception class.
import [Link];
class MyException extends Exception {
MyException(String message) {
super(message);
}
}
public class CustomExceptionExample {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a positive number: ");
int num = [Link]();
try {
if (num < 0) {
throw new MyException("Negative numbers are not allowed!");
} else {
[Link]("You entered: " + num);
}
} catch (MyException e) {
[Link]("Caught exception: " + [Link]());
}
[Link]();
}
}
Q11. Write a program to copy bytes from one file to another file.
import [Link].*;
public class CopyBytes {
public static void main(String[] args) {
try {
FileInputStream in = new FileInputStream("[Link]");
FileOutputStream out = new FileOutputStream("[Link]");
int b;
while ((b = [Link]()) != -1) {
[Link](b);
}
[Link]();
[Link]();
[Link]("File copied successfully!");
} catch (IOException e) {
[Link]("Error: " + [Link]());
}
}
}
Q12. Write a program to create threads using thread class.
class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
[Link]("Thread running: " + i);
}
}
}
public class ThreadExample {
public static void main(String[] args) {
MyThread t1 = new MyThread();
[Link]();
for (int i = 1; i <= 5; i++) {
[Link]("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++) {
[Link](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");
[Link](1);
[Link](5);
[Link](10);
[Link]();
[Link]();
[Link]();
}
}
Q14. Write a program to create login form.
import [Link].*;
import [Link].*;
import [Link].*;
public class loginform {
JFrame frame;
JTextField usernameField;
JPasswordField passwordField;
public loginform() {
frame = new JFrame("Login Form");
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(3, 2, 5, 5));
[Link](new JLabel("Username:"));
usernameField = new JTextField(15);
[Link](usernameField);
[Link](new JLabel("Password:"));
passwordField = new JPasswordField(15);
[Link](passwordField);
JButton loginButton = new JButton("Login");
[Link](new JLabel());
[Link](loginButton);
[Link](panel, [Link]);
[Link](e -> {
String username = [Link]();
String password = new String([Link]());
[Link](frame, "Username: " + username +
"\nPassword: " + password);
});
[Link](true);
}
public static void main(String[] args) {
new loginform();
}}
Q15. Write a program to create a notepad text editor.
import [Link].*;
import [Link].*;
import [Link].*;
public class Notepad {
JFrame frame;
JTextArea textArea;
public Notepad() {
frame = new JFrame("Notepad");
textArea = new JTextArea(20, 40);
[Link](new JScrollPane(textArea), [Link]);
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem saveItem = new JMenuItem("Save");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
[Link](saveItem);
[Link](fileMenu);
[Link](menuBar);
[Link]();
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new notepad();
}
}