PreLab 6: More Classes & Conditional Statements

  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.

    //*****************************************************
    // Student.java
    //
    // A class representing 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 -- creates a student object whose name 
      // is the value passed in.
      //---------------------------------------------------
      public Student(String studentName)
      {
          // Assign the instance variable name the value passed in
          // and initialize the two test grades to 0
    
    
    
    
    
    
      }
    
      //---------------------------------------------------------
      // inputGrades: Prompts for and reads in grades for 
      // the student's two tests.  No parameters or return value.
      //---------------------------------------------------------
      public void inputGrades()
      {
        ... body of inputGrades ...(don't fill in)
      }
    
      //---------------------------------------------------
      // getName: returns the student's name. 
      //---------------------------------------------------
    
      _________________________________________________  //fill in header
      {
          // fill in the body of the method (just one line!)
    
          _____________________________________________________;
    
      }
    
    
      //------------------------------------------------------
      // getAverage: Computes and returns the average of the 
      // student's test grades (type double).  No parameters.
      //------------------------------------------------------
    
    
      _____________________________________________________  //fill in header
      {
    
         // Compute and return the average (it can be done in one statement)
    
    
         _______________________________________________________ ;
      }
    
    
      //------------------------------------------------------
      // highestScore:  Returns the highest test grade - no
      // parameters.
      // -----------------------------------------------------
      
      ______________________________________________________  // fill in header
      {
         int highGrade;   // a local variable to hold the highest grade
    
         // Write an if statement to assign highGrade the highest of the
         // two test grades
    
    
    
    
    
    
    
    
    
    
         // Return the highest grade
    
         ________________________________________________________ ;
    
       }
    
    
       //--------------------------------------------------------
       // letterGrade: Returns 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. The method
       // has no parameters.
       //--------------------------------------------------------
    
       ______________________________________________________ (fill in the header)
    
       {
          double average = getAverage();  // call getAverage 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.
    public class StudentGrades
    {
       public static void main (String[] args)
       {
          Student student1 = new Student ("Sarah");
          Student student2 = new Student ("John");
    
          // Invoke input grades to read in the student grades
          student1.inputGrades();
          student2.inputGrades();
          double average1 = student1.getAverage();
          double average2 = student2.getAverage();
      
          // Write an if ...else... statement that prints the message
          // "Great work!" if Sarah'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.
    
    
    
    
    
    
    
    
    
          }
    }