creation: 09/06/99; modified 05/07/03


Fill in the Blanks

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.


1.      Design of the Telescope Class.     The three most important numbers describing a telescope are: the diameter of the main lens (the one in front), the focal length of the main lens, and the focal length of the eyepiece. From these values other charactersitics of the telescope such as its magnification and the f-number of the main lens are calculated.

Fill in the blanks in the following design for the class:

class 

A class that models a field telescope.

Constructors

Telescope ( double ,
            double ,
            double   )


Methods

// calculate the magnification of the telescope
double  

// calculate the f-number of the telescope
double  


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.

class TelescopeTester
{

  public static void main ( String[] args )
  {
     Telescope tele = new   ;

     System.out.println( "Power: "    + tele. +
                         "  F-number: " + tele. );

  }
}


3.      Skeleton of the Class.    Fill in the blanks that give the over-all design of the class.

class Telescope
{
  // 

  // 

  // 

}


4.      Fill in Instance Variables.    Fill in the data type of each instance variable.

class Telescope
{
  // Instance Variables
   diameter;
   mainLength;
   eyeLength;

  // Constructors

  // Methods

}


5.      Complete the Constructor.    The constructor will initialize the instance variables of the object being constructed.

class Telescope
{
  // Instance Variables
  double diameter;
  double mainLength;
  double eyeLength;

  // Constructors
  ( double , double , double   )
  {
    this.diameter   =     ;
    this.mainLength =   ;
    this.eyeLength  =    ;

  }


  // Methods

}

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

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 mainLength / eyeLength ;
  }

  double fNumber()
  {
    return  /  ;
  }
}


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:

import  ;

class TelescopeTester
{

  public static void main ( String[] args )
  {
     Telescope tele = new  Telescope( 3.0, 6.5, .8 ) ;

     System.out.println( "Power: "    + tele.magnification() +
                         "  F-number: " + tele.fNumber()  );

  }
}


9.      Compile each File.     Each of the two files must be compiled:

C:\MyFiles>javac  

C:\MyFiles>javac  


10.      Run the Program.     The java bytecode interpreter must be started with the *.class file that contains the main() method:

C:\MyFiles>java  


End of the Exercise. If you want to do it again, click on "Refresh" in your browser window. Click here to go back to the main menu.