无法运行演示代码:Bottle类演示

时间:2014-09-07 18:23:06

标签: java class methods

所以我无法运行我的演示代码。说实话,我不知道为什么它不会运行。有什么想法吗?我必须按照这个演示来生成我自己的代码。我在日食中运行鳕鱼而且出现的错误是"瓶子无法解析为类型"。此消息显示约8次。作业的描述如下:

  

写一个Bottle类。该类有以下14种方法:read(),set(int),set(Bottle),get()和(Bottle),减法(Bottle),乘(Bottle),除(Bottle),add(int), subtract(int),multiply(int),divide(int),equals(Bottle)和toString()。 toString()方法将在类中给出。所有add,subtract,multiply和divide方法都返回一个Bottle。您的瓶类必须保证瓶子始终具有正值,并且永远不会超过您选择的最大数量。这些数字被声明为类的常量。必须检查每个具有ha参数的方法以确定是否可以违反上限或下限。仔细考虑每种方法,只测试可能违反的条件。

守则:

import java.util.Scanner;
// test driver for the Bottle class
public class BottleDemo3
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int x;
        Bottle bottle1 = new Bottle();
        Bottle bottle2 = new Bottle();
        Bottle bottle3 = new Bottle();
        Bottle bottle4 = new Bottle();
        Bottle bottle5 = new Bottle();
        System.out.println("please enter a number for bottle1:");
        bottle1.read();
        System.out.println("Bottle1 is this value " + bottle1 + ".");
        System.out.println("Please enter a number for bottle2:");
        bottle2.read();
        bottle3 = bottle3.add(bottle1);
        bottle3 = bottle3.add(bottle2);
        bottle3 = bottle3.divide(2);
        System.out.println("The 2 bottle average is: " + bottle3 + ".");
        System.out.print("Subtracting bottle1 from bottle2 is: " );
        bottle3 = bottle2.subtract(bottle1);
        System.out.println( bottle3);
        bottle3 = bottle2.divide(bottle1);
        System.out.println("Dividing bottle2 with bottle1 is: " + bottle3 + ".");
        if (bottle1.equals(bottle2))
        {
            System.out.println("Bottle1 and bottle2 are equal.");
        }
        else
        {
            System.out.println("Bottle1 and bottle2 are not equal.");
        }
        System.out.println("Bottle4 is now given the value of 10 with the set() method.");
        bottle4.set(10);
        System.out.println("The value of bottle4 is " + bottle4 + ".");
        System.out.println("Bottle4 is now multipled with bottle1.  The value is placed in " +
                "bottle5.");
        bottle5 = bottle1.multiply(bottle4);
        System.out.println("The value of bottle5 is " + bottle5 + ".");
        System.out.println("Enter an integer to add to the value bottle1 has.");
        System.out.println("The sum will be put in bottle3.");
        x = scan.nextInt();
        bottle3 = bottle1.add(x);
        System.out.println("Adding your number " + x +
            " to bottle1 gives a new Bottle with " + bottle3 + " in it.");
        System.out.print("Adding the number " + bottle2 + " which is the number" +
                " in bottle2 to the\nnumber in ");
        bottle2 = bottle1.add(bottle2);
        System.out.println("bottle1 which is " + bottle1 +" gives " + bottle2 + ".");
    }
}

1 个答案:

答案 0 :(得分:0)

实际上,错误清楚地表明您没有实现在BottleDemo3课程中使用的Bottle类。实际上,在Java中,我们使用的所有类或方法都必须在库中,API或自定义用户定义的类中定义!

你的BottleDemo3类很好,但你需要一个名为Bottle的现有类,其中包含所有提到的方法,如read(),set(int),set(Bottle)等。已经由Bottle类实现,以便稍后在BottleDemo3中使用它。

因此,在您在BottleDemo3课程中使用它之前,请准备一个Bottle课程。它应该定义所有方法,以便您可以使用Bottle对象在Bottle类上调用这些方法!

  

我的教授说,瓶子演示应该独立运行并发挥作用   作为我们自己的瓶子类的一个例子。

实际上,他可能已经这样说了,因为您可能已经在之前的项目中实现了Bottle.java类!请在当前项目中使用导入import packageName.Bottle导入.java文件。

如果您仍有疑问,请在下面询问......