Write a program to convert from metric units to standard uni
Write a program to convert from metric units to standard units.
Specification
Create a MetricConverter class with the following public static functions to convert from metric units to standard units:
double KG_lbs(double kg)
double KM_miles(double km)
double Liters_quarts(double liters)
double Celsius_fahrenheit(double celsius)
Each function will accept a metric units value and return the corresponding standard units value.
Create a MetricConverterTest program to test the functionality of the MetricConverter class and run the program from this simple menu:
KG to lbs
KM to Miles
Liters to Quarts
Celsius to Fahrenheit
Exit
Create the following \"menu\" methods that are called from the corresponding menu option:
void convert_KG_lbs()
void convert_KM_miles()
void convert_Liters_quarts()
void convert_Celsius_fahrenheit()
Each of these menu methods should:
get input from the user
pass the input to the appropriate static MetricConverter function to perform the calculation
display the result
Solution
MetricConverter.java
public class MetricConverter {
//Default constructor
public MetricConverter()
{
}
//This method will converts kgs to lbs
double KG_lbs(double kg)
{
return kg*2.20462;
}
//This method will converts kms to miles
double KM_miles(double km)
{
return km*0.621371;
}
//This method will converts liters to quarts
double Liters_quarts(double liters)
{
return liters*1.05669;
}
//This method will converts celsius to fahrenheit
double Celsius_fahrenheit(double celsius)
{
return (celsius*1.8)+32;
}
}
______________________
MetricConverterTest.java
import java.text.DecimalFormat;
import java.util.Scanner;
public class MetricConverterTest {
//Scanner object is used to get the inputs entered by the user
static Scanner sc= new Scanner(System.in); ;
//DecimalFormat class is used to format the output
static DecimalFormat df=new DecimalFormat(\"#.##\");
public static void main(String[] args) {
//Declaring the variable
int choice;
//Displaying the menu
while (true) {
System.out.println(\"\ MENU\");
System.out.println(\"------------------\");
System.out.println(\"1.KG to lbs\");
System.out.println(\"2.KM to Miles\");
System.out.println(\"3.Liters to Quarts\");
System.out.println(\"4.Celsius to Fahrenheit\");
System.out.println(\"5.Exit\");
//Getting the choice entered by the user
System.out.print(\"Enter choice :\");
choice = sc.nextInt();
//Based on the user choice the corresponding case will be executed
switch (choice) {
case 1: {
//Calling the method
convert_KG_lbs();
continue;
}
case 2: {
//Calling the method
convert_KM_miles();
continue;
}
case 3: {
//Calling the method
convert_Liters_quarts();
continue;
}
case 4: {
//Calling the method
convert_Celsius_fahrenheit();
continue;
}
case 5: {
System.out.println(\"** Program Exit **\");
break;
}
}
break;
}
}
//This method will convert the celsius to fahrenheit
private static void convert_Celsius_fahrenheit() {
MetricConverter mc=new MetricConverter();
System.out.print(\"Enter the value of Celsius (c) :\");
double celsius=sc.nextDouble();
double fah=mc.Celsius_fahrenheit(celsius);
System.out.println(celsius+\"(c) to Fahrenheit(F) :\"+df.format(fah));
}
//This method will convert the liters to quarts
private static void convert_Liters_quarts() {
MetricConverter mc=new MetricConverter();
System.out.print(\"Enter the value of Liters (li) :\");
double liters=sc.nextDouble();
double quarts=mc.Liters_quarts(liters);
System.out.println(liters+\"(li) to quarts :\"+df.format(quarts));
}
//This method will convert the kilometers to miles
private static void convert_KM_miles() {
MetricConverter mc=new MetricConverter();
System.out.print(\"Enter the value of Kilometers (km) :\");
double kms=sc.nextDouble();
double miles=mc.KM_miles(kms);
System.out.println(kms+\"(km) to miles :\"+df.format(miles));
}
//This method will convert the kgs to lbs
private static void convert_KG_lbs() {
MetricConverter mc=new MetricConverter();
System.out.print(\"Enter the value of Kilometers (kg) :\");
double kg=sc.nextDouble();
double lbs=mc.KG_lbs(kg);
System.out.println(kg+\"(kg) to lbs :\"+df.format(lbs));
}
}
______________________
Output:
MENU
------------------
1.KG to lbs
2.KM to Miles
3.Liters to Quarts
4.Celsius to Fahrenheit
5.Exit
Enter choice :1
Enter the value of Kilometers (kg) :20
20.0(kg) to lbs :44.09
MENU
------------------
1.KG to lbs
2.KM to Miles
3.Liters to Quarts
4.Celsius to Fahrenheit
5.Exit
Enter choice :2
Enter the value of Kilometers (km) :20
20.0(km) to miles :12.43
MENU
------------------
1.KG to lbs
2.KM to Miles
3.Liters to Quarts
4.Celsius to Fahrenheit
5.Exit
Enter choice :3
Enter the value of Liters (li) :20
20.0(li) to quarts :21.13
MENU
------------------
1.KG to lbs
2.KM to Miles
3.Liters to Quarts
4.Celsius to Fahrenheit
5.Exit
Enter choice :4
Enter the value of Celsius (c) :20
20.0(c) to Fahrenheit(F) :68
MENU
------------------
1.KG to lbs
2.KM to Miles
3.Liters to Quarts
4.Celsius to Fahrenheit
5.Exit
Enter choice :5
** Program Exit **
___________Thank You




