0% found this document useful (0 votes)
5 views18 pages

Java Week10

The document contains information about a student's performance in programming assignments related to a traffic management system and a fruit festival. It includes problem statements, input/output formats, and sample test cases for both assignments, along with Java code implementations. The student, Arul Amudhan G, achieved full marks in both assignments.
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)
5 views18 pages

Java Week10

The document contains information about a student's performance in programming assignments related to a traffic management system and a fruit festival. It includes problem statements, input/output formats, and sample test cases for both assignments, along with Java code implementations. The student, Arul Amudhan G, achieved full marks in both assignments.
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

17 Rajalakshmi Engineering College

7
01

01

01
10

01

01

01
0
10

10

10

10
Name: Arul Amudhan G Scan to verify results
24

24

24

24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7

17

17
01

01
10

0
2024_28_III_OOPS Using Java Lab
01

01

01
00
10

10

10
1
24

24

24

24
2028_REC_OOPS using Java_Week 10_Q1

Attempt : 1
Total Mark : 10
Marks Obtained : 10

Section 1 : COD

1. Problem Statement
7

7
01

01

01

01
A city traffic management system needs to track vehicles entering a toll
01

01

01

01
10

10

10

booth. Each vehicle is uniquely identified by its registration number. The 10


24

24

24

24
system should allow adding vehicles to a record, ensuring that no
duplicate registration numbers exist. The vehicles should be stored in a
HashSet, which does not guarantee any specific order.

Your task is to implement a program using a HashSet that allows adding


vehicle details and displaying the records.

Input Format
The first line of input contains an integer N - the number of vehicles.
7

7
01

01

01

1
10
01

01

01

The next N lines contain details of each vehicle in the format: "RegNumber
0
10

10

10

10
24

24

24

24
OwnerName VehicleType"
17

7
01

01

01
10

01

01

01
0

1. RegNumber (String) - A unique registration number (Alphanumeric).


10

10

10

10
2. OwnerName (String) - The name of the vehicle owner.
24

24

24

24
3. VehicleType (String, Car, Bike, or Truck) - The type of vehicle.

If a vehicle with the same registration number is already present, ignore the
duplicate entry.
Output Format
The output prints the unique vehicle records in any order (since HashSet does
not maintain order).

Output format: "RegNumber OwnerName VehicleType"


7

17

17
01

01
10

0
01

01

01
00
10

10

10
1
24

24

24

24
Refer to the sample output for formatting specifications.
Sample Test Case
Input: 5
KA01AB1234 John Car
MH02CD5678 Alice Bike
DL03EF9012 Bob Truck
TN04GH3456 Mike Car
KA01AB1234 John Car
7

7
01

01

01

01
Output: TN04GH3456 Mike Car
01

01

01

01
10

10

10

10
KA01AB1234 John Car
24

24

24

24
MH02CD5678 Alice Bike
DL03EF9012 Bob Truck
Answer
import [Link];
import [Link];
import [Link];

class Vehicle {
private String regNumber;
7

private String ownerName;


01

01

01

1
10

private String vehicleType;


01

01

01

0
10

10

10

10
24

24

24

24
public Vehicle(String regNumber, String ownerName, String vehicleType) {
17

7
01

01

01
[Link] = regNumber;
10

01

01

01
0

[Link] = ownerName;
10

10

10

10
[Link] = vehicleType;
24

24

24

24
}

public String getRegNumber() {


return regNumber;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != [Link]()) return false;
7

17

17
01

01
Vehicle vehicle = (Vehicle) o;
10

0
01

01

01
00

return [Link]([Link]);
10

10

10
1

}
24

24

24

24
@Override
public int hashCode() {
return [Link]();
}

@Override
public String toString() {
return regNumber + " " + ownerName + " " + vehicleType;
7

7
01

01

01

01
}
01

01

01

01
}
10

10

10

10
24

24

24

24
class TrafficSystem {

public static void main(String[] args) {


Scanner scanner = new Scanner([Link]);
int N = [Link]();
[Link]();

Set<Vehicle> vehicleRecords = new HashSet<>();

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


7

7
01

01

01

String line = [Link]();


10
01

01

01

String[] parts = [Link](" ");


10

10

10

10
24

24

24

24
17

7
01

01

01
if ([Link] == 3) {
10

01

01

01
0

String regNumber = parts[0];


10

10

10

10
String ownerName = parts[1];
24

24

24

24
String vehicleType = parts[2];

Vehicle newVehicle = new Vehicle(regNumber, ownerName,


vehicleType);

[Link](newVehicle);
}
}

for (Vehicle vehicle : vehicleRecords) {


7

17

17
01

01
[Link](vehicle);
10

0
01

01

01
00

}
10

10

10
1
24

24

24

24
[Link]();
}
}

