I am partially thru a homework problem that uses a singly linked list to first a
ID: 3724094 • Letter: I
Question
I am partially thru a homework problem that uses a singly linked list to first asked the size of a data set and then how many student's info to enter. The user then enters the info (name, number and gpa) for the student(s). After that the user is presented with a options to insert, fetch, delete, update, show all and exit. I am stuck with the options and them actually working. If a user selects a name that is not in the database they should get an error stating so and if an operation is complete they should get a confirmation. The insert option should ask for the name, number and gpa for the new student and then insert the new student's info as long as there is room to do so. The fetch should retrieve the requested student's info and display it. The delete operation should delete the requested student. The update should allow the user to enter the name of the student to update and then ask for the new number and gpa. The show all should display the information for all the students currently in the database and finally the exit should exit. Any helpt would be apprciated. Here is my code thus far:
package listing;
import java.util.Scanner;
public class Listing
{
private String name;
private String number;
private String gpa;
public Listing(String name, String number, String gpa)
{
this.name = name;
this.number = number;
this.gpa = gpa;
}
public String toString()
{
return("Name is " + name + " Number is " + number + " GPA is " + gpa );
}
public Listing deepCopy()
{
Listing clone = new Listing(name, number, gpa);
return clone;
}
public int compareTo(String targetKey)
{
return (name.compareTo(targetKey));
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getNumber()
{
return number;
}
public void setNumber(String number)
{
this.number = number;
}
public String getGPA()
{
return gpa;
}
public void setGPA(String gpa)
{
this.gpa = gpa;
}
}
Linked List Code:
package listing;
import java.util.Scanner;
public class SinglyLinkedList
{
private Node h;
public SinglyLinkedList()
{
h = new Node();
h.l = null;
h.next = null;
}
public boolean insert(Listing newListing)
{
Node n = new Node();
if(n == null)
return false;
else
{
n.next = h.next;
h.next = n;
n.l = newListing.deepCopy();
return true;
}
}
public Listing fetch(String targetKey)
{
Node p = h.next;
while(p != null && !(p.l.compareTo(targetKey)== 0))
{
p = p.next;
}
if(p != null)
return p.l.deepCopy();
else
return null;
}
public boolean delete(String targetKey)
{
Node q = h;
Node p = h.next;
while(p != null && !(p.l.compareTo(targetKey) == 0))
{
q = p;
p = p.next;
}
if(p != null)
{
q.next = p.next;
return true;
}
else
return false;
}
public boolean update(String targetKey, Listing newListing)
{
if(delete(targetKey) == false)
return false;
else if(insert(newListing) == false)
return false;
return true;
}
public void showAll()
{
Node p = h.next;
while(p != null)
{
System.out.println(p.l.toString());
p = p.next;
}
}
public class Node
{
private Listing l;
private Node next;
public Node()
{
}
}
}
Main Code:
package listing;
import java.util.Scanner;
public class Main
{
/**
*
* @param args
*/
public static void main(String[] args)
{
int n1;
int n2;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the maximum size of the data set: ");
n1 = scan.nextInt();
Listing Listing[] = new Listing[n1];
System.out.println("Please enter the number of students to be entered: ");
n2 = scan.nextInt();
for(int i =0; i < n2; i++)
{
System.out.println("Please enter the information for " + (i+1)+ ": ");
Scanner userInput = new Scanner(System.in);
System.out.println("Please enter the name: ");
String name = userInput.nextLine();
System.out.println("Please enter the number: ");
String number = userInput.nextLine();
System.out.println("Please enter a GPA:");
String gpa = userInput.nextLine();
Listing[i] = new Listing(name, number, gpa);
}
int option;
SinglyLinkedList SinglyLinkedList = new SinglyLinkedList();
String name = null;
while(true)
{
System.out.println("Select 1 to insert a new student's information");
System.out.println("Select 2 to fetch and output a student's information");
System.out.println("Select 3 to delete a student's information");
System.out.println("Select 4 to update a new student's information");
System.out.println("Select 5 to output all the student information in sorted order");
System.out.println("Select 6 to exit the program");
System.out.println("Please select an option: ");
option = scan.nextInt();
scan.nextLine();
switch(option)
{
case 1:System.out.println("Please enter the info for the new listing: ");
SinglyLinkedList.insert(newListing);
System.out.println("Operation Finished");
break;
case 2:System.out.println("Please enter the name to fetch: ");
name = scan.nextLine();
SinglyLinkedList.fetch(name);
System.out.println("Operation Finished");
break;
case 3:System.out.println("Please enter the name to delete: ");
name = scan.nextLine();
SinglyLinkedList.delete(name);
System.out.println("Operation Finished");
break;
case 4:System.out.println("Please enter the name to update: ");
name = scan.nextLine();
SinglyLinkedList.update(name, newListing);
System.out.println("Operation Finished");
break;
case 5: SinglyLinkedList.showAll();
SinglyLinkedList.showAll();
System.out.println("Operation Finished");
break;
case 6: System.exit(0);
}
}
}
}
Explanation / Answer
The required changes have been made.
Considering the Student as an entity. I have changed the name of Listing class to Student.
Student Class Code :
package listing;
//Class Student
public class Student
{
private String name;
private String number;
private String gpa;
public Student(String name, String number, String gpa)
{
this.name = name;
this.number = number;
this.gpa = gpa;
}
public String toString()
{
return("Name is " + name + " Number is " + number + " GPA is " + gpa );
}
public Student deepCopy()
{
Student clone = new Student(name, number, gpa);
return clone;
}
public int compareTo(String targetKey)
{
return (name.compareTo(targetKey));
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getNumber()
{
return number;
}
public void setNumber(String number)
{
this.number = number;
}
public String getGPA()
{
return gpa;
}
public void setGPA(String gpa)
{
this.gpa = gpa;
}
}
//This code is same as Listing only name is changed.
//For Class Node: Changes are there as listing is now changed to Student
//Node Class Code :
package listing;
public class Node {
Student student;
Node next;
public Node()
{
}
}
//Singly List Code is same :
package listing;
public class SinglyLinkedList {
private Node h;
public SinglyLinkedList() {
h = new Node();
//h.student = null;
//h.next = null;
}
//Inserts a new Student record
public boolean insert(Student newStudent) {
Node n = new Node();
n.next = h.next;
h.next = n;
n.student = newStudent.deepCopy();
return true;
}
public Student fetch(String targetKey) {
Node p = h.next;
while (p != null && !(p.student.compareTo(targetKey) == 0)) {
p = p.next;
}
if (p != null)
return p.student.deepCopy();
else
return null;
}
public boolean delete(String targetKey) {
Node q = h;
Node p = h.next;
while (p != null && !(p.student.compareTo(targetKey) == 0)) {
q = p;
p = p.next;
}
if (p != null) {
q.next = p.next;
return true;
} else
return false;
}
public boolean update(String targetKey, Student newStudent) {
if (delete(targetKey) == false)
return false;
else if (insert(newStudent) == false)
return false;
return true;
}
public void showAll() {
Node p = h.next;
while (p!= null) {
System.out.println(p.student.toString());
p = p.next;
}
}
}
//Main Class Code
//Students records are inserted in SinglyLinkedList
package listing;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n1;
int n2;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the maximum size of the data set: ");
n1 = scan.nextInt();
System.out
.println("Please enter the number of students to be entered: ");
n2 = scan.nextInt();
// Checking for boundary Condition
while (n2 > n1) {
System.out
.println("No of students to be inserted exceeds no of students allowed");
System.out
.println("Please enter the number of students to be entered: ");
n2 = scan.nextInt();
}
SinglyLinkedList studentsList = new SinglyLinkedList();
for (int i = 0; i < n2; i++) {
System.out.println("Please enter the information for " + (i + 1)
+ ": ");
Scanner userInput = new Scanner(System.in);
System.out.println("Please enter the name: ");
String name = userInput.nextLine();
System.out.println("Please enter the number: ");
String number = userInput.nextLine();
System.out.println("Please enter a GPA:");
String gpa = userInput.nextLine();
Student newStudent = new Student(name, number, gpa);
// Records to be inserted in Linked List
studentsList.insert(newStudent);
}
int option;
String name = null;
while (true) {
System.out
.println("Select 1 to insert a new student's information");
System.out
.println("Select 2 to fetch and output a student's information");
System.out.println("Select 3 to delete a student's information");
System.out
.println("Select 4 to update a new student's information");
System.out
.println("Select 5 to output all the student information in sorted order");
System.out.println("Select 6 to exit the program");
System.out.println("Please select an option: ");
option = scan.nextInt();
scan.nextLine();
switch (option) {
case 1:
//If List full
if (n2 == n1) {
System.out.println("No new student can be added.");
break;
}
System.out
.println("Please enter the info for the new Student: ");
System.out.println("Please enter the name: ");
String newName = scan.nextLine();
System.out.println("Please enter the number: ");
String number = scan.nextLine();
System.out.println("Please enter a GPA:");
String gpa = scan.nextLine();
Student newStudent = new Student(newName, number, gpa);
studentsList.insert(newStudent);
System.out.println("Operation Finished");
//Counting the no of students inserted in list.
n2++;
break;
case 2:
System.out.println("Please enter the name to fetch: ");
String nameFetched = scan.nextLine();
Student studentFetched = studentsList.fetch(nameFetched);
if (null != studentFetched) {
System.out.println(studentFetched.toString());
} else {
//Wrong Name input.
System.out
.println("No student record found with the name :"
+ nameFetched);
}
System.out.println("Operation Finished");
break;
case 3:
System.out.println("Please enter the name to delete: ");
name = scan.nextLine();
boolean deleteFlag = studentsList.delete(name);
if (deleteFlag)
System.out.println("Student deleted Successfully");
else
//Wrong name input.
System.out
.println("Student deletion failed. Please check the name given by you. ");
System.out.println("Operation Finished");
break;
case 4:
System.out.println("Please enter the name to update: ");
name = scan.nextLine();
System.out.println("Please enter the number :");
number = scan.nextLine();
System.out.println("Please enter a GPA :");
gpa = scan.nextLine();
Student newStudent1 = new Student(name, number, gpa);
boolean updateFlag = studentsList.update(name, newStudent1);
if (updateFlag)
System.out.println("Student updated Successfully");
else
//Wrong name input
System.out
.println("Student update failed. Please check the name given by you. ");
System.out.println("Operation Finished");
break;
case 5:
studentsList.showAll();
System.out.println("Operation Finished");
break;
case 6:
System.exit(0);
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.