0
public class AppleOrchard
{
    public static void main(String [] args)
    {
        System.out.println("Rick...");
        BushelBasket rick = new BushelBasket(0);
        rick.print();
        rick.pick(11);
        rick.pick(22);
        rick.print();
        rick.eat(4);
        rick.print();
        rick.spill();
        rick.print();

        System.out.println("Newt...");
        BushelBasket newt = new BushelBasket(100);
        newt.print();

        System.out.println( newt.isEmpty() );
        System.out.println( newt.isFull() );
        System.out.println( newt.getApples() );
        System.out.println( newt.roomLeftInBasket() );

        System.out.println("Michele...");
        BushelBasket michele = new BushelBasket(0);
        System.out.println( michele.isEmpty() );
        System.out.println( michele.isFull() );
        michele.pick(25);
        System.out.println( michele.isEmpty() );
        System.out.println( michele.isFull() );
        michele.pick(100);
        System.out.println( michele.isEmpty() );
        System.out.println( michele.isFull() );

        System.out.println("Herman...");
        BushelBasket herman = new BushelBasket(-5);  // should default to 0
        herman.print();

        System.out.println("Jon...");
        BushelBasket jon = new BushelBasket(300);  // should default to 125
        jon.print();

        System.out.println("Ron...");
        BushelBasket ron = new BushelBasket(20);  // starts with 20
        ron.print();
        ron.eat(50);  // can only eat down to zero apples
        ron.print();  // should see zero apples
        ron.eat(10);  // back to 10
        ron.pick(1000);  // basket can only hold 125 apples
        ron.print();  // should print 125

        System.out.println("Gary...");
        BushelBasket gary = new BushelBasket();  // should default to 0
        gary.print();
    }
}

class BushelBasket
{
    int apples;

    BushelBasket(int apples)
    {   
        if(apples>125)
            this.apples = 125;
        else if(apples < 0)
            this.apples = 0;
        else
            this.apples = apples;
    }

    public void spill()
    {
        apples = 0;
    }

    public void pick(int x)
    {
        apples = apples + x;
            if (apples < 0)
                apples = 0;
            else if(apples > 125)
                apples = 125;
    }

    public void eat(int x)
    {
        apples = apples - x;
            if (apples < 0)
                apples = 0;
            else if(apples > 125)
                apples = 125;
    }

    public int getApples()
    {
        return this.apples;  
    }

    public void print()
    {
        int x = getApples();
        System.out.println("This bushel basket has " + x + " apples in it.");
    }

    public boolean isEmpty()
    {
        if (apples == 0)
        {   
            return true;
        }

        else 
        {
            return false;
        }
    }

    public boolean isFull()
    {
        int full = 125;

        if (apples >= full)
        {   
            return true;
        }

        else 
        {
            return false;
        }

    }

    public int roomLeftInBasket()
    {
        int room = 125 - apples;
        return room;                
    }
}

好的,所以一切正常,我的输出与它的假设相匹配,但只有当我注释掉我无法弄清楚的这两行时。

BushelBasket gary = new BushelBasket();
gary.print();

当我尝试编译时,这两行导致错误,我真的不明白如何修复它。它给出的错误是“错误:类 BushelBasket 中的构造函数 BushelBasket 不能应用于给定类型;” 我有点理解,因为在调用该方法的所有其他时间它都有一个调用它的数字,但不知道如果没有该怎么办。此外,如果在调用该方法时它没有任何对象,则它的假设默认为零。

4

1 回答 1

0

在 Java 中,如果方法或构造函数接受 1 个参数,则必须传递 1 个参数。

您的 BushelBasket 构造函数将 int 作为参数,因此您必须使用 int 作为参数调用它。

您可以添加另一个构造函数,该构造函数会将字段保留为其默认值:

int apples;

BushelBasket(int apples) {
    ...
}

BushelBasket() {
    // do nothing, so apples will be 0.
}

或者你可以定义另一个不带参数的构造函数,它会调用带有 int 参数的构造函数并将其传递给 0:

int apples;

BushelBasket(int apples) {
    ...
}

BushelBasket() {
    this(0); // calls the other BushelBasket constructor.
}
于 2013-03-17T14:23:42.067 回答