Monday, September 8, 2008

Major Assignment 1 Due September 15, 2008

These questions are taken from the Big Java book !

Program A:
Provide a class for authoring a simple letter. In the constructor, supply the names of the sender and the recipient:

public Letter(String from, String to)

Supply a method

public void addLine(String line)

to add a line of text to the body of the letter.

Supply a method

public String getText()

that returns the entire text of the letter. The text has the form:

Dear recipient name :
blank line
first line of the body
second line of the body
. . .
last line of the body
blank line
Sincerely,
blank line
sender name

Also supply a program LetterPrinter that prints this letter.

Dear John:

I am sorry we must part.
I wish you all the best.

Sincerely,

Mary

Construct an object of the Letter class and call addLine twice.

Hints: (1) Use the concat method to form a longer string from two shorter strings. (2) The special string “\n” represents a new line. For example, the statement

body = body.concat(''Sincerely,'').concat(''\n'');

You may also use the plus sign + to add two Strings together.

adds a line containing the string “Sincerely” to the body.

Complete the following class in your solution:

/**
This class models a simple letter.
*/
public class Letter
{
/**
Constructs a letter with a given sender and recipient.
@param from the sender
@param to the recipient
*/
public Letter(String from, String to)
{
. . .
}

/**
Adds a line to the body of this letter.
*/
public void addLine(String line)
{
. . .
}

/**
Gets the text of this letter.
*/
public String getText()
{
. . .
}
private String sender;
private String recipient;
private String body;
}


Program B:
You are to create a BankAccount class with the following capabilities. The bank will be charging a fee for every deposit and withdrawal. Supply a mechanism for setting the fee and modify the deposit and withdraw methods so that the fee is levied. Test your resulting class and check that the fee is computed correctly.

The bank will allow a fixed number of free transactions (7 deposits or withdrawals) every month, and charge for transactions exceeding the free allotment. The charge is not levied immediately but at the end of the month.

Supply a new method deductMonthlyCharge to the BankAccount class that deducts the monthly charge and resets the transaction count.

Produce a test program that verifies that the fees are calculated correctly over several months.

Five bonus points if you enable user input for each program. The input will be used to create the objects.

Five more bonus points if Program B continues to loop until user chooses to Quit.

Zip up both files as Netbeans projects.

No comments: