处理使用java计算求和的构造函数

时间:2017-04-19 22:00:26

标签: java constructor java.util.scanner

我想用家庭保险计算保险金额。最初的要求是:2个保险产品,每平方米的价格不同 1.紧凑型:每平方米650欧元 2.最佳:每平方米700欧元 该程序的2个输入是:“紧凑”或“最佳” 和平方米的生活空间。 输出应该是保险金额; 必须记下,该程序可能会被其他计算复杂的产品扩展。

我正在以这种方式进行,但由于我是java的新手,我陷入了如何处理构造函数的困境。在我的代码中,我想修复compact和Optimal,但我希望通过用户输入获得Living space的值,例如scanner类。在这种情况下我该怎么做呢。

My Insurance class 
public class Insurance {
  private Double Kompakt;
  private Double Optimal;

  public Insurance(Double kompakt, Double optimal) {

    this.Kompakt = kompakt;
    this.Optimal = optimal;
}

public Double getKompakt() {
    return Kompakt;
}

public void setKompakt(Double kompakt) {
    this.Kompakt = kompakt;
}

public Double getOptimal() {
    return Optimal;
}

public void setOptimal(Double optimal) {
    this.Optimal = optimal;
 }
}

我的总和等级是

public class Sum extends Insurance{

 private int livingSpace;

  public Summe(Double kompakt, Double optimal, int Wohnfläche) {
    super(1.0, 1.0);
    this.livingSpace=livingSpace;
    this.setCompakt(650.00); // I want to fix value of Compact
    this.setOptimal(700.00); // I want to fix Value of Optimal
    }


public double CompaktSum(){

        double sum= getCompakt()*livingSpace;
        return sum;
    }
    public double OptimalSum(){
        double sum =getOptimal()*livingSpace;
        return sum;
    }
}

现在在我的主类中我想手动设置livingSpace的值但是我不能在没有设置方法的情况下调用构造函数

 public static void main(String[] args) {
    Scanner sc1 =new Scanner(System.in);
    Scanner sc2 =new Scanner(System.in);
    System.out.println("1\tCompact: 650 € per m²\n2\tOptimal: 700 € per 
    m²");
    System.out.println("Please choose your 1 for Compact and 2 for product");
    int swichValue=sc1.nextInt();
    Summe hs=new Summe(1.0,1.0,5); // I am having problem here, I just want 
    //to call empty contructor so that I can set livingSpace manually

    switch(swichValue){
    case 1:{
    double res=hs.KompaktSumme();
    System.out.println("Versicherungssumme ist : "+res);
    break;
    }
    case 2:{
        double res=hs.OptimalSumme();
        System.out.println("Versicherungssumme ist : "+res);
        break;
    }
    case 3:{
        System.out.println("Error");

    }
    }
}

2 个答案:

答案 0 :(得分:0)

这是您重载构造函数的方法。但实际上你的代码不会编译,因为你的类的名称是Sum,你的构造函数的名称是Summe。构造函数应与类具有相同的名称。

public Sum() {
    super(650.00, 700.00);
}

答案 1 :(得分:0)

坦率地说,我不确定你想要达到什么目标,但如果我的理解是正确的话。 要修复Kompakt和Optimal的值,您可以简单地执行此操作:

public class Insurance {
  private Double Kompakt = 700.0; // fixed value
  private Double Optimal = 600.0; // fixed value

  public Insurance() {} // empty constructor for the superclass

 /* you can change the values and get them by using the same getters and setters you already created*/

现在在子类

public class Summe extends Insurance{

 private int livingSpace;

  public Summe(int Wohnfläche) { // you can use this in future if you wnat
   // no need to call the superclass constructor 
      livingSpace = Wohnfläche;
    }

    // another constructor that overloads the first (i.e empty one)
      public Summe() {
       // no need to call the superclass constrictor 
      }

 // create a setter and a getter for the the livingSpace
 public void setLivingSpace (int livingSpace){
     this.livingSpace = livingSpace;
 }

现在你在这里

public static void main(String[] args) {
    Scanner sc1 =new Scanner(System.in);
    Scanner sc2 =new Scanner(System.in);
    System.out.println("1\tCompact: 650 € per m²\n2\tOptimal: 700 € per 
    m²");
    System.out.println("Please choose your 1 for Compact and 2 for product");
    int swichValue=sc1.nextInt();
    Summe hs=new Summe(); 
    // set the values here manually as you described
    hs.setLivingSpace (0.5); // or whatever value, you may need to take it from user..etc 

    switch(swichValue){
    case 1:
          double res=hs.KompaktSumme();
          System.out.println("Versicherungssumme ist : "+res);
       break;

    case 2:
          double res=hs.OptimalSumme();
          System.out.println("Versicherungssumme ist : "+res);
        break;

    case 3:
         System.out.println("Error");
    }
}
相关问题