Nicholas N Peck Nicholas N Peck - 1 month ago 11
Java Question

I am trying to convert multiple metrics using methods and I'm stuck I dont know what to do next?

I have my code here it is a mess I know but I'm confused with my logic and need help trying to get this together. So any help is appreciated. I want the output to be this "

CONVERSION TOOL

1.Gallons to liters


  1. Liters to gallons

  2. Teaspoons to milliliters

  3. Milliliters to teaspoons



Please select the type of conversion you would like to make: 1

Please enter the gallons: 5

The total liters is 18.925"

import java.util.Scanner;
public class ConversionProgram {

public static final double LT_TO_GAL = .274;
public static final double GAL_TO_LT = 3.785;
public static final double TSP_TO_ML = 4.9289;
public static final double ML_TO_TSP = .202;


public static void main(String[] args) {
int nInitLit = 0;
int nInitGal = 0;
int nTotalLit = 0;
double dResult = 0;
double dNumber1 = 0;
double dNumber2 = 0;
int nPrice = 0;
double dTotalGal = 0.0;
Scanner input = new Scanner(System.in);

System.out.println("CONVERSION TOOL: ");
System.out.println("1. Gallons to liters ");
System.out.println("2. Liters to gallons");
System.out.println("3. Teaspoons to milliliters");
System.out.println("4. Milliliters to teaspoons");
System.out.print("");

System.out.print("Please select the type of conversion you would like to make: ");
int nConversion = input.nextInt();

if (nConversion == 1) {
System.out.print("Please enter the gallons: ");
double dGal = input.nextDouble();

}

else if (nConversion == 2) {
System.out.print("Please enter the total liters: ");
double dLit = input.nextDouble();
}

else if (nConversion == 3 ) {
System.out.print("Please enter the total teaspoons ");
double dTsp = input.nextDouble();
}

else if (nConversion == 4 ) {
System.out.print("Please enter the total milliliters ");
double dMil = input.nextDouble();
}


} //end main method

public static double calculateGallonsToLiters(double dGal) {
double dTotGal = 0;
dTotGal = dGal * GAL_TO_LT;
return dTotGal;
}



public static double calculateLitersToGallons(double dLit){

double dTotLt = 0;
dTotLt = dLit * LT_TO_GAL;
return dTotLt;
} //end


public static double calculateTeaspoonsToMliters(double dMlit){

double dTotMl = 0;
dTotMl = dMlit * TSP_TO_ML;
return dTotMl;
} //end


public static double calculateMlitersToTeaspoons(double dTsp){
double dTotTsp = 0;
dTotTsp = dTsp * ML_TO_TSP;
return dTotTsp;
} //end


public static void printResult(int nOpt, int nResultOperation){
System.out.println("The total gallons is : " + nResultOperation);

}//end method printResult} //end class

Answer

you need to call the conversion method as well. Change

public static void printResult(int nOpt, int nResultOperation){
    System.out.println("The total gallons is : " + nResultOperation);
}

into

public static void printResult(String unit, double result){
   System.out.println("The total " + unit + " is : " + result);
}

and

if (nConversion == 1) {        
    System.out.print("Please enter the gallons: ");
    double dGal = input.nextDouble();
}

into

if (nConversion == 1) {        
    System.out.print("Please enter the gallons: ");
    double dGal = input.nextDouble();
    double litres = calculateGallonsToLiters(dGal);
    printResult("litres", litres);
}
Comments