Java Programs
1. Program in Java to design simple calculator for (+, -, *, and /) using switch
case.
Ans.
import [Link];
public class SimpleCalculator {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
[Link]("Enter first number: ");
double num1 = [Link]();
[Link]("Enter second number: ");
double num2 = [Link]();
[Link]("Enter an operator (+, -, *, /): ");
char operator = [Link]().charAt(0);
double result;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num2 != 0 ? num1 / num2 : [Link];
break;
default:
[Link]("Invalid operator");
return;
}
[Link]("Result: " + result);
}
}
2. Program in Java to design accounts class and two functions withdraw() and
deposit ().
Ans.
public class Account {
private double balance;
public Account(double initialBalance) {
balance = initialBalance > 0 ? initialBalance : 0;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
[Link]("Deposited: " + amount);
} else {
[Link]("Deposit amount must be positive");
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
[Link]("Withdrew: " + amount);
} else {
[Link]("Insufficient balance or invalid amount");
}
}
public void displayBalance() {
[Link]("Current Balance: " + balance);
}
public static void main(String[] args) {
Account account = new Account(500);
[Link]();
[Link](200);
[Link]();
[Link](100);
[Link]();
}
}
3. Program in Java to search a particular element in a one dimensional array.
Ans.
import [Link];
public class ArraySearch {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
int[] array = {10, 20, 30, 40, 50, 60};
[Link]("Enter element to search: ");
int searchElement = [Link]();
boolean found = false;
for (int i = 0; i < [Link]; i++) {
if (array[i] == searchElement) {
[Link]("Element found at index: " + i);
found = true;
break;
}
}
if (!found) {
[Link]("Element not found in the array");
}
}
}
4. Program in Java to the concept of polymorphism by designing functions to
sum different type of numbers.
Ans.
public class SumNumbers {
public int sum(int a, int b) {
return a + b;
}
public double sum(double a, double b) {
return a + b;
}
public int sum(int[] numbers) {
int total = 0;
for (int number : numbers) {
total += number;
}
return total;
}
public static void main(String[] args) {
SumNumbers calculator = new SumNumbers();
[Link]("Sum of two integers: " + [Link](5, 10));
[Link]("Sum of two doubles: " + [Link](5.5, 10.2));
[Link]("Sum of an array: " + [Link](new int[]{1, 2, 3, 4,
5}));
}
}
5. Program to show the concept of method overriding in Java.
Ans.
class Animal {
public void sound() {
[Link]("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
public void sound() {
[Link]("Dog barks");
}
}
class Cat extends Animal {
@Override
public void sound() {
[Link]("Cat meows");
}
}
public class MethodOverridingExample {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myDog = new Dog();
Animal myCat = new Cat();
[Link]();
[Link]();
[Link]();
}
}
6. Program in Java that import the user define package and access the
Member variable of classes that Contained by Package.
Ans.
Step 1: Create the Package and Class
1. Create a folder named mypackage.
2. Inside this folder, create a file called [Link].
Code:
package mypackage;
public class Person {
public String name;
public int age;
public Person(String name, int age) {
[Link] = name;
[Link] = age;
}
public void displayInfo() {
[Link]("Name: " + name + ", Age: " + age);
}
}
Step 2: Write the Main Program
Code:
import [Link];
public class Main {
public static void main(String[] args) {
Person person = new Person("Alice", 25);
[Link]("Accessing member variables:");
[Link]("Name: " + [Link]);
[Link]("Age: " + [Link]);
[Link]("Calling method to display info:");
[Link]();
}
}
7. Program in Java to handle the Exception using try and multiple catch block.
Ans.
import [Link];
public class ExceptionHandlingExample {
public static void main(String[] args) {
Scanner input = new Scanner([Link]);
try {
[Link]("Enter a number: ");
int number = [Link]();
[Link]("Enter a divisor: ");
int divisor = [Link]();
int result = number / divisor;
[Link]("Result: " + result);
int[] array = {1, 2, 3};
[Link]("Accessing element at index 5: " + array[5]);
}
catch (ArithmeticException e) {
[Link]("Error: Division by zero is not allowed.");
}
catch (ArrayIndexOutOfBoundsException e) {
[Link]("Error: Array index out of bounds.");
}
catch (Exception e) {
[Link]("An unexpected error occurred: " + [Link]());
}
}
}
8. Program in Java demonstrating text I/O and binary I/O.
Ans.
import [Link].*;
public class TextAndBinaryIOExample {
public static void main(String[] args) {
textIOExample();
binaryIOExample();
}
public static void textIOExample() {
try {
FileWriter textWriter = new FileWriter("[Link]");
[Link]("Hello, this is a text file.\n");
[Link]("This is the second line.");
[Link]();
FileReader textReader = new FileReader("[Link]");
BufferedReader bufferedReader = new BufferedReader(textReader);
String line;
while ((line = [Link]()) != null) {
[Link](line);
}
[Link]();
} catch (IOException e) {
[Link]("Text I/O Error: " + [Link]());
}
}
public static void binaryIOExample() {
try {
FileOutputStream binaryWriter = new FileOutputStream("[Link]");
DataOutputStream dataOutput = new DataOutputStream(binaryWriter);
[Link](123);
[Link](456.78);
[Link]("Hello in binary format");
[Link]();
FileInputStream binaryReader = new FileInputStream("[Link]");
DataInputStream dataInput = new DataInputStream(binaryReader);
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link]();
} catch (IOException e) {
[Link]("Binary I/O Error: " + [Link]());
}
}
}
9. Program in Java using Multi threading.
Ans.
class MyThread extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
[Link]([Link]().getId() + " Value: " + i);
}
}
}
public class MultiThreadingExample {
public static void main(String[] args) {
MyThread thread1 = new MyThread();
MyThread thread2 = new MyThread();
[Link]();
[Link]();
}
}
10. Program in Java demonstrating Applet.
Ans.
import [Link];
import [Link];
public class HelloWorldApplet extends Applet {
public void paint(Graphics g) {
[Link]("Hello, World!", 20, 20);
}
}
HTML to run the applet:
<html>
<body>
<applet code="[Link]" width="300"
height="200"></applet>
</body>
</html>