Status : Correct Marks : 10/10


7

7
01

01

01

01
01

01

01

01
10

10

10

10
24

24

24

24
7

7
01

01

01

1
10
01

01

01

0
10

10

10

10
24

24

24

24
17 Rajalakshmi Engineering College

7
01

01

01
10

01

01

01
0
10

10

10

10
Name: Arul Amudhan G Scan to verify results
24

24

24

24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7

17

17
01

01
10

0
2024_28_III_OOPS Using Java Lab
01

01

01
00
10

10

10
1
24

24

24

24
2028_REC_OOPS using Java_Week 10_Q2

Attempt : 1
Total Mark : 10
Marks Obtained : 10

Section 1 : COD

1. Problem Statement
7

7
01

01

01

01
John is organizing a fruit festival, and the quantities of various fruits are
01

01

01

01
10

10

10

stored in a HashMap where fruit names are keys and quantities are values. 10
24

24

24

24

Help him develop a program to find the total quantity of fruits for the
festival by summing up the values in the HashMap.

Input Format
The input consists of fruit quantities in the format 'fruitName:quantity', where
fruitName is the name of the fruit(a string), and quantity is a double value
representing the quantity.
7

The input is terminated by entering "done".


01

01

01

1
10
01

01

01

Output Format
10

10

10

10
24

24

24

24
The output prints a double value, representing the sum of values in the
17

7
01

01

01
HashMap, rounded off to two decimal places.
10

01

01

01
0
10

10

10

10
If the value is not numeric, print "Invalid input".
24

24

24

24
If any special characters other than ':' are entered, print "Invalid format".

Refer to the sample output for formatting specifications.


Sample Test Case
Input: Banana:15.2
7

17

17
01

01
10

0
Orange:56.3
01

01

01
00

Mango:47.3
10

10

10
1
24

24

24

24
done
Output: 118.80
Answer
import [Link];
import [Link];
import [Link];

class FruitFestival {

public static void main(String[] args) {


7

7
01

01

01

01
Scanner scanner = new Scanner([Link]);
01

01

01

01
10

10

10

10
Map<String, Double> fruitQuantities = new HashMap<>();
24

24

24

24
double totalQuantity = 0.0;
String line;

while (!(line = [Link]()).equals("done")) {

if (![Link](":") || [Link](':') != [Link](':')) {


[Link]("Invalid format");
return;
}

String[] parts = [Link](":");


7

7
01

01

01

1
10
01

01

01

0
10

10

10

10

if ([Link] != 2) {
24

24

24

24
[Link]("Invalid format");
17

7
01

01

01
return;
10

01

01

01
0

}
10

10

10

10
24

24

24

24
String quantityStr = parts[1].trim();

try {
double quantity = [Link](quantityStr);
String fruitName = parts[0].trim();

[Link](fruitName, quantity);
totalQuantity += quantity;

} catch (NumberFormatException e) {
7

17

17
01

01
[Link]("Invalid input");
10

0
01

01

01
00

return;
10

10

10
1

}
24

24

24

24
}

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

[Link]();
}
}

Status : Correct Marks : 10/10


7

7
01

01

01

01
01

01

01

01
10

10

10

10
24

24

24

24
7

7
01

01

01

1
10
01

01

01

0
10

10

10

10
24

24

24

24
17 Rajalakshmi Engineering College

7
01

01

01
10

01

01

01
0
10

10

10

10
Name: Arul Amudhan G Scan to verify results
24

24

24

24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7

17

17
01

01
10

0
2024_28_III_OOPS Using Java Lab
01

01

01
00
10

10

10
1
24

24

24

24
2028_REC_OOPS using Java_Week 10_Q3

Attempt : 1
Total Mark : 10
Marks Obtained : 10

Section 1 : COD

1. Problem Statement
7

7
01

01

01

01
Priya is analyzing encrypted messages in a research project. She wants to
01

01

01

01
10

10

10

analyze the frequency of each character in a given paragraph. The 10


24

24

24

24
characters should be stored in a TreeMap so that the output is sorted in
ascending order of characters automatically.

You are required to build a Java program that:


Uses a TreeMap<Character, Integer> to count how many times each
character appears in the [Link] spaces and considers only
alphabets (case-sensitive).Outputs the frequencies of characters in sorted
order.
You must use a TreeMap in the class named MessageAnalyzer.
7

7
01

01

01

1
10
01

01

01

0
10

