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;
}
...
}
- The instance variable should be public so that other classes can see it.
- In the constructor,
balanceshould be assigned to a starting balance:double startingBalance = balance; - The constructor should be:
balance = startingBalance - 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));
- You cannot create a
Carobject that takes one parameter. - The
Carclass does not have adrivemethod so it is an error to call it - The
drivemethod has return typevoidand does not return a value to print - 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?
- Good|Bad
private double area;Compute the area instead of storing it. - Good|Bad
private double perimeter;Compute the perimeter instead of storing it. - Good|Bad
private double radius; - Good|Bad
private double pi;You don't need apivariable in eachCircleobject.
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.
- You need to recompile so your class will work with the new implementation
- You will need to rewrite some of your code before it will work
- You do not need to do anything
- You have to take some other action