Final Review Questions

Solutions to Final Exam Practice Problems

1 Getter and Setter

  • Assume that one of the instance variables of an object is a double value called weight. Write Java code for a setWeight method which does no more than assign the value of a double parameter called weight to the corresponding instance variable.
  • Write Java code for a method called getWeight. This method should be accessible from outside its class, and it should return the current value of an instance variable called weight, which is declared to be a double.

2 Simple Trace Problem

What are values of gus.age, gus.weitght, and gus.precentGrowthRate at the end of the exection of the below main() method.

public class Mouse2Driver
{
  public static void main(String[] args)
  {
    Mouse2 gus = new Mouse2();

    gus.setPercentGrowthRate(5);
    gus.grow();
  } // end main
} // end class Mouse2Driver

public class Mouse2
{
  private int age = 0;              // age of mouse in days
  private double weight = 1.0;      // mouse weight in grams
  private double percentGrowthRate; // increase per day

  public void setPercentGrowthRate(double percentGrowthRate)
  {
    this.percentGrowthRate = percentGrowthRate;
  } // end setPercentGrowthRate

  public void grow()
  {
    this.weight +=
      (.01 * this.percentGrowthRate * this.weight);
    this.age++;
  } // end grow
} // end class Mouse2

3 A Method

Write a Java method returns a random boolean variable which returns true half the time and false the rest of the time. Use the random() method in the Math class which returns random double numbers between 0 and 1. Below is a stub.

public static boolean randomBoolean(){





}

4 A Constructor

Assume you have a Car class that declares two private instance variables, String make; and String model. Write Java code that implements a two-parameter constructor that instantiates a Car object and initializes both of its instance variables.

Practice 1

Practice your pseudocode skills using the following exercises. Answers will be posted over the weekend.

  1. (Easy) Write an algorithm to return the absolute value of an input integer.
  2. (Easy) Write an algorithm which evaluates the quadratic \[ a x^2 + b x + c\] where $a$, $b$, $c$, and $x$ are given as inputs.
  3. (Medium) Write an income tax calculator that calculates the taxes of one or more people. Our simplified tax is divided into the following brackets based on a persons income.
    • Making less than $10,000 gives a tax rate of 0%
    • Making $10,000 or more but less than $40,000 gives a tax rate of 5%
    • Making $40,000 or more but less than $100,000 gives a tax rate of 10%
    • Making $100,000 or more gives a tax rate of 20%
    Ask for income of a person as input, determine how much they owe, and print it, then prompt whether another person will have their tax calculated.
  4. (Hard) Write an algorithm to calculate the nth Fibonacci number. If you have not seen them before, the Fibonacci numbers are defined recursively as follows.
    • $f_0 = 0$
    • $f_1 = 1$
    • $f_n = f_{n-1} + f_{n-2}$
    This means the Fibonacci numbers from 0 to 10 are
    $n$012345678910
    $f_n$011235813213455
    Your algorithm should prompt for $n$ and then print the nth Fibonacci number.
Solutions to Practice 1

The views and opinions expressed in this page are strictly those of the page author.
The contents of this page have not been reviewed or approved by the University of Minnesota.