PreLab 6: More Classes

  1. Find the base 10 value of each of the following 12-bit two's complement integers (show work), find the sum in 12-bit two's complement (as the computer would do it), and convert that 12-bit two's complement answer to base 10 (show work).
             Two's complement                Base 10 Equivalent
    
           1 1 1 1 0 0 1 1 1 1 0 0    ___________________________________
    
        +  1 1 1 1 1 1 1 0 1 0 0 0    ___________________________________
        --------------------------
    
    Sum  _________________________    ___________________________________
    
    
    
    
    
    
    
    
  2. Did overflow occur in the sum above? Give reasons for your answer.

     

     

  3. Constructors are a special type of method included in a class definition.
    1. When is a constructor executed?

       

       

    2. What are the rules for
      • the name of a constructor?

         

      • the return type?

         

  4. 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 return the average of the 
      // student's test grades.  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
         return highGrade;
       }
    }
    
  5. 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");
    
          student1.inputGrades();
          student2.inputGrades();
    
          double average1 = student1.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
    
    
    
    
    
    
    
    
          }
    }