Constructor

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

Join Date: Sep 2004
Posts: 42
Reputation: the b is an unknown quantity at this point 
Solved Threads: 0
the b the b is offline Offline
Light Poster

Constructor

 
0
  #1
Mar 11th, 2005
Hi, I am supposed to write a program for a school assignment that asks the user to enter the amount of coins, and then output the amount in quarters, dimes, nickels, and pennies. That was easy enough, but I found out that I need to use a constuctor to initialize the fields. When I try to use one I can't get the program to work. I get an error message that says:

Coins.java [4:1] <identifier> expected
{ <constructor>
^
Here is the code minus the constructor:
  1. public class Coins
  2. {
  3. public static void main(String args[])
  4. {
  5. EasyReader console = new EasyReader();
  6.  
  7. //declare variables
  8. int cents;
  9. int quarters;
  10. int dimes2;
  11. int nickels2;
  12. int pennies2;
  13.  
  14. //prompt user for the amount of cents.
  15. System.out.print("Enter amount of change in cents ");
  16. cents = console.readInt();
  17.  
  18. quarters = getQuarters(cents); //pass cents variable to getQuarters function for calculation
  19. System.out.println(quarters + " Quarters."); //display amount of quarters.
  20. cents = cents - (25 * quarters); //subtract the amount of quarters from cents variable.
  21.  
  22. dimes2 = getDimes(cents); //pass cents to getDimes function for calculation
  23. System.out.println(dimes2 + " Dimes."); //display amount of dimes.
  24. cents = cents - (10 * dimes2); //subtract dime amount from cents.
  25.  
  26. nickels2 = getNickels(cents); //pass cents to getNickels function for calculation.
  27. System.out.println(nickels2 + " Nickels."); //display amount of nickels.
  28. cents = cents - (5 * nickels2); //subtract nickel amount from cents.
  29.  
  30. pennies2 = getPennies(cents); //pass cents to getPennies function for calculation.
  31. System.out.println(pennies2 + " Pennies."); //display amount of pennies.
  32. cents = cents - pennies2; //subtract penny amount from cents.
  33.  
  34. }
  35.  
  36. //calculate quarter amount
  37. public static int getQuarters(int cents)
  38. {
  39. int quarters;
  40. quarters = cents / 25;
  41. return(quarters);
  42. }
  43.  
  44. //calculate dime amount.
  45. public static int getDimes(int cents)
  46. {
  47. int dimes;
  48. dimes = cents / 10;
  49. return(dimes);
  50. }
  51.  
  52. //calculate nickel amount.
  53. public static int getNickels(int cents)
  54. {
  55. int nickels;
  56. nickels = cents / 5;
  57. return(nickels);
  58. }
  59.  
  60. //calculate penny amount
  61. public static int getPennies(int cents)
  62. {
  63. int pennies;
  64. pennies = cents / 1;
  65. return(pennies);
  66. }
  67. }

Here is the code with the constuctor:
  1. public class Coins
  2. {
  3. public Coins
  4. {
  5. //declare variables
  6. int cents = 0;
  7. int quarters = 0;
  8. int dimes2 = 0;
  9. int nickels2 = 0;
  10. int pennies2 = 0;
  11. }
  12. public static void main(String args[])
  13. {
  14. EasyReader console = new EasyReader();
  15.  
  16.  
  17.  
  18. //prompt user for the amount of cents.
  19. System.out.print("Enter amount of change in cents ");
  20. cents = console.readInt();
  21.  
  22. quarters = getQuarters(cents); //pass cents variable to getQuarters function for calculation
  23. System.out.println(quarters + " Quarters."); //display amount of quarters.
  24. cents = cents - (25 * quarters); //subtract the amount of quarters from cents variable.
  25.  
  26. dimes2 = getDimes(cents); //pass cents to getDimes function for calculation
  27. System.out.println(dimes2 + " Dimes."); //display amount of dimes.
  28. cents = cents - (10 * dimes2); //subtract dime amount from cents.
  29.  
  30. nickels2 = getNickels(cents); //pass cents to getNickels function for calculation.
  31. System.out.println(nickels2 + " Nickels."); //display amount of nickels.
  32. cents = cents - (5 * nickels2); //subtract nickel amount from cents.
  33.  
  34. pennies2 = getPennies(cents); //pass cents to getPennies function for calculation.
  35. System.out.println(pennies2 + " Pennies."); //display amount of pennies.
  36. cents = cents - pennies2; //subtract penny amount from cents.
  37.  
  38. }
  39.  
  40. //calculate quarter amount
  41. public static int getQuarters(int cents)
  42. {
  43. int quarters;
  44. quarters = cents / 25;
  45. return(quarters);
  46. }
  47.  
  48. //calculate dime amount.
  49. public static int getDimes(int cents)
  50. {
  51. int dimes;
  52. dimes = cents / 10;
  53. return(dimes);
  54. }
  55.  
  56. //calculate nickel amount.
  57. public static int getNickels(int cents)
  58. {
  59. int nickels;
  60. nickels = cents / 5;
  61. return(nickels);
  62. }
  63.  
  64. //calculate penny amount
  65. public static int getPennies(int cents)
  66. {
  67. int pennies;
  68. pennies = cents / 1;
  69. return(pennies);
  70. }
  71. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 27
Reputation: DeepZ is an unknown quantity at this point 
Solved Threads: 0
DeepZ's Avatar
DeepZ DeepZ is offline Offline
Light Poster

Re: Constructor

 
0
  #2
Mar 11th, 2005
You provided a constructor (Which btw should have method brackets behind it) but you do not create an object of the class, so there are no variables to work with.

What you want is something like this :
  1. public static void main(String args[])
  2. {
  3. EasyReader console = new EasyReader();
  4.  
  5. Coins money = new Coins();
  6.  
  7. //prompt user for the amount of cents.
  8. System.out.print("Enter amount of change in cents ");
  9. money.cents = console.readInt();
  10. money.quarters = getQuarters(money.cents); //pass cents variable to getQuarters function for calculation
  11. System.out.println(money.quarters + " Quarters."); //display amount of quarters.
  12. money.cents = money.cents - (25 * money.quarters); //subtract the amount of quarters from cents variable.
  13.  
  14. etc....
  15.  
  16. <a rel="nofollow" class="t" href="http://www.daniweb.com/techtalkforums/thread19991.html#" target="_blank">[img]http://www.daniweb.com/techtalkforums/techtalk-images/buttons/reputation.gif[/img]</a> <a rel="nofollow" class="t" href="http://www.daniweb.com/techtalkforums/report.php?p=100681" target="_blank">http://www.daniweb.com/techtalkforum...ons/report.gif</a>
However, the use of static (class) methods is not very nice. If you define the methods with static, as normal instance methods, you could do something like :
  1. public Coins()
  2. {
  3. //declare variables
  4. int cents = 0;
  5. int quarters = 0;
  6. int dimes2 = 0;
  7. int nickels2 = 0;
  8. int pennies2 = 0;
  9. }
  10. //calculate quarter amount
  11. public int getQuarters()
  12. {
  13. quarters = cents / 25;
  14. }
  15. .......
  16.  
  17. money.cents = console.readInt();
  18. money.getQuarters(); //pass cents variable to getQuarters function for calculation
  19. System.out.println(money.quarters + " Quarters."); //display amount of quarters.
  20. money.cents = money.cents - (25 * money.quarters);
Hope you get the idea.
:?: Sometimes i wonder if i'm on the right planet :!:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 42
Reputation: the b is an unknown quantity at this point 
Solved Threads: 0
the b the b is offline Offline
Light Poster

Re: Constructor

 
0
  #3
Mar 14th, 2005
That makes alot of sense to me, but it still does not seem to work. I get two errors now (supposedly syntax) that say:
Coins.java [47:1] ')' expected
public static int getQuarters(int money.cents)
^
Coins.java [77:1] ';' expected
}
^

Also the calls to the functions ( money.quarters = getQuarters(money.cents) ) are underlined in red. Here is the new code:
  1. public class Coins
  2. {
  3. int cents;
  4. int quarters;
  5. int dimes;
  6. int nickels;
  7. int pennies;
  8.  
  9. public Coins()
  10. {
  11. //declare variables
  12. cents = 0;
  13. quarters = 0;
  14. dimes = 0;
  15. nickels = 0;
  16. pennies = 0;
  17. }
  18. public static void main(String args[])
  19. {
  20. EasyReader console = new EasyReader();
  21. Coins money = new Coins();
  22.  
  23.  
  24. //prompt user for the amount of cents.
  25. System.out.print("Enter amount of change in cents ");
  26. money.cents = console.readInt();
  27.  
  28. money.quarters = getQuarters(money.cents); //pass cents variable to getQuarters function for calculation
  29. System.out.println(money.quarters + " Quarters."); //display amount of quarters.
  30. money.cents = money.cents - (25 * money.quarters); //subtract the amount of quarters from cents variable.
  31.  
  32. money.dimes = getDimes(money.cents); //pass cents to getDimes function for calculation
  33. System.out.println(money.dimes + " Dimes."); //display amount of dimes.
  34. money.cents = money.cents - (10 * money.dimes); //subtract dime amount from cents.
  35.  
  36. money.nickels = getNickels(money.cents); //pass cents to getNickels function for calculation.
  37. System.out.println(money.nickels + " Nickels."); //display amount of nickels.
  38. money.cents = money.cents - (5 * money.nickels); //subtract nickel amount from cents.
  39.  
  40. money.pennies = getPennies(money.cents); //pass cents to getPennies function for calculation.
  41. System.out.println(money.pennies + " Pennies."); //display amount of pennies.
  42. money.cents = money.cents - pennies; //subtract penny amount from cents.
  43.  
  44. }
  45.  
  46. //calculate quarter amount
  47. public static int getQuarters(int money.cents)
  48. {
  49. int quarters;
  50. quarters = cents / 25;
  51. return(quarters);
  52. }
  53.  
  54. //calculate dime amount.
  55. public static int getDimes(int money.cents)
  56. {
  57. int dimes;
  58. dimes = cents / 10;
  59. return(dimes);
  60. }
  61.  
  62. //calculate nickel amount.
  63. public static int getNickels(int money.cents)
  64. {
  65. int nickels;
  66. nickels = cents / 5;
  67. return(nickels);
  68. }
  69.  
  70. //calculate penny amount
  71. public static int getPennies(int money.cents)
  72. {
  73. int pennies;
  74. pennies = cents / 1;
  75. return(pennies);
  76. }
  77. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 10
Reputation: Meldroz is an unknown quantity at this point 
Solved Threads: 0
Meldroz Meldroz is offline Offline
Newbie Poster

Re: Constructor

 
0
  #4
Mar 14th, 2005
Hey there,

the errors are coming from your methods.
for example:

public static int getQuarters(int money.cents) <---

firts off money is an object of the type Coins, not an int.
and money only exist in your main block ( scope resolution );

you want something like this:
public static int getQuarters(int cents)

this goes for all the other methods below that as well, make that change and it should at least compile now. :rolleyes:

Good luck,
Mel
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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