Professional Documents
Culture Documents
Practicalwidjava
Practicalwidjava
Procedure:
1. Read an integer value from the user using the Scanner class.
2. Define a method to check if a number is prime.
3. Use a loop to generate numbers within the given range.
4. Check each number if it is prime using the method defined in step 2.
5. Print the prime numbers found.
Source Code:
import java.util.Scanner;
Input:
Enter the range (start and end):
10 20
OUTPUT
10 is a prime number
11 is a prime number
13 is a prime number
17 is a prime number
19 is a prime number
Procedure:
1. Read the number of elements (n) from the user.
2. Create an array to store the n numbers.
3. Read the n numbers from the user and store them in the array.
4. Iterate through the array to find the largest number.
5. Print the largest number.
Source Code:
import java.util.Scanner;
Procedure:
1. Create a class with private instance variables.
2. Implement getter methods to access these variables.
3. Implement setter methods to modify these variables.
4. Create an object of the class and demonstrate the use of getter and setter
methods.
Source Code:
public class Person {
private String name;
private int age;
OUTPUT:
Name: Alice
Age: 30
Procedure:
1. Create a class with a constructor.
2. Initialize values in the constructor.
3. Create an object of the class.
4. Print the values.
Source Code:
public class Person {
private String name;
private int age;
INPUT:
Output:
Name: John
Age: 25
Procedure:
1. Create a class with multiple constructors that take different parameters.
2. Create multiple methods within the class that are overloaded with different
parameter lists.
3. Create objects of the class using the different constructors.
4. Call the overloaded methods with different arguments.
5. Print the results.
Source Code:
public class Calculator {
private int num1;
private int num2;
// Constructor overloading
public Calculator() {
num1 = 0;
num2 = 0;
}
// Method overloading
public int add() {
return num1 + num2;
}
OUTPUT:
calc1.add(): 0
calc2.add(20): 30
calc3.add(2, 3): 7
Procedure:
1. Create a Shape class with an abstract area() method.
2. Create subclasses for rectangle, square, and circle.
3. Override the area() method in each subclass to calculate the area of the
respective shape.
4. Create objects of the subclasses and call the area() method to calculate their
areas.
Source Code:
// Abstract Shape class
abstract class Shape {
abstract double area();
}
// Rectangle class
class Rectangle extends Shape {
private double length;
private double width;
@Override
public double area() {
return length * width;
}
}
// Square class
class Square extends Shape {
private double side;
@Override
public double area() {
return side * side;
}
}
// Circle class
class Circle extends Shape {
private double radius;
@Override
public double area() {
return Math.PI * radius * radius;
}
}
Procedure:
1. Create an abstract class with abstract and non-abstract methods.
2. Create an interface with abstract methods.
3. Create a concrete class that extends the abstract class and implements the
interface.
4. Create an object of the concrete class and call the methods.
Source Code:
// Abstract class
abstract class Animal {
public abstract void makeSound();
// Interface
interface Flyable {
void fly();
}
// Concrete class
class Bird extends Animal implements Flyable {
@Override
public void makeSound() {
System.out.println("The bird chirps.");
}
@Override
public void fly() {
System.out.println("The bird is flying.");
}
}
Procedure:
1. Create a base class called Animal.
2. Create a derived class called Dog that inherits from Animal (simple inheritance).
3. Create another derived class called Poodle that inherits from Dog (multilevel
inheritance).
4. Create another derived class called Cat that also inherits
from Animal (hierarchical inheritance).
5. Create objects of the derived classes and call their methods.
Source Code:
// Base class
class Animal {
public void eat() {
System.out.println("The animal is eating.");
}
}
// Multilevel inheritance
Poodle poodle = new Poodle();
poodle.eat();
poodle.bark();
poodle.swim();
// Hierarchical inheritance
Cat cat = new Cat();
cat.eat();
cat.meow();
}
}
Output:
The animal is eating.
The dog barks.
The animal is eating.
The dog barks.
The poodle is swimming.
The animal is eating.
The cat meows.
9.A java program to implement multiple inheritances
using interface.
Aim:
The aim of this program is to show how multiple inheritance can be achieved in Java
using interfaces.
Procedure:
1. Create multiple interfaces with different methods.
2. Create a class that implements these interfaces.
3. Implement the methods defined in the interfaces in the class.
4. Create an object of the class and call the methods.
Source Code:
// Interface 1
interface Swim {
void swim();
}
// Interface 2
interface Fly {
void fly();
}
@Override
public void fly() {
System.out.println("The bird is flying.");
}
}
Output:
The bird is swimming.
The bird is flying.
Procedure:
1. Create a method that throws an exception.
2. Use try-catch blocks to handle the exception.
3. Use throw to explicitly throw an exception.
4. Use throws to declare that a method can throw an exception.
5. Use finally to execute code regardless of whether an exception occurs or
not.
Source Code:
import java.util.Scanner;
try {
System.out.print("Enter a number: ");
int num = scanner.nextInt();
int result = divide(num);
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
} catch (Exception e) {
System.out.println("General exception occurred.");
} finally {
System.out.println("Finally block executed.");
}
}
Input:
Enter a number: 0
Output:
Error: Cannot divide by zero.
Finally block executed.
Procedure:
1. Create a custom exception class that extends the Exception class.
2. Create a method that can throw the custom exception.
3. Use a try-catch block to handle the custom exception.
4. Demonstrate the usage of the custom exception.
Source Code:
// Custom exception class
class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}
public class CustomException {
public static void main(String[] args) {
try {
checkAge(15);
checkAge(-5);
} catch (InvalidAgeException e) {
System.out.println("Error: " + e.getMessage());
}
}
Output:
Age is valid: 15
Error: Age cannot be negative.
Procedure:
a) Extending the Thread class:
1. Create a class that extends the Thread class.
2. Override the run() method to define the task to be performed by the thread.
3. Create objects of the class and start the threads.
b) Implementing the Runnable interface:
1. Create a class that implements the Runnable interface.
2. Implement the run() method to define the task to be performed by the
thread.
3. Create a Thread object and pass the instance of the class that
implements Runnable.
4. Start the thread.
Source Code:
a) Extending the Thread class:
class MyThread extends Thread {
@Override
public void run() {
System.out.println("Thread " + Thread.currentThread().getId() + " is running.");
}
}
thread1.start();
thread2.start();
}
}
Output:
The output may vary as the threads are executed concurrently, but it will generally
look similar to the following:
Thread 12 is running.
Thread 13 is running.
Thread 14 is running.
Thread 15 is running.