I'm having trouble with this assignment:
Write a class Coins with one constructor that takes a number of cents as a parameter. Supply four public methods, getQuarters, getNickels, getDimes, getPennies, that return the number of corresponding coins that add up to the amount (in the optimal representation with the smallest possible number of coins). Make sure Coins objects are immutable (that is, none of the class's methods changes any fields)

Here is my code so far:

package coins;
    import java.util.Scanner;

    public class Coins 
        int cents;
        public int quarter = 25;
        public int dime = 10;
        public int nickel = 5;
        public int penny = 1;

        public static void main(String[] args) 
            Scanner kboard = new Scanner(System.in);
            System.out.print("Enter number of cents:");
            int cents = kboard.nextInt();


        public int getQuarters()
            int quarters = (cents/quarter);
            return (quarters);

        public int getDimes()
            int dimes = cents - getQuarters()*25/dime;

        public int getNickels()
            int nickels = cents - (getQuarters()*25) - (getDimes()*10) / nickel;

        public int getPennies()
            int pennies = cents - (getQuarters()*25) - (getDimes()*10) - (getNickels()*5) / penny;
            return (pennies);

        System.out.println(cents + " cents equals: ");
        System.out.println("Quarters: " + getQuarters());
        System.out.println("Dimes: " + getDimes());
        System.out.println("Nickles: " + getNickels());
        System.out.println("Pennies: " + getPennies());


I'm lost, please help. thanks =)

4 Years
Discussion Span
Last Post by jalpesh_007

So what kind of questions do you have? What's happening when you run the program vs what you're expecting to happen? Are you getting any compiler or run-time errors?

In lines 45-49, you have the code just kind of hanging out there inside the class. You likely want that to be inside the main method at the end of it.


As you have mentioned in the problem,you haven't created constructor of coin class.
So, first create the constructor outside the main method.
Take the input of cents when you create object of the cents class.
then call each method by that class object..and do the rest....


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.