Part A. Change the move and turn methods of the
Bug2 class from Homework 3 Part C so that
bug.move(0.5)bug.turn(45) to turn 45 degrees clockwise.Hint: Both position and direction can now be arbitrary numbers. You need to know how to rotate a direction. The equations are
newdx = cos(α) dx + sin(α) dy
newdy = -sin(α) dx + cos(α) dy
Here, α is the angle in radians. There is a method in the Math
class to do the conversion from degrees to radian.
Draft: Provide a tester that
Bug2 (at the origin, facing the 3 o'clock
direction)For the draft, you submit only BugTester.java. It will be
tested with an actual Bug2 class (which you don't see).
For the final version, you submit both BugTester.java and
Bug2.java. Make sure that Bug2 has appropriate
Javadoc comments.
Part B. Write a tester for the following method of the
BankAccount class:
/** Adds a given percentage to the balance of this account @param n how often to add the interest (e.g. 10 for ten times) @param percent the percentage to add, e.g. 5.5 to add 5.5% */ public void addInterest(int n, double percent)
You don't write the BankAccount class—it's provided for you.
Actually, two classes are provided for you. For the draft, the
addInterest method doesn't use compound interest—it simply adds
n * percent interest. In the final, the addInterest
method uses compound interest. Your job is to test that the result is correct
according to the formula for compound interest:
interest = (1 + percent/100)n
For both draft and final, supply BankAccountTester.java that
construct a BankAccount with $1000, add 10 years of interest at 5
percent, and print the actual and expected balance. In the final version, be
sure to use Math.pow.
Part C. Write a class that helps in writing mail templates. Something like
Dear John: It appears you have not turned in CS46A homework 2 draft. Please remember that you are expected to attempt all assignments in this class. Sincerely, Professor Fiendish
Each mail has these items:
You construct a Mail object with the before and after parts and
the sender:
Mail warning = new Mail("It appears you have not turned in ",
". Please remember that\nyou are expected to attempt all assignments in this class.
",
"Professor Fiendish");
Then you can make as many text strings as you like, with the
createMessage method that has two parameters: the name and the
variable part.
String message1 = warning.createMessage("John", "CS46A homework 2 draft");
String message2 = warning.createMessage("Mary", "CS46A homework 2 draft");
String message3 = warning.createMessage("Shawn", "CS46A homework 2 final");
Draft: A Mail class that has the required constructor and
createMessage methods. The constructor need not do anything, but
createMessage should return a string like "Dear
John", i.e. "Dear" followed by the recipient, but not a
colon. Make sure to provide Javadoc comments.
Final: The complete class.
Hint: You can use the concat method to combine two strings. Try
out "Hello".concat("World") to see what it does.