Hello, how would I add up the total sum in my program below. The question was to
ID: 3579591 • Letter: H
Question
Hello, how would I add up the total sum in my program below. The question was to make a program with a single dimension array that prompts the user to enter California sales tax and Hawaii sales tax and then outputs the total sum. I'm having trouble as what to write in lines 41-43 to display the total sales tax for each state and then add up both states to show the entire cost. Thanks.
1 import java.util.Scanner;
2 public class Test
3 {
4 public static void main(String [] args)
5 {
6 Scanner input = new Scanner(System.in);
7 System.out.println("Enter how many months you'd like to calculate for California: ");
8 int i = input.nextInt();
9 double [] california = new double[i];
10 double monthlySales, totalTaxedSales;
11 for(int j = 0 ; j < i; j++)
12 {
13 System.out.println("Enter total sales in California for this month:");
14 monthlySales = input.nextDouble();
15
16 totalTaxedSales = (monthlySales * .09);
17 california[j] = totalTaxedSales;
18 System.out.println("California sales tax to pay for total receipts of $" + monthlySales + " is $" + totalTaxedSales);
19 System.out.println("");
20
21 }
22 System.out.println("Enter how many months you'd like to calculate for Hawaii: ");
23 int k = input.nextInt();
24 double [] hawaii = new double[k];
25
26 double monthlySale, totalTaxedSale;
27 for(int l = 0 ; l < k ; l++)
28 {
29 System.out.println("Enter total sales in Hawaii for this month:");
30 monthlySale = input.nextDouble();
31
32 totalTaxedSale = (monthlySale * .045);
33 hawaii[l] = totalTaxedSale;
34 System.out.println("Hawaii sales tax to pay for total receipts of $" + monthlySale + " is $" + totalTaxedSale);
35 System.out.println("");
36
37 }
38
39
40
41 System.out.println("Total taxes paid in California: " + );
42 System.out.println("Total taxes paid in Hawaii: " + );
43 System.out.println("Total taxes paid: " + ) ;
44
45 }
46 }
47
Explanation / Answer
Note: I entered some inputs to calculate the tax.Could you please check by entering with the inputs you have.
Test.java
import java.util.Scanner;
public class Test
{
public static void main(String [] args)
{
//Scanner class Object is used to read the inputs entered by the user
Scanner input = new Scanner(System.in);
//Getting the input entered by the user
System.out.println("Enter how many months you'd like to calculate for California: ");
int i = input.nextInt();
//Creating an array
double [] california = new double[i];
//Declaring variables
double monthlySales, totalTaxedSales,totCalTax=0.0,totHuwTax=0.0;
//This for loop will get the sales for each month entered by the user and calculate the tax
for(int j = 0 ; j < i; j++)
{
System.out.println("Enter total sales in California for month "+(j+1)+":");
monthlySales = input.nextDouble();
totalTaxedSales = (monthlySales * .09);
california[j] = totalTaxedSales;
//Calculating the total tax
totCalTax+=totalTaxedSales;
System.out.println("California sales tax to pay for total receipts of $" + monthlySales + " is $" + totalTaxedSales);
System.out.println("");
}
System.out.println("Enter how many months you'd like to calculate for Hawaii: ");
int k = input.nextInt();
double [] hawaii = new double[k];
double monthlySale, totalTaxedSale;
for(int l = 0 ; l < k ; l++)
{
System.out.println("Enter total sales in Hawaii for month :"+(l+1)+":");
monthlySale = input.nextDouble();
totalTaxedSale = (monthlySale * .045);
hawaii[l] = totalTaxedSale;
//Calculating the total tax
totHuwTax+=totalTaxedSale;
System.out.println("Hawaii sales tax to pay for total receipts of $" + monthlySale + " is $" + totalTaxedSale);
System.out.println("");
}
//Displaying the total tax paid in california
System.out.println("Total taxes paid in California: " +totCalTax );
//Displaying the total tax paid in huwaii
System.out.println("Total taxes paid in Hawaii: " + totHuwTax);
//Displaying the total tax paid in california and huwaii
System.out.println("Total taxes paid: " +(totCalTax+totHuwTax) ) ;
}
}
_________________________
Output:
Enter how many months you'd like to calculate for California:
5
Enter total sales in California for month 1:
12000
California sales tax to pay for total receipts of $12000.0 is $1080.0
Enter total sales in California for month 2:
14000
California sales tax to pay for total receipts of $14000.0 is $1260.0
Enter total sales in California for month 3:
15000
California sales tax to pay for total receipts of $15000.0 is $1350.0
Enter total sales in California for month 4:
11000
California sales tax to pay for total receipts of $11000.0 is $990.0
Enter total sales in California for month 5:
10000
California sales tax to pay for total receipts of $10000.0 is $900.0
Enter how many months you'd like to calculate for Hawaii:
4
Enter total sales in Hawaii for month :1:
18000
Hawaii sales tax to pay for total receipts of $18000.0 is $810.0
Enter total sales in Hawaii for month :2:
17000
Hawaii sales tax to pay for total receipts of $17000.0 is $765.0
Enter total sales in Hawaii for month :3:
15000
Hawaii sales tax to pay for total receipts of $15000.0 is $675.0
Enter total sales in Hawaii for month :4:
11000
Hawaii sales tax to pay for total receipts of $11000.0 is $495.0
Total taxes paid in California: 5580.0
Total taxes paid in Hawaii: 2745.0
Total taxes paid: 8325.0
____________Thank yOu
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.