【发布时间】:2017-01-26 01:27:47
【问题描述】:
我目前正在处理一项任务,该任务要求我为教授制作的预制“瓶子演示”文件创建一个“瓶子类”。这是作业的描述:
编写一个 Bottle 类。该类有这 14 个方法:read()、set(int)、>set(Bottle)、get()、and(Bottle)、subtract(Bottle)、multiply(Bottle)、>divide(Bottle)、add(int )、减法(int)、乘法(int)、除法(int)、>equals(Bottle)和toString()。 toString() 方法将在课堂上给出。所有 >add、subtract、multiply 和 divide 方法都返回一个 Bottle。您的 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 瓶加在一起,还是我采取了错误的方法?
【问题讨论】:
-
这听起来毫无意义。例如
add (int)是Bottle上的一个方法,它接受int并返回Bottle(this?) -
请注意,类名应该使用 PascalCase。 (每个单词的首字母大写(例如
Bottle或BigMac))另外,为什么你的数学函数返回bottle? -
为什么你的问题到处都是随机的">"。
-
几天后,阅读了我的课本中关于课程的章节,我重新编辑了我的代码: