0% found this document useful (0 votes)
2 views58 pages

Javaaa

Java notes

Uploaded by

pcuserof2025
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)
2 views58 pages

Javaaa

Java notes

Uploaded by

pcuserof2025
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

Exp.

Name: Classes and Objects Date: 2026-03-

1 :oN egaP 512337429061 :DI


[Link]: 1
Implementation 01

Aim:
Write a Java program that defines a class Student with two instance variables, id and
name, and a method display() to print student details.

Read the Student ID and Name from the user, create a Student object named s1, and
display the details using the display() method.

Input Format:
• The first line contains an integer representing the Student ID.
• The second line contains a string representing the Student Name.

Output Format:
• Display the student’s details in the following format:
ID: <student_id>, Name: <student_name>

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


Source Code:
q86064/[Link]

2 :oN egaP 512337429061 :DI


/*package q86064;
import [Link];

class {

// Method to display student details

public class StudentDetails {

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Create Student object

[Link] = [Link]();
[Link]();
[Link] = [Link]();
// Write your code here...

[Link]();
}
}
*/

package q86064;
import [Link];

class Student {
int id;
String name;

void display() {
[Link]("ID: " + id + ", Name: " + name);
}
}
public class StudentDetails {

3 :oN egaP 512337429061 :DI


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

Student s1 = new Student();

[Link] = [Link]();
[Link]();
[Link] = [Link]();

[Link]();

[Link]();
}
}

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


Execution Results - All test cases have succeeded!
Test Case - 1

User Output
101
Alice
ID: 101, Name: Alice

Test Case - 2

User Output
102
Bob
ID: 102, Name: Bob
Exp. Name: Default and Parameterized Date: 2026-03-
[Link]: 2

4 :oN egaP 512337429061 :DI


Constructors 01

Aim:
Write a Java program to demonstrate default and parameterized constructors using a
Person class.
• The Person class should have two attributes: name (String) and age (int).
• The default constructor should initialize name as "John" and age as 30.
• The parameterized constructor should initialize name and age with values
provided by the user.
• The program should display the details of the person created by both
constructors.

Input Format:
• The first line of input is a string that represents the name of the person.
• The second line of input is an integer that represents the age of the person.

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


Output Format:
• The first line of output should print details of person using default constructor
in the following format:
Name: John, Age: 30
• The second line of the output should print details of person using
parameterized constructor in the following format:
Name: <name>, Age: <age>
Source Code:
q77383/[Link]

5 :oN egaP 512337429061 :DI


/*package q77383;
import [Link];

class Person {

// Default constructor with fixed values

// Parameterized constructor

void display() {
[Link]("Name: " + name + ", Age: " + age);

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


}
}

public class Demo {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

//write your code here..

[Link]();
}
}
*/
package q77383;
import [Link];

class Person {
String name;
int age;

// Default constructor with fixed values


Person() {
name = "John";
age = 30;

6 :oN egaP 512337429061 :DI


}

// Parameterized constructor
Person(String name, int age) {
[Link] = name;
[Link] = age;
}

void display() {
[Link]("Name: " + name + ", Age: " + age);
}
}

