Please put your answer to each question on a separate page. The examination is designed to determine how well you know the material. Your goal is to get as many points as possible (out of a possible 100 points). Please keep this goal in mind as you budget your time. You have 3 hours to complete this examination.
Read each question carefully. Make sure that you clearly understand a question before answering it. You may wish to work out an answer on scratch paper before writing it on your answer page; answers that are difficult to read may lose points for that reason.
Question | Value | Score |
1 | 10 | |
2 | 20 | |
3 | 15 | |
4 | 5 | |
5 | 30 | |
6 | 20 | |
Total | 100 |
1. (10 points) Write Java statements to accomplish each of the following:
2. (20 points) Create a class called Complex for performing arithmetic with complex numbers. Write a driver program to test your class.
Complex numbers have the form:
realPart + imaginaryPart * iwhere i is the square root of -1.
Use floating-point variables to represent the private data of the class. Provide a constructor method that enables an object of this class to be initialized when it is declared. Provide a no-argument constructor with default values in case no initializers are provided. Provide public methods for each of the following:
3. (15 points) Consider the class Time below.
// Time class definition public class Time { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 - 59 // Time constructor initializes each instance variable // to zero. Ensures that each Time object starts in a // consistent state. public Time() { setTime( 0, 0, 0 ); } // Set a new Time value using military time. Perform // validity checks on the data. Set invalid values // to zero. public void setTime( int h, int m, int s ) { hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); second = ( ( s >= 0 && s < 60 ) ? s : 0 ); } // Convert Time to String in military-time format public String toMilitaryString() { return ( hour < 10 ? "0" : "" ) + hour + ( minute < 10 ? "0" : "" ) + minute; } // Convert Time to String in standard-time format public String toString() { return ( ( hour == 12 || hour == 0 ) ? 12 : hour % 12 ) + ":" + ( minute < 10 ? "0" : "" ) + minute + ":" + ( second < 10 ? "0" : "" ) + second + ( hour < 12 ? " AM" : " PM" ); } }
It would be perfectly reasonable for the Time class to represent the time internally as the number of seconds since midnight rather than the 3 integer values, hour, minute, and second. Clients could use the same public methods and get the same returned values. Modify the Time class to implement the Time as the number of seconds since midnight such that there is no visible change to the clients of the class.
4. (5 points) Give an example of a problem whose solution could profit from using one [or more] static class variable[s]. Explain how and why it would use it [them].
5. (30 points) Consider the classes defined below.
// Definition of abstract base class Shape public abstract class Shape { public double area() { return 0.0; } public double volume() { return 0.0; } public abstract String getName(); } //______________________________________________ // Definition of class Point public class Point extends Shape { protected double x, y; // coordinates of the Point // constructor public Point( double a, double b ) { setPoint( a, b ); } // Set x and y coordinates of Point public void setPoint( double a, double b ) { x = a; y = b; } // get x coordinate public double getX() { return x; } // get y coordinate public double getY() { return y; } // convert the point into a String representation public String toString() { return "[" + x + ", " + y + "]"; } // return the class name public String getName() { return "Point"; } } //______________________________________________ // Definition of class Circle public class Circle extends Point { // inherits from Point protected double radius; // no-argument constructor public Circle() { super( 0, 0 ); // call the base class constructor setRadius( 0 ); } // Constructor public Circle( double r, double a, double b ) { super( a, b ); // call the base class constructor setRadius( r ); } // Set radius of Circle public void setRadius( double r ) { radius = ( r >= 0 ? r : 0 ); } // Get radius of Circle public double getRadius() { return radius; } // Calculate area of Circle public double area() { return 3.14159 * radius * radius; } // convert the Circle to a String public String toString() { return "Center = " + super.toString() + "; Radius = " + radius; } // return the class name public String getName() { return "Circle"; } } //______________________________________________ // Definition of class Cylinder public class Cylinder extends Circle { protected double height; // height of Cylinder // Cylinder constructor calls Circle constructor public Cylinder( double h, double r, double a, double b ) { super( r, a, b ); // call base-class constructor setHeight( h ); } // Set height of Cylinder public void setHeight( double h ) { height = ( h >= 0 ? h : 0 ); } // Get height of Cylinder public double getHeight() { return height; } // Calculate area of Cylinder (i.e., surface area) public double area() { return 2 * super.area() + 2 * 3.14159 * radius * height; } // Calculate volume of Cylinder public double volume() { return super.area() * height; } // Convert a Cylinder to a String public String toString() { return super.toString() + "; Height = " + height; } // Return the class name public String getName() { return "Cylinder"; } }
Rewrite the Circle and Cylinder classes above as a Square and Cube classes in the following different ways:
6. (20 points) This question is based on the Shape, Point, Circle, and Cylinder classes defined in the previous question, as well as the applet that is partially defined below:
This question has 2 parts:// Driver for point, circle, cylinder hierarchy import java.awt.Graphics; import java.applet.Applet; public class Test extends Applet { private Point point; private Circle circle; private Cylinder cylinder; // declare an array of type Shape that contains 3 elements public void init() { // construct a point, circle, and cylinder // aim the 0th element of your array at your Point object // aim the 1st element of your array at object your object // aim the 2nd element of your array at object your object } public void paint( Graphics g ) { // Loop through your Shape array and print the name, // area, and volume of each object. } }