繁体   English   中英

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

[英]Assistance on making a class for a pre-made demo file

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

编写一个Bottle类。 该类具有以下14种方法:read(),set(int),> set(Bottle),get()和(Bottle),subtract(Bottle),multiple(Bottle),> divide(Bottle),add(int ),subtract(int),multipli(int),divide(int),> equals(Bottle)和toString()。 toString()方法将在类中给出。 所有>加,减,乘和除方法都返回一个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个错误,这是加,减,乘和除法后括号内的单词bottle。 因此,演示文件有8个错误,都是数学方法。 我不确定该怎么做,因为“瓶子”是对象吗? 那我该如何一起添加2瓶,还是我采用了错误的方法?

看来您步入正轨。 这些将有助于:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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