creation: 09/06/99; modified 05/07/03
Instructions: This is an ungraded fill-in-the-blank exercise. Each question consists of a sentence with one or two words left out. A button represents the missing word(s). For each question, think of the word or phrase that should fill each blank, then click on the buttons to see if you are correct. No grade is calculated for this exercise.
2. Checking the Design. To check the design, write a small program that uses the class to see if it works well. Write a program that creates a Telescope object with a main lens that has a diameter of 3.0 inches, a focal length of 6.5 inches, and an eyepiece focal length of 0.8 inches. Write out its magnification and f-number.
3. Skeleton of the Class. Fill in the blanks that give the over-all design of the class.
4. Fill in Instance Variables. Fill in the data type of each instance variable.
5. Complete the Constructor. The constructor will initialize the instance variables of the object being constructed.
When an instance variable and a parameter use the same identifier, you specify the instance variable of the object by saying "this.identifier" as in the above.
6. Complete a Method. The documentation for
the magnification()
method says it looks like this:
// calculate the magnification of the telescope double magnification()
The formula to use is: magnification = mainLength/eyeLength
class Telescope { // Instance Variables double diameter; double mainLength; double eyeLength; // Constructors Telescope ( double diameter, double mainLength, double eyeLength ) { this.diameter = diameter ; this.mainLength = mainLength ; this.eyeLength = eyeLength ; } // Methods double magnification() { return / ; } }
You don't have to use "this" in the method because it is clear that the identifiers
refer to the instance variables.
The magnification()
method can not even see the parameters of the constructor.
(Remember, the parameters are like a private telegram to the constructor.)
7. Complete the other Method. The documentation for the says it looks like this:
// calculate the magnification of the telescope double fNumber()
The formula to use is: fNumber = mainLength/diameter
8. Import the Class. At this point all the coding is done. Say that the small test program (class TelescopeTester) is in a file called TelescopeTester.java and that the complete definition of the Telescope class is in a file called Telescope.java. The code for TelescopeTester has to tell the compiler that it is using a class defined outside its file with an import statement:
9. Compile each File. Each of the two files must be compiled:
10. Run the Program. The java bytecode interpreter must be started with the *.class file that contains the main() method: