简体   繁体   中英

Coins : Invalid Method Declaration, return type required

I just started Computer Science last week, and we got a worksheet called Coins, in which I had to find out how many quarters, dimes, nickels and pennies there are in a set of coins. I am having a lot of trouble, and getting that error. Here's my code

package Coins;


public class Coins
{
    private int change;

    // two contructors
    Change()    //default constructor
    {
        change = 94;
    }

    Change( int c )
    {
        change = c;
    }

    // accessor method - change
    public int getChange()
    {
            return Change;
    }



    // mutator method - change
    public void setChange( int anotherChange)
    {
        change = anotherChange;
    }

    public void askUserForChange()
    {
        Scanner keyIn;
        keyIn = new Scanner(System.in);

        System.out.print("Please enter the amount of change: ");
        String input = keyIn.nextLine();

        int nChange = Integer.parseInt (input);

        setChange(nChange);
        // change = nChange

        printChangex();
    }

    // action method - take accessor figure out coins -> output
    // calculating the coins needed for the change
    public void printChangeRange(int start, int end)
    {
        for(int c = start; c <= end; c++
        {
            setChange(c);
            printChangex();
        }

    }
    public void printChangex()
    {

    int c = change;
    int quarter = c / 25;
    System.out.println("quarter = " + quarter);
    int a = c%25;
    int dime = a / 10;
    System.out.println("dime = " + dime);
    int b = a%10;
    int nickel = b / 5;
    System.out.println("nickel = " + nickel);
    int c = b%5;
    int penny = c / 1;
    System.out.println("penny = " + penny);

    }


    // instance variables - replace the example below with your own
    private int x;

    public Coins()
    {
        // initialise instance variables
        x = 0;
    }

    public int sampleMethod(int y)
    {
        // put your code here
        return x + y;
    }
}

You have a class named Coins and are trying to give it a constructor named Change . The class and constructor must have the same name. Just pick one.

To elaborate on the error in your title, I assume that "Invalid Method Declaration, return type required" refers to the line with Change() //default constructor . Since this is in a class called Coins it is not a constructor as the comment claims. The Java compiler thinks that it is a method. All methods must have a return type, so the compiler complains.

The actual constructors are at the bottom of your code. It is standard practice to put constructors first, so I suggest that you put these poperly-named constructors at the beginning of your Coins class. You probably just need to remove the Change() constructors completely.

Also as a tip for asking questions here, it is extremel critical that you post the complete error message you are getting. My answer is based on some educated guesses and certainly don't solve all the problems in your code. Feel free to come back with more questions as you keep trying to fix your program.

This

// two contructors
Change()    //default constructor
{
    change = 94;
}

Change( int c )
{
    change = c;
}

is unusual. You even have a constructor for the class Coins at the bottom of the file, so you would want to use that. Keep in mind that all Java classes have a constructor that are named the same as the class itself - even if it's the default constructor.

It's even more unusual that it has the magical value of 94 on instantiation...but in all seriousness, pick a class name and stick with it.

This

// accessor method - change
    public int getChange()
    {
            return Change;
    }

...is also odd. You may want to return the member variable change instead, so change that to a lower case C.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM