2014-11-22 94 views
0

我仍然是一个漂亮的新手程序员,所以请尽可能描述。我有问题让我所有的代码为我的任务工作。该作业为我提供了四个完全不需要更改的文件(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; 
    } 
} 

回答

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; 

在你的其他构造函数中应该做同样的事情。它用于设定数值,而不是用于读取它们。您的任务被逆转。左侧变量正在接收右侧的值,而不是相反的方向。

1

你有一些很大的误解,我会指出其中的一些。 首先我注意到你不能正确地创建你的构造函数。 在学生类构造函数应该是这样的:

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,你可以将构造函数变量name的值设置为Student的对象变量名的值; 当您编写this.name时,您指的是对象变量名称,但是当您只写入名称时,您指的是在构造函数中创建的局部变量名称。 此外,我建议你写你的get方法是这样的:

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

通过编写返回this.name你说清楚,你返回对象的名称变量。

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

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

通知我命名的构造函数的参数变量theCourseValue告诉你,它不会不管你如何命名在构造函数中的局部变量。在这种情况下,您还可以编写course = theCourseValue(不带THIS关键字),因为具有构造函数可以看到的名称课程的唯一变量是对象的课程变量。

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

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

这是没有错,但你有一个特殊的方法,只为你的学生班。这是平等的方法。这是你使用它的方式和身份证完全一样的东西:

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

我希望这给你一些方向。我强烈建议你阅读更多有关构造函数的内容,因为我看到你错过了一些有关构造函数的作用以及它们如何工作的重要概念。

+0

这是一吨的帮助,谢谢!它打印出我需要的所有东西,没有重复的东西。但它不是通过ID排序的,对该部分有任何建议? – Damoclyes 2014-11-22 01:17:12

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 ything进入榜单

编辑:更多的评论和while循环相同ID的识别...

+0

我总是错过了你只比较了相同ID的第一个节点。你也必须将它移到while循环中。因此,如果ID相同,比较将会是.compareTo(...)<= 0以及特殊处理。 – Oncaphillis 2014-11-22 01:32:33

+0

我真的不明白,这可能是因为iv今天编程了很长时间,但可以更详细地解释你的代码?我试图实现你的代码,并用头替换curr,然后在第二行到最后一行我假设pref.setNext被supoosed为prev.setNext?但是当我执行它时,它只给我海伦信息和所有这些。感谢所有的帮助! – Damoclyes 2014-11-22 01:40:00

+0

@Damoclyes添加了更多评论。希望有所帮助。 – Oncaphillis 2014-11-22 01:49:57