Java 通过在二元搜索树中搜索注册课程的学生,类平均结果为0
这是一个家庭作业,我很困惑为什么会出现这个错误。我有一个二叉搜索树(花名册),其中添加了学生对象。然后将学生放入课程(添加链接列表)。我可以把学生加入名册,把学生安排在课程中,然后打印出来。然而,当我试图找到学生的平均成绩时,我总是得到0的结果。我认为max没有正确相加,integer是一个更高的值,导致输出为0.0。我添加了其他打印方法,max打印为0.0,计数器打印为3。知道原因吗 输出:Java 通过在二元搜索树中搜索注册课程的学生,类平均结果为0,java,linked-list,binary-search-tree,Java,Linked List,Binary Search Tree,这是一个家庭作业,我很困惑为什么会出现这个错误。我有一个二叉搜索树(花名册),其中添加了学生对象。然后将学生放入课程(添加链接列表)。我可以把学生加入名册,把学生安排在课程中,然后打印出来。然而,当我试图找到学生的平均成绩时,我总是得到0的结果。我认为max没有正确相加,integer是一个更高的值,导致输出为0.0。我添加了其他打印方法,max打印为0.0,计数器打印为3。知道原因吗 输出: List of all students: 11111 11112 11113 11114 1111
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);
}
}
学生.班级:
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;
}
}
班级学生实施可比较的{
字符串id;
字符串名;
字符串lastName;
双峰;
LinkedList课程=新建LinkedList();
学生(字符串id、字符串fName、字符串lName){
this.id=id;
this.firstName=fName;
this.lastName=lName;
}
公共字符串getName(){
返回姓氏;
}
public void setName(字符串lName){
this.lastName=lName;
}
公共字符串getId(){
返回id;
}
公共无效集合id(字符串id){
this.id=id;
}
@凌驾
公共国际比较(学生其他){
将此.getId().compareTo(其他.getId())返回;
}
公共课程(课程){
课程。添加(课程);
}
公共链接列表getCourseList(){
返回课程;
}
公共布尔hasCourse(字符串courseId){
//增强for循环
//反复浏览课程链接列表并打印每门课程
用于(课程:课程){
//如果courseId等于当前courseId的courseId
//返回真值
if(courseId.equals(course.getCourseId())){
返回true;
}
}
返回false;
}
公共void getClassAverage(字符串courseId){
用于(课程:课程){
if(hasCourse(courseId)){
max+=course.getGrade();
}
}
}
公共课程(){
用于(课程:课程){
System.out.print(course.getCourseId()+
“成绩:“+course.getGrade()+”);
}
}
公共无效打印平均值(字符串courseId,平均整数){
getClassAverage(courseId);
系统输出打印项次(平均值);
}
}
班级课程{
字符串id;//课程id
国际等级;
课程(字符串id,整数等级){
this.id=id;
这个。等级=等级;
}
公共字符串getCourseId(){
返回id;
}
公共无效集合id(字符串id){
this.id=id;
}
public int getGrade(){
返回等级;
}
公共无效集合ID(整数级){
这个。等级=等级;
}
}
花名册.类别:
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花名册=新的BST();
公共名册(){
root=null;
numStudents=0;
}
公立学校学生(学生街){
名册.插入(st);
numStudents++;
}
公共图书馆所有学生(){
名册.导线测量(2);
}
公共学生查找(字符串id){
返回花名册。查找(id);
}
public void addCourse(字符串id,Course){
学生=花名册。查找(id);
学生。添加课程(课程);
}
公共空白显示学生(字符串courseId){
名册.印刷课程(courseId);
}
公共课程(字符串id){
学生=花名册。查找(id);
系统输出打印(id+“类:”);
学生。打印课程();
}
公共无效getCourseAverage(字符串courseId){
花名册。获取平均值(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;
}
}
package家庭作业5;
BST级{
私有节点根;
双平均值;
双峰;
整数计数器;
公共BST(){
root=null;
}
//通用查找方法
公共学生查找(字符串id){
节点电流=根;
//循环直到e。与当前元素的比较不等于0
while(id.compareTo(current.element.getId())!=0){
//!!!执行
//如果e.compare小于0,则将current设置为current.left
if(id.compareTo(current.element.getId())<0){
current=current.left;
}//否则,如果当前值为0或大于0,则设置当前值
//对,对
否则{
current=current.right;
}
//如果当前值为null,则返回null
如果(当前==null){
返回null;
}
}
//循环结束时返回当前值
返回当前元素;
}
公共空间插入(学生街){
节点newNode=新节点(st);
if(root==null){
根=新节点;
}否则{
节点电流=根;
节点父节点=null;
while(true){
父项=当前;
if(st.getId().compareTo(current.element.getId())<0){
current=current.left;
如果(当前==null){
parent.left=newNode;
返回;
}
}否则{
current=current.right;
如果(当前==null){
parent.right=newNode;
返回;
}
}
}
}
}
公共课程(字符串课程){
printCourseHelper(根,课程);
}
public void printCourseHelper(节点n,字符串课程){
如果(n.element.hasCourse(课程)){
System.out.print(n.element.getId()+);
}
如果(n.left!=null){