| | |
Constructor
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2004
Posts: 42
Reputation:
Solved Threads: 0
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:
Here is the code with the constuctor:
Coins.java [4:1] <identifier> expected
{ <constructor>
^
Here is the code minus the constructor:
Java Syntax (Toggle Plain Text)
public class Coins { public static void main(String args[]) { EasyReader console = new EasyReader(); //declare variables int cents; int quarters; int dimes2; int nickels2; int pennies2; //prompt user for the amount of cents. System.out.print("Enter amount of change in cents "); cents = console.readInt(); quarters = getQuarters(cents); //pass cents variable to getQuarters function for calculation System.out.println(quarters + " Quarters."); //display amount of quarters. cents = cents - (25 * quarters); //subtract the amount of quarters from cents variable. dimes2 = getDimes(cents); //pass cents to getDimes function for calculation System.out.println(dimes2 + " Dimes."); //display amount of dimes. cents = cents - (10 * dimes2); //subtract dime amount from cents. nickels2 = getNickels(cents); //pass cents to getNickels function for calculation. System.out.println(nickels2 + " Nickels."); //display amount of nickels. cents = cents - (5 * nickels2); //subtract nickel amount from cents. pennies2 = getPennies(cents); //pass cents to getPennies function for calculation. System.out.println(pennies2 + " Pennies."); //display amount of pennies. cents = cents - pennies2; //subtract penny amount from cents. } //calculate quarter amount public static int getQuarters(int cents) { int quarters; quarters = cents / 25; return(quarters); } //calculate dime amount. public static int getDimes(int cents) { int dimes; dimes = cents / 10; return(dimes); } //calculate nickel amount. public static int getNickels(int cents) { int nickels; nickels = cents / 5; return(nickels); } //calculate penny amount public static int getPennies(int cents) { int pennies; pennies = cents / 1; return(pennies); } }
Here is the code with the constuctor:
Java Syntax (Toggle Plain Text)
public class Coins { public Coins { //declare variables int cents = 0; int quarters = 0; int dimes2 = 0; int nickels2 = 0; int pennies2 = 0; } public static void main(String args[]) { EasyReader console = new EasyReader(); //prompt user for the amount of cents. System.out.print("Enter amount of change in cents "); cents = console.readInt(); quarters = getQuarters(cents); //pass cents variable to getQuarters function for calculation System.out.println(quarters + " Quarters."); //display amount of quarters. cents = cents - (25 * quarters); //subtract the amount of quarters from cents variable. dimes2 = getDimes(cents); //pass cents to getDimes function for calculation System.out.println(dimes2 + " Dimes."); //display amount of dimes. cents = cents - (10 * dimes2); //subtract dime amount from cents. nickels2 = getNickels(cents); //pass cents to getNickels function for calculation. System.out.println(nickels2 + " Nickels."); //display amount of nickels. cents = cents - (5 * nickels2); //subtract nickel amount from cents. pennies2 = getPennies(cents); //pass cents to getPennies function for calculation. System.out.println(pennies2 + " Pennies."); //display amount of pennies. cents = cents - pennies2; //subtract penny amount from cents. } //calculate quarter amount public static int getQuarters(int cents) { int quarters; quarters = cents / 25; return(quarters); } //calculate dime amount. public static int getDimes(int cents) { int dimes; dimes = cents / 10; return(dimes); } //calculate nickel amount. public static int getNickels(int cents) { int nickels; nickels = cents / 5; return(nickels); } //calculate penny amount public static int getPennies(int cents) { int pennies; pennies = cents / 1; return(pennies); } }
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 :
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 :
Hope you get the idea.
What you want is something like this :
Java Syntax (Toggle Plain Text)
public static void main(String args[]) { EasyReader console = new EasyReader(); Coins money = new Coins(); //prompt user for the amount of cents. System.out.print("Enter amount of change in cents "); money.cents = console.readInt(); money.quarters = getQuarters(money.cents); //pass cents variable to getQuarters function for calculation System.out.println(money.quarters + " Quarters."); //display amount of quarters. money.cents = money.cents - (25 * money.quarters); //subtract the amount of quarters from cents variable. etc.... <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>
Java Syntax (Toggle Plain Text)
public Coins() { //declare variables int cents = 0; int quarters = 0; int dimes2 = 0; int nickels2 = 0; int pennies2 = 0; } //calculate quarter amount public int getQuarters() { quarters = cents / 25; } ....... money.cents = console.readInt(); money.getQuarters(); //pass cents variable to getQuarters function for calculation System.out.println(money.quarters + " Quarters."); //display amount of quarters. money.cents = money.cents - (25 * money.quarters);
:?: Sometimes i wonder if i'm on the right planet :!:
•
•
Join Date: Sep 2004
Posts: 42
Reputation:
Solved Threads: 0
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:
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:
Java Syntax (Toggle Plain Text)
public class Coins { int cents; int quarters; int dimes; int nickels; int pennies; public Coins() { //declare variables cents = 0; quarters = 0; dimes = 0; nickels = 0; pennies = 0; } public static void main(String args[]) { EasyReader console = new EasyReader(); Coins money = new Coins(); //prompt user for the amount of cents. System.out.print("Enter amount of change in cents "); money.cents = console.readInt(); money.quarters = getQuarters(money.cents); //pass cents variable to getQuarters function for calculation System.out.println(money.quarters + " Quarters."); //display amount of quarters. money.cents = money.cents - (25 * money.quarters); //subtract the amount of quarters from cents variable. money.dimes = getDimes(money.cents); //pass cents to getDimes function for calculation System.out.println(money.dimes + " Dimes."); //display amount of dimes. money.cents = money.cents - (10 * money.dimes); //subtract dime amount from cents. money.nickels = getNickels(money.cents); //pass cents to getNickels function for calculation. System.out.println(money.nickels + " Nickels."); //display amount of nickels. money.cents = money.cents - (5 * money.nickels); //subtract nickel amount from cents. money.pennies = getPennies(money.cents); //pass cents to getPennies function for calculation. System.out.println(money.pennies + " Pennies."); //display amount of pennies. money.cents = money.cents - pennies; //subtract penny amount from cents. } //calculate quarter amount public static int getQuarters(int money.cents) { int quarters; quarters = cents / 25; return(quarters); } //calculate dime amount. public static int getDimes(int money.cents) { int dimes; dimes = cents / 10; return(dimes); } //calculate nickel amount. public static int getNickels(int money.cents) { int nickels; nickels = cents / 5; return(nickels); } //calculate penny amount public static int getPennies(int money.cents) { int pennies; pennies = cents / 1; return(pennies); } }
•
•
Join Date: Mar 2004
Posts: 10
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- Copy constructor in derived class (C)
- copy constructor problem (C++)
- How to call constructor for descendant of TObject ? (C++)
- Seg Fault from Matrix Data Constructor (C++)
- Constructor and Convertion operator are the same,how to avoid memory leak? (C++)
Other Threads in the Java Forum
- Previous Thread: applet not showing up
- Next Thread: Adding components to JApplet
| Thread Tools | Search this Thread |
-xlint add android api applet application array arrays automation bi binary blackberry bluetooth chat class classes client code compile compiler component converter database digit eclipse equation error event exception fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying page pearl print problem program programming project qt recursion scanner screen scrollbar server set size sms sort spamblocker sql string superclass swing system thread threads time tree variablebinding windows xor





