CS102 Lab session 7
Complete the following exercises pasted below. For each exercise, give an algorithm/java code. This lab provides practice on using Collections Framework in Java
1. Create class Student
2. Variables:
int id
String name
private int grade
3. Methods
Constructor with parameter;
Appropraite getters and setter
toString() method
Write a test.java class to test the following:
Make a arraylist of Student
Enter the following details
| ID | Name | Grade |
| --: | ------ | ----: |
| 101 | Ali | 82 |
| 102 | Sara | 91 |
| 103 | Ahmed | 76 |
| 104 | Fatima | 88 |
| 105 | Omar | 69 |
| 103 | Ahmed | 76 |
//display the list of student using for loop
//display the first student in the list
//sort the list in ascending order ordered by id
//remove the first student from the list
//Add a new student to the begining of the list
//Add a new student to the end of the list
//Add a new student to the middle of the list
//Use iterator to traverse the list. print all student details
Write a test2.java class to test the following:
//Make a Linked List of Student
Enter the following details
| ID | Name | Grade |
| --: | ------ | ----: |
| 101 | Ali | 82 |
| 102 | Sara | 91 |
| 103 | Ahmed | 76 |
| 104 | Fatima | 88 |
| 105 | Omar | 69 |
| 103 | Ahmed | 76 |
write comparators for collection to sort
-Ascending order by id
-Descending order by id
-Ascending order by grade
-Descending order by grade
-Ascending order by name
-Descending order by name
//sort the list using Descending order by id
//show the last element
//sort the list using Ascending order by grade
//use a while loop to display all the values in the list
//Hint: use previous
//remove the first student from the list
//Use iterator to traverse the list.
//Print details for only those students
//whose grade is less than 80
1. Create class Car
2. Variables:
private String model;
private String brand;
private double price;
3. Methods
Parametrized constructor
getters and setters
Override toString method
equals or compareTo method
Create a class called CarShowRoom.java as a tester class
Create a HashSet of Cars
Add the following to the HashSet
/*
| Model | Brand | Price (SAR) |
| ------- | ------- | ----------: |
| Camry | Toyota | 115000 |
| Corolla | Toyota | 82000 |
| RAV4 | Toyota | 125000 |
| Accord | Honda | 119000 |
| Civic | Honda | 89000 |
| Patrol | Nissan | 235000 |
| X-Trail | Nissan | 110000 |
| Tucson | Hyundai | 105000 |
| Elantra | Hyundai | 82000 |
| Model 3 | Tesla | 165000 |
| Model Y | Tesla | 195000 |
| Camry | Toyota | 115000 |
*/
//Use an enhanced for loop to display all cars in the set.
//Check if there are any duplicates?
//Use the contains() method to determine whether the showroom contains the following cars:
//Toyota Camry - 115000 SAR
//Nissan Patrol - 235000 SAR
//BMW X5 - 250000 SAR
//print appropriate message for each search
//remove the following
//Toyota Land Cruiser - 220000 SAR
//what happens?
Create a class called CarShowRoom2.java as a tester class
//create a LinkedHashSet of Cars
//Add the following to the LinkedHashSet
/*
| Model | Brand | Price (SAR) |
| ------- | ------- | ----------: |
| Camry | Toyota | 115000 |
| Corolla | Toyota | 82000 |
| RAV4 | Toyota | 125000 |
| Accord | Honda | 119000 |
| Civic | Honda | 89000 |
| Patrol | Nissan | 235000 |
| X-Trail | Nissan | 110000 |
| Tucson | Hyundai | 105000 |
| Elantra | Hyundai | 82000 |
| Model 3 | Tesla | 165000 |
| Model Y | Tesla | 195000 |
| Camry | Toyota | 115000 |
*/
//Display all cars in the set.
//Is there a difference between this and HashSet output
//Check if there are any duplicates?
//Add the following cars to your LinkedHashSet:
/*
Toyota - Camry - 115000
Toyota - Camry - 115000
Toyota - Camry - 120000
Honda - Camry - 115000
*/
//Are these cars different?
//Toyota - Camry - 115000
//Toyota - Camry - 120000
//Do set operations
//A. Union
//Determine all cars available after combining:
//Original showroom + New shipment
//Hint: Use addAll();
//B. Intersection
//Determine which cars are present in both:
//Original showroom + New shipment
//Hint: Use retainAll()
//C. Difference
//Determine which cars are in the new shipment but were not already in the showroom.
//Hint: Use removeAll()
1. Create class Animal
2. Variables:
private int id;
private String name;
private String species;
private int age;
private int priority;
priority levels(1 → Emergency
2 → High
3 → Normal
4 → Low)
3. Methods
Parametrized constructor
getters and setters
Overrider toString method so a Animal is displayed:
ID: 101 | Simba | Lion | Age: 5 | Priority: 2
Create a class called zoo.java as a tester class.
// Create a Hashmap with
/*
Add these animal to the HashMap
| ID | Name | Species | Age | Priority |
| --: | ----- | -------- | --: | -------: |
| 101 | Simba | Lion | 5 | 2 |
| 102 | Max | Dog | 3 | 3 |
| 103 | Leo | Leopard | 7 | 1 |
| 104 | Coco | Parrot | 2 | 4 |
| 105 | Rocky | Tiger | 6 | 1 |
| 106 | Luna | Cat | 4 | 3 |
| 107 | Bella | Elephant | 12 | 2 |
*/
//display the animal with id 103
//display the animal with id 999
//check and display is animal with id 105 exists
//check and display is animal with id 200 exists
//display the total number of animals in the zoo
//change Lion Max's priorty from 3 to 1
//remove animal with id 104
//display the animal using keySet()
//display the animal using values()
Create a class called zoo2.java as a tester class.
//Create a PriorityQueue
//Use a Comparator so that animals with the lowest priority number are processed first.
//Priority 1 → first
//Priority 2 → second
//Priority 3 → third
//Priority 4 → last
/*
Add these animal to the PriorityQueue
| ID | Name | Species | Age | Priority |
| --: | ----- | -------- | --: | -------: |
| 101 | Simba | Lion | 5 | 2 |
| 102 | Max | Dog | 3 | 3 |
| 103 | Leo | Leopard | 7 | 1 |
| 104 | Coco | Parrot | 2 | 4 |
| 105 | Rocky | Tiger | 6 | 1 |
| 106 | Luna | Cat | 4 | 3 |
| 107 | Bella | Elephant | 12 | 2 |
*/
//display the animal with the priority 1 use peek()
// run loop to poll animals with priority 1 then 2 and then 3.
/* add a new animal
ID: 108
Name: Thor
Species: Bear
Age: 4
Priority: 1
*/
//check which animal should be treated first
//write a new comparator on age.
//list all animals ordered by age (youngest comes first)