LAB 34 – Creating Classes (part 3)

Lab Exercises

Topics

n      To create additional classes.

n      To

 

Exercises

11.8 Creating Another Class

 

i) Open a new file in Dr. Java and type in the following enumerated data type:

/**

 * Semester enumerated data type

 */

enum Semester {FALL, SPRING, SUMMER}

 

ii) Open a new file in Dr. Java and type in the following class:

 

/*  ClassPeriod represents a class period with teacher, semester, and students

 *

 * @author Wayne Summers

 * @date   Nov. 9, 2007

 */

public class ClassPeriod

{

  //////////////constants and data fields /////////////////

  final int NBRSTUDENTS = 35;

  private String teacherName;

  private Semester semester;

  private Student[] studentArray = new Student[NBRSTUDENTS];

 

  //////////////constructors //////////

  public ClassPeriod ()  {  }                   //empty constructor

 

  public ClassPeriod (String theName)  {  this.teacherName = theName; }

 

  public ClassPeriod (String theName, Semester theSemester)

  {

    this.teacherName = theName; 

    this.semester = theSemester;

  }

 

  //////////////methods ///////////////

   /**

   * Method to get the name for this teacher

   * @return the teacherName

   */

   public String getTeacherName()  { return this.teacherName; }

  

   /**

   * Method to get the semester

   * @return the semester

   */

   public Semester getSemester()  { return this.semester; }

  

  /**

  * Method to get the student at index for this ClassPeriod

  * @param index – position of student to be retrieved

  * @return the student at location index

  */

  public Student getStudent (int index) { return this.studentArray[index]; }

 

  /**

   * Method to set the teacher's name for this class

   * @param theName - the teacher for this class

   */

  public void setTeacherName (String theName) { this.teacherName = theName; }

 

   /**

   * Method to set the semester

   * @param theSemester - the semester for this class

   */

  public void setSemester (Semester theSemester) { this.semester = theSemester; }

 

   /**

   * Method to set the student at an index

   * @param studentObj = the student to add

   * @param index = the index of the student in the ClassPeriod

   */

  public void setStudent (Student studentObj, int index) { studentArray[index] = studentObj; }

  


/**

  * Method that constructs all information about this class period

  * @return result = string containing class period information

  */

  public String toString()

  {

    String result = "Teacher\'s name is " + this.teacherName;

    result += " \n" + "Semester is " + this.semester + " \n\n";

    result += "Student list:\n";

    for (int index = 0; index < studentArray.length; index++)

      if (studentArray[index] != null)

          result += studentArray[index] + " \n";

    return result;

  }

}

 

ii)                   Use Lab 32 and 33 to create a class ProcessClass where the main method creates two students with three grades each and then displays the two students.

iii)                 Modify the main method of ProcessClass by

a.       Declaring myClass of type ClassPeriod

b.      Instantiate a class with your name as the teacher’s name

c.       Set the semester to Semester.SPRING.

d.      Adds the two students from ii).

e.       Displays myClass using a System.out.println.

 

QUESTIONS:

 

Submit the ProcessClass.java, ClassPeriod, Semester.java, and Student.java files through the DropBox in WebCT.