CS 1063-001,   Exam 1, Fall 2005

Directions: Fill in answers on the pages below. Don't spend too much time on any one problem.

First example class: the Student class.

    Consider the following Student and StudentTest classes, which are almost the same as an example in class. (Some features are omitted or simplified.) Answer the questions following the listing.

    package studentpkg;
    
    public class Student {
       private String myName; // student's name
       private int myNumQuizzes;// # of quizzes
       private int myTotalQuizScore; // sum
    
       public Student(String name) {
          // new student, with name, no quizzes
          myName = name;
          myNumQuizzes = 0;
          myTotalQuizScore = 0;
       }
          
       public String getName() {
          // return the student's name
          return myName;
       }
       
       public int getNumQuizzes() {
          // return the number of quizzes taken
          return myNumQuizzes;
       }
       
       public int getTotal() {
          // return total scores on all quizzes
          return myTotalQuizScore;
       }
       
       public void addQuiz(int score) {
          // record a new quiz
          myTotalQuizScore = 
             myTotalQuizScore + score;
          myNumQuizzes++;
       }
       
       public double getAverageScore() {
          // calculate average quiz grade 
          return (double)myTotalQuizScore/
                (double)myNumQuizzes;
       }
    }
    
    package studentpkg;
    
    public class StudentTest {
    
       public static void main(String[] args) {
          Student s1 =
             new Student("Frank Griswold");
          s1.addQuiz(18);
          s1.addQuiz(20);
          s1.addQuiz(16);
          System.out.println(s1.getName());
          Student s2 = 
             new Student("Sarah Conner");
          
          
          
          
          
          
          
          
          
       }
    }
    
    Output:
    Frank Griswold
    

  1. Identify the parts of the classes above by drawing a circle around the part and writing in its name. In each case there may be one part or more than one part, and in some cases you may need more than one circle. Thus for part i. below, you should circle part of the code and write in "data member(s)" beside the circle.

    1. The data member(s).
    2. The constructor(s).
    3. The accessor method(s).
    4. The helper method(s).
    5. Code which creates instance(s) of the Student class.
    6. Use(s) of an accessor method.

  2. In the space provided inside the StudentTest class, add code that will do the following:

    1. Print the name of the student s2 (so that Sarah Conner should be printed).
    2. Print the average quiz score of the student s1 (so that 18.0 should be printed).

Second example class: the circle class.

  1. Complete the code for a Circle class, similar to the Rectangle class that we studied.

    The Circle class has data fields giving the circle's radius, x-coordinate of center, and y-coordinate of center.

    There are methods to return each of these fields, as well the methods getArea() and getCircumference(). (Recall for a circle of radius r, the area is pi * r 2, and the circumference is * pi * r. You should also use Math.PI as the source of the constant pi.)

    There are two constructors, the second one letting the coordinates of the center default to zeros.

    Fill in the blanks below to complete the Java implementation of this class:

  2. Fill in code in the test program below to do the following:
    1. Instantiate a circle object with radius 10, and with x and y coordinates of the center (2, 3), using circle1 as its variable name.
    2. Give code to print the area of circle1.
    3. Similarly, use the second constructor to instantiate another object with radius 5 and center (0, 0), using circle2 as its variable name.
    4. Give code to print the circumference of circle2.

  3. Write a toString method for the Circle class so that when you try to print circle1 in the main method, it will print the values of the data members. For full credit, toString should print the characters below exactly as shown. (Hint: to skip to a newline on output, you can just put newline characters (\n) into the String returned by toString.)

     

     

     

     

     


Points for each problem: 1-25, 2-15, 3-25, 4-20, 5-15.