Thursday July 6, 2000


Quiz 16


1. Even though a return type is not included in the signature of the constructor of a class it does have an understood return type. What type of thing does it return? (Hint: Think about how you call the constructor.)

2. Given the statement if (a == 3) b = 4; else b = 5;, use the shortcut to an if with an else to rewrite this statement.

3. What type of variable can we use in a switch statement?

4. What is a feature called that has become obsolete and been replaced as Java has evolved?




























Suppose you are given the following problem:

Write a class called Spaceship.

  • It should have two instance variables called jupiter and mars.

  • It should have two instance methods which will compute the distance to each of the planets using formulas based on fourteenth order differential equations. The methods must take into account any asteroid belts or other bodies that might damage the spaceship.

  • The class should have a toString method which will output the distance to each planet.

  • The constructor for the class should call the two methods to initialize the instance variables jupiter and mars.

    The body of the methods will be somewhat difficult to implement. However, their implementation is irrelevant to the class structure. We can put the class together without having the implementation. Let us see how we can do this.

    Since the methods are going to return distances we can assume the return types are ints.

    The signature of the one of the methods might be

    public int distance_to_mars()
    
    Since the class structure doesn't depend on the implementation of this method, let's temporarily have it return an arbitray int.

    public int distance_to_mars() {
       return(0);
    }
    
    Likewise we can do something similar for the other instance method. Note that we will use a different int so that we can make sure the correct method is being called.

    public int distance_to_jupiter() {
       return(1);
    }
    
    The constructor is going to make a call to these methods to initialize the instance variables jupiter and mars. So a constructor for this method would be the following.

    public Spaceship() {
       jupiter = distance_to_jupiter();
       mars = distance_to_mars();
    }
    
    The toString method will display the distances found.

    public String toString() {
       String outputLine = "\n";
       outputLine += "Distance to Mars:" + "\t" + mars + "\n";
       outputLine += "Distance to Jupiter:" + "\t" + jupiter + "\n";
       return(outputLine);
    }
    
    In the main method we will create a new instance of the Spaceship class and send that object to System.out.println.

    public static void main(String[] args) {
       Spaceship mySpaceship = new Spaceship();
    
       System.out.println(mySpaceship);
    }
    
    Now that we have all the peices written let us write our class definition.

    public class Spaceship {
       int jupiter;
       int mars;
    
       public Spaceship() {
          jupiter = distance_to_jupiter();
          mars = distance_to_mars();
       }
    
       public int distance_to_mars() {
          return(0);
       }
    
       public int distance_to_jupiter() {
          return(1);
       }
    
       public String toString() {
          String outputLine = "\n";
          outputLine += "Distance to Mars:" + "\t" + mars + "\n";
          outputLine += "Distance to Jupiter:" + "\t" + jupiter + "\n";
          return(outputLine);
       }
    
       public static void main(String[] args) {
          Spaceship mySpaceship = new Spaceship();
    
          System.out.println(mySpaceship);
       }
    }
    
    When executed the program produces the following output.

    Distance to Mars:       0
    Distance to Jupiter:	1
    
    Note that in order for the program to execute the implementation of the methods is irrelevant. You may take the same approach in your programming assignment.

    Let us recall some of the topics we covered yesterday.

  • Short-Circuit Evaluation

    When the compiler can determine the result of a logical and or a logical or by evaluating the first condition, then it will not look at the second condition.

    For example, in the expression (3==4) && (x==1), since the first condition is false we know the logical and will be false so the compiler won't examine the second condition.

  • Switch Statement

    The variable in a swith statement must be an ordinal type, that is something that can be counted.

    For example, given the integer 1 we know the next integer is 2, and so on. Given the character `a' we know the next character is `b', and so on.

    So ints and chars are examples of ordinal types.

    However, given the real number 0.03, what is the next real number? Given the string "Hello", what is the next string?

    So real numbers and strings are not examples of ordinal types.

    So in your program you might use a switch statement similar to the following.

    String output = "";
    
    switch (decimalEquivalent) {
       case 0 : output = "0"; break;
       case 1 : output = "1"; break;
       case 2 : output = "2"; break;
       case 3 : output = "3"; break;
    }
    
    Problems

    Let's consider the following problems from the book.

    23. A student traveling to Florida for Spring Break will consider a particular airline if the round trip ticket costs less than $200 and has a layover of no longer than 4 hours; or if the ticket costs between $200 and $300 and has no layover. Write a program to input the name of an airline, cost of ticket, and layover time; output the name of the airline only if it meets the student's criteria.

    24.A student choosing among payment plans for a college loan wants to keep the monthly payments to less than $200. If the amount of the loan is $5000 then write a program to calculate which plans are acceptable given different loan lengths and simple interest rates.

    30. A bicycle shop in Hyannis rents bicycles by the day at different rates throughout the year according to the season. The proprietor also gives a 25% discount if the rental period is greater than 7 days. Renters must also pay a $50 returnable deposit for each bicycle rented. Write a program to input the season and the number of days of rental and then calculate and display a total charge that includes the deposit.

    Season Charge
    Spring $5.00
    Summer $7.50
    Autumn $3.75
    Winter $2.50