【问题标题】:Need some revision on this code.需要对此代码进行一些修改。
【发布时间】:2015-10-14 08:50:30
【问题描述】:

帮助,我需要对这段代码进行一些修改。如何获得 65 和 106 的值而不将其从exercise3 myExer3 = new Exercise3(65,106);

基类:

public class Exercise3 {

private int Voltage;
private int Resistance;




public void setVoltage(int temp){
   if (Voltage == 65)   
   Voltage = temp;
}

public void setResistance(int temp){
      if (Resistance == 106 )    
      Resistance =106;       
}


public int getVoltage (){
        return (Voltage);
}

public int getResistance(){
return(Resistance);
}



}

测试类:

 public class Test_Excercise3 {
    public static void main(String []args){


 Exercise3 myExer3 = new Exercise3(65,106);

System.out.println("Voltage: "+myExer3.getVoltage());
System.out.println("Resistance: "+myExer3.getResistance());
System.out.println("Current : "+    (myExer3.getVoltage()/myExer3.getResistance()));


}
}

这样我就可以得到 0.61 欧姆或电流的结果。

【问题讨论】:

  • 将参数化构造函数添加到您的练习 3。
  • @ShadowDroid 是的,但我还没有任何想法如何制作构造函数。编程新手
  • @BlackHusky 浏览 Sajan 提供的链接...了解构造函数是什么...然后在您的代码中实现它..如果遇到错误,请顺便编辑问题欢迎使用 JAVA 编程
  • 对于浮点 (0.61 Ω) 使用 double 而不是 int。 (不过一般 int 更好。)

标签: java oop accessor


【解决方案1】:

你的类需要一个构造函数:

public class Exercise3 {
    private int voltage;
    private int resistance;
    public Exercise3(int voltage, int resistance) {
        this.voltage = voltage;
        this.resistance = resistance;
    }
    ...
}

更多信息,请咨询Java Tutorials on providing constructors for your classes

【讨论】:

    【解决方案2】:

    在 Excercise3 中添加一个构造函数并更正设置器,同时将结果转换为 (double)。

    public class JavaApplication27
    {
    
        public static class Exercise3
        {
    
            private int voltage;
            private int resistance;
    
            public void setVoltage(int v)
            {
                voltage = v;
            }
    
            public void setResistance(int res)
            {
                resistance = res;        
            }
    
            public int getVoltage()
            {
                return voltage;
            }
    
            public int getResistance()
            {
                return resistance;
            }
    
            public Exercise3(int v, int res)
            {
                setVoltage(v);
                setResistance(res);
            }
    
            public double getCurrent() //helper method :)
            {
                return (double) getVoltage() / getResistance();
            }
        }
    
        public static void main(String[] args)
        {
    
            Exercise3 myExer3 = new Exercise3(65, 106);
    
            System.out.println("Voltage   : "    + myExer3.getVoltage());
            System.out.println("Resistance: "    + myExer3.getResistance());
            System.out.println("Current   : "    + ( (double) myExer3.getVoltage() / myExer3.getResistance())); // Since resistance and voltage are int's, the result of int/int division is int. To get a double) result use (double) :).
            System.out.println("Current   : "    + myExer3.getCurrent()); //you may also use helper method to calculate current
            System.out.format( "Current   : %.2f", myExer3.getCurrent() ); // to get .61 must use formatter
        }
    }
    

    输出:

    Voltage   : 65
    Resistance: 106
    Current   : 0.6132075471698113
    Current   : 0.6132075471698113
    Current   : 0.61
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多