如何正确地将我的所有方法链接到我的客户端

时间:2013-11-16 18:26:32

标签: java arrays class parameters client

我将在Java Intro课程中继续我的最后一个项目。我制作了GradeBook的类文件来显示计算方法。我在清理GradeBookClient时遇到了麻烦,因为我在课堂上制作对象时不得不作弊并输入变量。我的老师然后通过电子邮件发送给我说我需要创建两个相同的数组来测试我的客户端中的 .equals方法。如何制作两个新阵列?所以我的主要目标是创建两个数组来测试我的equals方法,并弄清楚如何用我已经拥有的参数制作我的对象。

我的 .equals 方法来自类文件:

public boolean equals( Object a )
   {
      if ( !( a instanceof GradeBook ) )
         return false;
      else
      {
         GradeBook objGB = ( GradeBook ) a;

         if ( gradeBook.length != objGB.gradeBook.length )
            return false;

         for ( int i = 0; i <= gradeBook.length - 1; i++ )
         {
            if ( gradeBook[i] != objGB.gradeBook[i] )
               return false;
         }
         return true;
      }
   }

}

客户页面:

public class GradeBookClient
{
   public static GradeBook classRoom1, classRoom2;
   public static int astudents;
   public static void main( String [] args )
   {  


      astudents = 100;
      classRoom1 = new GradeBook (astudents);
      System.out.println( "The class size is " + classRoom1.getStudent() + " students." + "\n" + classRoom1.toString() ); 

      astudents = 100;
      classRoom2 = new GradeBook (astudents);
      System.out.println( "The class size is " + classRoom2.getStudent() + " students." + "\n" + classRoom2.toString() );

      if ( classRoom1.equals( classRoom2 ))
      System.out.println("Classroom 1 has the same grades and class size as Classroom 2.");
      else
      System.out.println("Classroom 1 and Classroom 2 have different grades and class sizes.");
   }
}

我作弊的部分是当我把astudents = 100填入我的对象的参数时。我的构造函数如下:

import java.util.Arrays;

public class GradeBook {

   int[] gradeBook;
   int student;
   int MINgrade = 0;
   int MAXgrade = 100;

//default constructor
   public GradeBook()
   {
      student = 0;
      gradeBook = new int [student];
   }

//constructor
   public GradeBook(int student1)
   {
      student = student1;
   //instantiate array with same length as parameter
      gradeBook = new int[student];

      for ( int i = 0; i <= gradeBook.length-1; i++ )
      {
         gradeBook[i] = (int) Math.floor(Math.random()*101);
      }
      Arrays.sort(gradeBook);

   }

1 个答案:

答案 0 :(得分:0)

将构造函数更改为:

 public GradeBook(int[] grades)
 {
      //instantiate array with same length as parameter
      gradeBook = new int[grades.length];

      for ( int i = 0; i <= gradeBook.length-1; i++ )
      {
         gradeBook[i] = grades[i];
      }
   }

main中使用您拥有的代码创建随机数组并将其传递给构造函数。

public static void main( String [] args )
{  
  int[] grades = new int[100];

  for (int i=0; i<grades.length; i++)
  {
     grades[i] = (int) Math.floor(Math.random()*101);
  }

  classRoom1 = new GradeBook(grades);
  System.out.println( "The class size is " + classRoom1.getStudent() + " students." + "\n" + classRoom1.toString() ); 

  classRoom2 = new GradeBook(grades);
  System.out.println( "The class size is " + classRoom2.getStudent() + " students." + "\n" + classRoom2.toString() );

  if ( classRoom1.equals( classRoom2 ))
  System.out.println("Classroom 1 has the same grades and class size as Classroom 2.");
  else
  System.out.println("Classroom 1 and Classroom 2 have different grades and class sizes.");
}