PROGRAMMING
IN
JAVA
LAB MANUAL
1) Write a program to perform multiplication in java
import java.util.Scanner;
public class MultiplyExample3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number = ");
double myDouble1 = scanner.nextDouble();
System.out.print("Enter second number = ");
double myDouble2 = scanner.nextDouble();
scanner.close();
double result = myDouble1*myDouble2;
System.out.println(myDouble1 + "*" + myDouble2 + " = " + result);
}
}
Here is the result:
Enter first number = 5
Enter second number = 12
5.0*12.0 = 60.0
2) Write a program to compute the area of circle in java
import java.util.Scanner;
public class CircleArea {
public static void main(String[] args) {
// Create a Scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Ask the user for the radius of the circle
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();
// Calculate the area of the circle
double area = Math.PI * Math.pow(radius, 2);
// Display the area
System.out.println("The area of the circle is: " + area);
// Close the scanner object
scanner.close();
}
}
Here is the result:
Enter the radius of the circle: 5
The area of the circle is: 78.53981633974483
3) Write a program to print pyramid star pattern in java
import java.util.Scanner;
public class PyramidStarPattern {
public static void main(String[] args) {
// Create a Scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Ask the user for the number of rows for the pyramid
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
// Loop through each row
for (int i = 1; i <= rows; i++) {
// Print spaces before the stars in each row
for (int j = i; j < rows; j++) {
System.out.print(" ");
}
// Print stars for the pyramid pattern
for (int k = 1; k <= (2 * i - 1); k++) {
System.out.print("*");
}
// Move to the next line after printing each row
System.out.println();
}
// Close the scanner object
scanner.close();
}
}
Here is the result:
*
***
*****
*******
*********
4) Write a program in java to implement the classes and object
// Define the Car class
class Car {
// Instance variables (fields)
String make;
String model;
int year;
// Constructor to initialize the Car object
public Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
// Method to display car details
public void displayCarDetails() {
System.out.println("Car Details:");
System.out.println("Make: " + make);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
}
}
public class CarDemo {
public static void main(String[] args) {
// Creating an object (instance) of the Car class
Car myCar = new Car("Toyota", "Corolla", 2020);
// Calling the method to display car details
myCar.displayCarDetails();
}
}
Here is the result:
Car Details:
Make: Toyota
Model: Corolla
Year: 2020
5) Write a program in java to implement the method overloading
public class MethodOverloadingExample {
// Method to add two integers
public int add(int a, int b) {
return a + b;
}
// Overloaded method to add three integers
public int add(int a, int b, int c) {
return a + b + c;
}
// Overloaded method to add two double numbers
public double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
// Creating an object of the MethodOverloadingExample class
MethodOverloadingExample obj = new MethodOverloadingExample();
// Calling the add method with two integers
System.out.println("Sum of 5 and 10 (int): " + obj.add(5, 10));
// Calling the add method with three integers
System.out.println("Sum of 5, 10, and 15 (int): " + obj.add(5, 10, 15));
// Calling the add method with two double numbers
System.out.println("Sum of 5.5 and 10.5 (double): " + obj.add(5.5, 10.5));
}
}
Here is the result:
Sum of 5 and 10 (int): 15
Sum of 5, 10, and 15 (int): 30
Sum of 5.5 and 10.5 (double): 16.0
6) Write a program in java to implement inheritance
// Parent class (Base class)
class Animal {
// Field of the parent class
String name;
// Constructor of the parent class
public Animal(String name) {
this.name = name;
}
// Method in the parent class
public void makeSound() {
System.out.println(name + " makes a sound.");
}
}
// Child class (Derived class) that inherits from Animal
class Dog extends Animal {
// Constructor of the child class
public Dog(String name) {
// Call the constructor of the parent class
super(name);
}
// Method overriding the parent class method
@Override
public void makeSound() {
System.out.println(name + " barks.");
}
// A method specific to the Dog class
public void fetch() {
System.out.println(name + " is fetching the ball.");
}
}
// Another child class (Derived class)
class Cat extends Animal {
// Constructor of the child class
public Cat(String name) {
// Call the constructor of the parent class
super(name);
}
// Method overriding the parent class method
@Override
public void makeSound() {
System.out.println(name + " meows.");
}
// A method specific to the Cat class
public void scratch() {
System.out.println(name + " is scratching the post.");
}
}
public class InheritanceExample {
public static void main(String[] args) {
// Create an object of the Dog class
Dog myDog = new Dog("Buddy");
myDog.makeSound(); // Calling the overridden method
myDog.fetch(); // Calling a method specific to Dog
// Create an object of the Cat class
Cat myCat = new Cat("Whiskers");
myCat.makeSound(); // Calling the overridden method
myCat.scratch(); // Calling a method specific to Cat
}
}
Here is the result:
Buddy barks.
Buddy is fetching the ball.
Whiskers meows.
Whiskers is scratching the post.
7) Write a program in java to implement interface
// Define the interface
interface Animal {
// Abstract method (does not have a body)
void sound();
// Default method (with implementation)
default void sleep() {
System.out.println("This animal is sleeping.");
}
// Static method (with implementation)
static void description() {
System.out.println("Animals are living creatures.");
}
}
// Implementing the interface in a class
class Dog implements Animal {
// Implementing the abstract method 'sound'
public void sound() {
System.out.println("The dog barks.");
}
}
class Cat implements Animal {
// Implementing the abstract method 'sound'
public void sound() {
System.out.println("The cat meows.");
}
}
public class InterfaceExample {
public static void main(String[] args) {
// Creating objects of Dog and Cat class
Dog dog = new Dog();
Cat cat = new Cat();
// Calling the implemented method from Dog class
dog.sound(); // Output: The dog barks.
dog.sleep(); // Output: This animal is sleeping.
// Calling the implemented method from Cat class
cat.sound(); // Output: The cat meows.
cat.sleep(); // Output: This animal is sleeping.
// Calling the static method from the Animal interface
Animal.description(); // Output: Animals are living creatures.
}
}
Here is the result:
The dog barks.
This animal is sleeping.
The cat meows.
This animal is sleeping.
Animals are living creatures.
8) Write a program in java to implement multi-threading
// Creating a thread by extending the Thread class
class MyThread extends Thread {
// Overriding the run() method to define the task
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getId() + " Value " + i);
}
}
}
public class MultiThreadingExample {
public static void main(String[] args) {
// Creating instances of MyThread class
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
// Starting the threads
t1.start(); // Executes the run() method
t2.start(); // Executes the run() method
}
}
Here is the result:
1 Value 1
2 Value 1
1 Value 2
2 Value 2
1 Value 3
2 Value 3
1 Value 4
2 Value 4
1 Value 5
2 Value 5
9) Write a program in java to implement packages
import java.util.Scanner; // import the Scanner class
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
String userName;
// Enter username and press Enter
System.out.println("Enter username");
userName = myObj.nextLine();
System.out.println("Username is: " + userName);
}
}
Here is the result:
Enter username
Java
Username is ; java
10) Write a program in java to implement applets
import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet
int height, width;
public void init()
height = getSize().height;
width = getSize().width;
setName("MyApplet");
public void paint(Graphics g)
g.drawRoundRect(10, 30, 120, 120, 2, 3);
}
Here is the result: