Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IF YOU CANT SEE IMAGES CLEARLY, RIGHT CLICK AND OPEN IMAGE IN NEW TAB. sample1.txt QUARTER:10 DIME:5 NICKEL:2 PENNY:4 sample2.txt penny: 6 Dime: 3 Quarter:

image text in transcribedimage text in transcribed

IF YOU CANT SEE IMAGES CLEARLY, RIGHT CLICK AND OPEN IMAGE IN NEW TAB.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedsample1.txt

QUARTER:10 DIME:5 NICKEL:2 PENNY:4

sample2.txt

penny: 6 Dime: 3 Quarter: 6 PENNY: 4 NICKEL : 5 Dime: 2

sample3.txt

penny: 6 Dime: 3 Quarters 6 : 4 NICKEL : 5 cent: 2

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++; }

}

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" "quARTEr: 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 samplel.txt, sample2.txt, and sample3.txt. The sample3.txt file contains lines mal-formatted and a line including an incorrect coin name. Those lines must be skipped. Only lines with correct format and coin names defined in the enum Coin 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 save BankSummary() method should not throw any exception. If an IOException may be thrown in the save BankSummary(), you have to catch it. addCoins public void addCoins (java.lang.String command) throws java.util.zip.DataFormatException Adds a number of the same coin type with respect to a provided command line. The format of the command line is "coin_name:coins_count". Such command line refers to adding coins_count of coin_name to this bank. For instance, "PENNY:5", or "Penny: 5" refer to adding 5 pennies to this bank. If the format of the provided command line is incorrect, no coins Parameters: command - command line to add a number of coins of the same type to this bank. Throws: java.lang.IllegalArgumentException - with the following error message "WARNING! The addCoins() method does not accept a null reference as input." if command is null. java.util.zip.DataFormatException - with the following error message "The format of the command line" + command + " is incorrect." if the format of the provided command is incorrect. An add command line is correctly formatted if it consists of two parts separated by a colon. The first part is a String and the second must be a positive Integer. Extra spaces at the beginning of each argument/part must be ignored. java.util. NoSuchElementException - with the following error message "The coin name provided in the command line" + command + " is invalid." if coin_name argument within the provided command line does not refer to a valid coin name with respect to the constant names defined in the enum Coin. Note that the comparison to check the validity of coin_name must be case insensitive. loadCoins public void loadCoins (java.io.File file) throws java.io.FileNotFoundException Load a list of coins from a file object which refers to a data file written in a specific format (a set of lines each formatted as follows "coin_name:coin_count"). Lines formatted correctly will be added as new coins to this elastic bank. Lines formatted incorrectly must be skipped (go to the next line). This method prints/displays the following message for every skipped line "WARNING! Skipping line. " + "raison_of_the error". Parameters: file-file object which refers to a data file of coin_names and their counts. Throws: java.lang.NullPointerException - with or without error message if file is null. java.io.FileNotFoundException - with or without error message if file is not found. saveBank Summary public void saveBankSummary(java.io.File file) Save the summary of this bank to the provided file in a specific format for each line: coin_name:coin_count. For instance, if a bank contains 2 quarters, 1 dime, 5 nickels, and 10 pennies, its contents will be saved as follows: PENNY:10 NICKEL:5 DIME:1 QUARTER:2 Note that the order of lines does not matter. Parameters: file - File object where a summary of the contents of this bank will be saved Throws: java.lang.NullPointerException - with or without error message if file is null. testAddCoin public static boolean testAddCoin() This method checks whether the ExceptionalBank.addCoin() method throws an IllegalArgumentException with an appropriate error message, when it is passed a null reference. Returns: true when this test verifies a correct functionality, and false otherwise testRemoveCoinEmpty Bank public static boolean testRemoveCoinEmptyBank) This method checks whether the Exceptional Bank.removeCoin() method throws a NoSuch ElementException with an appropriate error message, when it is called on an empty bank. Returns: true when this test verifies a correct functionality, and false otherwise testAddCoinsValidFormat public static boolean testAddCoinsValidFormat) This method checks whether the ExceptionalBank.addCoins() works appropriately when it is passed a String with a valid format. You can consider the following test scenarios, for instance. First, Create a new ExceptionalBank with initial capacity 20. Then, call.addCoins("QUARTER:2"). Then, verify that 2 quarters have been added to the bank without errors. Then, call.addCoins(" Penny :3 "); Then, verify that 3 pennies have been added to the bank without errors. You can consider further scenarios. No exceptions must be thrown by the call of .addCoins() method with valid input arguments. Returns: true when this test verifies a correct functionality, and false otherwise. testAddCoinsinvalidDataFormat public static boolean testAddCoins InvalidDataFormat() This method checks whether ExceptionalBank.addCoins() method throws a DataFormatException with an appropriate error message when it is passed an incorrectly formatted string object, for instance "quarter: five", or ":6", or "DIME:-5" Returns: true when this test verifies a correct functionality, and false otherwise. testAddCoins No SuchElement public static boolean testAddCoins NoSuchElement() This method checks whether Exceptional Bank.addCoins() method throws a NoSuchElementException with an appropriate error message when it is passed a String object with a correct format (meaning "string:positive_number"), but with a coin name not defined in the enum Coin's constants. For instance, when it is passed "blabla:10". Returns: true when this test verifies a correct functionality, and false otherwise. testAddCoinslllegalArgument public static boolean testAddCoins IllegalArgument() This method checks whether ExceptionalBank.addCoins() method throws an IllegalArgumentException with an appropriate error message when it is passed a null reference as input argument. Returns: true when this test verifies a correct functionality, and false otherwise. testLoadCoins NullReference public static boolean testLoadCoinsNullReference This method checks whether Exceptional Bank.loadCoins() method throws a NullPointerException when it is passed a null reference. Returns: true when this test verifies a correct functionality, and false otherwise. testLoadCoinsFileNotFound public static boolean testLoadCoinsFileNotFound) This method checks whether ExceptionalBank.loadCoins() method throws a FileNotFoundException when it is passed a non found file. Returns: true when this test verifies a correct functionality, and false otherwise. testLoadCoins File Found public static boolean testLoadCoinsFileFound() This method checks whether ExceptionalBank.loadCoins() method loads appropriately without throwing any exception when it is passed a found file. Returns: true when this test verifies a correct functionality, and false otherwise. Enum Coin java lang Object java.lang. Enum Coins All Implemented interface ava. 10. Serializable java.lang.Comparable Coins public enun coin extends java.lang.Enun coin This enum represents an enumeration of the US Currency Coin constants Author Mouna Enum Constant Summary Enum Constants Enum Constant DIE NICKEL PENNY QUARTER Method Summary Instance Methods All Methods Static Methods Modifier and Type int Concrete Methods Method value) valueof(ava.lang.string name) static coin static Coin 1 Description Returns the value in cents of this colm Returns the enam constant of this type with the specified name Returns an array containing the constants of this enum type in the caderthes are declared values Methods inherited from class java.lang.Enum compareto, equals, Betrectaringclass, hashcode, rane, ordinal, tostrine, valueof Methods inherited from class java.lang.Object getclass, notify, notifyAll, wait,wait,wait Enum Constant Detail PENNY public static finel Coin PENNY NICKEL public static final con NECKEL public static final coin DIME QUARTER public static finel Coin QUARTER Method Detail values public static coin[] values() Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows: for (Coin C : Coin.values() System.out.println(); Returns: an array containing the constants of this enum type, in the order they are declared value Of public static coin valueof(java.lang.String name) Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.) Parameters: name - the name of the enum constant to be returned. Returns: the enum constant with the specified name Throws: java.lang. IllegalArgumentException - if this enum type has no constant with the specified name java.lang.NullPointerException - if the argument is null value public int value) Returns the value in cents of this coin Returns: the value of this coin 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" "quARTEr: 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 samplel.txt, sample2.txt, and sample3.txt. The sample3.txt file contains lines mal-formatted and a line including an incorrect coin name. Those lines must be skipped. Only lines with correct format and coin names defined in the enum Coin 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 save BankSummary() method should not throw any exception. If an IOException may be thrown in the save BankSummary(), you have to catch it. addCoins public void addCoins (java.lang.String command) throws java.util.zip.DataFormatException Adds a number of the same coin type with respect to a provided command line. The format of the command line is "coin_name:coins_count". Such command line refers to adding coins_count of coin_name to this bank. For instance, "PENNY:5", or "Penny: 5" refer to adding 5 pennies to this bank. If the format of the provided command line is incorrect, no coins Parameters: command - command line to add a number of coins of the same type to this bank. Throws: java.lang.IllegalArgumentException - with the following error message "WARNING! The addCoins() method does not accept a null reference as input." if command is null. java.util.zip.DataFormatException - with the following error message "The format of the command line" + command + " is incorrect." if the format of the provided command is incorrect. An add command line is correctly formatted if it consists of two parts separated by a colon. The first part is a String and the second must be a positive Integer. Extra spaces at the beginning of each argument/part must be ignored. java.util. NoSuchElementException - with the following error message "The coin name provided in the command line" + command + " is invalid." if coin_name argument within the provided command line does not refer to a valid coin name with respect to the constant names defined in the enum Coin. Note that the comparison to check the validity of coin_name must be case insensitive. loadCoins public void loadCoins (java.io.File file) throws java.io.FileNotFoundException Load a list of coins from a file object which refers to a data file written in a specific format (a set of lines each formatted as follows "coin_name:coin_count"). Lines formatted correctly will be added as new coins to this elastic bank. Lines formatted incorrectly must be skipped (go to the next line). This method prints/displays the following message for every skipped line "WARNING! Skipping line. " + "raison_of_the error". Parameters: file-file object which refers to a data file of coin_names and their counts. Throws: java.lang.NullPointerException - with or without error message if file is null. java.io.FileNotFoundException - with or without error message if file is not found. saveBank Summary public void saveBankSummary(java.io.File file) Save the summary of this bank to the provided file in a specific format for each line: coin_name:coin_count. For instance, if a bank contains 2 quarters, 1 dime, 5 nickels, and 10 pennies, its contents will be saved as follows: PENNY:10 NICKEL:5 DIME:1 QUARTER:2 Note that the order of lines does not matter. Parameters: file - File object where a summary of the contents of this bank will be saved Throws: java.lang.NullPointerException - with or without error message if file is null. testAddCoin public static boolean testAddCoin() This method checks whether the ExceptionalBank.addCoin() method throws an IllegalArgumentException with an appropriate error message, when it is passed a null reference. Returns: true when this test verifies a correct functionality, and false otherwise testRemoveCoinEmpty Bank public static boolean testRemoveCoinEmptyBank) This method checks whether the Exceptional Bank.removeCoin() method throws a NoSuch ElementException with an appropriate error message, when it is called on an empty bank. Returns: true when this test verifies a correct functionality, and false otherwise testAddCoinsValidFormat public static boolean testAddCoinsValidFormat) This method checks whether the ExceptionalBank.addCoins() works appropriately when it is passed a String with a valid format. You can consider the following test scenarios, for instance. First, Create a new ExceptionalBank with initial capacity 20. Then, call.addCoins("QUARTER:2"). Then, verify that 2 quarters have been added to the bank without errors. Then, call.addCoins(" Penny :3 "); Then, verify that 3 pennies have been added to the bank without errors. You can consider further scenarios. No exceptions must be thrown by the call of .addCoins() method with valid input arguments. Returns: true when this test verifies a correct functionality, and false otherwise. testAddCoinsinvalidDataFormat public static boolean testAddCoins InvalidDataFormat() This method checks whether ExceptionalBank.addCoins() method throws a DataFormatException with an appropriate error message when it is passed an incorrectly formatted string object, for instance "quarter: five", or ":6", or "DIME:-5" Returns: true when this test verifies a correct functionality, and false otherwise. testAddCoins No SuchElement public static boolean testAddCoins NoSuchElement() This method checks whether Exceptional Bank.addCoins() method throws a NoSuchElementException with an appropriate error message when it is passed a String object with a correct format (meaning "string:positive_number"), but with a coin name not defined in the enum Coin's constants. For instance, when it is passed "blabla:10". Returns: true when this test verifies a correct functionality, and false otherwise. testAddCoinslllegalArgument public static boolean testAddCoins IllegalArgument() This method checks whether ExceptionalBank.addCoins() method throws an IllegalArgumentException with an appropriate error message when it is passed a null reference as input argument. Returns: true when this test verifies a correct functionality, and false otherwise. testLoadCoins NullReference public static boolean testLoadCoinsNullReference This method checks whether Exceptional Bank.loadCoins() method throws a NullPointerException when it is passed a null reference. Returns: true when this test verifies a correct functionality, and false otherwise. testLoadCoinsFileNotFound public static boolean testLoadCoinsFileNotFound) This method checks whether ExceptionalBank.loadCoins() method throws a FileNotFoundException when it is passed a non found file. Returns: true when this test verifies a correct functionality, and false otherwise. testLoadCoins File Found public static boolean testLoadCoinsFileFound() This method checks whether ExceptionalBank.loadCoins() method loads appropriately without throwing any exception when it is passed a found file. Returns: true when this test verifies a correct functionality, and false otherwise. Enum Coin java lang Object java.lang. Enum Coins All Implemented interface ava. 10. Serializable java.lang.Comparable Coins public enun coin extends java.lang.Enun coin This enum represents an enumeration of the US Currency Coin constants Author Mouna Enum Constant Summary Enum Constants Enum Constant DIE NICKEL PENNY QUARTER Method Summary Instance Methods All Methods Static Methods Modifier and Type int Concrete Methods Method value) valueof(ava.lang.string name) static coin static Coin 1 Description Returns the value in cents of this colm Returns the enam constant of this type with the specified name Returns an array containing the constants of this enum type in the caderthes are declared values Methods inherited from class java.lang.Enum compareto, equals, Betrectaringclass, hashcode, rane, ordinal, tostrine, valueof Methods inherited from class java.lang.Object getclass, notify, notifyAll, wait,wait,wait Enum Constant Detail PENNY public static finel Coin PENNY NICKEL public static final con NECKEL public static final coin DIME QUARTER public static finel Coin QUARTER Method Detail values public static coin[] values() Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows: for (Coin C : Coin.values() System.out.println(); Returns: an array containing the constants of this enum type, in the order they are declared value Of public static coin valueof(java.lang.String name) Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.) Parameters: name - the name of the enum constant to be returned. Returns: the enum constant with the specified name Throws: java.lang. IllegalArgumentException - if this enum type has no constant with the specified name java.lang.NullPointerException - if the argument is null value public int value) Returns the value in cents of this coin Returns: the value of this coin

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Demystifying Databases A Hands On Guide For Database Management

Authors: Shiva Sukula

1st Edition

8170005345, 978-8170005346

More Books

Students also viewed these Databases questions