So Im trying to make a Coinstar Simulation and pretty much what it does is it adds up all the money and gives me a total. Im a computer science student learning java for the first time so I'm a bit confused as to what code i should put next to get the total. Also if i had to add a percent of tax in there...how would i do that? thanks a lot !

public static void main(String[] args) {
        // TODO code application logic here
       double dollars, halfdollars, quarters, dimes, nickels, pennies, amount; 
        Scanner coinstar = new Scanner (System.in);
       
       System.out.println("Enter Number Of");
       System.out.print("Dollars:");
       dollars = coinstar.nextDouble();
       System.out.print("Half Dollars:");
       halfdollars = coinstar.nextDouble();
       System.out.print("Quarters:");
       quarters = coinstar.nextDouble();
       System.out.print("Dimes:");
       dimes = coinstar.nextDouble();
       System.out.print("Nickels:");
       nickels = coinstar.nextDouble();
       System.out.print("Pennies:");
       pennies = coinstar.nextDouble();
       
       dollars= 1.00;
       halfdollars = 0.50;
       quarters = 0.25;
       dimes = 0.10;
       nickels = 0.05;
       pennies = 0.01;
       
       
       System.out.println("Cash Value = $" );
     
       
       
       
       
               
       
       
    }
}

Recommended Answers

All 3 Replies

I'm a bit at a loss here... why do you enter the values manually to overwrite them with hardcoded values?

also, what exactly do you mean by

I'm a bit confused as to what code i should put next to get the total. Also if i had to add a percent of tax in there...how would i do that?

So Im trying to make a Coinstar Simulation and pretty much what it does is it adds up all the money and gives me a total. Im a computer science student learning java for the first time so I'm a bit confused as to what code i should put next to get the total. Also if i had to add a percent of tax in there...how would i do that? thanks a lot !

public static void main(String[] args) {
        // TODO code application logic here
       double dollars, halfdollars, quarters, dimes, nickels, pennies, amount; 
        Scanner coinstar = new Scanner (System.in);
       
       System.out.println("Enter Number Of");
       System.out.print("Dollars:");
       dollars = coinstar.nextDouble();
       System.out.print("Half Dollars:");
       halfdollars = coinstar.nextDouble();
       System.out.print("Quarters:");
       quarters = coinstar.nextDouble();
       System.out.print("Dimes:");
       dimes = coinstar.nextDouble();
       System.out.print("Nickels:");
       nickels = coinstar.nextDouble();
       System.out.print("Pennies:");
       pennies = coinstar.nextDouble();
       
       dollars= 1.00;
       halfdollars = 0.50;
       quarters = 0.25;
       dimes = 0.10;
       nickels = 0.05;
       pennies = 0.01;
       
       
       System.out.println("Cash Value = $" );
     
       
       
       
       
               
       
       
    }
}

well besides the hardcoded errors that you made while getting the values, this is really easy, how do you normally add up money? with '+' or '-', and also to add tax you just multiply (*) the total by tax-rate-in-percentage/100.

here is some psuedo code to put into perspective:

int x=9;
int y=245;
int z=900;

double taxRate=0.15;//15% or 15/100.

int totalBeforeTax=x+y+z;

System.out.println("Total before TAX: "+totalBeforeTax);

double  taxedAmount=(totalBeforeTax*taxRate);//calculate total tax on amount: amount*taxrate

double totalAfterTax = totalBeforeTax+taxedAmount;//Amount Before tax+tax On Amount

System.out.println("Total TAX on amount: "+taxedAmount +" TAXED @"+(taxRate*100)+"%");

System.out.println("Total amount including TAX: "+ totalAfterTax);

I'd put all the variables as ints since you dont want the user to enter that he has 1.5 dollars , and 2.6 quarters etc...

then in a double variable, add each ints multiplied by their % value of 1.0 dollars

quarters are 0.25 , pennies are 0.01 etc

int iDollars = 3;
int iQuarters = 7;
int iDimes = 90;

//should sum up to 13.75$
double dTotal = 1.0*iDollars + 0.25*iQuarters + 0.1*iDimes;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.