协助为预先制作的演示文件制作课程

时间:2017-01-26 01:27:47

标签: java eclipse

我目前正在完成一项任务,要求我为我教授制作的预先制作的“瓶子演示”文件创建一个“瓶子类”。这是作业的描述:

  

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

以下是演示代码:

public static void main(String[] args) {
        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(); // affected my max and min
        System.out.println("Bottle1 is this value " + bottle1 + ".");
        System.out.println("Please enter a number for bottle2:");
        bottle2.read();  // affected by max and min
        bottle3.set(0);
        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 multiplied 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.");
        Scanner keyboard = new Scanner(System.in);
        x = keyboard.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 + ".");

    }

}

这是我到目前为止所做的代码:

public class bottle {
    Scanner scan = new Scanner(System.in);
    private int value;

    public void Bottle() {

    value = 0;
    }
    public void read() {
    value = scan.nextInt();
    }
    public void set(bottle) {
    value = bottle1.value;
    }
    public void set(int bottle1) {
    value = bottle1;
    }
    public bottle add(bottle) {
    value = value + bottle1.value;

    }
    public bottle subtract(bottle) {


    }
    public bottle multiply(bottle) {


    }
    public bottle divide(bottle) {


    }
    public bottle add(int bottle) {


    }
    public bottle subtract(int bottle) {


    }
    public bottle multiply(int bottle) {

    }
    public bottle divide(int bottle) {
    value = value / bottle;

    }
    public String toString() {
    String name = null;
    return name;
    }
    public boolean equals(bottle bottle) {
    if (this == bottle) {
        return true;
    }
    else {
        return false;
        }
      }
    }

我需要帮助的是如何让我的方法工作? (add(int),divide(bottle),divide(int)等)

并且对于用户可以输入的值的最大值和最小值,我知道它可以放在类代码的顶部,但是我如何设置它以便每次用户输入一个数字和数学输出每次检查最大值和最小值以查看是否有任何数字违反设定规则?

我知道我的类代码缺少许多关键组件(我认为返回数学部分的方法)但我正在努力保持理智,试图弄清楚要做什么。任何帮助将不胜感激,并提前感谢。

我也会尽我所能回答你可能遇到的任何问题。

编辑:我在阅读了教科书中关于课程的章节后重新编写了我的代码,而且我的知识比以前好一些。这是我的新代码:

Scanner scan = new Scanner(System.in);
private int value;
private int max = 100;
private int min = 0;

public bottle() {
 // sets default value as zero
    this.value = 0;
}
public void read() {
    value = scan.nextInt();
}
public void set(int value) {
    this.value = value;
}    
public int add(bottle) {
    if (this.value + bottle < this.max && this.value + bottle > this.min)
        return this.value + bottle;
    else
        System.out.println("Please enter another number");
        int x =  scan.nextInt();
        return add(x);
    // the few lines above checks to see if the number violates the max and min

}
public int subtract(bottle) {
    if (this.value - bottle < this.max && this.value - bottle > this.min)
        return this.value - bottle;
    else 
        System.out.println("Please enter another number");
        int x = scan.nextInt();
        return subtract(x);

}
// though there is this error under the word bottle in the parentheses
public int multiply(bottle) {
    if (this.value * bottle < this.max && this.value * bottle > this.min)
        return this.value * bottle;
    else 
        System.out.println("Please enter another number");
        int x = scan.nextInt();
        return multiply(x);
}


public int divide(bottle) {
    if (this.value / bottle < this.max && this.value / bottle > this.min)
        return this.value / bottle;
    else 
        System.out.println("Please enter another number");
        int x = scan.nextInt();
        return divide(x);

}
// the String toString method, format as shown by the professor.
public String toString()
{
    return this.max + " " + this.min + " " + this.value;

虽然我的课程中仍然有4个错误,这是我的加,减,乘,除法后括号内的单词瓶。因此,演示文件有8个错误,这些错误都是数学方法。我不知道该怎么办,因为“瓶子”是一个对象吗?那么如何将2瓶装在一起,或者我采取了错误的方法?

1 个答案:

答案 0 :(得分:0)

看起来你几乎走在正确的轨道上。这些将有所帮助:

https://www.tutorialspoint.com/java/java_object_classes.htm

https://www.tutorialspoint.com/java/java_methods.htm

注意实例是什么,以及如何将参数传递给方法以及返回。