实现java类并测试方法中的许多条件

问题描述 投票:-1回答:1

我必须实现一个Java类,它“模拟”一个名为Student的对象。此课程中的学生对象代表学生成绩记录。有12个变量,3个构造函数和12个方法。

有一个预先写入的应用程序和文本文件(此处未显示)将填充每个学生对象中的所有数据字段,并存储对数组中每个学生对象的引用。

我最麻烦的部分是(1)如何正确创建构造函数? (2)如何正确创建setHomework等方法,这些方法接受参数,将它们与方法中的多个条件进行比较,并将它们分配给数组元素?

无论如何,这是我到目前为止的不完整代码:

public class Student{ 

private String name;

private String sid;

private double homework = NUM_HOMEWORK; //reference variable 

private double quizzes = NUM_QUIZZES; //reference variable 

private double exams = NUM_EXAMS; //reference variable 

final static int NUM_HOMEWORK = 4;

final static int NUM_QUIZZES = 4;

final static int NUM_EXAMS = 2;

final static double HOMEWORK_MAX_POINTS = 5;

final static double QUIZ_MAX_POINTS = 20;

final static double MIDTERM_MAX_POINTS = 40;

final static double FINAL_MAX_POINTS = 60;


/*1st constructor. No-arg constructor. Sets object’s name variable to default value “Newstudent, A.” and object’s sid var to default of “0000000”.  Also allocates 3 arrays; homework with length of NUM_HOMEWORK, quizzes with length of NUM_QUIZZES, and exams with length of NUM_EXAMS.*/
Student(){ 
   name = "Newstudent, A";
   sid = "0000000";
   int[] homework = new int[NUM_HOMEWORK];
   int[] quizzes = new int[NUM_QUIZZES];
   int[] exams = new int[NUM_EXAMS];
}



/*2nd constructor. Takes 1 string arg and stores it in parameter newName.  Param represents the name of a new Student object.  Constructor sets object’s name var to the given newName and object’s sid var to default value of “0000000”.  Also allocates same three arrays as first constructor.*/ 
Student(String newName) {
   name = newName;
   sid = "0000000";
   int[] homework = new int[NUM_HOMEWORK];
   int[] quizzes = new int[NUM_QUIZZES];
   int[] exams = new int[NUM_EXAMS];
}


/*3rd constructor. Takes 2 string args and stores them in string params newName and newSid. Params represent the name and sid of a new Student object. Constructor sets object’s name var to the given newName and object’s sid var to the given newSid.  Also allocates same three arrays as first constructor*/
Student(String newName, newSid){ 
   name = newName;
   newSid = "0000000";
   int[] homework = new int[NUM_HOMEWORK];
   int[] quizzes = new int[NUM_QUIZZES];
   int[] exams = new int[NUM_EXAMS];
}

/*Method takes string arg and stores it in string newName.  Sets object’s name variable to newName.  Returns void.*/
public static void setName(String newName){ 
   name = newName;
}

public static getName(){//Takes no args. Returns value of the object’s name variable.
   return String name;
}

/*Takes 1 String arg and stores it in string param newSid. Sets object’s sid var to the given newSid.  Returns void.*/
public static void setSid(String newSid){   
   sid = newSid;
}

/*Takes an int arg and stores it in int param homeworkNumber, and a double arg and stores it in double param score.  Method checks if homeworkNumber is a # between 1 and NUM_HOMEWORK and if score is a # between 0 and HOMEWORK_MAX_POINTS. If both those conditions are true it assigns the corresponding element of the homework array the value of score. Example: if homeworkNumber is 1 and score is 5 the element homework[0] is assigned value of 5.  Returns void.*/
public static void setHomework(int homeworkNumber, double score){  
   if(homeworkNumber > 1 && homeworkNumber < NUM_HOMEWORK && score > 0 && score < HOMEWORK_MAX_POINTS)
   then score = homework;
}

/*Takes one int arg and stores it in a param homeworkNumber.  Method returns one of the values in the object’s homework array.  Method checks if homeworkNumber is # between 1 and NUM_HOMEWORK, and if this condition is true, method returns the double value at index homeworkNumber – 1 in the homework array.  Otherwise, method returns 0.  Method returns double.*/
public static getHomework(int homeworkNumber){ 
   if(homeworkNumber > 1 && homeworkNumber < NUM_HOMEWORK)
   then return homeworkNumber – 1;    
   else return 0;
}

/*Method takes an int param named quizNumberand and a double param named score.  Method checks if quizNumber is a between 1 and NUM_QUIZZES and if score is between 0 and QUIZ_MAX_POINTS. If both conditions are true assigns the corresponding element of the quizzes array the value of score.  Example: if quizNumber is 1 and score is 18 the element quiz[0] is assigned 18. Method returns void.*/
public static void setQuiz(int quizNumber, double score){       
      if (quizNumber > 1 && quizNumber < NUM_QUIZZES && score > 0 && score < QUIZ_MAX_POINTS) 
      quizzes = score; 
}

/*Takes one int arg and stores it in param quizNumber.  Returns one of the values in the object’s quizzes array.  Method checks if quizNumber is between 1 and NUM_QUIZZES and if true returns the value at index quizNumber – 1 in the quizzes array.  Otherwise, method returns 0. Method returns double.*/
public static getQuiz(int quizNumber){ 
      if(quizNumber > 1 && quizNumber < NUM_QUIZZES) 
      return double quizNumber[] – 1        
      else return 0; 
}

/*Takes one double args, stores it in param score. Checks if score is between 0 and MIDTERM_MAX_POINTS and if true, assign the element exams[0] the value of score.  Returns void.*/
public static void setMidtermExam(double score){  

     if(score > 0 && score < MIDTERM_MAX_POINTS)
     score = exams[0];      
}

public static getMidtermExam(){//Takes no args. Return double value of exams[0].  
    return exams[0];   
}

/*Takes one double arg, stores in in param score. Checks if score is between 0 and FINAL_MAX_POINTS and iff true assigns exams[1] the value of score. Method returns void.*/
public static void setFinalExam(double score){ 
if (score > 0 && score < FINAL_MAX_POINTS)
    score = exams[1];
}

public static getFinalExam(){ //Takes no args. Returns double value of exams[1].  
return exams[1];
}

/*Takes no args. Returns a String which the student object’s name, sid, homework scores, quiz scores and exam scores are concatenated.*/
public static toString(){  
return "Student total information. " + name + ", " + sid + ", homework: " + homework + ", quizzes: " + quizzes + ", exams: " + exams;
}



}
java object constructor
1个回答
0
投票

构造函数:

public Student(String newName, String newSid){... } 

setHomework:

public static void setHomework(int homeworkNumber, double score) {
    if (homeworkNumber > 1 && homeworkNumber < NUM_HOMEWORK && score > 0 && score < HOMEWORK_MAX_POINTS) {
        homework[homeworkNumber - 1] = score;
    }
}

家庭作业

被声明为双,但我认为你的意思是它是一个数组?

private double[] homework;
© www.soinside.com 2019 - 2024. All rights reserved.