تم الحل ✓
categoryبرمجة وتطوير البرمجيات
schoolبكالوريوس
event_available2026-07-14
السؤال
Transcribed Image Text:
In this problem you are going to implement a single linked list of cars. A linked list is composed of nodes.
So first of all, you need a CarNode class. Each car node should hold its brand and ID. In addition to that,
each node must hold a pointer or reference to the next node in the list, because this is a linked list and
not a simple array.
The CarList would hold only a reference to the first node in the list, also known as the head. From this
node, we can start traversing the list of cars by following the next pointers in each student node. The list
also has the following methods:
•
isEmpty(): returns true if the list does not have any node.
addFirst(String brand, int ID): adds a new CarNode to the start of the list.
addLast(String brand, int ID): adds a new CarNode to the end of the list.
print(): print the list by calling toString() for each node.
getID(String brand): given the brand of a car, search in the list, and return its ID. If not found
throw new CarNotFoundException.
removeAll(): delete all nodes from the list.
- brand: String
- ID: int
-next: CarNode
CarNode
+CarNode (brand: String, ID: int)
// setters & getters
+ toString(): String
CarList
-head: CarNode
+ CarList ( )
+ isEmpty(): boolean
+ addFirst (brand:String, ID:int)
+ addLast (brand:String, ID:int)
+ print ( ) : void
+ getID (brand: String): int
+ removeAll()
CarNotFoundException
- message: String
+ CarNotFoundException()
+ CarNotFoundException(message: String)
+ getMessage() : String
CarNotFoundException class
This class extends the Exception class provided by Java.
It has a private String message variable holding the message "Car not found.".
The default (no-parameter) constructor should call the superclass constructor super().
⚫ The other constructor takes a String a message as a parameter and calls the superclass
constructor with the same parameter super(message).
• Also, provide a method getMessage() that returns the message as a String, so that you use it in
the try-catch statements in the Tester class. As shown in Problem 1.
Tester class
1. Initialize a CarList object.
2. Read 4 car brands and IDs from the scanner. Add the first one using addLast(), the second one
using addFirst(), the third and fourth one using addLast().
3. Print the entire list using print().
4. Call getID("Ferrari") and print its result.
5. Call getID("VW") and print its result.
6. Call getID("Toyota") and print its result.
7. Call isEmpty() and print its result.
8. Call removeAll() and print "Deleted all nodes".
9. Call isEmpty() and print its result.
Sample Input:
BMW 123
VW 321
Ferrari 111
Toyota 333
Sample Output:
VW 321
BMW 123
Ferrari 111
Toyota 333
Ferrari ID: 111
VW ID: 321
Toyota ID: 333
Empty: false
Deleted all nodes.
Empty: true
Using the same classes created in Problem 2, add the following methods to CarList:
•
.
•
•
.
addAtIndex(int index, String brand, int ID): adds a new CarNode at the specified index in the
linked list. Index zero is equivalent to addFirst(). Return type: void
getIndex(String brand): searches for the node with the specified brand and returns its index. If
not found, throw new CarNotFoundException. Return type: int
removeAtIndex(int index): removes node at the specified index. Return type: void
remove(String brand): searches for the node with the specified brand and removes it. Return
type: void
reverse(): reverse the order of the entire list.
Use the same Tester class, leave it as it is.
We will just call some additional methods in Tester.
In problem 2, the last few methods were isEmpty(), then removeAll(), then isEmpty().
We will insert some new code before this chunk of code containing these three methods.
Before these three methods, do the following:
•
call addAtIndex(1, "Kia", 444)
.
call addAtIndex(0, "Subaru", 666)
•
call addAtIndex(6, "Mercedes", 888)
•
call print()
.
call getIndex("Subaru")
•
call getIndex("Mercedes")
•
call getIndex("Toyota")
.
call remove("Mercedes")
•
call removeAtIndex(5)
•
call removeAtIndex(2)
.
call removeAtIndex(0)
•
call print()
•
call reverse(), and print "Reversing list.".
•
call print()
•
here you should have the three last methods that you called in Problem 2 (isEmpty(),
removeAll(), isEmpty()).
The input is the same, but the output is different.
Sample Input:
BMW 123
VW 321
Ferrari 111
Toyota 333
Sample Output:
VW 321
BMW 123
Ferrari 111
Toyota 333
Ferrari ID: 111
VW ID: 321
Toyota ID: 333
Subaru 666
VW 321
Kia 444
BMW 123
Ferrari 111
Toyota 333
Mercedes 888
VW 321
BMW 123
Ferrari 111
Reversing list.
Ferrari 111
BMW 123
VW 321
Empty: false
Deleted all nodes.
Empty: true
check_circle الجواب — حل مفصل خطوة بخطوة
hourglass_top
🔒
الحل الكامل متاح للمشتركين
اشترك في أرشيف الأسئلة لعرض هذا الحل وآلاف الحلول المفصلة خطوة بخطوة من معلمين معتمدين.