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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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();
  
  
}
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Mathematical functions
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education