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. This will also use your 'Currency' and 'Money' classes from the previous lab, so everything for this lab will be in a new file for 'main'. (Programming language Java) 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. In the 'main', first ask the user for the number of elements, not to exceed SORT_MAX_SIZE = 16 (put appropriate input validation) 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. Based on the input, create an appropriate array for the data to be entered. Then sort the array using your sort function of step 1. Take screenshots to be uploaded with the project. 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. Ensure input and any other data validations as needed and provide descriptive prompts with emphasis on usability. The image below are code used for the assignment

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter10: Introduction To Inheritance
Section: Chapter Questions
Problem 17RQ
icon
Related questions
Question

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. This will also use your 'Currency' and 'Money' classes from the previous lab, so everything for this lab will be in a new file for 'main'. (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:
// 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;
%3D
/**
* 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";
%D
}
/**
*
@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;
%3D
// get notes and coins value
int notes = super.getNoteValue() + other.getNoteValue();
int coins = super.getCoinValue() + other.getCoinValue();
%3D
// 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();
%3D
// if coins is greater than 100
100) {
// increment the notes count
if (coins
>=
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 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) {
+ super.getCoinValue() +
+ coinName);
II I
System.out.print(super.getNoteValue() +
IIII
+ currencyName +
} else {
+ coinName);
II II
System.out.print(super.getNoteValue() + ".O " + currencyName +
+ super.getCoinValue() +
II
}
}
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: ");
II
String val = scanner.nextLine();
// split the string to notes and coins
int noteValue = Integer.valueOf(val.split("\,")[0]);
%3D
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.println("\n**
***** List of Money Objects created*****");
// Iterate through the currencies
for (Currency currency : currencies) {
// print the currency
currency.printCurrency();
System.out.printlIn();
}
// adding first money object to second and print the result
System.out.printIn("\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.printIn("\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.printlIn("\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();
}
}
Transcribed Image 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; %3D /** * 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"; %D } /** * @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; %3D // get notes and coins value int notes = super.getNoteValue() + other.getNoteValue(); int coins = super.getCoinValue() + other.getCoinValue(); %3D // 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(); %3D // if coins is greater than 100 100) { // increment the notes count if (coins >= 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 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) { + super.getCoinValue() + + coinName); II I System.out.print(super.getNoteValue() + IIII + currencyName + } else { + coinName); II II System.out.print(super.getNoteValue() + ".O " + currencyName + + super.getCoinValue() + II } } 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: "); II String val = scanner.nextLine(); // split the string to notes and coins int noteValue = Integer.valueOf(val.split("\,")[0]); %3D 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.println("\n** ***** List of Money Objects created*****"); // Iterate through the currencies for (Currency currency : currencies) { // print the currency currency.printCurrency(); System.out.printlIn(); } // adding first money object to second and print the result System.out.printIn("\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.printIn("\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.printlIn("\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(); } }
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,