Question
In this assignment, you will implement three ADTs: Money An immutable ADT representing an exact amount of dollars and cents. Example: $3.14 Account A mutable
In this assignment, you will implement three ADTs: Money An immutable ADT representing an exact amount of dollars and cents. Example: $3.14 Account A mutable ADT for a bank account with an owner, a identifier (usually a sting of digits), a minimum balance and current balance. Example: A credit card account with a minimum of ($1000.00) and a current balance of ($245.99), These amounts are negative (as signified by the parentheses). Transaction An immutable ADT for a transfer of money from one account to another. Nor- mally the transaction shouldnt overdraw the account the money is drawn from. Example: Transfer (payment) of $100.00 from my checking account to my credit card account. We provide JUnit test cases to test your code. Some of the tests are locked as described in the lab exercise. Use this link to accept the assignment:
1 Immutable ADTs and standard methods This week, two of our ADTs are immutableall fields are final. The fields are assigned in the constructor and never changed. If you find yourself adding non-final fields or wanting to change the value of a field, you are doing something wrong. Immutable ADTs simplify programming because then you dont need to worry that (say) a dollar suddenly becomes a cent. This is one of many examples where reducing power makes programming clearer and simpler. In most situations (including this Homework), immutable ADTs should override the standard implementations of the following methods: equals(Object) Return true if the argument has the same value as this. hashCode() Return an integer that summarizes this value. This method should be efficient. Do not create a string in the process. toString() Return a descriptive string. Additionally, if the ADT represents a comparable quantity, it should declare that it implements Comparable
2 Concerning the Money ADT The Money ADT represents a fixed whole number cents (pennies). A negative number of cents represents a debt and is written with parentheses as in ($20.00). Internally, the whole number is represented by a (signed) long, which has a wide but still finite range. Thus the largest amount of money that can be represented is $92233720368547758.07 (92 quadrillion dollars and then some) which is well over a thousand times the US National debt (which is more than thirty trillion dollars). In the unlikely event that someone tries to accumulate more than this in their account (or worse, accumulate a debt this high in their account), the ADT will throw an ArithmeticException to indicate that it is unable to represent the amount. Those who know twos complement representations will know that the minimum long is slightly different: 9223372036854775808. To retain symmetry, this debt of cents will not be allowed: the largest debt that Money will represent is the negation of the maximum. Those of you with knowledge of IEEE floating point representations will know that a Java double cannot exactly capture the value of a long. For example, if one takes the maximum money value given earlier and assigned it to a double and then subtracts 3.14, the result is the same: double max = 92_233_720_368_547_758.07; double less = max - 3.14; if (less == max) System.out.println("The same!"); This code will print The same! For this reason, we use long rather than double to represent money. The ADT is immutable; so a dollar cannot be converted into a cent, or vice versa. It provides the following operations: Money(double) Construct a money object where the double value is rounded to the nearest cent (use Math.round). As explained above, this constructor cannot be used to construct every possible value. But if a double larger than the maximum (or smaller than the minimum debt) is passed, the constructor should throw an ArithmeticException. asDouble() Return the money as a double number of dollars. This loses precision with very large amounts or debts. (Done for you.) isPositive() Whether the money amount is a positive amount of money. isNegative() Whether the money represents a debt. negate() Return the money that if added to this money, the result would be zero. add(Money) A precise addition of other money. Return a new amount of money. This will throw an ArithmeticException if the result would fall beyond the range of doubles. See exact operations in the Math library class. sub(Money) As with addition, but subtraction. Must use exact arithmetic. mul(double) Return a scaling of this money with the given number, rounded to the nearest cent. This operation is not precise with large numbers.
div(Money) Return the ratio between two sums of money. Can return Infinite or NaN if the argument is zero. toString() Return a string showing the exact number of cents, with a dollar sign and decimal, and with parentheses to show negative amounts. You will have to handle the decimal point manually. Dont try to format a doubleit loses precision. As mentioned previously, the ADT is immutable and (by convention) provides useful equals and hashCode methods. It also provides a compareTo method: which returns a negative number, zero or a positive number if this amount is less than, equal to or greater than the argument, respectively. Internally a Money object is represented in terms of a integral number of cents. 3 Concerning the Account ADT This ADT models a simple bank account which has an owner, a secret identification number, a minimum balance (which is not always enforced) and a current balance. This is a mutable ADT in which the first two fields are final, the third (minimum) could in principle change (but doesnt for us) and the last one frequently changes. The identification string (usually but not necessarily all digits) must have at least four char- acters, and when an account is printed, it shows only the last four characters of the identification number. There are three constructors to create a new account. Two of them dont take an identification string, and instead generate one. They should delegate most of the work to the last constructor (using this(...) syntax). Indeed, in our solution, the simplest constructor delegates to the second constructor which then delegates to the last (and most complete) constructor. This chaining of constructors avoid code duplication. The toString method (which is already written for you) lists the owner of the account, a space, and the last four characters of the account identification code. As well as getters for all four values, the Account ADT provides a single additional method: adjust which takes an amount of money and a force boolean. The amount of money is added to the balance, but if the amount is negative, we sometimes check to see if there is an overdraft, a warning that the amount would fall below the minimum. Overdrafts are only checked when the amount is decreased and not checked if the force boolean argument is true. If there would be an overdraft, no change is made to the account balance and instead an overdraft exception is thrown which names the account with the overdraft, and the amount the account would have gone below its minimum balance, if the adjustment had been made. The force boolean prevents any overdraft check. If the new balance would fall out of the legal range of money, this method throws an arithmetic exception, but this effect doesnt need to be done directly. It is easiest (and best) to let the exception propagate from a Money method call. 4 Concerning the Transaction ADT A transaction is the transfer of money between two places. A transaction should either be com- pletely performed or should have no effect. We dont want money to be lost in the transition. The Transaction ADT is an immutable ADT with four parts: a memo that gives information about the transaction, the source and destination accounts and a (positive) amount of money to be transferred. A transaction with zero money isnt allowed, since it would have no effect.
Either the source or destination account (both not both) can be null, in which case it is under- stood that the source or destination is the bank itself, which will be assumed to have an unbounded amount of money available. The source and destination accounts must be different. A transac- tion with a null source is called a deposit and a transaction with a null destination is called a withdrawal. A transaction can be created with or without a memo string. If no string is given, the default value is the empty string (not null). Use the this(...) syntax to avoid code duplication between your constructors. As this ADT is immutable, you will need to override equals, hashCode and toString. The latter method will produce a string of the form: KIND [ from source][ to destination]: amount memo where the kind is one of TRANSFER, DEPOSIT and WITHDRAWAL and we only give the source and destination accounts if they are not null. The last method provided by ADT is the most complex one for this homework: perform takes a force boolean, and performs the transaction returning any balance that goes to the bank. The force boolean is used to handle the transfer out of the source; if not true then the account must have sufficient funds. Recall that a transaction must be performed completely or have no effect. So if an exception is thrown, it must not leave any account changed. The return value of perform is the amount that the bank gains by performing the transaction. The result is positive if the transaction is a withdrawal because the bank handles the withdrawn money. It will be a negative amount in the case of a deposit (the bank has to make up the money). 5 Test Programs We provide three JUnit test suites, TestMoney, TestAccount and TestTransaction, that you should use to test your ADTs. Do not change them. In Eclipse, you can right click a test and RunAs>JUnit Test. Your assignment will be graded according to the first failure of the regular tests. So concentrate on the earlier tests before trying to handle later tests.
What you need to do You need to finish the implementations of Money, Account and Transaction. For the first two classes, we have written all the method headers and documentation. For the Transaction class, you need to write documentation comments for all public methods and constructors that are not marked @Override. For overriding methods, you should give the reason for the overriding in a short one-word comment: required A compiler error results without this definitionan implementation has been promised but otherwise not given. implementation The implementation being overridden does the wrong thing. efficiency The implementation being overridden works less efficiently than this implementa- tion.
public void test() { // Read assignment for how money is convert to a string! assertEquals(???, Money.CENT.toString());
HELP ME TO UNLOOK IT PLEASE AND WRITE THE CODE IF U NEED ANY INFORMATION ASK ME PLEASE
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started