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.
Monday, September 8, 2008
Find an article about something that interests you...
Write a very brief summary about an article that interests you.
September 8, 2008
Write a guessing game with Java where the user guesses a number between one and 100 and you give tips based on how close or far they are. If they are 20+ digits away you can laugh at them! If they are within 10-20, say they are within a decent range, otherwise say they are getting closer!
You may work with a classmate or two or by yourself.
When you are finished you may add the random number ability:
int ranNumber = (int)(Math.random()*100);
if(ranNumber > 50)
{
System.out.println(ranNumber + " is greater than 50");
}
else if(ranNumber > 25)
{
System.out.println(ranNumber + " is greater than 25");
}
else
{
System.out.println("Your number sucks!");
}
You may work with a classmate or two or by yourself.
When you are finished you may add the random number ability:
int ranNumber = (int)(Math.random()*100);
if(ranNumber > 50)
{
System.out.println(ranNumber + " is greater than 50");
}
else if(ranNumber > 25)
{
System.out.println(ranNumber + " is greater than 25");
}
else
{
System.out.println("Your number sucks!");
}
Wednesday, September 3, 2008
First Post: What are your expectations for the class?
Just briefly let me know what your expectations are for this class!
Client.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bankaccount;
/**
*
* @author dfreer
*/
public class Client {
//Client properties
private String firstName;
private String lastName;
private double balance;
private int socialSecurity;
//methods for the Client
public String returnName()
{
return firstName + lastName;
}
public double getBalance(){
if(balance > 0)
return balance;
else
return -9999;
}
public void deposit(double amt)
{
balance = balance + amt;
}
//this is the constructor for the client object
public Client(String fn, String ln, double b, int ss){
firstName = fn;
lastName = ln;
balance = b;
socialSecurity = ss;
}
public static void main (String [] args)
{
Client ourFirstClient = new Client("Dwyane", "Wade", 6787456.24, 123456798);
//we make an object of Client for Dwyane Wade and return his balance
System.out.println(ourFirstClient.getBalance());
//6787456.24 is expected
ourFirstClient.deposit(1000000);
System.out.println(ourFirstClient.getBalance());
//boolean a = true;
//add to the constructor an address, preferred customer capability,
//add a method to withdraw money but do not let customer withdraw more than they
//have
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bankaccount;
/**
*
* @author dfreer
*/
public class Client {
//Client properties
private String firstName;
private String lastName;
private double balance;
private int socialSecurity;
//methods for the Client
public String returnName()
{
return firstName + lastName;
}
public double getBalance(){
if(balance > 0)
return balance;
else
return -9999;
}
public void deposit(double amt)
{
balance = balance + amt;
}
//this is the constructor for the client object
public Client(String fn, String ln, double b, int ss){
firstName = fn;
lastName = ln;
balance = b;
socialSecurity = ss;
}
public static void main (String [] args)
{
Client ourFirstClient = new Client("Dwyane", "Wade", 6787456.24, 123456798);
//we make an object of Client for Dwyane Wade and return his balance
System.out.println(ourFirstClient.getBalance());
//6787456.24 is expected
ourFirstClient.deposit(1000000);
System.out.println(ourFirstClient.getBalance());
//boolean a = true;
//add to the constructor an address, preferred customer capability,
//add a method to withdraw money but do not let customer withdraw more than they
//have
}
}
Subscribe to:
Posts (Atom)
