Directions: Fill in answers on the pages below. Don't spend too much time on any one problem.
First example class: the Student class.
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
|
Second example class: the circle class.
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 2 * 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:
public class Circle {
private double myRadius;
private double myX; // x coordinate of center
private double myY; // y coordinate of center
public Circle(double radius, double x, double y) {
}
// center defaults to (0, 0)
public Circle(double radius) {
}
//accessors
public double getRadius( ) {
}
public double getX( ) {
}
public double getY( ) {
}
// other methods
public double getArea( ) {
}
public double getCircumference( ) {
}
}
public class CircleTest {
public static void main (String[] args) {
}
}
Circle: Radius: 10.0, Center: (2.0, 3.0), Area: 314.1592653589793, Circumference: 62.83185307179586