Write a program that will sort a prmiitive array of data using the following guidelines - DO NOT USE VECTORS, COLLECTIONS, SETS or any other data structures from your programming language. The codes...


Write a program that will sort a prmiitive array of data using the following guidelines - DO NOT USE VECTORS, COLLECTIONS, SETS or any other data structures from your programming language. The codes for both the money and currency should stay the same, the main focus should be on the main file code (Programming language Java)



  1. Create a helper function called 'RecurInsSort' such that:

    • It is a standalone function not part of any class from the prior lab or any new class you feel like creating here,

    • Takes in the same type of parameters as any standard Insertion Sort with recursion behavior, i.e.

      • void RecurInsSort(Currency arr[], int size)



    • Prints out how the array looks every time a recursive step returns back to its caller

    • The objects in the array should be Money objects added or manipulated using Currency references/pointers.

    • It is OK to print out the array partially when returning from a particular step as long as the process of sorting is clearly demonstrated in the output.



  2. In the 'main', first ask the user for the number of elements, not to exceed SORT_MAX_SIZE = 16 (put appropriate input validation)

  3. Then ask the user to provide Money objects similar to the previous lab. Try to use your Currency and Money classes as-is without making any changes. If you do make changes to be able to complete this lab, clearly mention what has been changed, otherwise there will be a small penalty if I have to compare your class files to the prior lab.

  4. Based on the input, create an appropriate array for the data to be entered.

  5. Then sort the array using your sort function of step 1. Take screenshots to be uploaded with the project.

  6. Make sure that the output is being written to console as well as an output file at the same time. Use an additional helper function if that makes things easier. Do not copy and paste text from your console to create the output file.

  7. Ensure input and any other data validations as needed and provide descriptive prompts with emphasis on usability.

  8. The image below are code used for the assignment


