包含链接列表的学生数据库

时间:2014-11-22 00:01:01

标签: java database sorting linked-list

我仍然是一个非常新手的程序员,所以请尽量描述。我在让我的所有代码都为我的作业工作时遇到了问题。该作业为我提供了四个不需要更改的完整文件(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

我的Student.java和StudentLL.java的代码

import 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;
    }
}

3 个答案:

答案 0 :(得分:1)

你有一些巨大的误解,我会指出其中的一些。 首先我注意到你没有正确地创建你的构造函数。 在类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而不是相反。如果编写name = this.name,则将构造函数变量名的值设置为Student的对象变量名的值; 当您编写this.name时,您指的是对象变量名称,但是当您只编写名称时,您指的是在构造函数中创建的局部变量名称。 另外我建议你写这样的get方法:

public String getName()
{
     return this.name;
}

通过编写return this.name,您可以清楚地看到您返回对象的名称变量。

你在StudentLL构造函数中犯了同样的错误。你是说构造函数的局部变量过程将获得对象的课程变量的值。所以构造函数应该是这样的:

public StudentLL(String theCourseValue)
{
     this.course = theCourseValue;
}

注意我将构造函数参数变量theCourseValue命名为向您显示在构造函数中如何命名局部变量并不重要。在这种情况下,您还可以编写course = theCourseValue(不带THIS关键字),因为构造函数可以看到的唯一具有名称课程的变量是对象的课程变量。

我注意到的另一件事是比较学生节点的方式。你用

  if(s.getId() == (current.getStd().getId()))

这没错,但是你的Student课程中只有一个特殊的方法。这是等于方法。这是你使用它的方式,id完全相同:

if(s.equals(current.getStd()))

我希望这会给你一些指示。我强烈建议你阅读更多关于构造函数的内容,因为我发现你已经错过了一些关于构造函数的作用以及它们如何工作的重要概念。

答案 1 :(得分:0)

构造函数是您应该初始化对象字段的位置。你好像在阅读它们而不是在它们中设置值。

public Student (String name, int id, int totalGrades){
     name = this.getName();
     id = this.getId();

在这里,您将获取名称字段的空白值,并将其分配给名称参数,无论如何都会将其丢弃。相反,你应该使用:

    this.name = name;
    this.id = id;

下面:

    grades = new int[totalGrades]; 

您正在正确创建数组,但是您没有在字段中保留totalGrades变量,因此在打印时使用它时,它为零。所以:

    this.totalGrades = totalGrades;

同样应该在你的其他构造函数中完成。它用于设置值,而不是用于读取它们。你的任务是相反的。左手变量在右边接收值,而不是相反。

答案 2 :(得分:0)

这可能不是完整的解决方案,而是为我提供的一些东西:

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

如果没有人在列表中插入任何内容

,会给你一个NullPointerException

编辑:在while循环中更多注释和识别相同的ID ...