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

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 :

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....

[img]http://www.daniweb.com/techtalkforums/techtalk-images/buttons/reputation.gif[/img] [img]http://www.daniweb.com/techtalkforums/techtalk-images/buttons/report.gif[/img]

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 :

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

Hope you get the idea.

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:

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

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

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.