为温度转换器创建构造器

时间:2016-05-11 18:14:52

标签: java class constructor

写一个代表摄氏度和华氏度的温度的Temperature类,使用温度的浮点数和刻度的字符:“C”表示摄氏度或“F”表示华氏度。该课应该有

四个构造函数:

  • 一个度数,
  • 一个用于比例,
  • 一个用于度数和比例,以及
  • 默认构造函数。

对于这些构造函数中的每一个,如果没有指定值,则假设为零度;如果未给出比例,则假定为摄氏度。"

嘿,我是一名业余的java程序员...我被要求做上述陈述,我并不是那么熟悉构造函数,但我对任何知识都持开放态度:)显然我是不要求别人给我一个问题的答案,但也许有人可以就我如何开始给我一些建议......这是我到目前为止所做的:

public class TemperatureApparatus {

    public class temperature{
        private float c;
        private float F;

        public temperature(){

        }
        public temperature(){

        }
        public temperature(){

        }
        public temperature(){

        }
    }

    public static void main(String[] args) {

    }
}

2 个答案:

答案 0 :(得分:1)

  

四个构造函数:一个用于度数,一个用于比例,   一个用于度和度,以及一个默认构造函数。

这些是你的构造函数参数。参数通过参数传递给构造函数和方法,参数列在方法的括号内。

您的构造函数格式正确无误。它本质上是一个具有相同名称的类且没有返回类型的方法。现在你需要添加参数。

默认构造函数没有参数:

public Temperature() { //Class names should be capitalized!
    //Default constructors often do nothing, but you can set default values here if you want
}

这里是仅接受学位的构造函数的签名:

public Temperature(float degrees) {
     //Assign the "degrees" argument to an instance variable here
     //You might consider assigning a value to a "scale" variable by default as well
}

此分配可能希望您填写构造函数的主体,因此将给定值分配给类中的float。现在为其他所需的参数创建构造函数。由于作业需要' F'或者' C'对于比例,您可以安全地使用char作为该数据的参数。

完成作业后,您可能会考虑用以下方法挑战自己:鉴于用户现在可以拥有Temperaturefloat度和char的对象比例,您如何实施getTemperature()方法?

哦,你没有理由像你一样上课。如果您需要制作可运行的答案,请将Temperature放在与运行main的类不同的类中。

答案 1 :(得分:0)

你去吧

public class Temperature {
   private float temp;
   char scale;

   //first constructor
   /*
   *Notice how in this first constructor, I put scale
   *as 'C' since there was no char value being passed
   *as a parameter (no char scale inside the brackets)
   *Now i did get a float temp passed a a parameter so 
   *I set whatever that temp is to the temp of this class
   *also known as this.temp (this refers to THIS class
   */
   public Temperature(float temp) {
      this.temp = temp;
      scale = 'C';
      //other way
      //this(temp, 'C');  
   }

   //second constructor
   /*
   *Now this constructor is the exact same case at the 
   *first one, except this one has the char scale passed.
   *Since no temp is passed, I set it to 0 as no value is specified 
   */
   public Temperature(char scale) {
      this.scale = scale;
      temp = 0;
      //other way
      //this(0, scale); 
   }

   //third constructor
   /*
   *This third one is having both temp and scale being specified
   *which means you don't need to put any initial value (0 and Celcius)
   *Just set these objects to whatever is being passed by through the 
   *parameters
   */
   public Temperature(float temp, char scale) {
      this.temp = temp;
      this.scale = scale;
   }

   //fourth constructor
   /*
   *This last one has no parameters through so
   *you can just set both the scale and temp to 
   *0 and 'C' respectively.
   */
   public Temperature() {
      temp = 0;
      scale = 'C';
      //other way
      //this(0, 'C'); 
   }
}