Javaaa
Javaaa
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>
class {
[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 {
[Link] = [Link]();
[Link]();
[Link] = [Link]();
[Link]();
[Link]();
}
}
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
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.
class Person {
// Parameterized constructor
void display() {
[Link]("Name: " + name + ", Age: " + age);
[Link]();
}
}
*/
package q77383;
import [Link];
class Person {
String name;
int age;
// Parameterized constructor
Person(String name, int age) {
[Link] = name;
[Link] = age;
}
void display() {
[Link]("Name: " + name + ", Age: " + age);
}
}
[Link]();
}
}
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
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:
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]
class Person {
String name;
Scanner sc = new Scanner([Link]);
void inputName() {
[Link]("Enter name: ");
name = [Link]();
}
void displayName() {
[Link]("Name: " + name);
}
}
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]();
}
}
Test Case - 2
User Output
Enter name:
Shreya
Name: Shreya
Enter age:
38
Age: 38
Date: 2026-03-
[Link]: 4 Exp. Name: Multilevel Inheritance
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).
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>
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:
// Base class
class Person {
// Write the code...
}
// 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);
void displayPersonDetails() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}
void displayStudentDetails() {
displayPersonDetails();
[Link]("Student ID: " + studentId);
}
}
void displayExamDetails() {
displayStudentDetails();
[Link]("Marks: " + marks1 + ", " + marks2);
}
}
public class MultilevelInheritance {
[Link](name, age);
[Link](studentId);
[Link](marks1, marks2);
[Link]();
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
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.
Note:
• The main() method is already provided in the editor. Complete only the
Calculator class as instructed.
Source Code:
q79655/[Link]
import [Link];
class Calculator {
[Link]();
}
}
*/
package q79655;
import [Link];
class Calculator {
[Link]();
}
}
User Output
10 20
1.5 2.5
30
4.00
Test Case - 2
Test Case - 3
User Output
11
2.718 3.142
2
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.
Note:
• Print the area up to 2 decimal places.
• The main class has been provided to you in the editor.
Source Code:
q23030/[Link]
class Shape {
void calculateArea() {
[Link]("Calculating area of Shape");
}
}
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
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.
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]
}
public class Calc {
}
}
*/
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 {
@Override
public double subtract(double num1, double num2) {
return num1 - num2;
}
@Override
public double multiply(double num1, double num2) {
return num1 * num2;
}
@Override
[Link]();
}
}
Test Case - 2
User Output
10
20
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()
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]
int n = [Link]();
int[] arr = new int[n];
StatCalculator(int[] data) {
super(data);
}
@Override
public double calculateMean() {
double sum = 0;
@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;
}
}
}
int n = [Link]();
int[] arr = new int[n];
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
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".
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]
try {
}
finally {
[Link]();
}
}
}
*/
package q55563;
import [Link];
try {
String input = [Link]();
char ch = [Link](0);
if () {
throw new IllegalArgumentException("Invalid
input");
}
53 :oN egaP 512337429061 :DI
char lower = [Link](ch);
} catch (IllegalArgumentException e) {
[Link]("Error");
} finally {
[Link]();
}
}
}
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
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"
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).
import [Link];
// Thread subclass
class PriorityThread extends Thread {
//write your code here..
// Runnable implementation
// Main class
public class ThreadPriorityDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
*/
package q83533;
import [Link];
// Thread subclass
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;
// 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]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
User Output
1
2
3
Good Morning
Good Afternoon
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
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.
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]
class Product {
// Write the code...
}
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]();
}
class Product {
private int item;
private boolean available = false;
private int numItems;
Product(int numItems) {
[Link] = numItems;
}
@Override
public void run() {
for (int i = 0; i < [Link](); i++) {
@Override
public void run() {
for (int i = 0; i < [Link](); i++) {
[Link](); // GET is printed inside get() now
}
}
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
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:
int sum = 0;
while ([Link]()) {
int num = [Link]([Link]());
[Link](num);
sum += num;
[Link](sum);
[Link]();
}
}
User Output
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
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"
}
*/
package q47145;
if ([Link]()) {
[Link]("File exists: true");
[Link]("Readable: " + [Link]());
[Link]("Writable: " + [Link]());
if ([Link]()) {
[Link]("File type: File");
} else {
[Link]("File type: Directory");
}
[Link]();
}
[Link]
User Output
[Link]
Test Case - 2
User Output
[Link]
File does not exist
Date: 2026-03-
[Link]: 15 Exp. Name: Input and Output Streams
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".
try {
// Create FileInputStream to read data from the input
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]());
}
catch (IOException e) {
try {
// Create FileInputStream to read data from the input
file
fileInputStream = new FileInputStream(inputFile);
int content;
// Read each byte from the input file and write it to
the output file
while ((content = [Link]()) != -1) {
[Link](content);
}
} 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]
User Output
file name:
[Link]
Contents of the output file:
hello world
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