Data structures in java
Implement the following classes:
1. class Course that includes three instance variables: private String Name; // the course name private int ID; // the course ID private Course next; // the link
Your class should have the following:
A constructor that initializes the two instance variables id and name.
Set and get methods for each instance variable.
2. class Department that includes three instance variables:
private String deptName;
private Course head, tail;
Your class should have the following:
a constructor that initializes the department name
public boolean exist(int id) that checks whether the course
object with id passed as parameter exists in the list or not.
public boolean insert(int id, String n) that creates and adds the course if the course with id passed as parameter does not exist in the list. The course must be added in ascending order. The method will
return false if the id was a duplicate.
public boolean remove(int id) that deletes a course if it exists in
the list and returns true. The method will return false if the id was not found in the list.
public String findCourse(int id) that returns a course name if the id exists in the list.
11
public void print() that prints the department name and all the courses in the array.
Write the expected time and space complexity as a comment at the beginning of each method of your class.
3. Write a test application named Lab2Test. In the main method, do the following:
Input the department name then create a department object.
Display a menu to the user and asking for a choice to be entered.
As follows:
The program can perform the following: 1- insert a course
2- remove a course
3- search for a course
4- print courses
5- exit
Please enter your selection:
The program will perform the action selected by the user and display a proper message when necessary.
The program will repeat these actions until the user terminates the program (Hint: Use a loop).
Sample Output
Please enter the name of the department: Computer Engineering
The program can perform the following: 1- insert a course
2- remove a course
3- search for a course
4- print courses
5- exit
Please enter your selection: 30 Error! Incorrect choice.
The program can perform the following: 1- insert a course
2- remove a course
3- search for a course
4- print courses
5- exit
Please enter your selection: 4
Computer Engineering department does not have any courses.
The program can perform the following: 1- insert a course
2- remove a course
3- search for a course
4- print courses
5- exit
Please enter your selection: 1
Enter the course number or -1 to stop: 300
Enter the course name: Design and Analysis of Algorithms Enter the
course number or -1 to stop: 356
Enter the course name: Computer Networks I
Enter the course number or -1 to stop: 300
Enter the course name: testing duplicate
Error! duplicate ID number...
Enter the course number or -1 to stop: 325
Enter the course name: Human-Computer Interaction
Enter the course number or -1 to stop: 262
Enter the course name: Fundamentals of Digital Logic Enter the
course number or -1 to stop: -1
The program can perform the following: 1- insert a course
2- remove a course
3- search for a course
4- print courses
5- exit
Please enter your selection: 4
Courses in Computer Engineering department are: 1 - 262
Fundamentals of Digital Logic
13
2 - 300 Design and Analysis of Algorithms 3 - 325 Human-Computer
Interaction
4 - 356 Computer Networks I
The program can perform the following: 1- insert a course
2- remove a course
3- search for a course
4- print courses
5- exit
Please enter your selection: 2 Enter the course number: 325 The
course was removed
The program can perform the following: 1- insert a course
2- remove a course
3- search for a course
4- print courses
5- exit
Please enter your selection: 2 Enter the course number: 325 The
course is not in the list
The program can perform the following: 1- insert a course
2- remove a course
3- search for a course
4- print courses
5- exit
Please enter your selection: 4
Courses in Computer Engineering department are: 1 - 262
Fundamentals of Digital Logic
2 - 300 Design and Analysis 3 - 356 Computer Networks I
The program can perform the 1- insert a course
2- remove a course
3- search for a course
4- print courses
of Algorithms following:
5- exit
Please enter your selection: 3
Enter the course number: 300
The id: 300 corresponds to Design and Analysis of Algorithms
The program can perform the following: 1- insert a course
2- remove a course
3- search for a course
4- print courses
14
5- exit
Please enter your selection: 3
Enter the course number: 325
The id: 325 does not correspond to any course in the department
The program can perform the following: 1- insert a course
2- remove a course
3- search for a course
4- print courses
5- exit
Please enter your selection: 5 Exiting the program...
Answer number 1 : Following are the snapshot for the first above question, The user needs to write it on the java text editor and can be run by the help of java compiler.
Get Answers For Free
Most questions answered within 1 hours.