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");
System.out.println(s2.getName());
System.out.println(s1.getAverageScore());
}
}
Output:
Frank Griswold
Sarah Conner
18.0
|
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) {
myRadius = radius;
myX = x;
myY= y;
}
// center defaults to (0, 0)
public Circle(double radius) {
this(radius, 0, 0);
}
//accessors
public double getRadius() {
return myRadius;
}
public double getX() {
return myX;
}
public double getY() {
return myY;
}
// other methods
public double getArea() {
return Math.PI*getRadius()*getRadius();
}
public double getCircumference() {
return 2.0*Math.PI*getRadius();
}
public String toString() {
return "Circle:\n" +
" Radius: " + getRadius() + ",\n" +
" Center: (" + getX() + ", " + getY() + "),\n" +
" Area: " + getArea() + ",\n" +
" Circumference: " + getCircumference() + "\n";
}
}
public class CircleTest {
public static void main (String[] args) {
Circle circle1 = new Circle(10, 2, 3);
System.out.println(circle1.getArea());
Circle circle2 = new Circle(5);
System.out.println(circle2.getCircumference());
System.out.println(circle1);
System.out.println(circle2);
}
}
public String toString() {
return "Circle:\n" +
" Radius: " + myRadius + ",\n" +
" Center: (" + myX + ", " + myY + "),\n" +
" Area: " + getArea() + ",\n" +
" Circumference: " + getCircumference() + "\n";
}
Below is the output when the above main is executed.
314.1592653589793 62.83185307179586 Circle: Radius: 10.0, Center: (2.0, 3.0), Area: 314.1592653589793, Circumference: 62.83185307179586 Circle: Radius: 10.0, Center: (0.0, 0.0), Area: 314.1592653589793, Circumference: 62.83185307179586