How do I fix the tax and total in the Java code. Input: apples 5.32 bananas 3.78 cantaloupe 2.99 donuts 6.55 eggs 4.87 flour 3.21 grapes 5.89 hummus 7.63 ice 1.99 jello 2.75 Code answer: tax: $ 03.71 total: $ 41.27 Correct answer: tax: $ 03.94 total: $ 48.92 Code: import java.util.Scanner; public class ReceiptMaker { public static final String SENTINEL = "checkout"; //rename to MAX_NUM_ITEMS public final int MAX_NUM_ITEMS; //changed this to double public final double TAX_RATE; private String [] itemNames; //changed this to double array private double[] itemPrices; private int numItemsPurchased; public ReceiptMaker(){ MAX_NUM_ITEMS = 10; TAX_RATE = .0825; itemNames = new String[MAX_NUM_ITEMS]; itemPrices = new double[MAX_NUM_ITEMS]; numItemsPurchased = 0; } public ReceiptMaker(int maxNumItems, double taxRate){ MAX_NUM_ITEMS = maxNumItems; TAX_RATE = taxRate; itemNames = new String[MAX_NUM_ITEMS]; itemPrices = new double[MAX_NUM_ITEMS]; numItemsPurchased = 0; } public void greetUser(){ System.out.println("Welcome to the "+MAX_NUM_ITEMS+" items or less checkout line"); } public void promptUserForProductEntry(){ System.out.println("Enter item #"+(numItemsPurchased+1)+"'s name and price separated by a space, or enter \"checkout\" to end transaction early"); } public void addNextPurchaseItemFromUser(String itemName, double itemPrice){ //changed this itemNames[numItemsPurchased] = itemName; itemPrices[numItemsPurchased] = itemPrice; numItemsPurchased++; } public double getSubtotal(){ double subTotal = 0; for(int i=0; i 0) ? Integer.MIN_VALUE : 0; for(int i=0; i maxPrice){ maxPrice = itemPrices[i]; } } return maxPrice; } public int getIndexOfMaxPrice(){ int indexOfMax = 0; for(int i=1; i itemPrices[indexOfMax]){ indexOfMax = i; } } return indexOfMax; } public double getMeanPrice(){ if(numItemsPurchased>0){ return getSubtotal()/numItemsPurchased; } return 0; } public double getTaxOnSubtotal(){ return getSubtotal() * TAX_RATE; } public double getTotal(){ return getSubtotal() - getTaxOnSubtotal(); } public void displayReceipt(){ System.out.println("-------------------------------------------------"); System.out.printf("Subtotal: $ %04.2f | # of Items %02d\n" , getSubtotal(),numItemsPurchased); System.out.printf(" Tax: $ %05.2f \n" , getTaxOnSubtotal()); System.out.printf(" Total: $ %04.2f \n" , getTotal()); System.out.println("--------------------THANK YOU--------------------"); } public void displayReceiptStats(){ System.out.println("\n-----------------RECEIPT STATS-----------------"); System.out.printf("Min Item Name: %10s | Price: $ %04.2f\n", itemNames[getIndexOfMinPrice()], getMinPrice()); System.out.printf("Max Item Name: %10s | Price: $ %04.2f\n", itemNames[getIndexOfMaxPrice()], getMaxPrice()); System.out.printf("Mean price of %02d items purchased: $ %04.2f\n\n", numItemsPurchased, getMeanPrice()); } public void displayAllItemsWithPrices(){ System.out.println("\n---------------RECEIPT BREAKDOWN---------------"); for(int i=0; i
Max Function
Statistical function is of many categories. One of them is a MAX function. The MAX function returns the largest value from the list of arguments passed to it. MAX function always ignores the empty cells when performing the calculation.
Power Function
A power function is a type of single-term function. Its definition states that it is a variable containing a base value raised to a constant value acting as an exponent. This variable may also have a coefficient. For instance, the area of a circle can be given as:
How do I fix the tax and total in the Java code.
Input:
apples 5.32
bananas 3.78
cantaloupe 2.99
donuts 6.55
eggs 4.87
flour 3.21
grapes 5.89
hummus 7.63
ice 1.99
jello 2.75
Code answer:
tax: $ 03.71
total: $ 41.27
Correct answer:
tax: $ 03.94
total: $ 48.92
Code:
import java.util.Scanner;
public class ReceiptMaker {
public static final String SENTINEL = "checkout";
//rename to MAX_NUM_ITEMS
public final int MAX_NUM_ITEMS;
//changed this to double
public final double TAX_RATE;
private String [] itemNames;
//changed this to double array
private double[] itemPrices;
private int numItemsPurchased;
public ReceiptMaker(){
MAX_NUM_ITEMS = 10;
TAX_RATE = .0825;
itemNames = new String[MAX_NUM_ITEMS];
itemPrices = new double[MAX_NUM_ITEMS];
numItemsPurchased = 0;
}
public ReceiptMaker(int maxNumItems, double taxRate){
MAX_NUM_ITEMS = maxNumItems;
TAX_RATE = taxRate;
itemNames = new String[MAX_NUM_ITEMS];
itemPrices = new double[MAX_NUM_ITEMS];
numItemsPurchased = 0;
}
public void greetUser(){
System.out.println("Welcome to the "+MAX_NUM_ITEMS+" items or less checkout line");
}
public void promptUserForProductEntry(){
System.out.println("Enter item #"+(numItemsPurchased+1)+"'s name and price separated by a space, or enter \"checkout\" to end transaction early");
}
public void addNextPurchaseItemFromUser(String itemName, double itemPrice){
//changed this
itemNames[numItemsPurchased] = itemName;
itemPrices[numItemsPurchased] = itemPrice;
numItemsPurchased++;
}
public double getSubtotal(){
double subTotal = 0;
for(int i=0; i<numItemsPurchased; i++){
subTotal += itemPrices[i];
}
return subTotal;
}
public double getMinPrice(){
double minPrice = itemPrices[0];
for(int i=1; i<numItemsPurchased; i++){
if(itemPrices[i] < minPrice){
minPrice = itemPrices[i];
}
}
return minPrice;
}
public int getIndexOfMinPrice(){
int indexOfMin = 0;
for(int i=1; i<numItemsPurchased; i++){
if(itemPrices[i] < itemPrices[indexOfMin]){
indexOfMin = i;
}
}
return indexOfMin;
}
public double getMaxPrice(){
double maxPrice = (numItemsPurchased > 0) ? Integer.MIN_VALUE : 0;
for(int i=0; i<numItemsPurchased; i++){
if(itemPrices[i] > maxPrice){
maxPrice = itemPrices[i];
}
}
return maxPrice;
}
public int getIndexOfMaxPrice(){
int indexOfMax = 0;
for(int i=1; i<numItemsPurchased; i++){
if(itemPrices[i] > itemPrices[indexOfMax]){
indexOfMax = i;
}
}
return indexOfMax;
}
public double getMeanPrice(){
if(numItemsPurchased>0){
return getSubtotal()/numItemsPurchased;
}
return 0;
}
public double getTaxOnSubtotal(){
return getSubtotal() * TAX_RATE;
}
public double getTotal(){
return getSubtotal() - getTaxOnSubtotal();
}
public void displayReceipt(){
System.out.println("-------------------------------------------------");
System.out.printf("Subtotal: $ %04.2f | # of Items %02d\n" , getSubtotal(),numItemsPurchased);
System.out.printf(" Tax: $ %05.2f \n" , getTaxOnSubtotal());
System.out.printf(" Total: $ %04.2f \n" , getTotal());
System.out.println("--------------------THANK YOU--------------------");
}
public void displayReceiptStats(){
System.out.println("\n-----------------RECEIPT STATS-----------------");
System.out.printf("Min Item Name: %10s | Price: $ %04.2f\n", itemNames[getIndexOfMinPrice()], getMinPrice());
System.out.printf("Max Item Name: %10s | Price: $ %04.2f\n", itemNames[getIndexOfMaxPrice()], getMaxPrice());
System.out.printf("Mean price of %02d items purchased: $ %04.2f\n\n", numItemsPurchased, getMeanPrice());
}
public void displayAllItemsWithPrices(){
System.out.println("\n---------------RECEIPT BREAKDOWN---------------");
for(int i=0; i<numItemsPurchased; i++){
System.out.printf("Item #%02d Name: %10s | Price: $ %04.2f\n",(i+1), itemNames[i], itemPrices[i]);
}
}
private double getItemPriceFromUser(Scanner scanner){
double price = -1;
while( (price = scanner.nextDouble()) < 0 ){
System.out.println("Price \""+price+ "\" cannot be negative.\nReenter price");
}
return price;
}
public void scanCartItems(Scanner scanner){
while(numItemsPurchased < MAX_NUM_ITEMS){
promptUserForProductEntry();
String token1;
double token2=-1;
if(!(token1 = scanner.next()).equalsIgnoreCase(SENTINEL)){
token2 = getItemPriceFromUser(scanner);
addNextPurchaseItemFromUser(token1, token2);
}
else{
break;
}
}
}
public static void main(String [] args){
Scanner scanner = new Scanner(System.in);
ReceiptMaker rm = new ReceiptMaker();
rm.greetUser();
rm.scanCartItems(scanner);
rm.displayReceipt();
rm.displayReceiptStats();
rm.displayAllItemsWithPrices();
scanner.close();
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images