绘制转换(JAVA NETBEANS)

绘制转换(JAVA NETBEANS),java,methods,Java,Methods,我想做一个油漆转换项目。最终我将制作一个GUI,但我想 先把它充实起来。我有下面的代码。有没有人我可以简化这一点而不是 只是有一堆不同的方法?下面是主类和方法类,它们显示了如何 我计算一切。请原谅我在这里随意设置格式,这是我第一次发帖 package paintconversion; /** @author JoshS */ public class Paint2Quart { private double Quart; private double Gallon; privat

我想做一个油漆转换项目。最终我将制作一个GUI,但我想 先把它充实起来。我有下面的代码。有没有人我可以简化这一点而不是 只是有一堆不同的方法?下面是主类和方法类,它们显示了如何
我计算一切。请原谅我在这里随意设置格式,这是我第一次发帖

package paintconversion;

/** @author JoshS */
public class Paint2Quart {

  private double Quart;
  private double Gallon;
  private double Pint;

  public Paint2Quart() {
    Quart = 0;
    Gallon = 0;
    Pint = 0;
  }
  // Method to convert Quart to Gallon
  public void setQuartToGallon(double conQuart) {
    if (conQuart > 0) {
      Quart = conQuart / 4;
      System.out.println(conQuart + " quart equals " + Quart + " Gallon(s)");
    } else {
      System.out.println("Numbers Must Be Positive!");
    }
  }
  // Method to convert Gallon To Quart
  public void setGallonToQuart(double conGallon) {
    if (conGallon > 0) {
      Gallon = conGallon * 4;
      System.out.println(conGallon + " gallon equals " + Gallon + " Quart(s)");
    } else {
      System.out.println("Numbers Must Be Positive!");
    }
  }

  public void setPintToGallon(double conPint) {
    if (conPint > 0) {
      Pint = conPint / 8;
      System.out.println(conPint + " pint equals " + Pint + " Gallon(s)");
    } else {
      System.out.println("Numbers Must Be Positive!");
    }
  }

  public void setPintToQuart(double conPint) {
    if (conPint > 0) {
      Pint = conPint / 2;
      System.out.println(conPint + " pint equals " + Pint + " Quart(s)");
    } else {

      System.out.println("Numbers Must Be Positive!");
    }
  }

  public void setGallonToPint(double conGallon) {
    if (conGallon > 0) {
      Gallon = conGallon * 8;
      System.out.println(conGallon + " Gallon equals " + Gallon + " Pint(s)");
    } else {

      System.out.println("Numbers Must Be Positive!");
    }
  }

  public void setQuartToPint(double conQuart) {
    if (conQuart > 0) {
      Pint = conQuart * (2);
      System.out.println(conQuart + " Quart equals " + Pint + " Pint(s)");
    } else {

      System.out.println("Numbers Must Be Positive!");
    }
  }
}

我会在幕后将所有数量转换为一个公共单位,然后根据需要将它们转换回。比如,在一个名为Volume的类中:

public class Volume {
    private double gallons;

    private Volume(double gallons) {
        if (gallons < 0) {
            throw new IllegalArgumentException("value must be positive");
        }
        this.gallons = gallons;
    }

    public static Volume ofGallons(double gallons) {
        return new Volume(gallons);
    }

    public static Volume ofQuarts(double quarts) {
        return new Volume(quarts / 4);
    }

    public static Volume ofPints(double pints) {
        return new Volume(pints / 8);
    }

    public static Volume ofLiters(double liters) {
        return new Volume(0.264172 * liters);
    }

    public double asGallons() {
        return gallons;
    }

    public double asQuarts() {
        return gallons * 4;
    }

    public double asPints() {
        return gallons * 8;
    }

    public double asLiters() {
        return gallons / 0.264172;
    }

    @Override
    public String toString() {
        return gallons + " gallon" + (gallons == 1 ? "" : "s");
    }
}

我会在幕后将所有数量转换为一个公共单位,然后根据需要将它们转换回。比如,在一个名为Volume的类中:

public class Volume {
    private double gallons;

    private Volume(double gallons) {
        if (gallons < 0) {
            throw new IllegalArgumentException("value must be positive");
        }
        this.gallons = gallons;
    }

    public static Volume ofGallons(double gallons) {
        return new Volume(gallons);
    }

    public static Volume ofQuarts(double quarts) {
        return new Volume(quarts / 4);
    }

    public static Volume ofPints(double pints) {
        return new Volume(pints / 8);
    }

    public static Volume ofLiters(double liters) {
        return new Volume(0.264172 * liters);
    }

    public double asGallons() {
        return gallons;
    }

    public double asQuarts() {
        return gallons * 4;
    }

    public double asPints() {
        return gallons * 8;
    }

    public double asLiters() {
        return gallons / 0.264172;
    }

    @Override
    public String toString() {
        return gallons + " gallon" + (gallons == 1 ? "" : "s");
    }
}


“请原谅任何倾斜格式”此处的大多数格式问题都是在您复制并粘贴的原始代码中出现的。你希望阅读你的代码的每个人(包括你自己)都能学会格式化你的代码吗;了解如何让IDE为您设置格式。一些一般性意见:不要将字段名称大写。按照Java中的惯例,它们以小写字母开头。通常,包名基于倒置的域名,例如com.google.something。如果您没有域名可供使用,我将完全不使用package语句,而将类保留在默认的未命名包中“Java”是混合编写的,您使用NetBeans这一事实与问题无关。这是一个课堂作业吗?@AndyTurner谢谢,这是我的第一个课外项目,所以可以说我正在脱离训练轮。@DavidConrad正式指出,谢谢。“请原谅任何倾斜格式”这里的大多数格式问题都会出现在您复制并粘贴的原始代码中。你希望阅读你的代码的每个人(包括你自己)都能学会格式化你的代码吗;了解如何让IDE为您设置格式。一些一般性意见:不要将字段名称大写。按照Java中的惯例,它们以小写字母开头。通常,包名基于倒置的域名,例如com.google.something。如果您没有域名可供使用,我将完全不使用package语句,而将类保留在默认的未命名包中“Java”是混合编写的,您使用NetBeans这一事实与问题无关。这是一个课堂作业吗?@AndyTurner谢谢,这是我的第一个课外项目,所以可以说我正在脱离训练轮。@DavidConrad正式指出,谢谢。在我看来,您的……()方法应该是静态的。@Abra哇!他们应该是。接得好,谢谢<点的代码>:一加仑中肯定有8品脱,而不是一品脱中的8加仑。@AndyTurner这就是我跑得太快而没有编写单元测试的原因。谢谢谢谢你的反馈。关于@override部分的快速问题?这到底是什么意思?我们谈到了it类,但那是几个月前的事了。在我看来,你的…()方法应该是静态的。@Abra哇!他们应该是。接得好,谢谢<点的代码>:一加仑中肯定有8品脱,而不是一品脱中的8加仑。@AndyTurner这就是我跑得太快而没有编写单元测试的原因。谢谢谢谢你的反馈。关于@override部分的快速问题?这到底是什么意思?我们上过it课,但那是几个月前的事了。