Currency.java:<br>// abstract base class called Currency with two integer attributes<br>public abstract class Currency{<br>protected int noteValue;<br>protected int coinValue;<br>/**<br>* Default Construction<br>*/<br>public Currency() {<br>this.noteValue = 0;<br>this.coinValue = 0;<br>}<br>/**<br>* Construction based on parameters for all attributes<br>* @param noteValue<br>@param coinValue<br>*/<br>public Currency(int noteValue, int coinValue) {<br>this.noteValue = noteValue;<br>this.coinValue = coinValue;<br>}<br>/**<br>Copy Constructor<br>* @param currency<br>*/<br>public Currency(Currency currency) {<br>this.noteValue = currency.noteValue;<br>this.coinValue = currency.coinValue;<br>}<br>/**<br>* Getter for noteValue<br>* @return the noteValue<br>*/<br>public int getNoteValue() {<br>return noteValue;<br>}<br>/**<br>* Setters for noteValue<br>@param noteValue the noteValue to set<br>*/<br>public void setNoteValue(int noteValue) {<br>this.noteValue = noteValue;<br>}<br>/**<br>* Getter for coinValue<br>@return the coinValue<br>*/<br>public int getCoinValue() {<br>return coinValue;<br>}<br>/**<br>* Setter for coinValue<br>* @param coinValue the coinValue to set<br>*/<br>public void setCoinValue(int coinValue) {<br>this.coinValue = coinValue;<br>}<br>// Adding two objects of the same currency<br>public abstract Currency addCurrency(Currency c);<br>// Subtracting one object from another object of the same currency<br>public abstract Currency subtractCurrency(Currency c);<br>/* Comparing two objects of the same currency for equality/inequality Comparing<br>* two objects of the same currency to identify which object is larger or<br>* smaller*/<br>public abstract int compareCurrency(Currency c);<br>// Print method to print details of a currency object<br>public abstract void printCurrency();<br>}<br>Money.java:<br>// derived class - Money<br>public class Money extends Currency {<br>// two non-public attributes<br>private String currencyName;<br>private String coinName;<br>public Money() {<br>super();<br>this.currencyName = = 100) { // increment the notes count notes++; // decrement 100 from coins coins -= 100; } return new Money(notes, coins); } /** * Subtracting one object from another object of the same currency * @param currency */ */ public Currency addCurrency(Currency currency) { // Cast the currency to be Money Money other = (Money) currency; // get notes and coins value int notes = super.getNoteValue() + other.getNoteValue(); int coins = super.getCoinValue() + other.getCoinValue(); // if coins is greater than 10O if (coins >= 100) { // increment the notes count notes++; // decrement 100 from coins coins -= 100O; } return new Money(notes, coins); } /** * Subtracting one object from another object of the same currency @param currency */ public Currency subtractCurrency(Currency currency) { // Cast the currency to be Money Money other = (Money) currency; // get notes and coins value int notes = super.getNoteValue() - other.getNoteValue(); int coins = super.getCoinValue() - other.getCoinValue(); // if no coins if (coins < 0)="" {="" increment="" the="" notes="" count="" notes--;="" decrement="" 100="" from="" coins="" coins="" +="100;" }="" return="" new="" money(notes,="" coins);="" }="" **="" *="" comparing="" two="" objects="" of="" the="" same="" currency="" for="" equality/inequality="" comparing="" *="" two="" objects="" of="" the="" same="" currency="" to="" identify="" which="" object="" is="" larger="" or="" *="" smaller="" */="" public="" int="" comparecurrency(currency="" currency)="" {="" money="" other="(Money)" currency;="" if="" (this.getnotevalue()=""> other.getNoteValue()) return 1; else if (this.getNoteValue() < other.getnotevalue())="" return="" -1;="" else="" if="" (this.getcoinvalue()=""> other.getCoinValue()) return 1; else if (this.getCoinValue() < other.getcoinvalue())="" return="" -1;="" else="" return="" 0;="" }="" **="" *="" print="" method="" to="" print="" details="" of="" a="" currency="" object="" */="" public="" void="" printcurrency()="" {="" coin="" value="" is="" greater="" than="" 10="" cents="" if="" (super.getcoinvalue()=""> 10) { System.out.print(super.getNoteValue() + " " + currencyName + " " + super.getCoinValue() + "" + coinName); } else { System.out.print(super.getNoteValue() + ".O " + currencyName + + super.getCoinValue() + "" + coinName); } } } labimain.java: import java.util.Scanner; public class lab1main { public static void main(String[] args) { // Create currency objects Currency[] currencies = new Currency[5]; Scanner scanner = new Scanner(System.in); // iterate to get 5 currencies for (int currencyltem = 0; currencyltem < 5;="" currencyltem++)="" {="" system.out.print("enter="" currency="" "+="" (currencyltem="" +="" 1)="" +="" "="" value:="" ");="" %3d="" string="" val="scanner.nextLine();" split="" the="" string="" to="" notes="" and="" coins="" int="" notevalue="Integer.valueOf(val.split("\\,")[0]);" int="" coinvalue="Integer.valueOf(val.split("\\.")[1]);" check="" if="" coin="" value="" is="" less="" than="" 10,="" multiply="" the="" coin="" vlaue="" by="" 10="" if(coinvalue="">< 10)="" {="" coinvalue="" *="10;" }="" create="" a="" money="" instance="" currencies[currencyltem]="new" money(notevalue,="" coinvalue);="" }="" system.out.printin("\n*****="" list="" of="" money="" objects="" created*****");="" iterate="" through="" the="" currencies="" for="" (currency="" currency="" :="" currencies)="" {="" print="" the="" currency="" currency.printcurrency();="" system.out.println();="" }="" adding="" first="" money="" object="" to="" second="" and="" print="" the="" result="" system.out.println("\n***="" adding="" the="" first="" money="" object="" to="" the="" second="" ***="" ");="" currencies[0].addcurrency(currencies[1]).printcurrency();="" subtract="" the="" first="" money="" object="" from="" the="" third="" and="" print="" the="" result="" system.out.println("\n\n**="" subtract="" the="" first="" money="" object="" from="" the="" third="" ***");="" currencies[2].subtractcurrency(currencies[0]).printcurrency();="" compare="" the="" first="" and="" fourth="" money="" object="" system.out.printin("\n\n***="" comparing="" first="" and="" fourth="" money="" object="" ***");="" if="" (currencies[0].comparecurrency(currencies[3]):="" 0)="" {="=" currencies[0].printcurrency();="" system.out.print("="" is="" equal="" to="" ");="" currencies[3].printcurrency();="" }="" else="" {="" currencies[0].printcurrency();="" system.out.print("="" is="" not="" equal="" to="" ");="" currencies[3].printcurrency();="" }="" compare="" the="" first="" money="" object="" to="" the="" fifth="" system.out.printin("\n\n***="" comparing="" first="" and="" fifth="" money="" object="" ***");="" if="" (currencies[0].comparecurrency(currencies[4])="" :="" 1)="" {="=" currencies[0].printcurrency();="" system.out.print("="" is="" greater="" than="" ");="" currencies[4].printcurrency();="" }="" else="" if="" (currencies[0].comparecurrency(currencies[4])="=" -1)="" {="" currencies[4].printcurrency();="" system.out.print("="" is="" greater="" than="" ");="" currencies[0].printcurrency();="" }="" scanner.close();="" }="" }="" "/="">
Extracted text: Currency.java: // abstract base class called Currency with two integer attributes public abstract class Currency{ protected int noteValue; protected int coinValue; /** * Default Construction */ public Currency() { this.noteValue = 0; this.coinValue = 0; } /** * Construction based on parameters for all attributes * @param noteValue @param coinValue */ public Currency(int noteValue, int coinValue) { this.noteValue = noteValue; this.coinValue = coinValue; } /** Copy Constructor * @param currency */ public Currency(Currency currency) { this.noteValue = currency.noteValue; this.coinValue = currency.coinValue; } /** * Getter for noteValue * @return the noteValue */ public int getNoteValue() { return noteValue; } /** * Setters for noteValue @param noteValue the noteValue to set */ public void setNoteValue(int noteValue) { this.noteValue = noteValue; } /** * Getter for coinValue @return the coinValue */ public int getCoinValue() { return coinValue; } /** * Setter for coinValue * @param coinValue the coinValue to set */ public void setCoinValue(int coinValue) { this.coinValue = coinValue; } // Adding two objects of the same currency public abstract Currency addCurrency(Currency c); // Subtracting one object from another object of the same currency public abstract Currency subtractCurrency(Currency c); /* Comparing two objects of the same currency for equality/inequality Comparing * two objects of the same currency to identify which object is larger or * smaller*/ public abstract int compareCurrency(Currency c); // Print method to print details of a currency object public abstract void printCurrency(); } Money.java: // derived class - Money public class Money extends Currency { // two non-public attributes private String currencyName; private String coinName; public Money() { super(); this.currencyName = "Dollar"; this.coinName = "Cent"; } /** * @param noteValue * @param coinValue */ public Money(int noteValue, int coinValue) { super(noteValue, coinValue); this.currencyName = "Dollar"; this.coinName = "Cent"; } /** * Adding two objects of the same currency * @param currency */ public Currency addCurrency(Currency currency) { // Cast the currency to be Money Money other = (Money) currency; // get notes and coins value int notes = super.getNoteValue() + other.getNoteValue(); int coins = super.getCoinValue() + other.getCoinValue(); // if coins is greater than 100 if (coins >= 100) { // increment the notes count notes++; // decrement 100 from coins coins -= 100; } return new Money(notes, coins); } /** * Subtracting one object from another object of the same currency * @param currency */ */ public Currency addCurrency(Currency currency) { // Cast the currency to be Money Money other = (Money) currency; // get notes and coins value int notes = super.getNoteValue() + other.getNoteValue(); int coins = super.getCoinValue() + other.getCoinValue(); // if coins is greater than 10O if (coins >= 100) { // increment the notes count notes++; // decrement 100 from coins coins -= 100O; } return new Money(notes, coins); } /** * Subtracting one object from another object of the same currency @param currency */ public Currency subtractCurrency(Currency currency) { // Cast the currency to be Money Money other = (Money) currency; // get notes and coins value int notes = super.getNoteValue() - other.getNoteValue(); int coins = super.getCoinValue() - other.getCoinValue(); // if no coins if (coins < 0)="" {="" increment="" the="" notes="" count="" notes--;="" decrement="" 100="" from="" coins="" coins="" +="100;" }="" return="" new="" money(notes,="" coins);="" }="" **="" *="" comparing="" two="" objects="" of="" the="" same="" currency="" for="" equality/inequality="" comparing="" *="" two="" objects="" of="" the="" same="" currency="" to="" identify="" which="" object="" is="" larger="" or="" *="" smaller="" */="" public="" int="" comparecurrency(currency="" currency)="" {="" money="" other="(Money)" currency;="" if="" (this.getnotevalue()=""> other.getNoteValue()) return 1; else if (this.getNoteValue() < other.getnotevalue())="" return="" -1;="" else="" if="" (this.getcoinvalue()=""> other.getCoinValue()) return 1; else if (this.getCoinValue() < other.getcoinvalue())="" return="" -1;="" else="" return="" 0;="" }="" **="" *="" print="" method="" to="" print="" details="" of="" a="" currency="" object="" */="" public="" void="" printcurrency()="" {="" coin="" value="" is="" greater="" than="" 10="" cents="" if="" (super.getcoinvalue()=""> 10) { System.out.print(super.getNoteValue() + " " + currencyName + " " + super.getCoinValue() + "" + coinName); } else { System.out.print(super.getNoteValue() + ".O " + currencyName + + super.getCoinValue() + "" + coinName); } } } labimain.java: import java.util.Scanner; public class lab1main { public static void main(String[] args) { // Create currency objects Currency[] currencies = new Currency[5]; Scanner scanner = new Scanner(System.in); // iterate to get 5 currencies for (int currencyltem = 0; currencyltem < 5;="" currencyltem++)="" {="" system.out.print("enter="" currency="" "+="" (currencyltem="" +="" 1)="" +="" "="" value:="" ");="" %3d="" string="" val="scanner.nextLine();" split="" the="" string="" to="" notes="" and="" coins="" int="" notevalue="Integer.valueOf(val.split("\\,")[0]);" int="" coinvalue="Integer.valueOf(val.split("\\.")[1]);" check="" if="" coin="" value="" is="" less="" than="" 10,="" multiply="" the="" coin="" vlaue="" by="" 10="" if(coinvalue="">< 10) { coinvalue *= 10; } // create a money instance currencies[currencyltem] = new money(notevalue, coinvalue); } system.out.printin("\n***** list of money objects created*****"); // iterate through the currencies for (currency currency : currencies) { // print the currency currency.printcurrency(); system.out.println(); } // adding first money object to second and print the result system.out.println("\n*** adding the first money object to the second *** "); currencies[0].addcurrency(currencies[1]).printcurrency(); // subtract the first money object from the third and print the result system.out.println("\n\n** subtract the first money object from the third ***"); currencies[2].subtractcurrency(currencies[0]).printcurrency(); // compare the first and fourth money object system.out.printin("\n\n*** comparing first and fourth money object ***"); if (currencies[0].comparecurrency(currencies[3]): 0) { == currencies[0].printcurrency(); system.out.print(" is equal to "); currencies[3].printcurrency(); } else { currencies[0].printcurrency(); system.out.print(" is not equal to "); currencies[3].printcurrency(); } // compare the first money object to the fifth system.out.printin("\n\n*** comparing first and fifth money object ***"); if (currencies[0].comparecurrency(currencies[4]) : 1) { == currencies[0].printcurrency(); system.out.print(" is greater than "); currencies[4].printcurrency(); } else if (currencies[0].comparecurrency(currencies[4]) == -1) { currencies[4].printcurrency(); system.out.print(" is greater than "); currencies[0].printcurrency(); } scanner.close(); } } 10)="" {="" coinvalue="" *="10;" }="" create="" a="" money="" instance="" currencies[currencyltem]="new" money(notevalue,="" coinvalue);="" }="" system.out.printin("\n*****="" list="" of="" money="" objects="" created*****");="" iterate="" through="" the="" currencies="" for="" (currency="" currency="" :="" currencies)="" {="" print="" the="" currency="" currency.printcurrency();="" system.out.println();="" }="" adding="" first="" money="" object="" to="" second="" and="" print="" the="" result="" system.out.println("\n***="" adding="" the="" first="" money="" object="" to="" the="" second="" ***="" ");="" currencies[0].addcurrency(currencies[1]).printcurrency();="" subtract="" the="" first="" money="" object="" from="" the="" third="" and="" print="" the="" result="" system.out.println("\n\n**="" subtract="" the="" first="" money="" object="" from="" the="" third="" ***");="" currencies[2].subtractcurrency(currencies[0]).printcurrency();="" compare="" the="" first="" and="" fourth="" money="" object="" system.out.printin("\n\n***="" comparing="" first="" and="" fourth="" money="" object="" ***");="" if="" (currencies[0].comparecurrency(currencies[3]):="" 0)="" {="=" currencies[0].printcurrency();="" system.out.print("="" is="" equal="" to="" ");="" currencies[3].printcurrency();="" }="" else="" {="" currencies[0].printcurrency();="" system.out.print("="" is="" not="" equal="" to="" ");="" currencies[3].printcurrency();="" }="" compare="" the="" first="" money="" object="" to="" the="" fifth="" system.out.printin("\n\n***="" comparing="" first="" and="" fifth="" money="" object="" ***");="" if="" (currencies[0].comparecurrency(currencies[4])="" :="" 1)="" {="=" currencies[0].printcurrency();="" system.out.print("="" is="" greater="" than="" ");="" currencies[4].printcurrency();="" }="" else="" if="" (currencies[0].comparecurrency(currencies[4])="=" -1)="" {="" currencies[4].printcurrency();="" system.out.print("="" is="" greater="" than="" ");="" currencies[0].printcurrency();="" }="" scanner.close();="" }="">
Jun 08, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here