我仍然是一个新手程序员,所以请尽可能描述性。我在让所有代码都用于我的任务时遇到问题。作业给了我四个不需要更改的完整文件(StudentIf.java StudentCollectionIf.java StudentLLNode.java和StudentRecords.java)和一个测试文本文件,其中包含名称列表,ID和五个成绩。
作业说明链接:https://www.cs.colostate.edu/~cs161/Fall14/more_assignments/P5/P5.html
我修改的两个文件是 Student.java 和 StudentLL.java同样在我的运行配置参数中,我有"cs161 5"我知道这很多,但任何帮助都会很棒。
我目前得到这个输出:(最高分和平均分尚未实现)
Course cs161: 5 grades
Top Score: 10.0
Avg Score: 10.0
Course: null
null 0 score: 81.00
应该生产什么
Course cs161: 5 grades
Top Score: 90.0
Ave Score: 76.16
Course: cs161
Jim 1234 50 40 50 60 70 score: 54.00
John 1243 60 70 80 55 55 score: 64.00
Mick 1324 70 60 70 80 90 score: 74.00
Mike 1342 60 70 80 90 99 score: 79.80
Lena 1423 99 50 90 90 85 score: 82.80
Leila 1432 60 70 60 70 60 score: 64.00
Ada 2134 90 90 90 90 90 score: 90.00
Adam 2143 85 95 85 75 65 score: 81.00
Helen 2314 89 79 99 89 88 score: 88.80
Ellen 2341 90 95 88 77 66 score: 83.20
我的学生.java和学生LL代码
.javaimport java.text.DecimalFormat;
import java.util.Arrays;
public class Student implements StudentIF{
private String name;
private int id;
private int[] grades;
private int numGrades;
private int totalGrades;
// The constructor
// initializes the instance variables
// name, id, grades = new int[totalGrades], and numGrades = 0;
public Student (String name, int id, int totalGrades){
name = this.getName();
id = this.getId();
grades = new int[totalGrades];
numGrades= 0;
//System.out.println(name+" "+id+" "+grades[0]+" "+grades[1]+" "+grades[2]+" "+grades[3]+" "+grades[4]+" "+" "+totalGrades);
}
public String toString() {
String res = name + "t" + id + " ";
for (int i=0; i < totalGrades; i++) {
res += " " + grades[i];
}
res += "tscore: " + new DecimalFormat("0.00").format(computeScore());
return res;
}
@Override
public int compareTo(StudentIF arg0) {
if (arg0 == null) {
return 1;
}
if (this.id > arg0.getId())
return 1;
else if (this.id < arg0.getId())
return -1;
else
return 0;
}
@Override
public String getName() {
return name;
}
@Override
public int getId() {
return id;
}
@Override
public double computeScore() {
double total = 0;
if (numGrades == 0) {
return total;
}
if (numGrades > grades.length) {
numGrades = grades.length;
}
for (int i = 0; i < numGrades; i++) {
total += grades[i];
}
return total / numGrades;
//return 0;
}
@Override
public boolean addGrade(int newGrade) {
if(numGrades<grades.length){
grades[numGrades] = newGrade;
numGrades++;
// System.out.println(grades[0]+" "+grades[1]+" "+grades[2]+" "+grades[3]+" "+grades[4]);
return true;
}
return false;
}
@Override
public boolean equals(StudentIF other) {
if (other.getId() == this.getId()) {
return true;
}
return false;
}
}
========
public class StudentLL implements StudentCollectionIF{
private String course;
private StudentLLNode head;
private int size;
private boolean debug; // you can set debug in main
// the client code provides the course name
public StudentLL(String course){
course = this.course;
}
public String toString(){
String res = "Course: " + course + "n";
for(StudentLLNode curr = head; curr !=null; curr=curr.getNext()){
StudentIF nS = curr.getStd();
res = res + nS + "n";
}
return res;
}
@Override
public boolean insort(StudentIF s) {
StudentLLNode curr = head;
if (s == null) {
return false;
}
if (head == null) {
StudentLLNode student = new StudentLLNode(s);
head = student;
size++;
//System.out.println("working");
return true;
} else {
if (curr.getStd().compareTo(s) == 0) {
//System.out.println("working");
return false;
}
while (curr.getNext() != null) {
if(s.compareTo(curr.getStd()) == 1){
//c
}
curr = curr.getNext();
}
//c
StudentLLNode student1 = new StudentLLNode(s);
curr.setNext(student1);
size++;
return true;
}
}
@Override
public boolean remove(StudentIF s) {
StudentLLNode current = head;
if(s == null){
return false;
}
if(s.getId() == (head.getStd().getId())){
//StudentLLNode top = head;
head = head.getNext();
size--;
return true;
}
else{
StudentLLNode previous, next;
previous = current;
current = current.getNext();
while(current != null){
next = current.getNext();
if(s.getId() == (current.getStd().getId())){
previous.setNext(next); //doesn't matter if next is null or not
size--;
return true;
}
previous = current;
current = next;
}
}
return false;
}
@Override
public int size() {
// TODO Auto-generated method stub
return size;
}
@Override
public double classAvg() {
//double total = 0.0;
//for (int i=0; i<this.size(); i++) {
// total += grades[i];
//}
//return total / grades.length;
return 10;
}
@Override
public double classTopScore() {
return 10;
}
}
你有一些巨大的误解,我会指出其中的一些。首先,我注意到您没有正确创建构造函数。在 Student 类中,您的构造函数应该是这样的:
public Student (String name, int id, int totalGrades){
this.name = name;
this.id = id;
grades = new int[totalGrades];
this.totalGrades = totalGrades;
numGrades= 0;
}
所以在这里你说 this.name=名称,而不是相反。如果编写 name=this.name,则将构造函数变量名称的值设置为学生的对象变量名称的值;当你写 this.name 时,你指的是对象变量名,但当你只写 name 时,你指的是在构造函数中创建的局部变量名。另外,我建议您像这样编写get方法:
public String getName()
{
return this.name;
}
通过编写返回 this.name 可以清楚地返回对象的 name 变量。
你正在用 StudentLL 构造函数犯同样的错误。你是说构造函数的局部变量 course 将获得对象的 course 变量的值。所以构造函数应该是这样的:
public StudentLL(String theCourseValue)
{
this.course = theCourseValue;
}
请注意,我将构造函数参数变量命名为 theCourseValue,以表明如何在构造函数中命名局部变量并不重要。在这种情况下,您还可以编写 course = theCourseValue(不带 THIS 关键字),因为构造函数可以看到的唯一具有名称 course 的变量是对象的 course 变量。
我注意到的另一件事是你比较学生节点的方式。你使用
if(s.getId() == (current.getStd().getId()))
这没有错,但是您在学生班中有一个特殊的方法。这是平等的方法。这是你使用它的方式,id做完全相同的事情:
if(s.equals(current.getStd()))
我希望这能给你一些方向。我强烈建议您阅读更多有关构造函数的信息,因为我发现您错过了一些关于构造函数角色及其工作原理的重要概念。
构造函数是初始化对象字段的位置。您似乎正在阅读它们,而不是在其中设置值。
public Student (String name, int id, int totalGrades){
name = this.getName();
id = this.getId();
在这里,您获取名称字段的空值并将其分配给无论如何都将被丢弃的 name 参数。相反,您应该使用:
this.name = name;
this.id = id;
这里:
grades = new int[totalGrades];
您正在正确创建数组,但您没有将 totalGrades 变量保留在您的字段中,因此当需要在打印中使用它时,它是零。所以:
this.totalGrades = totalGrades;
在其他构造函数中也应该执行相同的操作。它用于设置值,而不是用于读取它们。您的作业将被撤销。左边的变量正在接收右边的值,而不是相反。
这可能不是完整的解决方案,但有几件事对我来说很突出:
In insort()
// Is it clear that you should ignore a student with the
// same id or should you overwrite ?
if (curr.getStd().compareTo(s) == 0) {
// curr.setStd(s); ?
return false;
}
// You iterate over the full list and compare
// to your new node. But never break based
// in this comparison. Furthermore you have
// to iterate until the new node is **smaller**
// then the current one and insert **before** it
StudentLLNode prev=null; // The node before the current node
while (curr != null) {
// If we e.g. we have a list [5,7,9,12] and want to insert
// 8 we iterate until we hit 9 and insert **before** 9
if(s.compareTo(curr.getStd()) == 0){
return false; // should we overwrite ?
}
if(s.compareTo(curr.getStd()) == -1){
break; //c
}
// prev always refers to the node of the previous iteration
// in our example #7 when we break
prev = curr;
curr = curr.getNext();
}
StudentLLNode student1 = new StudentLLNode(s);
// If prev is still null then the very first
// node eas bigger then the new one and we have
// to insert before head
if(prev==null) {
student1.setNext(head);
head = student1;
} else {
student1.setNext(prev.getNext());
prev.setNext(student1);
}
在删除中这
if(s.getId() == (head.getStd().getId())){
//StudentLLNode top = head;
head = head.getNext();
size--;
return true;
}
会给你一个空指针异常,如果没有人插入任何内容到列表中
编辑:更多注释和相同ID的识别在while循环...