Home  | 



 ONLINE WEB SESSION   D 

Covering Gaddis chapter 6


Answer the following multiple choice questions for chapter 6

Multiple Choice  ch 6

  1. This is a collection of programming statements that specify the fields and methods that a particular type of object may have.
    1. class
    2. method
    3. parameter
    4. instance
  1. A class is analogous to a(n)       .
    1. house
    2. blueprint
    3. drafting table
    4. architect
  1. An object is a(n)       .
    1. blueprint
    2. primitive data type
    3. variable
    4. instance of a class
    5.  
  1. This is a class member that holds data.
    1. method
    2. instance
    3. field
    4. constructor
  1. This key word causes an object to be created in memory.
    1. create
    2. new
    3. object
    4. construct
  1. This is a method that gets a value from a class’s field, but does not change it.
    1. accessor
    2. constructor
    3. void
    4. mutator
  1. This is a method that stores a value in a field or in some other way changes the value of a field.
    1. accessor
    2. constructor
    3. void
    4. mutator

 

  1. When the value of an item is dependent on other data, and that item is not updated when the other data is changed, what has the value become?
    1. bitter
    2. stale
    3. asynchronous
    4. moldy

 

9.       This is a method that is automatically called when an instance of a class is created.

    1. accessor
    2. constructor
    3. void
    4. mutator
  1. When a local variable has the same name as a field, the local variable’s name does this to the field’s name.
    1. shadows
    2. complements
    3. deletes
    4. merges with

 

    1.  
  1. This is automatically provided for a class if you do not write one yourself.
    1. accessor method
    2. default instance
    3. default constructor
    4. variable declaration
  1. Two or more methods in a class may have the same name, as long as this is different.
    1. their return values
    2. their access specifier
    3. their parameter lists
    4. their memory address
  1. The process of matching a method call with the correct method is known as       .
    1. matching
    2. binding
    3. linking
    4. connecting
  1. A class’s responsibilities are       .
    1. the objects created from the class
    2. things the class knows
    3. actions the class performs
    4. both b and c

 DEFINE THE FOLLOWING TERMS

Constructors

Overloading

Mutators

Accessors

REPRESENTATIVE SAMPLE PROGRAMS FOR CHAPTER 6

SIMILAR PROGRAMS  are in the chapter examples that you downloaded

optional - not to be turned in

type in your name and RUN THEM BOTH using JGrasp


 

THE FIRST IS THE BankAccount CLASS PROGRAM

/**
The BankAccount class simulates a bank account.
*/

public class BankAccount
{
private double balance; // Account balance

/**
This constructor sets the starting balance
at 0.0.
*/

public BankAccount()
{
balance = 0.0;
}

/**
This constructor sets the starting balance
to the value passed as an argument.
@param startBalance The starting balance.
*/

public BankAccount(double startBalance)
{
balance = startBalance;
}

/**
This constructor sets the starting balance
to the value in the String argument.
@param str The starting balance, as a String.
*/

public BankAccount(String str)
{
balance = Double.parseDouble(str);
}

/**
The deposit method makes a deposit into
the account.
@param amount The amount to add to the
balance field.
*/

public void deposit(double amount)
{
balance += amount;
}

/**
The deposit method makes a deposit into
the account.
@param str The amount to add to the
balance field, as a String.
*/

public void deposit(String str)
{
balance += Double.parseDouble(str);
}

/**
The withdraw method withdraws an amount
from the account.
@param amount The amount to subtract from
the balance field.
*/

public void withdraw(double amount)
{
balance -= amount;
}

/**
The withdraw method withdraws an amount
from the account.
@param str The amount to subtract from
the balance field, as a String.
*/

public void withdraw(String str)
{
balance -= Double.parseDouble(str);
}

/**
The setBalance method sets the account balance.
@param b The value to store in the balance field.
*/

public void setBalance(double b)
{
balance = b;
}

/**
The setBalance method sets the account balance.
@param str The value, as a String, to store in
the balance field.
*/

public void setBalance(String str)
{
balance = Double.parseDouble(str);
}

/**
The getBalance method returns the
account balance.
@return The value in the balance field.
*/

public double getBalance()
{
return balance;
}
}

THE SECOND IS THE AccountTest DEMO PROGRAM

import javax.swing.JOptionPane; // For the JOptionPane class

/**
This program demonstrates the BankAccount class.
*/

public class AccountTest
{
public static void main(String[] args)
{
String input; // To hold user input

// Get the starting balance.
input = JOptionPane.showInputDialog(
"What is your account's starting balance?");

// Create a BankAccount object.
BankAccount account = new BankAccount(input);

// Get the amount of pay.
input = JOptionPane.showInputDialog(
"How much were you paid this month? ");

// Deposit the user's pay into the account.
account.deposit(input);

// Display the new balance.
JOptionPane.showMessageDialog(null,
String.format("Your pay has been deposited.\n" +
"Your current balance is $%,.2f",
account.getBalance()));

// Withdraw some cash from the account.
input = JOptionPane.showInputDialog(
"How much would you like to withdraw? ");
account.withdraw(input);

// Display the new balance
JOptionPane.showMessageDialog(null,
String.format("Now your balance is $%,.2f",
account.getBalance()));

System.exit(0);
}
}


 

HINTS FOR CHAPTER 6

Read the following hints for correcting your JAVA programs

·         Putting a semicolon at the end of a method header. A semicolon never appears at the end of a method header.

·         Declaring a variable to reference an object, but forgetting to use the new key word to create the object. Declaring a variable to reference an object does not create an object. You must use the new key word to create the object.

·         Forgetting the parentheses that must appear after the class name, which appears after the new key word. The name of a class appears after the new key word, and a set of parentheses appears after the class name. You must write the parentheses even if no arguments are passed to the constructor.

·         Forgetting to provide arguments when a constructor requires them. When using a constructor that has parameter variables, you must provide arguments for them.

·         Trying to overload methods by giving them different return types. Overloaded methods must have unique parameter lists.

·         Forgetting to write a no-arg constructor for a class that you want to be able to create instances of without passing arguments to the constructor. If you write a constructor that accepts arguments, you must also write a no-arg constructor for the same class if you want to be able to create instances of the class without passing arguments to the constructor.

·         Unintentionally declaring a local variable with the same name as a field of the same class in a method. When a method’s local variable has the same name as a field in the same class, the local variable’s name shadows the field’s name.

Chapter 6

Multiple Choice

1.         a

2.         b

3.         d

4.         c

5.         b

6.         a

7.         d

8.         b

9.         b

10.       a

11.       c

12.       c

13.       b

14.       d