Question
Coin.java /** * This enum represents an enumeration of the US Currency Coin constants * * */ public enum Coin { PENNY(1), NICKEL(5), DIME(10), QUARTER(25);
Coin.java
/** * This enum represents an enumeration of the US Currency Coin constants * * */ public enum Coin { PENNY(1), NICKEL(5), DIME(10), QUARTER(25);
private final int value; // value of this coins
/** * The constructor for an enum type must be package-private or private access. This private * constructor is only used to create the constants that are defined at the beginning of this * enumeration's body. * * @param value value in cents of this coin */ private Coin(int value) { this.value = value; }
/** * Returns the value in cents of this coin * * @return the value of this coin */ public int value() { return value; }
}
ExceptionalBank.java
import java.util.Arrays; import java.util.Random;
/** * This class implements an expanded version of elastic bank application * */ public class ExceptionalBank { private Coin[] coins; // array which stores all coins held in this elastic bank private int size; // size of this elastic bank private int expansionsLeft; // number of expansions left for this elastic bank private static Random rand = new Random(100); // random integers generator
/** * Creates a new elastic bank object with a given initial capacity * * @param initialCapacity initial capacity of this elastic bank */ public ExceptionalBank(int initialCapacity) { coins = new Coin[initialCapacity]; this.expansionsLeft = 2; }
/** * Creates a new elastic bank object with an initial capacity equal to 10 */ public ExceptionalBank() { this(10); }
/** * Returns the capacity of this elastic bank * * @return the capacity of this elastic bank */ public int capacity() { return coins.length; }
/** * Returns the expansions left for this elastic bank * * @return the expansions left for this elastic bank */ public int getExpansions() { return this.expansionsLeft; }
/** * Returns the number of coins held in this elastic bank * * @return the size of this elastic bank */ public int getSize() { return this.size; }
/** * Returns the value in cents of coins held in this elastic bank * * @return the balance of this elastic bank */ public int getBalance() { int balance = 0; // add the value of each coin held in this bank to balance, then return it for (int i = 0; i
/** * Returns the number of coins with a specific coinName held in this bank. The coin name * comparison is case insensitive * * @param coinName name of a coin * @return the count of coins having the provided coinName, held in this bank */ public int getSpecificCoinCount(String coinName) { int count = 0; for (int i = 0; i
/** * Returns a string representation of all the coins held in this elastic bank. Each coin is * represented by the pair "(name, value)", and the string representation should contain all of * these pairs in one space-separated line. For example: "(PENNY, 1) (QUARTER, 25) (PENNY, 1) * (DIME, 10) (NICKEL, 5)" * * @return a String representation of the contents of the bank. */ public String getCoins() { String contents = ""; // traverse the coins oversize array and add each coin's string representation to the string to // be returned for (int i = 0; i
/** * Returns a summary of this bank contents * * @return an empty string if this bank is empty, and a string representation of the summary of * this bank otherwise. The summary of the bank is a set of lines. Each line is formatted * as follows "coin_name:coin_count" */ public String getSummary() { String summary = ""; Coin[] values = Coin.values(); // traverse this bank contents and update its summary for (int i = 0; i
}
/** * Removes and returns a coin at a random position from this elastic bank * * @return the removed coin or null if this bank is empty */ public Coin removeCoin() { if(size == 0) return null; int randPosition = rand.nextInt(size); // get a random position from 0 .. size-1 Coin removedCoin = coins[randPosition]; // store the coin to be removed // The order of the coins within this bank (a piggy bank) is not important // So, move the coin at the end of the coins array to the random position // and set that last element to null. coins[randPosition] = coins[size - 1]; coins[size - 1] = null; size--; // update size return removedCoin; }
/** * Removes all the coins from this elastic bank */ public void empty() { // set all the non-null references within the coins array to null for (int i = 0; i
/** * adds a Coin to the bank and adjusts the capacity of coins if necessary and possible * * @param c coin to be added to this elastic bank */ public void addCoin(Coin c) { // check if this bank is full if (size == coins.length) { // check whether there are expansions left if (this.expansionsLeft > 0) { // expand the capacity of this elastic bank by 10 coins = Arrays.copyOf(coins, coins.length + 10); this.expansionsLeft--; } else { // no expansions left // empty this elastic bank empty(); } } // add c at the end of this bank coins[size] = c; size++; }
}
Additional Assignment Requirements All String comparisons in this assignment are CASE INSENSITIVE. You MUST NOT add any additional fields either instance or static, and any public methods either static or instance to your Exceptional Bank class, other than those defined in this write-up and these javadocs. You CAN define local variables that you may need to implement the methods defined in this program You CAN define private helper methods to help implement the different public methods defined in this program, if needed. You CAN define private static helper methods to help implement the different public static methods defined in this write-up. All your test methods must be public static. Also, they must take zero arguments, return a boolean, and must be defined and implemented in your ExceptionalBankTester.java. Your Exceptional BankTester class must implement at least the test methods defined in these javadocs with exactly the same signatures. Feel free to add additional test methods and/or consider additional test scenarios to further convince yourself of the correctness of your implementation. All implemented methods MUST have their own javadoc-style method headers with complete information, with accordance to the CS300 Course Style Guide. . Feel free to reuse the information provided in these javadocs in your own java-doc style method headers. 1 Getting Started Start by creating a new Java Project in eclipse called P04 Exceptional Piggy Bank, for instance. You have to ensure that your new project uses Java 11, by setting the "Use an execution environment JRE:" drop down setting to "JavaSE-11.0.X within the new Java Project dialog box. In this assignment, we are going to provide you with a source code similar to your implementation of P03 Elastic Bank, but using an enumeration to define the Coin type rather than an instantiable class. To do so, download first these two source files Coin.java and ExceptionalBank.java. Then, add both of them to the default package of the src folder of your project. Then, create and add a third file named ExceptionalBankTester.java to the same project. Note that this file should include all your test methods and a main() method. We note also that Appendix A provides links to java API of the set of exception classes that you may use in this program. Appendix B presents a set of useful methods that you may use while developing this assignment. 2 Complete the implementation of ExceptionalBank class Start first by reading the provided code. You should be familiar now with the elastic piggy bank application. Notice that there are additional methods not provided in P03 added to this project, for instance get SpecificCoin Count() and getSummary() methods. Notice also that none of the implemented methods use hard coding of the coin names or values. Our implementation should work appropriately regardless of the set of constants defined in the enum Coin and their values. 2.1 Update the constructor, addCoin(), and removeCoin() methods Start by updating the implementation details of the constructor of the class ExceptionalBank(int), and the instance methods addCoin() and removeCoin) according to their detailed javadocs description provided within these javadocs. These methods are expected to work as described in P03 when they terminate without errors. Read carefully the provided javadoc method headers, and pay close attention to the exceptions that should be thrown by the constructor and the public methods. Add appropriate @throws annotations to the javadoc style method header of the constructor of your ExceptionalBank class, addCoin), and removeCoin) methods. Keep in mind to NOT catch an exception within the implementation details of a method if that exception has been declared to be thrown using throws annotation in its javadoc method header. Let the exception propagate to the method's caller. To check the correctness of your ExceptionalBank class so far, implement the test AddCoin(), testExceptionalBankConstructor(), testGoodExceptionalBankConstructor(), and testRemoveCoinEmptyBank) test methods with accordance to the details provided in their javadoc method headers. We provide you in the following with an example of implementation of the testExceptionalBankConstructor() test method. Feel free to reuse the provided code in your own submission. You can define additional test scenarios to convince yourself that your test methods are able to detect defects of any wrong implementation, and pass otherwise. /** * This method checks whether the ExceptionalBank constructor throws an * IllegalArgumentException with appropriate error message, when it is passed * a zero or a negative capacity. This test must fail if another kind of exception * is thrown for such test scenario. * @return true when this test verifies a correct functionality, and false otherwise public static boolean testExceptionalBankConstructor() { try { // create an exceptional bank with a negative capacity ExceptionalBank bank = new ExceptionalBank(-10); System.out.println "Problem detected. The constructor call of the ExceptionalBank class did not " + "throw an IllegalArgumentException when it is passed a negative capacity."); return false; // return false if no exception has been thrown } catch (IllegalArgumentException e) { // check that the caught IllegalArgumentException includes // an appropriate error message if (e1.getMessage() == null // your test method should not throw // a NullPointerException, but must return false if e.getMessage is null Il !e1.getMessage().toLowerCase().contains ("must be a non-zero positive integer")) { System.out.println "Problem detected. The IllegalArgumentException thrown by the constructor" + "call of the ExceptionalBank class when it is passed a negative capacity" + "does not contain an appropriate error message."); return false; } catch (Exception e) { // an exception other than IllegalArgumentException has been thrown System.out.println( "Problem detected. An unexpected exception has been thrown when calling the + "constructor of the ExceptionBank class with a negative argument." + "An IllegalArgumentException was expected to be thrown." + "But, it was NOT the case."); e2.printStackTrace(); // to help locate the error within the bad ExceptionalBank // constructor code. return false; return true; // test passed None of your unit test methods should ever throw any exception. Your test method should catch any exceptions that may be thrown by the call of the constructor or the methods defined in the Exceptional Bank class, and returns true if the expected behavior has been satisfied, and false otherwise. 2.2 Implement and test addCoins (String) method The addCoins (String) is one of the new methods that we are going to add to this expanded version of the elastic piggy bank project. This method takes a String as input parameter which represents a command line to add new coins to the exceptional bank. You have to implement now this method with accordance to the details provided in its javadoc style method header comments available in these javadocs. If correctly formatted, the string provided to addCoins() method should consist of two parts separated by a colon".". The first part must refer to one of the constants defined in the enum Coin, which represent a coin's name, based on a case insensitive comparison. The second part must be a positive integer. It represents the number of coins with that name that must be added to the exceptional bank as result of running addCoins() method. No coin will be added to the bank if the format of the provided string is incorrect. Do not forget to implement the test methods related to check the correctness of the addCoins () defined in the ExceptionalBankTester class according to these javadocsfirst. The following are examples of valid string addCoins commands to add 2 quarters for instance: "QUARTER:2" "quarter:2" " Quarter : 2" "UARTET: 2" Whereas, the following are examples of invalid (mal-formatted) string objects to add 2 quarters for instance: "QUARTERS:2" "quarter:two" "quarter:-2" . "Quart : 2" "quarter 2" "25 : 2" "quarter:2 :penny:3" Notice also carefully that DataFormat Exception extends directly the Exception class. It is not a RuntimeException. It must be a checked exception. 2.3 Implement and test loadCoins() and saveBankSummary() methods Now, implement the loadCoins() and saveBankSummary() methods in your ExceptionalBank class and their test methods in your ExceptionalBankTester class with accordance to the details provided in these javadocs. These are examples of text files whose File objects can be passed as input parameters to your loadCoins() method for instance sample1.txt, sample2.txt, and sample3.txt. The sample3.txt file contains lines mal-formatted. Those lines must be skipped. Only lines with correct format will result in adding coins to our exceptional bank. The correct format of lines is the same as the correct format of the string provided as input to addCoins ()method described in the previous section. Note that ExceptionalBank.loadCoins() and ExceptionalBank.saveBankSummary() methods take a reference to an object of type java.io.File as an input parameter. In your test methods which may call these methods, create the file object using the constructor of the java.io.File class that takes a path name String as input parameter. Note also that your load Coins() method should be able to load a file saved by your save BankSummary method. Notice also that the saveBankSummary() method should not throw any exception. If an IOException may be thrown in the save BankSummary(), you have to catch it. Enum Coin java.lang. Object Java lang EnumStep 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