public class Demo {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


String name = [Link]();
int age = [Link]();

Person p1 = new Person();


[Link]();

Person p2 = new Person(name, age);


[Link]();

[Link]();
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
John
25
Name: John, Age: 30
Name: John, Age: 25
7 :oN egaP 512337429061 :DI D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL
Name: Alice, Age: 30
Name: John, Age: 30
User Output
Alice
30
Exp. Name: Person's name and age - Date: 2026-03-
[Link]: 3

8 :oN egaP 512337429061 :DI


Single Inheritance 01

Aim:
Write a Java program to demonstrate inheritance by creating two related classes:
Person and Citizen.

Class Person:
Create a class named Person with the following characteristics:
• A String variable name.
• A method inputName() that takes user input for the name.
• A method displayName() that prints the name to the console.

Class Citizen:
Create a class named Citizen that inherits from Person and has the following
characteristics:

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


• An int variable age.
• A method inputAge() that takes user input for the age.
• A method displayAge() that prints the age to the console.

Input/Output Format:
• First, prompt the user with: "Enter name: " followed by a string representing
the name.
• Then, display the name preceded by "Name: ".
• On the next line, prompt: "Enter age: " followed by an integer representing the
age.
• Finally, display the age preceded by "Age: ".

Sample Input/Output:
Enter name: John doe
Name: John doe
Enter age: 23
Age: 23

Note:
• The main class has been provided to you in the editor. The MainPerson class
creates an instance of Citizen, takes user input for name and age, and then
displays the entered name and age.
• The program demonstrates the use of inheritance, where the Citizen class
inherits attributes and methods from the Person class.
Source Code:
q23042/[Link]

9 :oN egaP 512337429061 :DI


/*package q23042;
import [Link];

// write your code here..

public class MainPerson {


public static void main(String[] args) {
Citizen citizen = new Citizen();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
*/

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


package q23042;
import [Link];

class Person {
String name;
Scanner sc = new Scanner([Link]);

void inputName() {
[Link]("Enter name: ");
name = [Link]();
}

void displayName() {
[Link]("Name: " + name);
}
}

class Citizen extends Person {


int age;

void inputAge() {
[Link]("Enter age: ");
age = [Link]();
}

void displayAge() {
[Link]("Age: " + age);
}
}
01 :oN egaP 512337429061 :DI
public class MainPerson {
public static void main(String[] args) {
Citizen citizen = new Citizen();
[Link]();
[Link]();
[Link]();
[Link]();
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


User Output
Enter name:
John doe
Name: John doe
Enter age:
23
Age: 23

Test Case - 2

User Output
Enter name:
Shreya
Name: Shreya
Enter age:
38
Age: 38
Date: 2026-03-
[Link]: 4 Exp. Name: Multilevel Inheritance

11 :oN egaP 512337429061 :DI


01

Aim:
Write a Java program to demonstrate multilevel inheritance. Your program must
define a three-level hierarchy of classes: Person (base class), Student (derived from
Person), and Exam (derived from Student).

1. The Person class (base class) should have:


• Attributes: String name, int age
• A method setPersonDetails(String name, int age) - sets the name and age
fields.
• A method displayPersonDetails() - prints the name and age in the format:
Name: <name>
Age: <age>

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


2. The Student class must inherit from Person and add:
• Attribute: int studentId
• A method setStudentDetails(int studentId) - sets the studentId field.
• A method displayStudentDetails() - first calls displayPersonDetails() (to print
name and age), then prints:
Student ID: <studentId>

3. The Exam class must inherit from Student and add:


• Attributes: int marks1, int marks2 (for two subjects).
• A method setExamDetails(int marks1, int marks2) - sets marks1 and marks2.
• A method displayExamDetails() - first calls displayStudentDetails() (to print
name, age, and studentId), then prints:
Marks: <marks1>, <marks2>

Your main() method should create an Exam object, take input from the user to set
all its attributes (from Person, Student, and Exam), and then call the final display
method to print all details.

Input Format:
• The first line contains a string representing the person's name(without spaces).
• The second line contains an integer representing the person's age.
• The third line contains a positive integer representing the student ID.
• The fourth line contains a positive integer representing marks in the first
subject.
• The fifth line contains a positive integer representing marks in the second
subject.

Output Format:
• The program will print three lines in the following format:
Name: <name>
Age: <age>

21 :oN egaP 512337429061 :DI


Student ID: <studentId>
Marks: <marks1>, <marks2>

Note:
• The provided driver code, you to implement the Person, Student, and Exam
classes, ensuring each derived class extends the previous one and uses
methods to display inherited details.
Source Code:

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


q81890/[Link]

31 :oN egaP 512337429061 :DI


/*package q81890;
import [Link].*;

// Base class
class Person {
// Write the code...
}

// Derived class from Person


class Student extends Person {
// Write the code...
}

// Most derived class from Student


class Exam extends Student {

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


// Write the code...
}

public class MultilevelInheritance {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Create Exam object


Exam e = new Exam();

// Input details
String name = [Link]();
int age = [Link]();
int studentId = [Link]();
int marks1 = [Link]();
int marks2 = [Link]();

// Set details
[Link](name, age);
[Link](studentId);
[Link](marks1, marks2);

// Display all details


[Link]();
}
}
*/
package q81890;
import [Link].*;
41 :oN egaP 512337429061 :DI
// Base class
class Person {
String name;
int age;

void setPersonDetails(String name, int age) {


[Link] = name;
[Link] = age;
}

void displayPersonDetails() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


// Derived class from Person
class Student extends Person {
int studentId;

void setStudentDetails(int studentId) {


[Link] = studentId;
}

void displayStudentDetails() {
displayPersonDetails();
[Link]("Student ID: " + studentId);
}
}

// Most derived class from Student


class Exam extends Student {
int marks1, marks2;

void setExamDetails(int marks1, int marks2) {


this.marks1 = marks1;
this.marks2 = marks2;
}

void displayExamDetails() {
displayStudentDetails();
[Link]("Marks: " + marks1 + ", " + marks2);
}
}
public class MultilevelInheritance {

51 :oN egaP 512337429061 :DI


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

Exam e = new Exam();

String name = [Link]();


int age = [Link]();
int studentId = [Link]();
int marks1 = [Link]();
int marks2 = [Link]();

[Link](name, age);
[Link](studentId);
[Link](marks1, marks2);

[Link]();

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Alice
19
101
85
90
Name: Alice
Age: 19
Student ID: 101
Marks: 85, 90

Test Case - 2

User Output
Charlie
22
61 :oN egaP 512337429061 :DI D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL
Student ID: 303
Name: Charlie

Marks: 50, 65
Age: 22
50
65
Exp. Name: Method Overloading with Date: 2026-03-
[Link]: 5

71 :oN egaP 512337429061 :DI


addfunc() Methods 01

Aim:
Write a Java program that defines a class with two methods named addfunc(), one
accepting two integer parameters and returning their sum, and another accepting
two double parameters and returning their sum. The appropriate method is selected
based on the parameter types passed.

Input Format:
• The first line prompts to enter two space-separated integers for the integer
sum.
• The second line prompts the two space-separated doubles for the double sum.

Output Format:
• The first line prints the sum of the two integers.

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


• The second line prints the sum of the two doubles formatted to exactly two
decimal places.

Note:
• The main() method is already provided in the editor. Complete only the
Calculator class as instructed.
Source Code:
q79655/[Link]

81 :oN egaP 512337429061 :DI


/*package q79655;

import [Link];

class Calculator {

//write your code here...

public class Main {


public static void main(String[] args) {

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


Scanner scanner = new Scanner([Link]);

// Create Calculator object


Calculator calc = new Calculator();

// Read two integers


int num1 = [Link]();
int num2 = [Link]();

// Read two doubles


double num3 = [Link]();
double num4 = [Link]();

// Call addfunc with integers


int intSum = [Link](num1, num2);
[Link](intSum);

// Call addfunc with doubles


double doubleSum = [Link](num3, num4);
[Link]("%.2f%n", doubleSum);

[Link]();
}
}
*/
package q79655;

import [Link];
class Calculator {

91 :oN egaP 512337429061 :DI


int addfunc(int a, int b) {
return a + b;
}

double addfunc(double a, double b) {


return a + b;
}
}

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

Calculator calc = new Calculator();

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


int num1 = [Link]();
int num2 = [Link]();

double num3 = [Link]();


double num4 = [Link]();

int intSum = [Link](num1, num2);


[Link](intSum);

double doubleSum = [Link](num3, num4);


[Link]("%.2f%n", doubleSum);

[Link]();
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
10 20
1.5 2.5
30
4.00
Test Case - 2

02 :oN egaP 512337429061 :DI


User Output
1000000 1000000
0.0 0.0
2000000
0.00

Test Case - 3

User Output
11
2.718 3.142
2

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


5.86
Date: 2026-03-
[Link]: 7 Exp. Name: Basic Shape Calculator

12 :oN egaP 512337429061 :DI


01

Aim:
You are tasked with implementing a basic shape calculator in Java.
• Design a class hierarchy with a base class Shape and two derived classes
Circle and Rectangle.
• The Shape class should have a method calculateArea() that prints "Calculating
area of Shape."
• The Circle class should override the calculateArea() method to calculate and
display the area of a circle by taking the radius (double) of the circle as input
from the user. Use [Link] for the calculation.
• The Rectangle class should also override the calculateArea() method to
calculate and display the area of a rectangle by taking the length (double) and
width (double) of the rectangle as input from the user.

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


Formulae:
• Area of Circle=π × radius
2

• Area of Rectangle = length × width

Input and Output Format:


• The base class Shape displays:
Calculating area of Shape
• The Circle class displays the prompt and output sequentially:
radius of circle: <user input of type double>
Area of circle: <area up to 2 decimal places>
• The Rectangle class displays the prompts and output sequentially:
length of rectangle: <user input of type double>
width of rectangle: <user input of type double>
Area of rectangle: <area up to 2 decimal places>

Note:
• Print the area up to 2 decimal places.
• The main class has been provided to you in the editor.
Source Code:
q23030/[Link]

22 :oN egaP 512337429061 :DI


/*package q23030;
import [Link];

// write your code here..

public class ShapeTest {


public static void main(String[] args) {
Shape shape = new Shape();
[Link]();

Circle circle = new Circle();


[Link]();

Rectangle rectangle = new Rectangle();

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


[Link]();
}
}
*/
package q23030;
import [Link];

class Shape {
void calculateArea() {
[Link]("Calculating area of Shape");
}
}

class Circle extends Shape {


@Override
void calculateArea() {
Scanner sc = new Scanner([Link]);
[Link]("radius of circle: ");
double radius = [Link]();
double area = [Link] * radius * radius;
[Link]("Area of circle: %.2f%n", area);
}
}

class Rectangle extends Shape {


@Override
void calculateArea() {
Scanner sc = new Scanner([Link]);
[Link]("length of rectangle: ");
double length = [Link]();

32 :oN egaP 512337429061 :DI


[Link]("width of rectangle: ");
double width = [Link]();
double area = length * width;
[Link]("Area of rectangle: %.2f%n", area);
}
}

public class ShapeTest {


public static void main(String[] args) {
Shape shape = new Shape();
[Link]();

Circle circle = new Circle();


[Link]();

Rectangle rectangle = new Rectangle();

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


[Link]();
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Calculating area of Shape
radius of circle:
12.5
Area of circle: 490.87
length of rectangle:
10.2
width of rectangle:
11.1
Area of rectangle: 113.22

Test Case - 2

User Output
Calculating area of Shape
radius of circle:
10.6
Area of circle: 352.99

42 :oN egaP 512337429061 :DI


length of rectangle:
77.2
width of rectangle:
22.1
Area of rectangle: 1706.12

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


Date: 2026-03-
[Link]: 8 Exp. Name: Calculator Interface

52 :oN egaP 512337429061 :DI


01

Aim:
Design an interface named Calculator that includes essential methods of type double
for basic arithmetic operations. All methods must return a value of type double.

The interface should declare the following methods:


• double add(double num1, double num2) – returns the sum of two numbers
• double subtract(double num1, double num2) – returns the difference of two
numbers
• double multiply(double num1, double num2) – returns the product of two
numbers
• double divide(double num1, double num2) – returns the result of dividing the
first number by the second

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


Create a class named BasicCalculator that implements the Calculator interface and
provides concrete implementations for all the above methods.

Input Format:
• The first line contains an integer num1, representing the first number.
• The second line contains an integer num2, representing the second number.

Output Format:
• The program displays the results of all four arithmetic operations in the
following order, each on a new line:
Addition: <num1 + num2>
Subtraction: <num1 - num2>
Multiplication: <num1 * num2>
Division: <num1 / num2>
• For the division operation, <num2> should not be equal to 0.

Sample Input:
5
10
Sample Output:
Addition: 15
Subtraction: -5
Multiplication: 50
Division: 0.5

Note:
• The main class has been provided to you in the editor.
Source Code:
q18023/[Link]

62 :oN egaP 512337429061 :DI


/*package q18023;
// import required classes
// Define interface Calculator { }
// Write your code here...

class BasicCalculator implements Calculator {

// Define required methods

}
public class Calc {

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


public static void main(String[] args) {
Calculator calculator = new BasicCalculator();
Scanner sc=new Scanner([Link]);
int a=[Link]();
int b=[Link]();
double result1 = [Link](a, b);
double result2 = [Link](a, b);
double result3 = [Link](a, b);
double result4 = [Link](a, b);

[Link]("Addition: " + result1);


[Link]("Subtraction: " + result2);
[Link]("Multiplication: " + result3);
[Link]("Division: " + result4);

}
}
*/
package q18023;
import [Link];

interface Calculator {
double add(double num1, double num2);
double subtract(double num1, double num2);
double multiply(double num1, double num2);
double divide(double num1, double num2);
}
class BasicCalculator implements Calculator {

72 :oN egaP 512337429061 :DI


@Override
public double add(double num1, double num2) {
return num1 + num2;
}

@Override
public double subtract(double num1, double num2) {
return num1 - num2;
}

@Override
public double multiply(double num1, double num2) {
return num1 * num2;
}

@Override

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


public double divide(double num1, double num2) {
return num1 / num2;
}
}

public class Calc {


public static void main(String[] args) {
Calculator calculator = new BasicCalculator();
Scanner sc = new Scanner([Link]);
int a = [Link]();
int b = [Link]();

double result1 = [Link](a, b);


double result2 = [Link](a, b);
double result3 = [Link](a, b);
double result4 = [Link](a, b);

[Link]("Addition: " + result1);


[Link]("Subtraction: " + result2);
[Link]("Multiplication: " + result3);
[Link]("Division: " + result4);

[Link]();
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

82 :oN egaP 512337429061 :DI


User Output
5
10
Addition: 15.0
Subtraction: -5.0
Multiplication: 50.0
Division: 0.5

Test Case - 2

User Output
10
20

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


Addition: 30.0
Subtraction: -10.0
Multiplication: 200.0
Division: 0.5
Exp. Name: Mean and Median Date: 2026-03-
[Link]: 9

92 :oN egaP 512337429061 :DI


Calculation 01

Aim:
Write a Java program using abstraction to compute basic statistical values for a
dataset.

Your program must include an abstract class named Statistics that stores the dataset
and declares two abstract methods:
• calculateMean()
• calculateMedian()

Create a class StatCalculator that extends Statistics and:


1. Implements calculateMean() to return a double value representing the average of
all numbers in the dataset.
2. Implements calculateMedian() to return a double value as follows:

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


• Sort the dataset.
• If the number of elements is odd → return the middle element.
• If even → return the average of the two middle elements.

The main method is already provided, and it handles all input and output operations.
Your task is to correctly implement the mean and median logic using the two abstract
methods with the specified return type.

Input Format:
• The first line contains an integer n, representing the number of elements in the
dataset.
• The second line contains n space-separated integers, which may be positive or
negative.

Output Format:
• The output contains one line:
<Mean> <Median>
• Both values must be printed as floating-point numbers, formatted to exactly
two decimal places and separated by a single space.
Source Code:
q82717/[Link]

03 :oN egaP 512337429061 :DI


/*package q82717;
import [Link].*;

abstract class Statistics {


//write your code here...

class StatCalculator extends Statistics {


//write your code here...

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


}

public class StatisticsCalculator {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

int n = [Link]();
int[] arr = new int[n];

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


arr[i] = [Link]();
}

StatCalculator stats = new StatCalculator(arr);

double mean = [Link]();


double median = [Link]();

// Print exactly once — formatted to two decimals


[Link]("%.2f %.2f", mean, median);
}
}
*/
package q82717;
import [Link].*;

abstract class Statistics {


protected int[] data;
Statistics(int[] data) {

13 :oN egaP 512337429061 :DI


[Link] = data;
}

abstract double calculateMean();


abstract double calculateMedian();
}

class StatCalculator extends Statistics {

StatCalculator(int[] data) {
super(data);
}

@Override
public double calculateMean() {
double sum = 0;

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


for (int num : data) {
sum += num;
}
return sum / [Link];
}

@Override
public double calculateMedian() {
int[] sorted = [Link]();
[Link](sorted);
int n = [Link];

if (n % 2 != 0) {
return (double) sorted[n / 2];
} else {
return (sorted[n / 2 - 1] + sorted[n / 2]) / 2.0;
}
}
}

public class StatisticsCalculator {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

int n = [Link]();
int[] arr = new int[n];

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


arr[i] = [Link]();
}

23 :oN egaP 512337429061 :DI


StatCalculator stats = new StatCalculator(arr);

double mean = [Link]();


double median = [Link]();

[Link]("%.2f %.2f%n", mean, median);


}
}

Execution Results - All test cases have succeeded!


Test Case - 1

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


User Output
5
15 4 25 14 36
18.80 15.00

Test Case - 2

User Output
6
5 12 32 -10 45 52
22.67 22.00
Date: 2026-03-
[Link]: 10 Exp. Name: Exception Handling

33 :oN egaP 512337429061 :DI


01

Aim:
Write a Java program to check whether a given character is a vowel or a consonant.
The program should handle any invalid input gracefully by throwing an exception if
the user does not enter a letter.

Requirements:
• The program should take a single character as input from the user.
• If the input character is a vowel (A, E, I, O, U, or their lowercase forms), the
program should print "<character> is a vowel".
• If the input character is a consonant (any letter that is not a vowel), the
program should print "<character> is a consonant".
• If the input is not a letter (e.g., a number or special character), the program
should throw an exception and print an error message: "Error".

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


Case Sensitivity Requirement:
• The program should check vowels in a case-insensitive manner, but the output
must preserve and display the character exactly as entered by the user.

Input Format:
• The program will take a single character as input.

Output Format:
The output will print one of the following messages based on the input:
• If the character is a vowel, print: "<character> is a vowel"
• If the character is a consonant, print: "<character> is a consonant"
• If the input is invalid, print: "Error"
Source Code:
q55563/[Link]

43 :oN egaP 512337429061 :DI


/*package q55563;
import [Link];

public class VowelConsonantExample {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

try {

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


}
catch ( ) {

}
finally {
[Link]();
}
}
}
*/
package q55563;
import [Link];

public class VowelConsonantExample {


public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

try {
String input = [Link]();
char ch = [Link](0);

if (![Link](ch)) {
throw new IllegalArgumentException("Invalid
input");
}
53 :oN egaP 512337429061 :DI
char lower = [Link](ch);

if (lower == 'a' || lower == 'e' || lower == 'i' ||


lower == 'o' || lower == 'u') {
[Link](ch + " is a vowel");
} else {
[Link](ch + " is a consonant");
}

} catch (IllegalArgumentException e) {
[Link]("Error");
} finally {
[Link]();
}
}
}

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


Execution Results - All test cases have succeeded!
Test Case - 1

User Output
a
a is a vowel

Test Case - 2

User Output
H
H is a consonant

Test Case - 3

User Output
25
Error
Exp. Name: Thread Priority Message Date: 2026-03-
[Link]: 11

63 :oN egaP 512337429061 :DI


Program 01

Aim:
Write a Java program that demonstrates thread creation in two ways:
• By extending the Thread class
• By implementing the Runnable interface
The program creates three threads, each associated with a priority number entered
by the user. Each thread prints a message corresponding to its priority.

Requirements:
Thread Messages:
1 - "Good Morning"
2 - "Good Afternoon"
3 - "Good Evening"
4 - "Good Night"

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


Classes and Methods:
1. Thread subclass
• Class name: PriorityThread
• Constructor: Accepts an integer priority to set the thread’s priority.
• Method: run() Prints the message corresponding to the priority when the
thread runs.
2. Runnable implementation
• Class name: PriorityRunnable
• Constructor: Accepts an integer priority to set the thread’s priority.
• Method: run() Prints the message corresponding to the priority when the
thread runs.
3. Main class
• Class name: ThreadPriorityDemo
• Reads three integers from the user, representing priorities for three threads.
• Creates and starts three threads using PriorityThread.
• Creates and starts three threads using PriorityRunnable.

Input Format:
• The input consists of three integers(each on a separate line) representing the
priorities of three threads:
<Priority for first thread>
<Priority for second thread>
<Priority for third thread>
• Input must be 1 - 4, any other number is ignored.

Output Format:
• The program prints the messages corresponding to the thread priorities.
• Each message appears twice (once from the Thread subclass, once from
Runnable).

73 :oN egaP 512337429061 :DI


Notes:
• The program does not use setPriority() or [Link](); it only prints
messages based on the integer input.
Source Code:

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


q83533/[Link]

83 :oN egaP 512337429061 :DI


/*package q83533;

import [Link];

// Thread subclass
class PriorityThread extends Thread {
//write your code here..

// Runnable implementation

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


class PriorityRunnable implements Runnable {
//write your code here...

// Main class
public class ThreadPriorityDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

// Input priorities for three threads


int p1 = [Link]();
int p2 = [Link]();
int p3 = [Link]();

// Using Thread subclass


Thread t1 = new PriorityThread(p1);
Thread t2 = new PriorityThread(p2);
Thread t3 = new PriorityThread(p3);

[Link]();
[Link]();
[Link]();

// Using Runnable implementation


Thread r1 = new Thread(new PriorityRunnable(p1));

93 :oN egaP 512337429061 :DI


Thread r2 = new Thread(new PriorityRunnable(p2));
Thread r3 = new Thread(new PriorityRunnable(p3));

[Link]();
[Link]();
[Link]();

[Link]();
}
}
*/
package q83533;

import [Link];

// Thread subclass

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


class PriorityThread extends Thread {
private int priority;

PriorityThread(int priority) {
[Link] = priority;
}

@Override
public void run() {
switch (priority) {
case 1: [Link]("Good Morning"); break;
case 2: [Link]("Good Afternoon"); break;
case 3: [Link]("Good Evening"); break;
case 4: [Link]("Good Night"); break;
}
}
}

// Runnable implementation
class PriorityRunnable implements Runnable {
private int priority;

PriorityRunnable(int priority) {
[Link] = priority;
}

@Override
public void run() {
switch (priority) {
case 1: [Link]("Good Morning"); break;

04 :oN egaP 512337429061 :DI


case 2: [Link]("Good Afternoon"); break;
case 3: [Link]("Good Evening"); break;
case 4: [Link]("Good Night"); break;
}
}
}

// Main class
public class ThreadPriorityDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

int p1 = [Link]();
int p2 = [Link]();
int p3 = [Link]();

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


// Using Thread subclass
Thread t1 = new PriorityThread(p1);
Thread t2 = new PriorityThread(p2);
Thread t3 = new PriorityThread(p3);

[Link]();
[Link]();
[Link]();

// Wait for Thread subclass threads to finish before


starting Runnable threads
try {
[Link]();
[Link]();
[Link]();
} catch (InterruptedException e) {
[Link]().interrupt();
}

// Using Runnable implementation


Thread r1 = new Thread(new PriorityRunnable(p1));
Thread r2 = new Thread(new PriorityRunnable(p2));
Thread r3 = new Thread(new PriorityRunnable(p3));

[Link]();
[Link]();
[Link]();

[Link]();
}

14 :oN egaP 512337429061 :DI


}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
1
2
3
Good Morning
Good Afternoon

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


Good Evening
Good Morning
Good Afternoon
Good Evening

Test Case - 2

User Output
2
4
1
Good Afternoon
Good Night
Good Morning
Good Afternoon
Good Night
Good Morning
Exp. Name: Producer and Consumer Date: 2026-03-
[Link]: 12

24 :oN egaP 512337429061 :DI


Problem 01

Aim:
Write a Java program to implement the Producer-Consumer problem using
multithreading and synchronization.

Program Requirements:
1. Prompt the user to enter the number of items n to be produced and consumed.
2. Use separate threads for:
• Producer: produces items numbered from 0 to n − 1
• Consumer: consumes the produced items in order
3. Implement a shared resource class (Product) that:
• Allows the producer to put an item only when the previous item has been
consumed.
• Allows the consumer to get an item only when a new item has been produced.

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


4. Print:
• "PUT: <number>" when the producer places an item
• "GET: <number>" when the consumer consumes an item
5. Use proper synchronization (synchronized, wait(), notify()) to avoid race
conditions,to coordinate the producer and consumer without race conditions.

Input Format:
• A single integer n representing how many items should be produced and
consumed as:
number of items to be produced and consumed: <user_input>

Output Format:
The output displays producer and consumer actions:
• Producer: "PUT: <number>" for each item produced (0 to n − 1).
• Consumer: "GET: <number>" for each item consumed in order (0 to n − 1 ).
The execution order will alternate correctly due to synchronization.

Constraint:
•n ≥ 0

Note: Refer to visible test cases for print statements and a better understanding.
Source Code:
q63229/[Link]

34 :oN egaP 512337429061 :DI


/*package q63229;
import [Link];
class ProdCons {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("number of items to be produced and
consumed: ");
int numItems = [Link]();
Product p = new Product(numItems);
Thread producerThread = new Thread(new Producer(p));
Thread consumerThread = new Thread(new Consumer(p));
[Link]();
[Link]();
[Link]();
}

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


}

class Product {
// Write the code...
}

class Producer implements Runnable {


// Write the code...
}

class Consumer implements Runnable {


// Write the code...
}
*/
package q63229;
import [Link];

class ProdCons {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("number of items to be produced and
consumed: ");
int numItems = [Link]();
Product p = new Product(numItems);
Thread producerThread = new Thread(new Producer(p));
Thread consumerThread = new Thread(new Consumer(p));
[Link]();
[Link]();
[Link]();
}

44 :oN egaP 512337429061 :DI


}

class Product {
private int item;
private boolean available = false;
private int numItems;

Product(int numItems) {
[Link] = numItems;
}

public int getNumItems() {


return numItems;
}

public synchronized void put(int item) {

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


while (available) {
try { wait(); } catch (InterruptedException e) {
[Link]().interrupt(); }
}
[Link] = item;
available = true;
[Link]("PUT: " + item);
notify();
}

public synchronized void get() {


while (!available) {
try { wait(); } catch (InterruptedException e) {
[Link]().interrupt(); }
}
[Link]("GET: " + item); // Print BEFORE
releasing lock
available = false;
notify();
}
}

class Producer implements Runnable {


private Product product;

Producer(Product product) { [Link] = product; }

@Override
public void run() {
for (int i = 0; i < [Link](); i++) {

54 :oN egaP 512337429061 :DI


[Link](i);
}
}
}

class Consumer implements Runnable {


private Product product;

Consumer(Product product) { [Link] = product; }

@Override
public void run() {
for (int i = 0; i < [Link](); i++) {
[Link](); // GET is printed inside get() now
}
}

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
number of items to be produced and consumed:
5
PUT: 0
GET: 0
PUT: 1
GET: 1
PUT: 2
GET: 2
PUT: 3
GET: 3
PUT: 4
GET: 4

Test Case - 2

User Output
number of items to be produced and consumed:
64 :oN egaP 512337429061 :DI D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL
PUT: 0
GET: 0
Date: 2026-03-
[Link]: 13 Exp. Name: Sum of Integers

74 :oN egaP 512337429061 :DI


01

Aim:
Write a Java program that reads a line of integers (separated by spaces), then
displays each integer on a new line, followed by the sum of all the integers. Use the
StringTokenizer class from [Link] to tokenize the input.

Input Format:
• A single line of integers separated by spaces.

Output Format:
• Each integer should be displayed on a new line.
• After displaying all integers, print the sum of all integers.
Source Code:

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


[Link]

84 :oN egaP 512337429061 :DI


/*import [Link];
import [Link];

public class sumofIntegers {

public static void main(String[] args) {


Scanner scanner = new Scanner([Link]);

String input = [Link]();

// Use StringTokenizer to split the input by spaces


StringTokenizer tokenizer = new StringTokenizer(input);

// Write your code here...

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


[Link]();
}
}
*/
import [Link];
import [Link];

public class sumofIntegers {

public static void main(String[] args) {


Scanner scanner = new Scanner([Link]);

String input = [Link]();

// Use StringTokenizer to split the input by spaces


StringTokenizer tokenizer = new StringTokenizer(input);

int sum = 0;

while ([Link]()) {
int num = [Link]([Link]());
[Link](num);
sum += num;

94 :oN egaP 512337429061 :DI


}

[Link](sum);

[Link]();
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


12345
1
2
3
4
5
15

Test Case - 2

User Output
12 13 15 17 21 23 36
12
13
15
17
21
23
36
137
Date: 2026-03-
[Link]: 14 Exp. Name: File Information

05 :oN egaP 512337429061 :DI


01

Aim:
Write a Java program that reads a file name from the user and then displays
information about the file. The program should indicate whether the file exists,
whether it is readable, whether it is writable, the type of file (file or directory), and
the length of the file in bytes.

Input Format:
• The input will be a single line string representing the file name.

Output Format:
If the file exists, the program should output the following as mentioned:
• "File exists: true"
• "Readable: true" or "Readable: false"

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


• "Writable: true" or "Writable: false"
• "File type: File" or "File type: Directory"
• "Length: " <X bytes (where X is the length of the file in bytes)>
If the file does not exist, the program should output:
• File does not exist
Source Code:
q47145/[Link]

15 :oN egaP 512337429061 :DI


/*package q47145;
import [Link];
import [Link];

public class FileInfo {


public static void main(String[] args) {
// Write your code here...

}
*/
package q47145;

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


import [Link];
import [Link];

public class FileInfo {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String fileName = [Link]();

File file = new File(fileName);

if ([Link]()) {
[Link]("File exists: true");
[Link]("Readable: " + [Link]());
[Link]("Writable: " + [Link]());

if ([Link]()) {
[Link]("File type: File");
} else {
[Link]("File type: Directory");
}

[Link]("Length: " + [Link]() + "


bytes");
} else {
[Link]("File does not exist");
}

[Link]();
}

25 :oN egaP 512337429061 :DI


}

[Link]

Java is a high-level, object-oriented programming language

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
[Link]

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


File exists: true
Readable: true
Writable: true
File type: File
Length: 58 bytes

Test Case - 2

User Output
[Link]
File does not exist
Date: 2026-03-
[Link]: 15 Exp. Name: Input and Output Streams

35 :oN egaP 512337429061 :DI


01

Aim:
Write a Java program that demonstrates the use of input and output streams. The
program should:
1. Read data from an input file using an input stream.
2. Write data to an output file named [Link] using an output stream.
3. After copying, read and display the contents of the output file.

Input Format:
• Input is a string which represents the input file name.

Output Format:
• The program should display the contents of the [Link] file.
• If the input file file is exist. Otherwise it should be "No such file".

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


Source Code:
[Link]

45 :oN egaP 512337429061 :DI


/*import [Link].*;
import [Link];
public class IOStreamExample {
public static void main(String[] args) {
// File paths for input and output files
[Link]("file name:");

String outputFile = "[Link]";

// Create InputStream and OutputStream objects

try {
// Create FileInputStream to read data from the input

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


file

// Create FileOutputStream to write data to the


output file

int content;
// Read each byte from the input file and write it to
the output file
while ( ){

}
// Reading from the output file and printing its
contents
FileInputStream outputFileStream = new
FileInputStream(outputFile);
StringBuilder outputContent = new StringBuilder();
while ((content = [Link]()) != -1) {
[Link]((char) content);
}
[Link]("Contents of the
output file:");
[Link]([Link]());

// Close the output file stream

}
catch (IOException e) {

55 :oN egaP 512337429061 :DI


[Link]("No such file");
} finally {
try {
// Close the streams to release resources
if (fileInputStream != null) {
[Link]();
}
if (fileOutputStream != null) {
[Link]();
}
} catch (IOException e) {
[Link]("Error while closing streams:
" + [Link]());
}
}
}

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


}
*/
import [Link].*;
import [Link];

public class IOStreamExample {


public static void main(String[] args) {
[Link]("file name:");
Scanner sc = new Scanner([Link]);
String inputFile = [Link]();
String outputFile = "[Link]";

FileInputStream fileInputStream = null;


FileOutputStream fileOutputStream = null;

try {
// Create FileInputStream to read data from the input
file
fileInputStream = new FileInputStream(inputFile);

// Create FileOutputStream to write data to the


output file
fileOutputStream = new FileOutputStream(outputFile);

int content;
// Read each byte from the input file and write it to
the output file
while ((content = [Link]()) != -1) {
[Link](content);
}

65 :oN egaP 512337429061 :DI


// Close output stream before reading back
[Link]();
fileOutputStream = null;

// Reading from the output file and printing its


contents
FileInputStream outputFileStream = new
FileInputStream(outputFile);
StringBuilder outputContent = new StringBuilder();
while ((content = [Link]()) != -1) {
[Link]((char) content);
}
[Link]();

[Link]("Contents of the output file:");

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


[Link]([Link]());

} catch (IOException e) {
[Link]("No such file");
} finally {
try {
if (fileInputStream != null) {
[Link]();
}
if (fileOutputStream != null) {
[Link]();
}
} catch (IOException e) {
[Link]("Error while closing streams:
" + [Link]());
}
}
}
}

[Link]

hello world
[Link]

75 :oN egaP 512337429061 :DI


Hello, welcome to Java I/O Streams!

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
file name:
[Link]
Contents of the output file:
hello world

D-ESC-8202-4202 ygolonhceT dna gnireenignE fo etutitsnI sdroL


Test Case - 2

User Output
file name:
[Link]
Contents of the output file:
Hello, welcome to Java I/O Streams!

Test Case - 3

User Output
file name:
[Link]
No such file

You might also like