Use the following class to respond to questions on the next page.

 

a.) public class investmentAccount

    {

            private int numShares;                                      

            private int stockValue;                                     

            private int accountBalance;

 

b.)        public investmentAccount(int openingBalance) {

                        accountBalance = openingBalance;

                        numShares = 0;                                                           

                        stockValue = 0;

            }

 

c.)        public void depositFunds(int depositAmount) {

                        accountBalance += depositAmount;

             }

 

d.)        public void buyStock(int numberToBuy, int cost) {

                        numShares += numberToBuy;

                        stockValue += (cost * numberToBuy);

                        accountBalance -= (cost * numberToBuy);      

            }

 

e.)        public int getBalance () {

                        return accountBalance;

            }   

  

f.)         public int getStockValue() {

                        return stockValue;

            }

 

g.)        public void sellStock (int numberToSell, int stockPrice) {

if ((stockValue) >= (numberToSell * stockPrice)) {

                        stockValue -= (numberToSell*stockPrice);

                        accountBalance += (numberToSell * stockPrice);

}else{

                        System.out.println (“You do not own enough stock.”);

}//end if

            }//end sellStock

}//end investmentAccount

 

 

 

 

 

Refer to the class above to answer the following.

  1. How many fields are there in the class?  __________________________________
  2. How many constructors?          ______________________________________________
  3.  Which methods are accessor methods? ______________________________________
  4. Which are mutator methods?     ______________________________________________
  5. Which is the constructor?          ______________________________________________
  6. What is the scope of int numberToBuy?           __________________________________
  7. Which methods contain expressions?                 __________________________________
  8. Which section is the header located in?  __________________________________
  9. Which method contains a conditional?   __________________________________
  10. Write a method that to withdraw funds from the account and then print the new balance to the terminal window.

public _________ withdrawFunds(________________) {

 

 

 

 

}

 

public void addNumbers(int firstNumber) {
            int secondNumber = 3.
            System.out.println(firstNumber + secondNumber);
}

 

  1. The variables used in the above method are ________________variables.

 

  1. The variables and parameters used in the above method will exist only while __________________________________________________________________.

 

 

 

  1. What will be returned by the following:
    1. “The Book”.substring(4,7);                   _____________________________
    2.  “The Book”.substring(2,5);                  _____________________________
    3.  “The Book”.length();                            _____________________________
    4. “The Book.”.length();                            _____________________________
  2. Create a class named payroll.
    1. Create fields for employeeName and salaryAmount.
    2. Create a constructor that takes name and salary as parameters.
    3. Assign the value received by the constructor parameters to the appropriate fields.
    4. Write a method called getPayDue that will calculate and return the pay due.

(Hint: getPayDue will have a return type and a parameter.)

public class payroll

{

//fields

 

 

 

//constructor

 

 

 

 

 

//methods

 

 

 

 

 

}

 

Answer Key

 

  1. 3
  2. 1
  3. e & f
  4. c, d, & g
  5. b
  6. only within the method buyStock
  7. c, d, & g
  8. a
  9. g
  10.  
  11. public void withdrawFunds (int amount) {

                      accountBalance -=  amount;

                      System.out.println(“New Balance: “ + accountBalance + “.”);

          }

 

  1. local
  2. The method is in use.
  3.  
    1. “Boo”
    2. “e B”
    3. 8
    4. 9

 

  1.  

public class payroll

{

//fields

String employeeName;

int salaryAmount;

 

//constructor

            public payroll(String name, int salary) {

                        employeeName = name;

                        salaryAmount = salary;

            }

                       

//methods

            public int getPayDue(int hours) {

                        int pay;

                        pay = hours * salaryAmount;

                        return pay;

                        }

            }