SJSU/Udacity CS046

Problem Set 2

  1. Circle API
  2. Square Constructor
  3. Car Tester
  4. Instance Variables, Local Variables, Parameters, Return Values
  5. Draw W
  6. Credit Card
  7. Inch Worm
  8. Flower

Instance Variables, Local Variables, Parameters, Return Values

What is wrong with this code?

public class BankAccount
{
   private double balance;
   public BankAccount(double startingBalance)
   {
      double balance = startingBalance;
   }
   ...
}
  1. The instance variable should be public so that other classes can see it.
  2. In the constructor, balance should be assigned to a starting balance: double startingBalance = balance;
  3. The constructor should be: balance = startingBalance
  4. The constructor needs a return type.
You should not define a local variable when you want to access the instance variable.

What is wrong with this sequence of statements?

Car mazda = new Car(30);
System.out.println(mazda.drive(50));
  1. You cannot create a Car object that takes one parameter.
  2. The Car class does not have a drive method so it is an error to call it
  3. The drive method has return type void and does not return a value to print
  4. The drive method does not take a parameter
An object's _____________________ variables store the data required for executing its methods. instance
The _________________ name is always the same as the class name. constructor
A _________________________ variable is a variable that is declared in the body of a method. local
Write the code to declare theArea as a local variable of type double and then initialize it to 0 – all in one statement. double theArea = 0;
What is the implicit parameter in this method call? You do not need to know anything about the how the method is implemented to answer the question.
employee.raiseSalary(10)
employee
What is the explicit parameter in the method call of the preceding question? 10
The _______ reference denotes the implicit parameter this
The Car class has this instance variable:
private double milesPerGallon; 
Assume that the constructor were defined like this:
public Car(double milesPerGallon)
Write the code to assign the local variable milesPerGallon to the instance variable milesPerGallon so the class will continue to work correctly. Pay attention to the “camel case” in milesPerGallon.
this.milesPerGallon = milesPerGallon;

Which of the following are good instance variables for a class named Circle whose radius is specified in the constructor?

  1. Good|Bad private double area;
    Compute the area instead of storing it.
  2. Good|Bad private double perimeter;
    Compute the perimeter instead of storing it.
  3. Good|Bad private double radius;
  4. Good|Bad private double pi;
    You don't need a pi variable in each Circle object.

Suppose the implementer of the Color class changes the implementation, keeping the public interface unchanged. As a programmer who uses the Color class, what do you need to change in your classes that use Color objects.

  1. You need to recompile so your class will work with the new implementation
  2. You will need to rewrite some of your code before it will work
  3. You do not need to do anything
  4. You have to take some other action

Problem on this page?

Your name:

Your email address:

Problem description:

To protect against spam robots, please answer this simple math problem:
× =