



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CMSC 131 Spring 2005 Quiz 5 Worksheet
The fifth Quiz of the course will be on Wednesday March 9 in lab session.
The following list provides more information about the quiz:
The following exercises subsume the material to be covered in Quiz #5. Solutions to these exercises will not be provided, but you are welcome to discuss your solutions with TAs and instructors during office hours.
Problem 1
For this problem you will write a complete Java class called Triangle , which represents a mathematical triangle. The class has the following private instance variables:
base – double value representing the base of a triangle height – double value representing the height of the rectangle
All the following methods are non-static except the method named area.
Problem 2
Define a Student class that represents a college student. A student has a name (String reference), a gpa (double), and an id (integer). The class has the following methods that you must consider non-static unless indicated otherwise.
Problem 3
Define a class called Scores. The class keeps track of scores associated with a student. The class has the following instance variables:
The class has the following non-static methods:
Problem 5
Note: This problem was last semester’s quiz. A solution has been provided at the end.
Write a complete Java class called Circle , which represents a mathematical circle. The class has only one private instance variable name radius which represents the radius of the circle. All the methods of the class are non-static except the method named area. The methods you must implement are:
Restrictions/Assumptions .
Problem 5, One possible solution
public class Circle { private double radius;
public Circle() { radius = 1.0; }
public Circle(double radiusIn) { radius = radiusIn; }
public double getRadius() { return radius; }
public void increaseRadius(double delta) { radius += delta; }
public String toString() { return ("Radius: " + radius); }
public boolean equals(Circle circleIn) { if (radius == circleIn.radius) return true; else return false; }
public Circle doubleRadius() { return (new Circle(2 * radius)); }
public static double area(Circle circleIn) { double r = circleIn.radius; return Math.PI * r * r; }
public static void main(String[] args) { Circle c1 = new Circle(); System.out.println(c1);
Circle c2 = new Circle(20.0); c2.increaseRadius(10); System.out.println("After increasing: " + c2.getRadius());
String message = "different"; if (c1.equals(c2)) message = "Same"; System.out.println(message);
Circle c3 = new Circle(4); Circle c4 = new Circle(5); Circle c5 = c3.doubleRadius(); System.out.println("Doubling " + c5);
System.out.println("Area: " + Circle.area(c3)); } }