Part 1
Part 1
The program should allow the user to perform the following operations:
Each student should have attributes like name, roll number, and grade. Implement a Student class to
represent a student, and a StudentManager class to manage the list of students using iterators.\
1-Mohit-2-A-3-4-2-B-3-2-2-3-5
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
// Class representing a Student with attributes name, roll number, and grade
class Student {
this.name = name;
this.rollNumber = rollNumber;
this.grade = grade;
}
// Getter methods for Student attributes
return name;
return rollNumber;
return grade;
this.grade = grade;
@Override
return "Student{" +
'}';
}
class StudentManager {
public StudentManager() {
students.add(student);
while (iterator.hasNext()) {
if (student.getRollNumber() == rollNumber) {
iterator.remove();
return;
}
if (students.isEmpty()) {
} else {
System.out.println(student);
if (student.getRollNumber() == rollNumber) {
student.setGrade(newGrade);
return;
int choice;
do {
System.out.println("5. Exit");
choice = scanner.nextInt();
switch (choice) {
case 1:
studentManager.addStudent(student);
break;
case 2:
studentManager.removeStudent(removeRollNumber);
break;
case 3:
studentManager.displayStudents();
break;
case 4:
studentManager.replaceGrade(replaceRollNumber, newGrade);
break;
case 5:
System.out.println("Exiting...");
break;
default:
scanner.close();
}
2. Write a Java program to manage employees in a company. The company has
different types of employees, including full-time employees and part-time employees. Each employee
has a name, employee ID, and hourly rate.
Full-time employees have additional attributes such as salary and benefits, (Use super keyword)
while part-time employees have attributes for the number of hours worked and overtime rate. (Use super
keyword)
Your task is to implement a Java program using inheritance to represent these different types of
employees and their attributes. Additionally, the program should provide functionality to calculate the
monthly salary for each type of employee.
John-101-50-5000- 800-jane-102-20-100-5
import java.util.Scanner;
class Employee {
this.name = name;
this.employeeID = employeeID;
this.hourlyRate = hourlyRate;
}
// Getter methods for Employee attributes
return name;
return employeeID;
return hourlyRate;
public FullTimeEmployee(String name, int employeeID, double hourlyRate, double salary, double
benefits) {
super(name, employeeID, hourlyRate);
this.salary = salary;
this.benefits = benefits;
return salary;
return benefits;
@Override
@Override
return "FullTimeEmployee{" +
'}';
public PartTimeEmployee(String name, int employeeID, double hourlyRate, int hoursWorked, double
overtimeRate) {
this.hoursWorked = hoursWorked;
this.overtimeRate = overtimeRate;
return hoursWorked;
return overtimeRate;
}
@Override
@Override
return "PartTimeEmployee{" +
'}';
System.out.print("Name: ");
System.out.print("Salary: ");
System.out.print("Benefits: ");
System.out.print("Name: ");
System.out.println(fullTimeEmployee);
System.out.println(partTimeEmployee);
scanner.close();
}
3. You are required to design a system for managing different types of shapes. The
system should be able to calculate the area and perimeter of various shapes such as circles, rectangles, and
triangles. Each shape has different methods to calculate its area and perimeter. (Use abstract methods-
calculateArea(), calculatePerimeter(), displayDetails()). Your task is to implement a Java program using
polymorphism to represent these different types of shapes and calculate their area and perimeter.
Additionally, the program should provide functionality to display the details of each shape.
5-4-7-3-4-5
import java.util.Scanner;
}
// Class for Circle, extending the Shape class
this.radius = radius;
return radius;
@Override
@Override
}
// Display details of the Circle
@Override
System.out.println("Shape: Circle");
this.length = length;
this.width = width;
return length;
}
// Getter for width
return width;
@Override
@Override
@Override
System.out.println("Shape: Rectangle");
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
return sideA;
return sideB;
}
// Getter for side C
return sideC;
@Override
@Override
@Override
System.out.println("Shape: Triangle");
System.out.print("Radius: ");
System.out.print("Length: ");
System.out.print("Width: ");
System.out.print("Side A: ");
double sideA = scanner.nextDouble();
System.out.print("Side B: ");
System.out.print("Side C: ");
System.out.println("\nCircle Details:");
circle.displayDetails();
System.out.println();
System.out.println("Rectangle Details:");
rectangle.displayDetails();
System.out.println();
System.out.println("Triangle Details:");
triangle.displayDetails();
scanner.close();
}
4. Implement a program for a temperature converter. The program should convert
temperatures between Celsius and Fahrenheit. However, there are certain constraints:
Ii. The conversion should only be performed if the input temperature is within the valid range.
Your task is to implement a Java program that handles these constraints using custom exceptions. Define
a custom exception class InvalidTemperatureException to handle the case when the input temperature is
outside the valid range.
25-77
import java.util.Scanner;
super(message);
class TemperatureConverter {
return celsius;
try {
celsius = TemperatureConverter.fahrenheitToCelsius(fahrenheit);
} catch (InvalidTemperatureException e) {
System.err.println(e.getMessage());
} catch (Exception e) {
// Handle other exceptions (e.g., invalid input) and print a generic error message
scanner.close();
}
5. Implement a Java program for calculating the area of geometric shapes. The
program should support calculating the area of a rectangle, a square, and a circle. Each shape has a
different method to calculate its area. Your task is to implement a Java program that demonstrates method
overloading by providing multiple versions of the calculateArea() method to calculate the area of each
shape. (Use method overloading)
4-7-5-5
import java.util.Scanner;
class AreaCalculator {
if (isCircle) {
} else {
scanner.close();
}
6. implement a Java program to represent a simple calculator. The calculator should
have the following features:
Additionally, the program should initialize a constant value for the value of PI (3.14) and print a welcome
message when an instance of the calculator is created. (static block)
Your task is to implement a Java program that demonstrates the use of constructors, static block, and
instance block to achieve these features.
20-10
import java.util.Scanner;
class Calculator {
static {
PI = 3.14;
// Constructor
public Calculator() {
return a + b;
return a - b;
}
return a * b;
if (b == 0) {
return a / b;
try {
} catch (ArithmeticException e) {
scanner.close();
}
7. Implement a Java program to represent a shape hierarchy. The program should have
interfaces for different types of shapes, such as Drawable for shapes that can be drawn and Resizable for
shapes that can be resized.
Implement classes for specific shapes such as circle, rectangle and demonstrate multiple inheritance by
implementing both Drawable and Resizable interfaces in appropriate classes.
5-4-7-5
import java.util.Scanner;
interface Drawable {
this.radius = radius;
@Override
@Override
radius *= factor;
}
// Getter method for the radius
return radius;
this.length = length;
this.width = width;
@Override
System.out.println("Drawing a rectangle with length: " + length + " and width: " + width);
return length;
}
return width;
// Draw shapes
// Resize circle
5-1-2-3-4-5
import java.util.Arrays;
import java.util.Scanner;
array = elements.clone();
int sum = 0;
sum += num;
return sum;
}
// Find the maximum element in the array
max = num;
return max;
min = num;
return min;
Arrays.sort(array);
}
// Reverse the elements of the array
int n = array.length;
array[n - i - 1] = temp;
System.out.println(Arrays.toString(array));
int n = scanner.nextInt();
arrayOps.initializeArray(elements);
System.out.println("Initialized Array:");
arrayOps.displayArray();
arrayOps.sortArray();
System.out.println("Sorted Array:");
arrayOps.displayArray();
arrayOps.reverseArray();
System.out.println("Reversed Array:");
arrayOps.displayArray();
scanner.close(); // Close the scanner
9. Implement a Java program to perform basic operations on strings. The program should support the
following operations:
import java.util.Scanner;
return str.length();
return str.toUpperCase();
return str.toLowerCase();
return str.contains(substring);
// Get user input for the substring to check in the first string
// Check if the first string contains the specified substring and display the result
// Get user input for the substring to replace and the new substring
// Replace the specified substring with the new substring in the first string and display the result
}
10. You are tasked with creating a Java program that counts the number of unique words in a given text
using a HashSet. Requirements: WordCounter Class:
A method countUniqueWords(String text) that takes a text as input and returns thecount of unique words.
Use a HashSet to store unique words. Consider a word as any sequence of characters separated by
whitespace. Main Application: Implement a main application that demonstrates the functionality of the
WordCounter class.Allow the user to input a text string. Use the WordCounter class to count and display
the number of unique words in the input text.
Sample Output : Enter a text string: This is a simple Java program. Java is powerful and simple.
Number of unique words: 8
import java.util.HashSet;
import java.util.Scanner;
class WordCounter {
uniqueWords.add(word);
// Return the size of the HashSet, which represents the count of unique words
return uniqueWords.size();
scanner.close();
b. Write a Java program to check whether two strings are anagram or not?
c. Take two DOB in string format from user .Compare it and display the
153
import java.util.Scanner;
if (isArmstrong(number)) {
} else {
scanner.close();
originalNum = num;
while (originalNum != 0) {
originalNum /= 10;
b. Write a Java program to check whether two strings are anagram or not?
import java.util.Scanner;
if (areAnagrams(str1, str2)) {
System.out.println("The strings \"" + str1 + "\" and \"" + str2 + "\" are anagrams.");
} else {
System.out.println("The strings \"" + str1 + "\" and \"" + str2 + "\" are not anagrams.");
scanner.close();
Arrays.sort(charArray1);
Arrays.sort(charArray2);
c. Take two DOB in string format from user .Compare it and display the
2004-09-02
2004-09-04
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
if (dob1.isBefore(dob2)) {
} else if (dob1.isAfter(dob2)) {
} else {
scanner.close();
}
d. Check that given no. is prime or not.
import java.util.Scanner;
if (isPrime(number)) {
} else {
scanner.close();
return false;
if (num % i == 0) {
return false;
return true;
}}
12. a. Create arrayList, add the integer elements in arrayList using asList().Remove the
duplicate values and return an arrayList containing unique values. Implement the logic inside
removeDuplicates() method. Test the functionalities using the main () method of the Tester class. Sample
Input and Output---------10, 20, 10, 15,40,15,40 --- 10,20,15,40.
input:
12345671
Choose operation:
9. Exit
Choose operation:
9. Exit
Choose operation:
9. Exit
Choose operation:
9. Exit
Choose operation:
9. Exit
Choose operation:
9. Exit
Choose operation:
9. Exit
Enter the element to find its first and last occurrences in the ArrayList: 1
First occurrence of 1: 0
Last occurrence of 1: 0
Choose operation:
9. Exit
Exiting...
code:
import java.util.*;
// Read input elements from the user and add them to the ArrayList
arrayList.add(Integer.parseInt(element));
while (true) {
System.out.println("\nChoose operation:");
System.out.println("9. Exit");
switch (choice) {
case 1:
break;
case 2:
} else {
System.out.println("Invalid index.");
break;
case 3:
arrayList.remove(indexToRemove);
} else {
System.out.println("Invalid index.");
break;
case 4:
arrayList.set(indexToReplace, newElement);
System.out.println("ArrayList after replacing element at index " + indexToReplace + "
with " + newElement + ": " + arrayList);
} else {
System.out.println("Invalid index.");
break;
case 5:
Collections.sort(arrayList);
break;
case 6:
Collections.reverse(arrayList);
break;
case 7:
break;
case 8:
System.out.print("Enter the element to find its first and last occurrences in the ArrayList: ");
if (firstOccurrence != -1) {
} else {
break;
case 9:
System.out.println("Exiting...");
scanner.close();
System.exit(0);
break;
default:
System.out.println("Invalid choice!");
// Iterate through the original list and add elements to the uniqueList if they are not already present
for (Integer num : arrayList) {
if (!uniqueList.contains(num)) {
uniqueList.add(num);
return uniqueList;