i'm trying to make a paint conversion project. Eventually I will make a GUI but want to sort of just flesh it out first. I have the code here below. Is there anyone I could maybe simplify this rather than just having a bunch of different methods? Below will the main class and the method class which shows how i calculate everything. Forgive any slopping formatting on here its my first time posting a question.
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!");
}
}
}
I would convert all the quantities to a common unit behind the scenes, and then convert them back as needed. Say, in a class called 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");
}
}
Then, to convert a volume from, say, pints to quarts:
quarts = Volume.ofPints(pints).asQuarts();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.