Writing Classes Lab 2

advertisement
Writing Classes Lab 2
Prelab: Answer the following question before you start working on the Adler lab computer.
1. What is the difference between an object and a class?
2. Consider the following method along with the member data declaration in the class:
public class Student {
private String name;
private int id;
private int year;
…
//a mutator method to reset the state of the object
public void set(String n, int i, int y){
name = n;
id = i;
year = y;
}
…
Write a valid call to the method set(). Don’t forget to create the object first.
3. What are constructors used for? Create a constructor for the above Student class.
Part 1: Where is the bug?
1.Import the code called Rectangle.java and RectTest.java into Eclipse.
2.RectangeTest is an application designed to calculate the area of a rectangle defined by
Rectangle.java. Two rectangle objects are instantiated and the area of both rectangles is
calculated.
3.OOPS! There are a few errors in the code. For starters, the dimensions of r2 are supposed to
be 22×9. Find and correct all the errors for full credit.
Part 2: Vote Counter
Write a class called VoteCounter. A VoteCounter object will tally yes/no votes for any given
question. Your class should contain the following instance data and methods:
Instance data:
- a string to hold the question being voted on
- an integer to count the number of "yes" votes
- an integer to count the number of "no" votes
Methods:
- constructor - takes the vote question as a parameter, sets the question, and sets yes and no vote
counts both to zero.
- voteYes and voteNo methods - increments the appropriate vote count. (calling either method
represents one vote, yes or no)
- getResult - returns a string: "yes" if there are more yes votes, "no" if there are more no votes, or
"tie" otherwise.
- toString - returns a nicely formatted string containing the question, the number of yes votes, the
number of no votes, and the result. (Hint: use your getResult method inside this method)
Now write a main method to test all your other methods. First declare a VoteCounter object and
instantiate it with a question. Then call the yes/no vote methods as many times as you like, to
simulate a tally or survey. (Or you can do this part in a fancy loop, if you want.) When the voting
is finished, print everything out using the toString method.
Optional (Credit Cards)
In this exercise, you will write a class to represent a credit card. The class details are as follows:
1. Static Variables
a. baseRate - The minimum interest rate that the card can have. This should
be a constant that is hard coded.
b. lastAccountNumber - The last account number assigned to a customer.
Account numbers should be assigned sequentially.
2. Instance Variables
a. accountholder - The name of the person who owns the card.
b. accountNumber - A unique 7-digit identifier number.
c. creditScore - The account holder's credit score.
d. rate - The annual interest rate charged to the card.
e. balance - The current balance on the card.
f. creditLimit - The card holder's credit limit.
3. Methods
a. Constructor – Should only take the account holder's name and a credit
score. The next account number available should be assigned. The balance
will be 0 as nothing has been charged yet. The rate and credit limit will be
determined by the credit score. Use the following table to set the rate and
credit limit.
Credit Score
Rate
Limit
0-300
baseRate + 10% $1000
300-500
baseRate + 7% $3000
500-700
baseRate + 4% $7000
700+
baseRate + 1% $15000
b. makePurchase - Takes a purchase amount as a parameter and updates the
current balance. If the amount + balance > creditLimit, deny the
transaction.
c. makePayment - Takes a payment amount as a parameter and updates the
current balance. If the payment is greater than the balance, set the balance
to zero and print an appropriate message. If the payment is less than 10%
of the balance, apply the payment and raise the account holder’s rate by
1%. If the balance is paid off entirely, raise the creditScore by 10. If the
increased creditScore changes where the account holder falls in the above
table, change the rate and limit as appropriate.
d. raiseRate - Raises the account holder's rate by a given percentage.
e. raiseLimit - Raises the account holder's limit by a given dollar amount.
f. calculateBalance - Calculates the balance on a monthly basis. Remember
that the rate is yearly, so the formula for calculating the balance monthly is
balance + (balance * (rate / 12)).
g. toString - Print the account holder's name, account number obscured with
stars (ex: 1234567 would display as ****567), balance, and limit.
To test your class, write a driver program to track an account holder over six months. Each
month, allow the account holder to make as many purchases as he wishes. At the end of each
month, if the balance is greater than 0, ask the user if he would like to make a payment. Apply
the payment if he makes one. If a payment is not made during any given month when there is a
balance on the card, raise the rate by 2%. Finally, calculate the new balance at the end of each
month.
Download