10

10

10

Input Format
24

24

24

24
The first line of input contains an integer n, the number of lines in the message.
17

7
01

01

01
10

01

01

01
0

The next n lines each contain a string (the encrypted message line).
10

10

10

10
24

24

24

24
Output Format
The first line of output prints: "Character Frequency:"

Then print each character and its frequency in the format: "<character>: <count>"

Refer to the sample output for formatting specifications.


7

17

17
Sample Test Case
01

01
10

0
01

01

01
00

Input: 2
10

10

10
1
24

24

24

24
Hello World
Java
Output: Character Frequency:
H: 1
J: 1
W: 1
a: 2
d: 1
e: 1
l: 3
7

7
o: 2
01

01

01

01
r: 1
01

01

01

01
10

10

10

10
v: 1
24

24

24

24
Answer
import [Link];
import [Link];
import [Link];

class MessageAnalyzer{
public static void main(String args[]){
Scanner ip = new Scanner([Link]);
int n = [Link]();
7

[Link]();
01

01

01

1
10
01

01

01

0
10

10

10

10

TreeMap <Character, Integer> frequencyMap = new TreeMap<>();


24

24

24

24
17

7
01

01

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

01

01

01
0

String line = [Link]();


10

10

10

10
for(char c : [Link]()){
24

24

24

24
if([Link](c)){
[Link](c, [Link](c,0)+1);
}
}
}

[Link]("Character Frequency:");

for([Link] <Character, Integer> entry : [Link]()){


[Link]([Link]() + ": " + [Link]());
7

17

17
01

01
}
10

0
01

01

01
00
10

10

10
1

[Link]();
24

24

24

24
}
}

Status : Correct Marks : 10/10


7

7
01

01

01

01
01

01

01

01
10

10

10

10
24

24

24

24
7

7
01

01

01

1
10
01

01

01

0
10

10

10

10
24

24

24

24
17 Rajalakshmi Engineering College

7
01

01

01
10

01

01

01
0
10

10

10

10
Name: Arul Amudhan G Scan to verify results
24

24

24

24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7

17

17
01

01
10

0
2024_28_III_OOPS Using Java Lab
01

01

01
00
10

10

10
1
24

24

24

24
2028_REC_OOPS using Java_Week 10_Q4

Attempt : 1
Total Mark : 10
Marks Obtained : 10

Section 1 : COD

1. Problem Statement
7

7
01

01

01

01
In a ticket reservation system, you store the available seat numbers in a
01

01

01

01
10

10

10

TreeSet. Users input their desired seat number, and the program checks 10
24

24

24

24
whether the chosen seat is available.

Using a TreeSet ensures quick and efficient verification of seat availability,


ensuring a smooth and organized ticket booking process.

Input Format
The first line of input contains a single integer n, representing the number of
available seats.
7

The second line contains n space-separated integers, representing the available


01

01

01

1
10

seat numbers.
01

01

01

0
10

10

10

10
24

24

24

24
The third line contains an integer m, representing the seat number that needs to
17

7
01

01

01
be searched.
10

01

01

01
0
10

10

10

10
Output Format
24

24

24

24
The output displays "[m] is present!" if the given seat is available. Otherwise, it
displays "[m] is not present!"

Refer to the sample output for the formatting specifications.


Sample Test Case
Input: 4
7

17

17
01

01
10

0
2456
01

01

01
00

5
10

10

10
1
24

24

24

24
Output: 5 is present!
Answer
import [Link];
import [Link];
import [Link];

class TicketReservation{
public static void main(String args[]){
Scanner ip = new Scanner([Link]);
7

7
int n = [Link]();
01

01

01

01
01

01

01

01
10

10

10

10
Set<Integer> availableSeats = new TreeSet<>();
24

24

24

24
for(int i=0; i<n; i++){
[Link]([Link]());
}

int m = [Link]();

if([Link](m)){
[Link](m + " is present!");
}
else{
7

[Link](m + " is not present!");


01

01

01

1
10

}
01

01

01

0
10

10

10

10
24

24

24

24
24 24 24 24
10 10 10 10

}
01 01 01 0
}
01 01 01 10
7 7 7 17
[Link]();

Status : Correct

24 24 24 24
10 10 1 00 10
01 01 10 01
01 01 01
7 7 17 7

24 24 24 24
10 10 10 10
01 01 01 01
01 01 01 01
7 7 7 7

24 24 24 24
10 10 10 10
Marks : 10/10

0 10 01 01 01
1 01 0 01
7 7 17 7
17 Rajalakshmi Engineering College

7
01

01

01
10

01

01

