我需要帮助创建一个平均方法

时间:2015-10-14 17:19:56

标签: java methods

我需要帮助创建一个平均方法。我完成了其他一切。它需要计算并返回所有学生的平均考试成绩。我只有return语句。我在提出实际方法时遇到了麻烦。到目前为止,这是我的代码。第一个是Course课程,第二个是Student课程。

public class Course  
{
private String course;
private Student s1, s2, s3, s4, s5;
private int studentcount = 0;

public Course (String name)
{
course = name;
}

public Student addStudent(String first, String last, Address home, Address school)
{

if (studentcount == 0){
  s1 = new Student(first,last,home,school);
  studentcount++;          
  return s1;
}    

 if (studentcount == 1) {
  s2 = new Student(first,last,home,school);
      studentcount++;
  return s2;

}
else if (studentcount == 2){
  s3 = new Student(first,last,home,school);
  studentcount++;
      return s3;

}
else if (studentcount == 3){
  s4 = new Student(first,last,home,school);
      studentcount++;
  return s4;

}
else if (studentcount == 4) {
  s5 = new Student(first,last,home,school);
      studentcount++;
  return s5;

}
else { //the course can only have five students
    System.out.println("No More students allowed in the class");
    return null;
}

}

public double average() //returns the average
{
   return (s1.average() + s1.average() + s1.average() + s1.average() + s1.average()) / 5.0;
 }

public String roll() //returns student info for each number of students
{
 String results = "";

if (studentcount == 1){
  results += s1.toString () +"n";
  return results;
}    

 if (studentcount == 2) {
  results += s1.toString () +"n";
  results += s2.toString () +"n";
  return results;

}
else if (studentcount == 3){
  results += s1.toString () +"n";
  results += s2.toString () +"n";
  results += s3.toString () +"n";
  return results;

}
else if (studentcount == 4){
  results += s1.toString () +"n";
  results += s2.toString () +"n";
  results += s3.toString () +"n";
  results += s4.toString () +"n";
  return results;

}
else if (studentcount == 5) {
  results += s1.toString () +"n";
  results += s2.toString () +"n";
  results += s3.toString () +"n";
  results += s4.toString () +"n";
  results += s5.toString () +"n";

  return results;
    } 
    else{
  return null;
 }   

 }
 }


public class Student
{
private String firstName, lastName;
private Address homeAddress, schoolAddress;
private int test1, test2, test3;
//-----------------------------------------------------------------
// Sets up this Student object with the specified initial values.
//-----------------------------------------------------------------
public Student (String first, String last, Address home, Address school)
{
    firstName = first;
    lastName = last;
    homeAddress = home;
    schoolAddress = school;
    test1 = 0;
    test2 = 0;
    test3 = 0;
}
//-----------------------------------------------------------------
// Returns this Student object as a string.
//-----------------------------------------------------------------
 public String toString()
 {
    String result;
    result = firstName + " " + lastName + "\n";
    result += "Home Address:\n" + homeAddress + "\n";
    result += "School Address:\n" + schoolAddress;
    return result;
 }

 public void setTestScore(int t, int g) //sets the test score
 {
   if (t == 1)
   {
       test1 = g;
   }
    else if (t == 2)
    {
        test2 = g;
   }  
    else if (t == 3)
    {
        test3 = g;
   }
 }

 public int getTestScore (int t) //returns the test score
 {
   if (t == 1)
   {
       return test1;
    }
    else if (t == 2)
    {
        return test2;
    }
    else 
    {
        return test3;
    } 
 }
 }

1 个答案:

答案 0 :(得分:1)

我不认为这样的练习应该给初学者。它(将在这里可以看到)诱使他们产生令人厌烦和烦人的重复代码。已经开发了编程语言以使编程变得容易,而不是颈部疼痛。

OTOH,我们在这里有限制的问题(没有数组)可以被看作是制定战略的动机,没有“禁止”功能。但话说回来,我想知道编程的介绍是否应该在这个早期阶段深入研究这些细节。

那就是说,我提出改写班级学生。见评论。

//禁止使用数组和列表。

public class Course {
    private String course;
    private Student s1, s2, s3, s4, s5;
    private int studentcount = 0;

    public Course (String name) {
        course = name;
    }

方法addStudent使用“移动”已存储学生的简单技巧,为s1的下一位学生腾出空间。虽然不必要地复制了一些空值,但是重复测试并不是更昂贵,而且肯定不那么容易出错。

    public Student addStudent(String first, String last, String home, String school){
        if( studentcount < 5 ){
            studentcount++;
            s5 = s4; s4 = s3; s3 = s2; s2 = s1;
            return s1 = new Student(first,last,home,school);
        }    
        System.out.println("No more students allowed in the class");
        return null;
    }

方法卷使用另一种标准技巧来避免重复行。必须为所有大于1的学生计数提供s1的数据,必须打印s2所有大于2的计数,等等。应该避免重复扩展字符串,因此StringBuilder。

    public String roll(){
        StringBuilder sb = new StringBuilder();
        if (studentcount >= 1)  sb.append( s1.toString() ).append( "\n" );
        if (studentcount >= 2)  sb.append( s2.toString() ).append( "\n" );
        if (studentcount >= 3)  sb.append( s3.toString() ).append( "\n" );
        if (studentcount >= 4)  sb.append( s4.toString() ).append( "\n" );
        if (studentcount >= 5)  sb.append( s5.toString() ).append( "\n" );
        return sb.toString();
    } 

average方法使用相同的技术,现在累积分数。请注意,为了计算商,转换为double。

    public double average(){
        int scores = 0;
        if (studentcount >= 1)  
            scores += s1.getTestScore(1) + s1.getTestScore(2) + s1.getTestScore(3);
        if (studentcount >= 2)  
            scores += s2.getTestScore(1) + s2.getTestScore(2) + s2.getTestScore(3);
        if (studentcount >= 3)  
            scores += s3.getTestScore(1) + s3.getTestScore(2) + s3.getTestScore(3);
        if (studentcount >= 4)  
            scores += s4.getTestScore(1) + s4.getTestScore(2) + s4.getTestScore(3);
        if (studentcount >= 5)  
            scores += s5.getTestScore(1) + s5.getTestScore(2) + s5.getTestScore(3);
        return (double)scores/(studentcount*3);
    }
}
相关问题