Practical Lab for Java Program Questions
1. Question : Write a Java program to find the largest of three numbers.
Solution :
import [Link];
public class LargestOfThree {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter first number:");
int num1 = [Link]();
[Link]("Enter second number:");
int num2 = [Link]();
[Link]("Enter third number:");
int num3 = [Link]();
if (num1 >= num2 && num1 >= num3) {
[Link](num1 + " is the largest number.");
} else if (num2 >= num1 && num2 >= num3) {
[Link](num2 + " is the largest number.");
} else {
[Link](num3 + " is the largest number.");
}
[Link]();
}
}
2. Question: Write a Java program to calculate the factorial of a given
number.
Solution :
import [Link];
public class Factorial {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a non-negative integer:");
int number = [Link]();
long factorial = 1;
if (number < 0) {
[Link]("Factorial is not defined for negative
numbers.");
} else {
for (int i = 1; i <= number; i++) {
factorial *= i;
}
[Link]("Factorial of " + number + " is: " + factorial);
}
[Link]();
}
}
3. Question: Write a Java program to check if a number is prime or not.
Solution :
import [Link];
public class PrimeChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number:");
int num = [Link]();
boolean isPrime = true;
if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= [Link](num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) {
[Link](num + " is a prime number.");
} else {
[Link](num + " is not a prime number.");
}
[Link]();
}
}
4 Question: Write a program to demonstrate method overloading.
Solution:
public class MethodOverloadingDemo {
// Method to add two integers
public int add(int a, int b) {
return a + b;
}
// Method to add two doubles
public double add(double a, double b) {
return a + b;
}
// Method to concatenate two strings
public String add(String a, String b) {
return a + b;
}
public static void main(String[] args) {
MethodOverloadingDemo demo = new MethodOverloadingDemo();
[Link]("Sum of 10 and 20 (int): " + [Link](10, 20));
[Link]("Sum of 10.5 and 20.5 (double): " +
[Link](10.5, 20.5));
[Link]("Concatenation of 'Hello' and ' World' (String): " +
[Link]("Hello", " World"));
}
}
5 Question: Write a program to demonstrate single inheritance.
Solution :
// Superclass
class Animal {
void eat() {
[Link]("Animal eats food.");
}
}
// Subclass inheriting from Animal
class Dog extends Animal {
void bark() {
[Link]("Dog barks.");
}
}
public class SingleInheritanceDemo {
public static void main(String[] args) {
Dog myDog = new Dog();
[Link](); // Inherited method
[Link](); // Own method
}
}
6 Question: Write a Java program to reverse a string.
Solution :
import [Link];
public class StringReverse {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string to reverse:");
String original = [Link]();
String reversed = "";
for (int i = [Link]() - 1; i >= 0; i--) {
reversed += [Link](i);
}
[Link]("Original string: " + original);
[Link]("Reversed string: " + reversed);
[Link]();
}
}
7 Question: Write a program to handle exceptions using try-catch blocks.
Solution:
import [Link];
public class ExceptionHandlingDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter two numbers:");
int num1 = [Link]();
int num2 = [Link]();
try {
int result = num1 / num2;
[Link]("Result of division: " + result);
} catch (ArithmeticException e) {
// Catching the specific exception for division by zero
[Link]("Error: Cannot divide by zero.");
[Link]("Exception message: " + [Link]());
} finally {
// This block always executes, regardless of exception occurrence
[Link]("Finally block executed. Cleaning up
resources.");
[Link]();
}
}
}