CSC224 Homework #1

Due: April 2 (Monday, midnight)


Program Assignments

Write, compile and run the following 5 programs. Each program requires you to write one class. Each class should be in a separater .java file. Zip the 5 files (or use some other compression utility) and submit the single zip file for hw1 on Course Online.

  1. Write a class called Consultant1 with a main method that prompts for and reads the user's name (might contain spaces). Then the it does the following:

  2. Write a class called Consultant2 like Consultant1 except it should use JOptionPane dialog boxes for input and output.

  3. Write a progam ReplaceTester that encodes a string by replacing all letters "i" with "!" and all letters "s" with "$". Use the String method replace. Your test program should demonstrate that you can correctly encode the string "Mississippi" and the string "Missouri".

  4. Write a program HollePrinter that switches the letters "e" and "o" in a string. That is, all letters that were 'e' should be replaced by 'o' and all letters that were 'o' should be replaced by 'e'. Use the String replace method repeatedly. Your program should demonstrate that the string "Hello, World!" turns into "Holle, Werld!" and that the string "Bottled Bones" turns into "Bettlod Benos".

  5. (25 pts) Write a Sort3 class with a main method that (a) prints a message of what it does, (b) prompts for 3 strings and then (c) prints the same 3 strings, but in sorted order.

    Do not use arrays or any library sorting method. Declare 3 String variables: str1, str2, str3 to initially hold the 3 input strings. Then by comparing the strings and swapping the values as necessary, get the smallest string in str1, the next smallest in str2, and the largest in str3. Here 'smallest' string means the one that comes first in the usual ordering of strings. (Remember to use the String compareTo method to compare the strings.)

    Sample Run

    The underlined strings are entered by the user. The program should print all the rest!

    This program will sort any 3 input strings.
    
    First string: morning
    Second string: afternoon
    Third string: night
    
    Sorted Strings:
    afternoon
    morning
    night	  
    	

Review (but don't submit) these exercises/questions for chapter 3.

Review Exercises

  1. 3.1

    Why is the BankAccount(double initialBalance) constructor not striclty necessary? (See BankAcount.java)

  2. 3.2

    Explain the difference between

     BankAccount b;
    	

    and

     BankAccount b = new BankAccount(5000);	  
    	
  3. 3.3

    Explain the difference between

     new BankAccount(5000);	  
    	

    and

     BankAccount b = new BankAccount(5000);	  
    	
  4. 3.4

    What happens in our implementation of the BankAccount class when more money is withdrawn from the account than the current balance?

  5. 3.5

    What is the value returned from the method call b.getBalance() after the following operations?

     BankAccount b = new BankAccount(10);
     b.deposit(5000);
     b.withdraw(b.getBalance() / 2);	  
    	
  6. 3.6

    If b1 and b2 refer to objects of class BankAccount, consider the following instructions.

     b1.deposit(b2.getBalance());
     b2.deposit(b1.getBalance());	  
    	

    Are the balances of b1 and b2 now identical? Explain.

  7. 3.7

    What is the this reference? Why would you use it?

  8. 3.8

    What does the following method do? Give an example of how you can call the method.

     public class BankAccount
     {
       public void mystery(BankAccount that, double amount)
       {
         this.balance = this.balance - amount;
         that.balance = that.balance + amount;
       }
       ... // Other bank account methods, constructors, and instance
     fields (data members)
     }
    	
  9. 3.9

    Suppose you want to implement a class TimeDepositAccount. A time deposit account has a fixed interest rate that should be set in the constructor, together with the initial balance. Provide a method to get the current balance. Provide a method to add the earned interest to the account. This method should have no parameters because the interest rate is already known (stored in a data member by the constructor). It should have no return value because you already provided a method for aobtaining the current balance. It is not possible to deposit additional funds into thie account. Provide a withdraw method that remoes the entire balance. Partial withdrawals are not allowed.

  10. 3.10

    What are the accessors and mutators of the CashRegister class?

  11. 3.11

    Explain the difference between a local variable and a parameter variable.

  12. 3.12

    Explain the difference between an instance field and a local variable.

What/Where to submit

Although you are expected to compile and run the programs, only submit your 5 source .java files.

Since there will be a number of files, please put all the .java files for this assignment into one .zip file (or other compressed file).

Submit your compressed file for hw1 on the Course Online site for CSC224.

Coding Guidelines

Remember to include a header comment suitable for the javadoc utility in each file minimally giving this information for the public class.


/** Description: (The Exercise or Project number for this file or a
   brief description of the public class.)
   Author:  your name
   Class: CSC224
 */

More complete guidelines are in appendix A for formatting your programs for this course.