繁体   English   中英

根据教授需要弄清楚如何操纵变异子以找出一个类,我对此感到困惑

[英]need to figure out how to manipulate the mutator to figure out a class according to professor, I am confusd on how to do that

我知道我们不应该提出家庭作业问题,但是我遇到的问题是我和他交谈过的,在45分钟之后,我比以前更加困惑,直到他说这被认为是令人困惑的时候。 因此,我们正在创建自己的类,其中一个正在创建一个类,稍后再用于其他行星(主要是月亮,水星,金星,木星和土星)的重量转换器中。下面)

/*
*   WeightConverter class
*   Class Description - A Java class for converting weight on different plants
*   Author: J. Wilson
*   Date:   2/24/2015
*   Filename:   WeightConverter.java
*/


// class beginning
class  WeightConverter {


//create the variable that stores the conversion rate
private double weightchange;

//the constructor
public WeightConverter(double weight){
weightchange=weight;
}
//accessor
public double smallstep(){
return weightchange;
}
//mutator for the needed variable
public void setweightratio(double number){
   weightchange=number;

   }

//and the method convert.
public double convert(double planet){
return planet*weightchange;
}
}
//end of class

但是他补充说,“在这个WeightCalculator类中,我希望您至少使用一次创建的每种方法。否则,对于您不使用的每种方法都将失去一个分数”,我问他如何解决这个问题。分钟的谈话我比以前更困惑,在他提到我之前,我可以带上我的变速器,然后操纵一下,这就是我目前所拥有的,并且到目前为止

/*
*   WeightConverter on other planets
*   Program Description - weightchanger
*   Author: J.Wilson
*   Date:   2/24/2015
*   Filename:   WeightCalculator.java
*/


//import statements
import java.util.*;     //example

// class beginning
class WeightCalculator {
   public static void main(String[] args ) {
      //Declare variables area

      WeightConverter test;
      Scanner scan = new Scanner ( System.in );//reads what is entered into the keyboard by the user
      double pounds;
      //Program beginning messages to user here

      System.out.println("Hello! Welcome to my weight converter program");
      System.out.println();//blank space
      System.out.println("Please enter your weight (in pounds): ");
      pounds=scan.nextDouble();
      WeightConverter moon = new WeightConverter(.167);
      System.out.println("Your weight on the moon is " + moon.convert(pounds));


      //Collect inputs from user or read in data here




      //Echo input values back to user here




      //Main calculations and code to



      //Output results here




      //End program message
      System.out.println();
      System.out.println("Hope you enjoyed using this program!");
       }// end main method


}// end class

谁能多次使用我的增效剂来解释他的意思? 还是用外行人的话去做呢?

听起来每次要获取新星球上的重量时,都应该使用setWeightRatio()。 首先创建月亮对象并设置其重量,然后在moon.convert()中使用该重量设置。接下来,您应该将WeightRatio()设置为不同的量(对于木星或金星或其他物体),并将weightChange变量设置为您刚刚传入的新double。然后,当您使用convert()时,它将访问新的weightChange变量并基于该变量进行计算。

我将创建一个行星对象,然后根据需要通过增幅器重置其weightChange变量。 然后在设置新的权重比之间使用convert()。

您的教授说您必须至少使用一次课堂中的所有方法,对吗?

您的setweightratio方法是没有用的(这很明显,因为您还没有使用它!)。 它将类变量weightchange设置为其输入参数,但是在构造函数中发生了同样的事情,因此您无需再次调用它。

您有两种选择:

您已经将月球重量返回给用户。 当下一个行星有问题时, 请保留您的WeightConverter实例,然后调用moon.setweightratio(ratio of Venus or whatever) 然后使用moon.convert(pounds)将使用新的比率。

另外,也可以一起删除setweightratio方法(这样就不会因不使用它而获得分数),并为每个行星创建WeightConverter对象。 这不太优雅。

暂无
暂无

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

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