0% found this document useful (0 votes)
10 views

Experiment Java

Uploaded by

sairajpatil810
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Experiment Java

Uploaded by

sairajpatil810
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Experiment No 1

Java Program to Accept Five Integers and Print Them.

import java.util.Scanner;
public class PrintIntegers {

public static void main(String[] args) {

// Create a Scanner object to read input from the user

Scanner scanner = new Scanner(System.in);

// Declare an array to hold five integers

int[] numbers = new int[5];

// Prompt the user to enter five integers

System.out.println("Please enter 5 integers:");

// Read the five integers from the user

for (int i = 0; i < 5; i++) {

System.out.print("Enter integer " + (i + 1) + ": ");

numbers[i] = scanner.nextInt();
}

System.out.println("\nYou entered the following integers:");

for (int i = 0; i < 5; i++) {


System.out.println("Integer " + (i + 1) + ": " + numbers[i]);

scanner.close();

}
OUTPUT
Please enter 5 integers:

Enter integer 1: 10

Enter integer 2: 20

Enter integer 3: 30

Enter integer 4: 40

Enter integer 5: 50

You entered the following integers:


Integer 1: 10

Integer 2: 20
Integer 3: 30

Integer 4: 40

Integer 5: 50
Experiment 2

Java Program to Add Two 3x3 Matrices

import java.util.Scanner;

public class MatrixAddition {

public static void main(String[] args) {

// Create a Scanner object to read input from the user

Scanner scanner = new Scanner(System.in);

// Declare two 3x3 matrices and the result matrix

int[][] matrix1 = new int[3][3];

int[][] matrix2 = new int[3][3];

int[][] result = new int[3][3];

// Input for matrix 1

System.out.println("Enter elements of the first 3x3 matrix:");

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {

System.out.print("Enter element at [" + i + "][" + j + "]: ");

matrix1[i][j] = scanner.nextInt();
}

// Input for matrix 2

System.out.println("Enter elements of the second 3x3 matrix:");

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
System.out.print("Enter element at [" + i + "][" + j + "]: ");

matrix2[i][j] = scanner.nextInt();

// Matrix addition

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {


result[i][j] = matrix1[i][j] + matrix2[i][j];

// Print the resulting matrix

System.out.println("\nResultant Matrix after addition:");

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

System.out.print(result[i][j] + " ");

System.out.println(); // For new line after each row

// Close the scanner to prevent resource leak

scanner.close();
}

}
OUTPUT
Enter elements of the first 3x3 matrix:

Enter element at [0][0]: 1

Enter element at [0][1]: 2

Enter element at [0][2]: 3

Enter element at [1][0]: 4

Enter element at [1][1]: 5

Enter element at [1][2]: 6


Enter element at [2][0]: 7

Enter element at [2][1]: 8

Enter element at [2][2]: 9

Enter elements of the second 3x3 matrix:

Enter element at [0][0]: 9

Enter element at [0][1]: 8

Enter element at [0][2]: 7

Enter element at [1][0]: 6

Enter element at [1][1]: 5

Enter element at [1][2]: 4

Enter element at [2][0]: 3

Enter element at [2][1]: 2


Enter element at [2][2]: 1

Resultant Matrix after addition:

10 10 10

10 10 10

10 10 10
Experiment No 3

Java Program to Multiply Two 3x3 Matrices

import java.util.Scanner;

public class MatrixMultiplication {

public static void main(String[] args) {

// Create a Scanner object to read input from the user

Scanner scanner = new Scanner(System.in);

// Declare two 3x3 matrices and the result matrix

int[][] matrix1 = new int[3][3];

int[][] matrix2 = new int[3][3];

int[][] result = new int[3][3];

// Input for matrix 1

System.out.println("Enter elements of the first 3x3 matrix:");

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

System.out.print("Enter element at [" + i + "][" + j + "]: ");

matrix1[i][j] = scanner.nextInt();

// Input for matrix 2

System.out.println("Enter elements of the second 3x3 matrix:");

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

System.out.print("Enter element at [" + i + "][" + j + "]: ");


matrix2[i][j] = scanner.nextInt();
}

// Matrix multiplication

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

result[i][j] = 0; // Initialize the result cell

for (int k = 0; k < 3; k++) {


result[i][j] += matrix1[i][k] * matrix2[k][j];

// Print the resulting matrix

System.out.println("\nResultant Matrix after multiplication:");

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

System.out.print(result[i][j] + " ");

System.out.println(); // For new line after each row

// Close the scanner to prevent resource leak


scanner.close();

}
OUTPUT
Enter elements of the first 3x3 matrix:

Enter element at [0][0]: 1

Enter element at [0][1]: 2

Enter element at [0][2]: 3

Enter element at [1][0]: 4

Enter element at [1][1]: 5

Enter element at [1][2]: 6


Enter element at [2][0]: 7

Enter element at [2][1]: 8

Enter element at [2][2]: 9

Enter elements of the second 3x3 matrix:

Enter element at [0][0]: 9

Enter element at [0][1]: 8

Enter element at [0][2]: 7

Enter element at [1][0]: 6

Enter element at [1][1]: 5

Enter element at [1][2]: 4

Enter element at [2][0]: 3

Enter element at [2][1]: 2


Enter element at [2][2]: 1

Resultant Matrix after multiplication:

30 24 18

84 69 54

138 114 90
Experiment No 4
Java program that uses a switch-case construct to display the day of the week based on
a number input

import java.util.Scanner;

public class SwitchCaseExample {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Prompt the user for input

System.out.println("Enter a number (1-7) to get the day of the week:");

int day = scanner.nextInt();

// Use switch-case to determine the day of the week


String dayName;

switch (day) {

case 1:

dayName = "Monday";

break;

case 2:

dayName = "Tuesday";

break;

case 3:

dayName = "Wednesday";

break;

case 4:

dayName = "Thursday";

break;

case 5:
dayName = "Friday";
break;

case 6:

dayName = "Saturday";

break;

case 7:

dayName = "Sunday";

break;

default:
dayName = "Invalid input! Please enter a number between 1 and 7.";

break;

// Display the result

System.out.println("Day: " + dayName);

OUTPUT
Enter a number (1-7) to get the day of the week:

Day: Wednesday

Enter a number (1-7) to get the day of the week:

10

Day: Invalid input! Please enter a number between 1 and 7.


Experiment No 5
Java program that demonstrates method overloading, where multiple methods have the same
name but different parameter lists.

public class MethodOverloadingExample {

// Method to calculate the area of a rectangle

public double calculateArea(double length, double width) {

return length * width;

// Overloaded method to calculate the area of a square

public double calculateArea(double side) {

return side * side;

// Overloaded method to calculate the area of a circle

public double calculateArea(double radius, boolean isCircle) {

if (isCircle) {
return Math.PI * radius * radius; // πr²

return 0; // Invalid call if isCircle is false


}

public static void main(String[] args) {

MethodOverloadingExample example = new MethodOverloadingExample();

// Calculating areas
double rectangleArea = example.calculateArea(5.0, 3.0); // Rectangle
double squareArea = example.calculateArea(4.0); // Square

double circleArea = example.calculateArea(3.0, true); // Circle

// Printing the results

System.out.println("Area of the rectangle: " + rectangleArea);

System.out.println("Area of the square: " + squareArea);

System.out.println("Area of the circle: " + circleArea);

}
}

OUTPUT
Area of the rectangle: 15.0

Area of the square: 16.0

Area of the circle: 28.274333882308138


Experiment No 6
Java program that demonstrates constructor overloading, where a class has multiple
constructors with different parameter lists to initialize objects in various ways

class Student {

private String name;

private int age;

private String course;

// Constructor with no parameters (default constructor)

public Student() {
this.name = "Unknown";

this.age = 0;

this.course = "Not Enrolled";

// Constructor with two parameters

public Student(String name, int age) {

this.name = name;
this.age = age;

this.course = "Not Enrolled";

// Constructor with three parameters

public Student(String name, int age, String course) {

this.name = name;

this.age = age;

this.course = course;
}
// Method to display student details

public void displayDetails() {

System.out.println("Name: " + name);

System.out.println("Age: " + age);

System.out.println("Course: " + course);

System.out.println();

}
}

public class ConstructorOverloadingExample {

public static void main(String[] args) {

// Using default constructor

Student student1 = new Student();

System.out.println("Student 1 Details:");

student1.displayDetails();

// Using constructor with two parameters

Student student2 = new Student("Alice", 20);

System.out.println("Student 2 Details:");

student2.displayDetails();

// Using constructor with three parameters


Student student3 = new Student("Bob", 22, "Computer Science");

System.out.println("Student 3 Details:");

student3.displayDetails();

}
OUTPUT
Student 1 Details:

Name: Unknown

Age: 0

Course: Not Enrolled

Student 2 Details:

Name: Alice
Age: 20

Course: Not Enrolled

Student 3 Details:

Name: Bob

Age: 22

Course: Computer Science


Experiment No 7
Java program demonstrating single inheritance, where one class (child) inherits from
another class (parent).

// Parent class

class Animal {

String name;

// Method in the parent class

void eat() {

System.out.println(name + " is eating.");


}

void sleep() {

System.out.println(name + " is sleeping.");

// Child class that inherits from Animal


class Dog extends Animal {

String breed;

// Method in the child class

void bark() {

System.out.println(name + " is barking. Breed: " + breed);

// Main class
public class SingleInheritanceExample {

public static void main(String[] args) {

// Creating an object of the child class

Dog dog = new Dog();

// Setting properties inherited from the parent class

dog.name = "Buddy";

dog.breed = "Golden Retriever";

// Calling methods from the parent and child classes

dog.eat(); // Inherited from Animal

dog.sleep(); // Inherited from Animal

dog.bark(); // Defined in Dog

OUTPUT
Buddy is eating.

Buddy is sleeping.

Buddy is barking. Breed: Golden Retriever


Experiment No 8
Java program that demonstrates the use of the Super keyword.
// Parent class

class Animal {

String name = "Generic Animal";

Animal() {

System.out.println("Animal constructor is called");

} void sound() {
System.out.println("Animals make sound.");

class Dog extends Animal {

String name = "Dog";

Dog() {

super(); // Calls the constructor of the parent class

System.out.println("Dog constructor is called");

void sound() {

super.sound(); // Calls the method of the parent class

System.out.println(name + " barks.");

}
void displayNames() {

System.out.println("Child class name: " + name); // Access child class variable

System.out.println("Parent class name: " + super.name); // Access parent class variable

}
public class SuperKeywordExample {

public static void main(String[] args) {

// Create an object of the child class

Dog dog = new Dog();

// Call the overridden method

dog.sound();

// Display names using super keyword

dog.displayNames();

OUTPUT
Animal constructor is called

Dog constructor is called

Animals make sound.

Dog barks.

Child class name: Dog

Parent class name: Generic Animal


Experiment No 9
Java program demonstrating exception handling
import java.util.Scanner;

public class ExceptionHandlingExample {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

try {

System.out.println("Enter the numerator:");

int numerator = scanner.nextInt();


System.out.println("Enter the denominator:");

int denominator = scanner.nextInt();

int result = numerator / denominator;

System.out.println("Result: " + result);

} catch (ArithmeticException e) {

System.out.println("Error: Division by zero is not allowed.");

} catch (Exception e) {

System.out.println("Error: Invalid input. Please enter numeric values.");

} finally {

System.out.println("Execution of the program is complete.");

} scanner.close();

}
OUTPUT
Enter the numerator:

10

Enter the denominator:

Result: 5
Execution of the program is complete.

You might also like