Checking if value in linked list is equal to String at each node - java

I'm writing a program for a homework assignment that has a binary search tree (Roster) that Student objects are inserted to by their String Id. Each student has a linked list that their courses are added to containing the String of the course and their grade. The binary search tree is my own version of an implementation.
I'm having trouble implementing my method to print the students that all contain a specific course. I think my implementation is off in my printCourseHelper() method as the if condition isn't working properly to check if the value in the list at each node is equal to the given value.
I'm looking for all student's enrolled in the course "Math161" which should be 3, and the String ID's of the student's in that class would be printed. I'm not getting any errors when I run my program, however only the functions being called above my displayStudents() are printing.
My issues I believe are in my BST.java, printCourse and printCourseHelper methods:
public void printCourse(String course) {
printCourseHelper(root, course);
}
public void printCourseHelper(Node n, String course) {
if(n.element.getCourseList().contains(course)) {
System.out.print(n.element.getId() + " ");
}
if (n.left != null) {
printCourseHelper(n.left, course);
}
if (n.right != null) {
printCourseHelper(n.right, course);
}
}
Homework5.class / main:
import java.util.LinkedList;
public class Homework5 {
static Roster rost = new Roster();
public static void main(String[] args) {
addStudent();
displayAllStudents();
lookupStudent("11114");
addCourse();
displayStudents("Math161");
}
// add students to the roster
static void addStudent() {
rost.addStudent(new Student("11111", "Jon", "Benson"));
rost.addStudent(new Student("11112", "Erick", "Hooper"));
rost.addStudent(new Student("11113", "Sam", "Shultz"));
rost.addStudent(new Student("11114", "Trent", "Black"));
rost.addStudent(new Student("11115", "Michell", "Waters"));
rost.addStudent(new Student("11116", "Kevin", "Johnson"));
}
// display all students in the roster
static void displayAllStudents() {
rost.displayAllStudents();
}
// lookup a student in the roster
static void lookupStudent(String id) {
if (rost.find(id) != null) {
System.out.println(id + " found");
} else {
System.out.println(id + " not found");
}
}
// add courses to the roster
static void addCourse() {
rost.addCourse("11111", new Course("CS116", 80));
rost.addCourse("11111", new Course("Math161", 90));
rost.addCourse("11112", new Course("Math161", 70));
rost.addCourse("11112", new Course("CS146", 90));
rost.addCourse("11112", new Course("CS105", 85));
rost.addCourse("11113", new Course("CS216", 90));
rost.addCourse("11114", new Course("CIS255", 75));
rost.addCourse("11114", new Course("CS216", 80));
rost.addCourse("11114", new Course("Math161", 60));
rost.addCourse("11114", new Course("COMM105", 90));
}
// display students enrolled in a given course id
static void displayStudents(String courseId) {
rost.displayStudents(courseId);
}
// display courses taken by a student
static void displayCourses(String id) {
rost.displayCourses("id");
}
// display the average grade for a student
static void getCourseAverage(String courseId) {
rost.getCourseAverage(courseId);
}
// display the average grade for a student
static void dropCoursesBelow(String id, int grade) {
rost.dropCoursesBelow(id, grade);
}
// drop a course from a student
static void dropCourse(String id, String courseId) {
rost.dropCourse(id, courseId);
}
// change the grade for a student
static void changeGrade(String id, String courseId, int grade) {
rost.changeGrade(id, courseId, grade);
}
}
Student.class
class Student implements Comparable<Student> {
String id;
String firstName;
String lastName;
LinkedList<Course> courses = new LinkedList<>();
Student(String id, String fName, String lName) {
this.id = id;
this.firstName = fName;
this.lastName = lName;
}
public String getName() {
return lastName;
}
public void setName(String lName) {
this.lastName = lName;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
#Override
public int compareTo(Student other) {
return this.getId().compareTo(other.getId());
}
public void addCourse(Course course) {
courses.add(course);
}
public LinkedList<Course> getCourseList() {
return courses;
}
}
Course.class:
class Course {
LinkedList<Course> course = new LinkedList<>();
String id; // course id
int grade;
Course(String id, int grade) {
this.id = id;
this.grade = grade;
}
public String getCourseId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getGrade() {
return grade;
}
public void setId(int grade) {
this.grade = grade;
}
}
Roster.class:
class Roster {
Student root;
int numStudents;
BST<Student> roster = new BST<>();
LinkedList<Course> courseList = new LinkedList<>();
public Roster() {
root = null;
numStudents = 0;
}
public void addStudent(Student st) {
roster.insert(st);
numStudents++;
}
public void displayAllStudents() {
roster.traverse(2);
}
public Student find(String id) {
return roster.find(id);
}
public void addCourse(String id, Course course) {
Student student = roster.find(id);
student.addCourse(course);
}
public void displayStudents(String courseId) {
roster.printCourse(courseId);
}
}
BST.java
class BST<Roster extends Comparable> {
private Node root;
public BST() {
root = null;
}
// Generic find method
public Student find(String id) {
Node current = root;
// Loop until e.compare to current element is not equal to 0
while (id.compareTo(current.element.getId()) != 0) {
//!!! implement
// if e.compare is less than 0 set current to current.left
if (id.compareTo(current.element.getId()) < 0) {
current = current.left;
} // else if current is 0 or greater than 0 set current
// to current.right
else {
current = current.right;
}
// if current is null, return null
if (current == null) {
return null;
}
}
// return current value when loop ends
return current.element;
}
public void insert(Student st) {
Node newNode = new Node(st);
if (root == null) {
root = newNode;
} else {
Node current = root;
Node parent = null;
while (true) {
parent = current;
if (st.getId().compareTo(current.element.getId()) < 0) {
current = current.left;
if (current == null) {
parent.left = newNode;
return;
}
} else {
current = current.right;
if (current == null) {
parent.right = newNode;
return;
}
}
}
}
}
public void printCourse(String course) {
printCourseHelper(root, course);
}
public void printCourseHelper(Node n, String course) {
if(n.element.getCourseList().contains(course)) {
System.out.print(n.element.getId() + " ");
}
if (n.left != null) {
printCourseHelper(n.left, course);
}
if (n.right != null) {
printCourseHelper(n.right, course);
}
}
public void traverse(int traverseType) {
switch (traverseType) {
case 1:
System.out.print("\nPreorder traversal: ");
// call preOrder(root) and implement preOrder()
preOrder(root);
break;
case 2:
System.out.print("\nList of all students: ");
inOrder(root);
break;
case 3:
System.out.print("\nPostorder traversal: ");
// call postOrder(root) and implement postOrder()
postOrder(root);
break;
}
System.out.println();
}
private void inOrder(Node localRoot) {
if (localRoot != null) {
inOrder(localRoot.left);
System.out.print(localRoot.element.getId() + " ");
inOrder(localRoot.right);
}
}
private void preOrder(Node localRoot) {
if (localRoot != null) {
System.out.print(localRoot.element + " ");
preOrder(localRoot.left);
preOrder(localRoot.right);
}
}
private void postOrder(Node localRoot) {
if (localRoot != null) {
postOrder(localRoot.left);
postOrder(localRoot.right);
System.out.print(localRoot.element + " ");
}
}
}
class Node {
protected Student element;
protected Node left;
protected Node right;
public Node(Student st) {
element = st;
}
}

The issue is that you are trying to see if a LinkedList of type Course contains a String. The contains method for LinkedList takes an Object as it's parameter type and that is why you are not getting a compilation issue.
The below code snippet is never true because a Course will never equal a String. I mention equal here because internally the LinkedList contains method checks equality for the Object you pass in against the Objects it contains.
if(n.element.returnList().contains(course)) {
System.out.print(n.element.getId() + " ");
}
Possible Solution using Map
Change courses to be a Map in Student class and then change the IF statement to check if the Map contains an element based on the course name. If the Map contains an object then it's true that the Student takes this course.
class Student implements Comparable<Student> {
String id;
String firstName;
String lastName;
Map<String, Course> courses = new HashMap<>();
Student(String id, String fName, String lName) {
this.id = id;
this.firstName = fName;
this.lastName = lName;
}
public String getName() {
return lastName;
}
public void setName(String lName) {
this.lastName = lName;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
#Override
public int compareTo(Student other) {
return this.getId().compareTo(other.getId());
}
public void addCourse(Course course) {
courses.put(course.getId(), course);
}
public Map<String, Course> getCourses() {
return courses;
}
}
IF statement
if(n.element.getCourses().get(course) != null) {
System.out.print(n.element.getId() + " ");
}
Possible Solution using List
Add new method to Student class.
public boolean takesCourse(String courseName){
for(Course course : courses){
if(courseName.equals(course.getId)) {
return true;
}
}
return false;
}
IF statement
if(n.element.takesCourse(course)) {
System.out.print(n.element.getId() + " ");
}

Related

Printing linked list from binary search tree

I'm writing a program that has a binary search tree (Roster) that Student objects are inserted to by their String Id. Each student has a linked list that their courses are added to containing the String of the course and their grade. The binary search tree is my own version of an implementation.
I'm having trouble implementing my method to print the students that all contain a specific course. I think my implementation is off in my printCourse method as I can't call to it from my displayStudent() method under my Roster class. This is a homework assignment and I have most of the other methods implemented, just struggling with this one, any help is greatly appreciated!
displayStudents("Math161");
is the method i'm correctly struggling to implement. It calls from the method in my roster class, which i'm trying to implement to search through my BST
In my BST the method printCourse() should check each students linked list and if it contains the course listed above and print each student that does. Here's what I have so far, which is not correct:
> public void printCourse(Node n, String course) {
> if (n != null) {
> inOrder(n);
> if (n.element.getId().equals(course)) {
> System.out.print(n.element.getId() + " ");
> }
> }
> }
Homework5.class / Main:
public class Homework5 {
static Roster rost = new Roster();
public static void main(String[] args) {
addStudent();
displayAllStudents();
lookupStudent("11114");
addCourse();
displayStudents("Math161");
}
// add students to the roster
static void addStudent() {
rost.addStudent(new Student("11111", "Jon", "Benson"));
rost.addStudent(new Student("11112", "Erick", "Hooper"));
rost.addStudent(new Student("11113", "Sam", "Shultz"));
rost.addStudent(new Student("11114", "Trent", "Black"));
rost.addStudent(new Student("11115", "Michell", "Waters"));
rost.addStudent(new Student("11116", "Kevin", "Johnson"));
}
// display all students in the roster
static void displayAllStudents() {
rost.displayAllStudents();
}
// lookup a student in the roster
static void lookupStudent(String id) {
if (rost.find(id) != null) {
System.out.println(id + " found");
} else {
System.out.println(id + " not found");
}
}
// add courses to the roster
static void addCourse() {
rost.addCourse("11111", new Course("CS116", 80));
rost.addCourse("11111", new Course("Math161", 90));
rost.addCourse("11112", new Course("Math161", 70));
rost.addCourse("11112", new Course("CS146", 90));
rost.addCourse("11112", new Course("CS105", 85));
rost.addCourse("11113", new Course("CS216", 90));
rost.addCourse("11114", new Course("CIS255", 75));
rost.addCourse("11114", new Course("CS216", 80));
rost.addCourse("11114", new Course("Math161", 60));
rost.addCourse("11114", new Course("COMM105", 90));
}
// display students enrolled in a given course id
static void displayStudents(String courseId) {
rost.displayStudents(courseId);
}
}
Student.class:
class Student implements Comparable<Student> {
String id;
String firstName;
String lastName;
LinkedList<Course> courses = new LinkedList<>();
Student(String id, String fName, String lName) {
this.id = id;
this.firstName = fName;
this.lastName = lName;
}
public String getName() {
return lastName;
}
public void setName(String lName) {
this.lastName = lName;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
#Override
public int compareTo(Student other) {
return this.getId().compareTo(other.getId());
}
public void addCourse(Course course) {
courses.add(course);
}
}
Course.class:
class Course {
LinkedList<Course> course = new LinkedList<>();
String id; // course id
int grade;
Course(String id, int grade) {
this.id = id;
this.grade = grade;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getGrade() {
return grade;
}
public void setId(int grade) {
this.grade = grade;
}
}
Roster.class:
class Roster {
Student root;
int numStudents;
BST<Student> roster = new BST<>();
LinkedList<Course> courseList = new LinkedList<>();
public Roster() {
root = null;
numStudents = 0;
}
public void addStudent(Student st) {
roster.insert(st);
numStudents++;
}
public void displayAllStudents() {
roster.traverse(2);
}
public Student find(String id) {
return roster.find(id);
}
public void addCourse(String id, Course course) {
Student student = roster.find(id);
student.addCourse(course);
}
public void displayStudents(String courseId) {
roster.printCourse(courseId);
}
}
BST.java
class BST<Roster extends Comparable> {
private Node root;
public BST() {
root = null;
}
// Generic find method
public Student find(String id) {
Node current = root;
while (id.compareTo(current.element.getId()) != 0) {
if (id.compareTo(current.element.getId()) < 0) {
current = current.left;
}
else {
current = current.right;
}
if (current == null) {
return null;
}
}
return current.element;
}
public void insert(Student st) {
Node newNode = new Node(st);
if (root == null) {
root = newNode;
} else {
Node current = root;
Node parent = null;
while (true) {
parent = current;
if (st.getId().compareTo(current.element.getId()) < 0) {
current = current.left;
if (current == null) {
parent.left = newNode;
return;
}
} else {
current = current.right;
if (current == null) {
parent.right = newNode;
return;
}
}
}
}
}
public void printCourse(Node n, String course) {
if (n != null) {
inOrder(n);
if (n.element.getId().equals(course)) {
System.out.print(n.element.getId() + " ");
}
}
}
public void traverse(int traverseType) {
switch (traverseType) {
case 1:
System.out.print("\nPreorder traversal: ");
// call preOrder(root) and implement preOrder()
preOrder(root);
break;
case 2:
System.out.print("\nList of all students: ");
inOrder(root);
break;
case 3:
System.out.print("\nPostorder traversal: ");
// call postOrder(root) and implement postOrder()
postOrder(root);
break;
}
System.out.println();
}
private void inOrder(Node localRoot) {
if (localRoot != null) {
inOrder(localRoot.left);
System.out.print(localRoot.element.getId() + " ");
inOrder(localRoot.right);
}
}
private void preOrder(Node localRoot) {
if (localRoot != null) {
System.out.print(localRoot.element + " ");
preOrder(localRoot.left);
preOrder(localRoot.right);
}
}
private void postOrder(Node localRoot) {
if (localRoot != null) {
postOrder(localRoot.left);
postOrder(localRoot.right);
System.out.print(localRoot.element + " ");
}
}
}
class Node {
protected Student element;
protected Node left;
protected Node right;
public Node(Student st) {
element = st;
}
}
I don't know if this is everything, but two things that I see that seem wrong are:
In the Roster.class: The roster.printCourse(courseId) call in displayStudents() only includes one parameter, the courseId, but the printCourse() method in BST.java has two parameters, a Node and a String.
In the Roster.class: The BST called roster is given a type <Student>, but the definition of BST in BST.java has it containing a Roster, not a Student. Note: You don't need to use <> with a type inside unless that type can change. If BST can only be applied to the type Student, then you don't need that in the definition or the creation of BST. On the other hand, if your BST should work for any type of data, you should define the BST as class BST <T extends Comparable> and make sure to use T wherever the type should go. (Look up Generic Types on Google for more examples.)

Class avg results in 0 from searching students in binary search tree enrolled in courses

This is a homework assignment, and i'm pretty stumped on why this bug is occuring. I have a binary search tree (Roster) in which student objects are added to. Students are then placed in courses (added with linked list). I was able to add students into the roster and place the students in courses and print those. However, when I try to find the average grade of students I just keep getting the output as 0. I believe the max is not adding properly and integer is a higher value resulting in the output being 0.0. I added other print methods and max is printing as 0.0 and counter is printing as 3. Any idea's why?
Output:
List of all students: 11111 11112 11113 11114 11115 11116
11114 found
Students enrolled in Math161: 11111 11112 11114
11112 classes: Math161 Grade: 70 CS146 Grade: 90 CS105 Grade: 85
Average grade of Math161 is 0.0
0.0 3
Homework5.java / main:
package homework5;
import java.util.LinkedList;
public class Homework5 {
static Roster rost = new Roster();
public static void main(String[] args) {
addStudent();
displayAllStudents();
lookupStudent("11114");
addCourse();
System.out.print("Students enrolled in Math161: ");
displayStudents("Math161");
System.out.println();
displayCourses("11112");
getCourseAverage("Math161");
}
// add students to the roster
static void addStudent() {
rost.addStudent(new Student("11111", "Jon", "Benson"));
rost.addStudent(new Student("11112", "Erick", "Hooper"));
rost.addStudent(new Student("11113", "Sam", "Shultz"));
rost.addStudent(new Student("11114", "Trent", "Black"));
rost.addStudent(new Student("11115", "Michell", "Waters"));
rost.addStudent(new Student("11116", "Kevin", "Johnson"));
}
// display all students in the roster
static void displayAllStudents() {
rost.displayAllStudents();
}
// lookup a student in the roster
static void lookupStudent(String id) {
if (rost.find(id) != null) {
System.out.println(id + " found");
} else {
System.out.println(id + " not found");
}
}
// add courses to the roster
static void addCourse() {
rost.addCourse("11111", new Course("CS116", 80));
rost.addCourse("11111", new Course("Math161", 90));
rost.addCourse("11112", new Course("Math161", 70));
rost.addCourse("11112", new Course("CS146", 90));
rost.addCourse("11112", new Course("CS105", 85));
rost.addCourse("11113", new Course("CS216", 90));
rost.addCourse("11114", new Course("CIS255", 75));
rost.addCourse("11114", new Course("CS216", 80));
rost.addCourse("11114", new Course("Math161", 60));
rost.addCourse("11114", new Course("COMM105", 90));
}
// display students enrolled in a given course id
static void displayStudents(String courseId) {
rost.displayStudents(courseId);
}
// display courses taken by a student
static void displayCourses(String id) {
rost.displayCourses(id);
}
// display the average grade for a student
static void getCourseAverage(String courseId) {
rost.getCourseAverage(courseId);
}
}
Student.class:
class Student implements Comparable<Student> {
String id;
String firstName;
String lastName;
double max;
LinkedList<Course> courses = new LinkedList<>();
Student(String id, String fName, String lName) {
this.id = id;
this.firstName = fName;
this.lastName = lName;
}
public String getName() {
return lastName;
}
public void setName(String lName) {
this.lastName = lName;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
#Override
public int compareTo(Student other) {
return this.getId().compareTo(other.getId());
}
public void addCourse(Course course) {
courses.add(course);
}
public LinkedList<Course> getCourseList() {
return courses;
}
public boolean hasCourse(String courseId) {
// Enhanced for loop
// Iterate through courses Linked List and print each course
for (Course course : courses) {
// if courseId is equal to courseId at the current courseId
// return true
if (courseId.equals(course.getCourseId())) {
return true;
}
}
return false;
}
public void getClassAverage(String courseId) {
for (Course course : courses) {
if (hasCourse(courseId)) {
max += course.getGrade();
}
}
}
public void printCourses() {
for (Course course : courses) {
System.out.print(course.getCourseId() +
" Grade: " + course.getGrade() + " ");
}
}
public void printAverage(String courseId, int avg) {
getClassAverage(courseId);
System.out.println(avg);
}
}
class Course {
String id; // course id
int grade;
Course(String id, int grade) {
this.id = id;
this.grade = grade;
}
public String getCourseId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getGrade() {
return grade;
}
public void setId(int grade) {
this.grade = grade;
}
}
Roster.class:
class Roster {
Student root;
int numStudents;
BST<Student> roster = new BST<>();
public Roster() {
root = null;
numStudents = 0;
}
public void addStudent(Student st) {
roster.insert(st);
numStudents++;
}
public void displayAllStudents() {
roster.traverse(2);
}
public Student find(String id) {
return roster.find(id);
}
public void addCourse(String id, Course course) {
Student student = roster.find(id);
student.addCourse(course);
}
public void displayStudents(String courseId) {
roster.printCourse(courseId);
}
public void displayCourses(String id) {
Student student = roster.find(id);
System.out.print(id + " classes: ");
student.printCourses();
}
public void getCourseAverage(String courseId) {
roster.getAverage(courseId);
}
}
BST.java:
package homework5;
class BST<Roster extends Comparable> {
private Node root;
double avg;
double max;
int counter;
public BST() {
root = null;
}
// Generic find method
public Student find(String id) {
Node current = root;
// Loop until e.compare to current element is not equal to 0
while (id.compareTo(current.element.getId()) != 0) {
//!!! implement
// if e.compare is less than 0 set current to current.left
if (id.compareTo(current.element.getId()) < 0) {
current = current.left;
} // else if current is 0 or greater than 0 set current
// to current.right
else {
current = current.right;
}
// if current is null, return null
if (current == null) {
return null;
}
}
// return current value when loop ends
return current.element;
}
public void insert(Student st) {
Node newNode = new Node(st);
if (root == null) {
root = newNode;
} else {
Node current = root;
Node parent = null;
while (true) {
parent = current;
if (st.getId().compareTo(current.element.getId()) < 0) {
current = current.left;
if (current == null) {
parent.left = newNode;
return;
}
} else {
current = current.right;
if (current == null) {
parent.right = newNode;
return;
}
}
}
}
}
public void printCourse(String course) {
printCourseHelper(root, course);
}
public void printCourseHelper(Node n, String course) {
if (n.element.hasCourse(course)) {
System.out.print(n.element.getId() + " ");
}
if (n.left != null) {
printCourseHelper(n.left, course);
}
if (n.right != null) {
printCourseHelper(n.right, course);
}
}
public void getAverage(String course) {
getAverageHelper(root, course);
avg = max / counter;
System.out.println("\nAverage grade of " + course + " is " + avg);
System.out.println(max + " " + counter);
}
public void getAverageHelper(Node n, String course) {
if (n.element.hasCourse(course)) {
n.element.getClassAverage(course);
counter++;
}
if (n.left != null) {
getAverageHelper(n.left, course);
}
if (n.right != null) {
getAverageHelper(n.right, course);
}
}
public void traverse(int traverseType) {
switch (traverseType) {
case 1:
System.out.print("\nPreorder traversal: ");
// call preOrder(root) and implement preOrder()
preOrder(root);
break;
case 2:
System.out.print("\nList of all students: ");
inOrder(root);
break;
case 3:
System.out.print("\nPostorder traversal: ");
// call postOrder(root) and implement postOrder()
postOrder(root);
break;
}
System.out.println();
}
// Recursive method - traverse generic BST
// While root is not equal to null visit left node and print value
// of root, then visit right node. Repeat until root becomes null
private void inOrder(Node localRoot) {
if (localRoot != null) {
inOrder(localRoot.left);
System.out.print(localRoot.element.getId() + " ");
inOrder(localRoot.right);
}
}
// Recursive method - traverse generic BST
// While root is not equal to null print the value of the root
// and visit left then right nodes. Repeat until root becomes null
private void preOrder(Node localRoot) {
if (localRoot != null) {
System.out.print(localRoot.element + " ");
preOrder(localRoot.left);
preOrder(localRoot.right);
}
}
// Recursive method - traverse generic BST
// While root is not equal to null visit left node and visit
// the right node and print value of root. Repeat until root becomes null
private void postOrder(Node localRoot) {
if (localRoot != null) {
postOrder(localRoot.left);
postOrder(localRoot.right);
System.out.print(localRoot.element + " ");
}
}
}
class Node {
protected Student element;
protected Node left;
protected Node right;
public Node(Student st) {
element = st;
}
}

Binary Search Tree find method returning not found

This is a homework assignment i'm working on and I'm having a little trouble with it. I've implemented my own version of a binary search tree rather than using JDK. I'm inserting multiple student objects into the binary search tree in terms of the student's ID, which is a type string. I'm not getting any compile errors but the program keeps returning that the value is not found when it should be the fourth student that was inserted into the tree. I've implemented all of the find methods already, but not sure where I'm going wrong with them, since the output should be saying that the value was found.
Output:
run:
11114 not found
BUILD SUCCESSFUL (total time: 0 seconds)
Homework5.class / main:
package homework5;
import java.util.LinkedList;
public class Homework5 {
static Roster rost = new Roster();
public static void main(String[] args) {
addStudent();
lookupStudent("11114");
}
// add students to the roster
static void addStudent() {
rost.addStudent(new Student("11111", "Jon", "Benson"));
rost.addStudent(new Student("11112", "Erick", "Hooper"));
rost.addStudent(new Student("11113", "Sam", "Shultz"));
rost.addStudent(new Student("11114", "Trent", "Black"));
rost.addStudent(new Student("11115", "Michell", "Waters"));
rost.addStudent(new Student("11116", "Kevin", "Johnson"));
}
// lookup a student in the roster
static void lookupStudent(String id) {
if (rost.find(id) != null) {
System.out.println(id + " found");
} else {
System.out.println(id + " not found");
}
}
}
Student.class
class Student implements Comparable<Student> {
String id;
String firstName;
String lastName;
Student(String id, String fName, String lName) {
this.id = id;
this.firstName = fName;
this.lastName = lName;
}
public String getName() {
return lastName;
}
public void setName(String lName) {
this.lastName = lName;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
#Override
public int compareTo(Student other) {
return this.getId().compareTo(other.getId());
}
public void addCourse(String id) {
LinkedList list = new LinkedList();
list.add(id);
}
}
Roster.class
class Roster {
Student root;
int numStudents;
BST<Student> roster = new BST<>();
public Roster() {
root = null;
numStudents = 0;
}
public void addStudent(Student st) {
roster.insert(st);
numStudents++;
}
public Student find(String id) {
roster.find(id);
return null;
}
BST.java
package homework5;
class BST<Roster extends Comparable> {
private Node root;
public BST() {
root = null;
}
// Generic find method
public Node find(String id) {
Node current = root;
while (id.compareTo(current.element.getId()) != 0) {
if (id.compareTo(current.element.getId()) < 0) {
current = current.left;
}
else {
current = current.right;
}
if (current == null) {
return null;
}
}
return current;
}
public void insert(Student st) {
Node newNode = new Node(st);
if (root == null) {
root = newNode;
} else {
Node current = root;
Node parent = null;
while (true) {
parent = current;
if (st.compareTo(current.element) < 0) {
current = current.left;
if (current == null) {
parent.left = newNode;
return;
}
} else {
current = current.right;
if (current == null) {
parent.right = newNode;
return;
}
}
}
}
}
// Recursive method - traverse generic BST
// While root is not equal to null visit left node and print value
// of root, then visit right node. Repeat until root becomes null
private void inOrder(Node localRoot) {
if (localRoot != null) {
inOrder(localRoot.left);
System.out.print(localRoot.element + " ");
inOrder(localRoot.right);
}
}
}
class Node {
protected Student element;
protected Node left;
protected Node right;
public Node(Student st) {
element = st;
}
}
your code looks fine... a few misconceptions like the comments suggested, just change those couple of lines
public Student find(String id) {
return roster.find(id);
}
and return current.element from roster.find() change the signature to public Student find(String id)
Note: keep in mind this will effect the generic property of the code, a better approach is to implement node as a generic data container e.g Node<Student> make roster.find() return a Node object and get the data from the node so you can return Student object at the end
or at least change only this function instead (it's a better solution than mine)
public Student find(String id) {
return roster.find(id).element;
}
Roster.find() always returns null.
Change it to
public Student find(String id) {
return roster.find(id).element;
}

How to pass node as a parameter

I have a custom linked list that has node objects of class Student. There are two recursive methods called countNodesRec(Node list) & worstStudentRec(Node List) that both need node object as a parameter.
list1.worstStudentRec(?????)
list1.countNodesRec(????)
Parameters I tried already that have given me errors
list1.list
list1
Not sure what to put there, please help!
Test Class
public class TestList {
public static void main(String[] args) {
Student s1 = new Student("Adams", 3.9, 26);
Student s2 = new Student("Lewis", 2.1, 29);
Student s3 = new Student("Lopez", 4.0, 53);
Student s4 = new Student("Smith", 3.2, 22);
Student s5 = new Student("Zeeler", 3.6, 38);
LinkedList list1 = new LinkedList();
LinkedList list2 = new LinkedList();
LinkedList list3 = new LinkedList();
//1
list1.addFront(s1);
list1.addFront(s2);
list1.addFront(s3);
list1.addFront(s4);
list1.addFront(s5);
list1.printLinkedList();
System.out.println("Worst Student" + list1.worstStudentRec());
System.out.println("Number of Students" + list1.countNodesRec());
}
}
Student Class
public class Student
{
private String lastName;
private double gpa;
private int age;
public Student(String lastName, double gpa, int age)
{
this.lastName = lastName;
this.gpa = gpa;
this.age = age;
}
public int compareTo(Student s)
{
if (gpa < s.gpa)
{
return -1;
}
else if (gpa > s.gpa)
{
return 1;
}
else
{
return 0;
}
}
public String toString()
{
return lastName + "\t" + gpa + "\t" + age;
}
public double getGpa()
{
return gpa;
}
}
Linked List Class
public class LinkedList
{
private class Node
{
public Student data;
public Node next;
public Node(Student s)
{
data = s;
next = null;
}
}
private Node list;
public LinkedList()
{
list = null;
}
public Student bestStudent()
{
Student bestStudent, bstStu;
Node current;
if (list == null)
{
return bestStudent = null;
}
else
{
current = list;
bstStu = new Student("", 0.00, 0);
while (current != null)
{
if (bstStu.getGpa() <= current.data.getGpa())
{
bstStu = current.data;
}
current = current.next;
}
bestStudent = bstStu;
}
return bestStudent;
}
public int countNodesRec(Node list)
{
if(list == null)
{
return 0;
}
else
{
return 1 + countNodesRec(list.next);
}
}
public Student worstStudentRec(Node list)
{
if (list == null)
{
return null;
}
else if (list.next == null)
{
return list.data;
}
Student worstStudent = worstStudentRec(list.next);
return (list.data.compareTo(worstStudent) <= 0) ? list.data : worstStudent;
}
}
You can create a getList method in Linked List class, then call the method in main:
list1.worstStudentRec(list1.getList ())
list1.countNodesRec(list1.getList ())

Returning object with max value inside of a linked List java

I'm working on method bestStudent(). I return the student with the highest GPA. I keep getting an error Student cannot be converted to LinkedList.node. I'm trying to use a while loop to return the object Student with the highest gpa.
public class LinkedListStud
{
private Node list;
public LinkedListStud()
{
list = null;
}
public boolean isEmpty()
{
return (list == null);
}
public void addFront(Student s)
{
Node newNode = new Node(s);
newNode.next = list;
list = newNode;
}
public void addTail(Student s)
{
Node newNode = new Node(s);
Node current;
if (isEmpty())
list = newNode;
else
{
current = list;
while(current.next !=null)
current = current.next;
current.next = newNode;
}
}
public Student bestStudent()
{
Node current = list;
while (current.next != null)
if (current.data.getGPA() > current.next.data.getGPA())
return current = current.data;
}
public void printLinkedList()
{
Node l = list;
while (list != null)
l.toString();
System.out.println("next Link: " + l.next);
l = l.next;
System.out.println();
}
private class Node
{
public Student data;
public Node next;
public Node(Student s)
{
data = s;
next = null;
}
}
}
public class Student implements Comparable<Student>
{
public String lastName;
public double gpa;
public int age;
public Student(String lastName, double gpa, int age)
{
this.lastName = lastName;
this.gpa = gpa;
this.age = age;
}
public int compareTo(Student s)
{
return (this.gpa < s.gpa) ? -1 : (this.gpa > s.gpa) ? 1: 0;
}
public String toString()
{
return "Student: \t" + lastName + "GPA: \t" + gpa+ "Age: \t" + age;
}
public double getGPA()
{
return gpa;
}
}
You should do something along the lines of below in order to make that method work.
public Student bestStudent() {
Node current = list;
Student bestStudent = null;
while(current.next != null) {
if(bestStudent == null || (current.next != null && (current.data.getGPA() > current.next.data.getGPA()))) {
bestStudent = current.data;
}
}
return bestStudent;
}

Resources