PreLab 6: Classes & Conditionals    Name__________________________________

  1. Suppose a teacher wants a program to keep track of grades for students and decides to create a student class for her program as follows:

    The following is an incomplete class declaration. Fill in the data declarations and methods as indicated in the comments and the above description of the class.

    //*****************************************************
    // Student.java -  Represents a student; holds info  
    // about the student's name and test grades.
    //*****************************************************
    public class Student
    {
      //declare instance data
    
      ____________________________________________  // name
                                   
      ____________________________________________  // grade for test #1
    
      ____________________________________________  // grade for test #2
    
    
      /**
       * Constructor -- initializes a student object using  
       * the name passed in.
       * @param studentName The name of the student
       */
      public Student(String studentName)
      {
          // Assign the instance variable name the value passed in
          // and initialize the two test grades to 0
    
    
    
    
    
    
    
      }
    
      /**
       * Assigns the score passed in to the test indicated by the
       * second parameter.
       * @param score The test score.
       * @param whichTest An integer indicating which test the
       *                  score is for (1 for test #1, 2 for test #2
       */
      public void setTestScore(int score, int whichTest)
      {
         // write an if statement to set the correct test grade 
    
    
    
    
    
    
    
    
    
      }
    
    
      /**
       * @return the student's name. 
       */
    
      _________________________________________________  //fill in header
      {
          // fill in the body of the method (just one line!)
    
          _____________________________________________________;
    
      }
    
    
      /**
       * Computes the average of the student's test grades.
       * @return The student's test average.
       */
    
      _____________________________________________________  //fill in header
      {
    
         // Compute and return the average (it can be done in one statement -
         // be sure to use floating point arithmetic)
    
    
         _______________________________________________________ ;
      }
    
    
      /**
       * Determines the highest test grade.
       * @return The higest test grade.
       */
      
      ______________________________________________________  // fill in header
      {
     
         // Fill in the blank to return the highest test grade - this
         // can be done in one statement using a method from the Math class.
    
    
         ______________________________________________________________ ;
    
       }
    
    
       /**
        * Finds the letter grade (type char) corresponding to the student's 
        * average assuming an average greater than or equal to 90 is an A, 
        * from 80 up to 90 is a B, and so on.  Below 60 is an F.
        * @return The letter grade.
        */
    
       ______________________________________________________ (fill in the header)
    
       {
          double average = averageScore();  // call the method to compute
                                            // the average
          char letter;
    
          // Complete the cascading if statement to assign the correct
          // letter grade to the variable letter
    
          if (average >= 90)
            letter = 'A';
          else if
    
    
    
    
    
    
    
    
    
    
    
    
    
        
    
    
    
          // Return the letter grade
          return letter;
       }
    }
    
  2. The following is part of a class that uses the Student class. Fill in the two if statements requested in the commments.
    import java.util.Scanner;
    
    public class StudentGrades
    {
       public static void main (String[] args)
       {
          Scanner scan = new Scanner (System.in);
          String name;
          int score;
    
          System.out.print ("Enter the first student's name: ");
          name = scan.nextLine();            
          Student student1 = new Student (name);
    
          System.out.print ("Enter the second student's name: ");
          name = scan.nextLine();
          Student student2 = new Student (name);
    
          System.out.print ("Enter " + student1.getName() + "'s test 1 grade: ");
          score = scan.nextInt();
    
          // Invoke setTestScore to set test 1 grade for student1
    
          ____________________________________________________________________;
    
    
          System.out.print ("Enter " + student1.getName() + "'s test 2 grade: ");
          score = scan.nextInt();
    
          // Invoke setTestScore to set test 2 grade for student1
    
          ____________________________________________________________________;
    
    
          // Invoke setTestScore to set student2's first test score to 78
          // and second test score to 83.
    
          ____________________________________________________________________;
    
          ____________________________________________________________________;
    
          // Determine each student's test average.
          double average1 = student1.averageScore();
          double average2 = student2.averageScore();
      
          // Write an if ...else... statement that prints the message
          // "Great work!" if student1's average is greater than 84; "Doing
          // fine" if the average is between 70 and 84 (inclusive);
          // "Work harder" otherwise
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
          // Write an if ... else that determines who has the highest
          // average - use the getName method to print the name of the person
          // Be sure to also test for a tie.
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
          }
    }