gpt4 book ai didi

java - 在二叉搜索树中声明学生名册时出现 ExceptionInInitialize 错误

转载 作者:行者123 更新时间:2023-12-02 11:21:49 24 4
gpt4 key购买 nike

我正在努力初始化包含学生的二叉搜索树名册。我试图首先将学生添加到名册中,但似乎无法让名册正常工作。我还创建了自己的 BST 类,而不是使用 java.utils。我将在下面发布我的所有代码。如何正确初始化我的名册 BST 以添加学生?

编译后出错:

run:
java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - Erroneous
tree type: roster.BST
at Roster.Roster.<init>(Roster.java:152)
at Roster.Roster_Test.<clinit>(Roster.java:6)
Exception in thread "main"
BUILD FAILED (total time: 0 seconds)

编译后我的错误发生在以下行:

名册.类(class):

  BST rost = new BST();

以及上面的主要方法:

  static Roster rost = new Roster();

主类:

package roster;
import java.util.LinkedList;

public class Roster_Test {

static Roster rost = new Roster();

public static void main(String[] args) {

addStudent();
displayAllStudents();
/*lookupStudent("11114");
addCourse();
displayStudents("Math161");
displayCourses("11112");
getCourseAverage("Math161");
dropCoursesBelow("11114", 65);
getCourseAverage("Math161");
dropCourse("11111", "Math161");
getCourseAverage("Math161");
changeGrade("11112", "Math161", 80);
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.inOrder();
}

// 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);
}

}

学生.类(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 int compareTo(Student st) {
int lastName = this.lastName.compareTo(st.getName());

if (lastName > 0) {
return 1;
} else if (lastName < 0) {
return -1;
} else {
return 0;
}
}

public void addCourse(String id) {
LinkedList list = new LinkedList();
list.add(id);
}

}

类(class).class:

class Course {
String id; // course id
int grade;

Course(String id, int grade) {
this.id = id;
this.grade = grade;
}

}

名册.类(class):

class Roster {
Student root;
int numStudents;

BST rost = new BST();

public Roster() {
root = null;
numStudents = 0;
}

public void addStudent(Student st) {
rost.insert(st);
numStudents++;
}

}

BST.java

package roster;

class BinarySearchTree<E extends Comparable<E>> {

private Node<E> root;

public BinarySearchTree() {
root = null;
}

// Generic find method
public Node find(E e) {
Node<E> current = root;

// Loop until e.compare to current element is not equal to 0
while (e.compareTo(current.element) != 0) {
// if e.compare is less than 0 set current to current.left
if (e.compareTo(current.element) < 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;
}

// Check whether the generic value was found and return true or false
public boolean findTF(E e) {

// if find(e) returns a value return true
// else return false if value was not found
if (find(e) != null) {
return true;
} else {
return false;
}

}

public void insert(E e) {
Node<E> newNode = new Node<>(e);

if (root == null) {
root = newNode;
} else {
Node<E> current = root;
Node<E> parent = null;

while (true) {
parent = current;
if (e.compareTo(current.element) < 0) {
current = current.left;
if (current == null) {
parent.left = newNode;
return;
}
} else {
current = current.right;
// if current is equal to null,
// set parent.right to newNode and return
if (current == null) {
parent.right = newNode;
return;
}
}
}
}
}

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("\nInorder traversal: ");
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<E> localRoot) {
if (localRoot != null) {
inOrder(localRoot.left);
System.out.print(localRoot.element + " ");
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<E> 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<E> localRoot) {
if (localRoot != null) {
postOrder(localRoot.left);
postOrder(localRoot.right);
System.out.print(localRoot.element + " ");
}
}

}

class Node<E> {

protected E element;
protected Node<E> left;
protected Node<E> right;

public Node(E e) {
element = e;
}
}

最佳答案

由于错误Caused by: java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: roster.BST 指示 -

您正在运行的代码中,一个/多个 java 文件尚未成功或正确编译。

我看到的一件事是,您正在使用 BST rost = new BST();,但您没有任何名为 BST 的类。不应该是 BinarySearchTree rost = new BinarySearchTree () 吗?

随后,清理/删除所有旧的类文件重新编译并尝试。

关于java - 在二叉搜索树中声明学生名册时出现 ExceptionInInitialize 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49855921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com