simple coin program

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2008
Posts: 9
Reputation: intelli is an unknown quantity at this point 
Solved Threads: 0
intelli intelli is offline Offline
Newbie Poster

simple coin program

 
0
  #1
Sep 22nd, 2008
i have to write a simple java program
write an application that determines the value of coins in a jar and prints the total in dollars and cents. Read integer values that represent the number of quarters, dimes, nickels and pennies

so i did it i don't know where to go from here help would be appreciated

public class coins {

public static void main(String[] args) {

double quarters;
double dimes;
double nickels;
double pennies;
double money;

Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of quarters in the jar.");
quarters = scan.nextInt();
System.out.println("Enter the number of dimes in the jar");
dimes= scan.nextInt();
System.out.println("Enter the number of nickels in the jar");
nickels = scan.nextInt();
System.out.println("Enter the number of pennies in the jar");
pennies = scan.nextInt();
money = quarters*25 + dimes*10 + nickels*5 + pennies*1;
System.out.println("Your total change in dollars and cents is "+money);



}
}

the last 2 have to be in dollar like 81 dollars and 27 cents but i get dollar is and cents is 81.00 i dont no the code to split it,i dont understand floating what where do i put it ??? any one help me out
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: simple coin program

 
1
  #2
Sep 22nd, 2008
Originally Posted by intelli View Post
i have to write a simple java program
write an application that determines the value of coins in a jar and prints the total in dollars and cents. Read integer values that represent the number of quarters, dimes, nickels and pennies

so i did it i don't know where to go from here help would be appreciated

public class coins {

public static void main(String[] args) {

double quarters;
double dimes;
double nickels;
double pennies;
double money;

Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of quarters in the jar.");
quarters = scan.nextInt();
System.out.println("Enter the number of dimes in the jar");
dimes= scan.nextInt();
System.out.println("Enter the number of nickels in the jar");
nickels = scan.nextInt();
System.out.println("Enter the number of pennies in the jar");
pennies = scan.nextInt();
money = quarters*25 + dimes*10 + nickels*5 + pennies*1;
System.out.println("Your total change in dollars and cents is "+money);



}
}

the last 2 have to be in dollar like 81 dollars and 27 cents but i get dollar is and cents is 81.00 i dont no the code to split it,i dont understand floating what where do i put it ??? any one help me out

Well, you can use DecimalFormat to format money after dividing by 100. quarters , dimes , nickels , pennies should be integers, not doubles.

quarters = scan.nextInt();
http://java.sun.com/j2se/1.5.0/docs/...malFormat.html

or you can make money an integer and have two variables, dollars and cents, and split money up into those components using the / and % operators. So if money = 2343, dollars would equal 23 and cents would equal 43. Then output a dollar sign, then dollars , then a decimal point, then cents . You'd have to pad cents with a 0 if it's less than ten cents.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 9
Reputation: intelli is an unknown quantity at this point 
Solved Threads: 0
intelli intelli is offline Offline
Newbie Poster

Re: simple coin program

 
0
  #3
Sep 23rd, 2008
i solved this thx
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC