Write a program called Volume and Weight Converter (VolumeWeightConverter java)
ID: 3889820 • Letter: W
Question
Write a program called Volume and Weight Converter (VolumeWeightConverter java) that will perform the following conversions: . . . Gallon to Liter Liter to Gallon Pound to Kilogram Kilogram to Pound In your main method, display a menu with each of these choices plus an additional choice to exit. Prompt the user for a menu option, then call the corresponding method to prompt for a value and display the converted result. Your resulting program should have five methods: main and one method for each of the conversion functions. The calculation must be performed in those four metheds, and the result should be printed from the main method. After the converted value is displayed, display the menu againa repeat until the user chooses the Exit option. Hints: Your menu and the calls to the other methods must beExplanation / Answer
import java.util.Scanner;
class VolumeWeightConverter
{
double gallonsToLiters(double qt) //converting function
{
return(qt*3.785);
}
double litersToGallons(double qt) //converting function
{
return(qt*0.264);
}
double poundsToKilograms(double qt) //converting function
{
return(qt*0.454);
}
double kilogramsToPounds(double qt) //converting function
{
return(qt*2.205);
}
public static void main(String args[]) //main function
{
int ch;
double st,an; //variable declaration
VolumeWeightConverter obj=new VolumeWeightConverter();
Scanner sc=new Scanner(System.in);
do{
System.out.println("VOLUME and WEIGHT CONVERTER"); //menu for user
System.out.println();
System.out.println("1. Gallons to Liters");
System.out.println("2. Liters to Gallons");
System.out.println("3. Pounds to Kilograms");
System.out.println("4. Kilograms to Pounds");
System.out.println();
System.out.println("0. Exit");
System.out.println("Choose: "); //acepting input from user
ch=sc.nextInt();
switch(ch) //condition matched according to the user choice
{
case 1:
System.out.println("Enter gallons: ");
st=sc.nextDouble();
an=obj.gallonsToLiters(st);
System.out.println(st+" gallons = "+an+" liters");
break;
case 2:
System.out.println("Enter liters: ");
st=sc.nextDouble();
an=obj.litersToGallons(st);
System.out.println(st+" liters = "+an+" gallons");
break;
case 3:
System.out.println("Enter pounds: ");
st=sc.nextDouble();
an=obj.poundsToKilograms(st);
System.out.println(st+" pounds = "+an+" kilograms");
break;
case 4:
System.out.println("Enter kilograms: ");
st=sc.nextDouble();
an=obj.kilogramsToPounds(st);
System.out.println(st+" kilograms = "+an+" pounds");
break;
case 0:
System.exit(0);
break;
default:
System.out.println("Invalid Choice");
break;
}
}while(ch!=0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.