My assignment is about a University database using LinkedList (Java). First, I w
ID: 3697905 • Letter: M
Question
My assignment is about a University database using LinkedList (Java). First, I want to make two functions which are AddCollege and DeleteCollege.
Here are the details:
\
You should create two classes, one for College and the other for Department.
A College should have at least the following parameters: (Please note that you may add additional parameters of your choice for each college if it helps you in any way)
College Name: string ( for example, Business, Engineering, etc.)
Number of departments: int (this will be the number of department within a college)
departments: Department (this field is of type Department reference/pointer which points to the object of type Department)
next: College (this reference/pointer will point to the next college in the list)
You should provide functions to support the following operations:
1. Add a new college in the list (take input from user, all the fields and add a new college)
2. Delete a college from the list (based on college’s name). Notice that deleting a college should delete all the departments of that college as well.
The head should be called PMU.
\
Please help me figure out those two functions first and tell me where do I define the head, too... like in which class? department class or college class?
Explanation / Answer
package assignment;
import java.util.Scanner;
public class CollegeDeptLinkedListTest {
public static void main(String[] args) {
CollegeLinkedList cll = new CollegeLinkedList();
cll.addCollege();
cll.addCollege();
cll.deleteCollege();
}
}
class CollegeLinkedList {
private College head;
public CollegeLinkedList() {
//this is dummy college for head
head = new College("PMU",0,null);
}
public void addCollege() {
Scanner sc = new Scanner(System.in);
String name = null;
String deptName = null;
int noOfDepts = 0;
Department depts = null;
Department temp = null;
System.out.println("Enter college name:");
name = sc.nextLine();
do {
System.out.println("Enter no of departments:");
noOfDepts = sc.nextInt();
}while(noOfDepts <= 0);
if(noOfDepts > 0) {
for(int i = 1; i <= noOfDepts; i++ ) {
System.out.println("Enter department#"+i+" name:");
deptName = sc.nextLine();
if(depts == null)
depts = new Department(deptName);
else {
depts.setNext(new Department(deptName));
}
}
}
head.setNext(new College(name,noOfDepts, depts));
}
public void deleteCollege() {
Scanner sc = new Scanner(System.in);
String collegeName = null;
College cur = head.getNext();
College prev = head;
System.out.println("Enter college name to delete:");
collegeName = sc.nextLine();
boolean found = false;
while(cur != null) {
if(cur.getName().equals(collegeName)) {
prev.setNext(cur.getNext());
found = true;
break;
}
prev = cur;
cur = cur.getNext();
}
if(!found)
System.out.println(" College : "+collegeName +" not found");
}
}
class College {
private String name;
private int noOfDepartments;
private Department departments;
private College next;
public College(String name,int noOfDepartments, Department departments ) {
this.name = name;
this.noOfDepartments = noOfDepartments;
this.departments = departments;
next = null;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNoOfDepartments() {
return noOfDepartments;
}
public void setNoOfDepartments(int noOfDepartments) {
this.noOfDepartments = noOfDepartments;
}
public Department getDepartments() {
return departments;
}
public void setDepartments(Department departments) {
this.departments = departments;
}
public College getNext() {
return next;
}
public void setNext(College next) {
this.next = next;
}
}
class Department {
private String name;
private Department next;
public Department(String name ) {
this.name = name;
next = null;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getNext() {
return next;
}
public void setNext(Department next) {
this.next = next;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.