Lab 5 Pre-Lab Assignment

    

Name ___________________________________

Show all work!!!!

  1. Find the base 10 value of each of the following 8-bit two's complement numbers:

    1. 11011101

       

       

    2. 01110100

     

     

  2. Convert the base 16 number 3C7F16 to
    1. base 2 (using the special relationship between base 16 and base 2)

       

       

    2. base 8 (using the special relationship between base 8 and base 2)

       

       

  3. What is the largest positive integer of type int (that is, the largest positive integer that can be represented in 32 bits)? Write the formula (in terms of powers of 2) and find its value in base 10.

     

     

     

  4. The 8-bit two's complement representation of 107 is 01101011 and the representation of 52 is 00110100.
    1. Find the 8-bit sum (two's complement) of 107 and 52.
                 0 1 1 0 1 0 1 1
               + 0 0 1 1 0 1 0 0
               ------------------
      					<---- your answer
               __________________
      
      
    2. Convert the two's complement answer to base 10.

       

       

       

    3. Did overflow occur? Explain.

       

       

       

  5. In class we talked about the bank account class on pages 178 and 179 in the text (with minor changes - see the text for the complete class). Part of it is below:
    //*****************************************************
    // Account.java
    //
    // Represents a bank account; allows deposits,
    // withdrawals, balance inquiries, etc.
    //*****************************************************
    public class Account
    {
      public static final double RATE = 0.035; // interest rate of 3.5%
    
      ___________________________________________________________; // $10 fee
    
      private long acctNumber;
      private double balance;
      private String name;
    
      //---------------------------------------------------
      // Constructor -- initializes instance data
      //---------------------------------------------------
      public Account(String owner, long account, double initial)
      {
        name = owner;
        acctNumber = account;
        balance = initial;
      }
    
      //---------------------------------------------------
      // Deposits the amount from the account and returns
      // the new balance.
      //---------------------------------------------------
      public double deposit (double amount)
      {
         balance += amount;
         return balance;
      }
    
      //---------------------------------------------------
      // Simple withdrawal -- deducts amount from balance
      // and returns the new balance. 
      //---------------------------------------------------
      public double withdraw(double amount)
      {
          balance -= amount;
          return balance;
      }
    
      //---------------------------------------------------
      // Adds interest to the account and returns the 
      // new balance.
      //---------------------------------------------------
      public double addInterest ()
      {
         balance = balance + balance * RATE;
         return balance;
      }
    
      //---------------------------------------------------
      // Returns the current balance.
      //---------------------------------------------------
      public double getBalance()
      {
         return balance;
      }
    
      //------------------------------------------------------
      // Deducts the service fee and returns the new balance.
      //------------------------------------------------------
      public double chargeFee()
      {
          // deduct the fee
    
          ____________________________________________________
    
    
          // return the updated balance
    
    
          ____________________________________________________
      }
    
      //---------------------------------------------------
      // Changes the name on the account to newName.
      //---------------------------------------------------
      public void setName(String newName)
      {
        
          ___________________________________________________
      }
    
      //--------------------------------------------------
      // Returns a string representation of the account.
      //--------------------------------------------------
      public String toString ()
      {
         return "Account number: " + acctNumber + "\nOwner: "
               + name + "\nCurrent balance: " + balance;
      }
    }
    
    
    1. Add a statement that declares a constant for a service fee of $10. Add this on the blank line after the declaration of the constant RATE. Choose the name of the constant using Java naming conventions.
    2. Fill in the code for method chargeFee, which should deduct a service fee of $10 (use the constant!) from the account, and then return the balance.
    3. In the last space, fill in the body for method setName that takes a string as a parameter and changes the name on the account to be that string.

  6. The Java program below uses the Account class defined in above. Complete the program, using method calls, as indicated by the comments.
    //*****************************************************
    // ManageAccounts.java
    //
    // Uses the Account class to create and
    // manipulate bank accounts for Sally and Joe.
    //*****************************************************
    public class ManageAccounts
    {
      public static void main(String[] args)
      {
        Account account1, account2;
     
        //create account1 for Sally for $1000
        account1 = new Account("Sally", 12345, 1000);
    
        //create account2 for Joe with $500 and account number 11229
    
        ______________________________________________
    
    
        //deposit $100 to Joe's account
    
        ______________________________________________
    
        //withdraw $50 from Sally's account
    
        ______________________________________________
    
        //charge fees to both accounts
    
        ______________________________________________
    
        ______________________________________________
    
    
        //change name on Joe's account to Joseph
    
        ______________________________________________
    
        //print summary for both accounts
    
        ______________________________________________
    
        ______________________________________________
       
      }
    }