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:
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:
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);
}
}