01
0
10

10

10

10
Name: Arul Amudhan G Scan to verify results
24

24

24

24
Email: 241001017@[Link]
Roll no: 241001017
Phone: 7397782797
Branch: REC
Department: IT - Section 3
Batch: 2028
Degree: B.E - IT
7

17

17
01

01
10

0
2024_28_III_OOPS Using Java Lab
01

01

01
00
10

10

10
1
24

24

24

24
REC_2028_OOPS using Java_Week 10_MCQ

Attempt : 1
Total Mark : 15
Marks Obtained : 15

Section 1 : MCQ

1. What happens when you add duplicate elements to a HashSet?


7

7
01

01

01

01
Answer
01

01

01

01
10

10

10

The duplicate is ignored 10


24

24

24

24

Status : Correct Marks : 1/1

2. What will happen if you add elements in descending order in a TreeSet?

Answer
They are sorted in ascending order
Status : Correct Marks : 1/1
7

7
01

01

01

1
10
01

01

01

0
10

10

10

10
24

24

24

24
3. What will be the output of the following code?
17

7
01

01

01
10

01

01

01
0

import [Link].*;
10

10

10

10
24

24

24

24
class Main {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
[Link]("A", "Apple");
[Link]("B", "Banana");
[Link]("C", "Cherry");
[Link]("B", "Blueberry");
[Link](map);
}
}
7

17

17
01

01
10

0
01

01

01
Answer
00
10

10

10
1
24

24

24

24
{A=Apple, B=Blueberry, C=Cherry}
Status : Correct Marks : 1/1

4. Which of the following is true about HashMap?

Answer
It is not synchronized
Status : Correct Marks : 1/1
7

7
01

01

01

01
01

01

01

01
10

10

10

5. Which of the following is true about TreeMap? 10


24

24

24

24

Answer
It maintains natural ordering
Status : Correct Marks : 1/1

6. What happens if two keys have the same hash code in a HashMap?

Answer
7

7
01

01

01

1
10
01

01

01

A linked list is used to store values with the same hash


0
10

10

10

10
24

24

24

24
Status : Correct Marks : 1/1
17

7
01

01

01
10

01

01

01
0
10

10

10

10
7. Which method retrieves the lowest key in a TreeMap?
24

24

24

24
Answer
firstKey()
Status : Correct Marks : 1/1

8. What is the time complexity of retrieving an element from a HashSet?

Answer
7

17

17
01

01
10

0
01

01

01
O(1)
00
10

10

10
1
24

24

24

24
Status : Correct Marks : 1/1

9. Which statement is true about HashSet and TreeSet?

Answer
TreeSet provides sorted elements
Status : Correct Marks : 1/1
7

7
01

01

01

01
10. What will be the output of the following code?
01

01

01

01
10

10

10

10
24

24

24

24
import [Link].*;
class Main {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
[Link]("A", 1);
[Link]("B", 2);
[Link]("C", 3);
[Link]([Link]("B"));
}
}
7

7
01

01

01

1
10
01

01

01

Answer
10

10

10

10
24

24

24

24
true
17

7
01

01

01
10

01

01

01
Status : Correct Marks : 1/1
0
10

10

10

10
24

24

24

24
11. Which method removes all elements from a Set?

Answer
clear()
Status : Correct Marks : 1/1

12. What will be the output of the following code?


7

17

17
01

01
10

0
01

01

01
import [Link].*;
00
10

10

10
1

class Main {
24

24

24

24
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
[Link]("X", 10);
[Link]("Y", 20);
[Link]("Z", 30);
[Link]("Y");
[Link](map);
}
}
7

7
01

01

01

01
Answer
01

01

01

01
10

10

10

{X=10, Z=30} 10
24

24

24

24

Status : Correct Marks : 1/1

13. What will happen if you add a null element to a TreeSet?

Answer
An exception occurs
Status : Correct Marks : 1/1
7

7
01

01

01

1
10
01

01

01

0
10

10

10

10

14. How does HashSet check for duplicate elements?


24

24

24

24
17

7
Answer

01

01

01
10

01

01

01
0

Using equals() and hashCode()


10

10

10

10
24

24

24

24
Status : Correct Marks : 1/1

15. Which of the following allows null keys in Java?

Answer
HashMap
Status : Correct Marks : 1/1
7

17

17
01

01
10

0
01

01

01
00
10

10

10
1
24

24

24

24
7

7
01

01

01

01
01

01

01

01
10

10

10

10
24

24

24

24
7

7
01

01

01

1
10
01

01

01

0
10

10

10

10
24

24

24

24

You might also like