Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a public class UnitConverter with static methods milesToKilometers, kilom

ID: 3698821 • Letter: C

Question

Create a public class UnitConverter with static methods milesToKilometers, kilometersToMiles, poundsToKilograms, and kilogramsToPounds.

Each method has a double parameter and returns a double value.

Each method performs a conversion using the parameter and a conversion formula, and returns the converted value. Your conversion factor MUST have 3-4 places past the decimal point. You can find the formulas here:

http://images.slideplayer.com/18/5674766/slides/slide_46.jpg (Links to an external site.)Links to an external site.

Be sure to test your methods with correct and incorrect values (including zero and negative numbers). No conversion should be done if the parameter is negative. Each method must return zero if the parameter is invalid.

NO INPUT OR OUTPUT SHOULD BE DONE IN THESE METHODS.

Create a progam UnitConverterTest that displays a menu allowing the user to perform different conversions, or quit the program.
Do the following in the UnitConverterTest program:

Use a do-while loop for the program. If the user enters an incorrect menu choice, just display the menu again. The program must keep running until the user chooses quit.

Create a static method getInput to prompt for and read in the input. Be sure to validate the input before returning it. There should be NO negative values allowed. Use a do-while loop for validation. The method should look like this:

              public static double getInput(String type)

To get miles, for example, you would call the method with the argument “miles”. Use the type in the prompt. The method will return the validated user input.

Use a switch statement for the menu choice. In each case, call the getInput method to get the value, then call the converter method and display the value returned by the method with ONE place past the decimal point.

You must call the static UnitConverter methods using the class name. Do NOT create an instance (object) of the UnitConverter class.

Use the command prompt window (terminal) for ALL input and output.

Your full name must appear in the output or 1 point will be deducted. You must follow the Programming Guidelines handout. All code must be correctly indented and commented. Your program must compile in order to be graded. Do NOT use Eclipse, NetBeans, or IntelliJ for any labs or assignments. Do NOT create packages or projects; otherwise I will not be able to grade your assignment.

Sample program run:

Explanation / Answer

UnitConverter.java

import java.util.Scanner;

public class UnitConverter {

//This method will get the valid user input entered by the user

public static double getInput(String type)

{

double res;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

System.out.print("Enter no of "+type+":");

do

{

res=sc.nextDouble();

if(res<=0)

{

System.out.println("** Must be greater than or equal to zero **");

System.out.print("Re-Enter :");

}

}while(res<=0);

return res;

}

//This method will convert miles to kilometers

public static double milesToKilometers(double miles)

{

return miles*1.60934;

}

//This method will convert kilometers to miles

public static double kilometersToMiles(double kilometers)

{

return kilometers*0.621371;

}

//This method will convert pounds to kilograms

public static double poundsToKilograms(double pounds)

{

return pounds*0.453592;

}

//This method will convert kilograms to pounds

public static double kilogramsToPounds(double kilograms)

{

return kilograms*2.20462;

}

}

_________________

UnitConverterTest.java

import java.util.Scanner;

public class UnitConverterTest {

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

public static void main(String[] args) {

  

int choice;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

/* This while loop continues to execute

* until the user enters a valid choice or 5

*/

do

{

//displaying the menu

System.out.println(" Welcome to the Unit Converter.");

System.out.println("1. Convert miles to kilometers.");

System.out.println("2. Convert kilometers to miles.");

System.out.println("3. Convert pounds to kilograms.");

System.out.println("4. Convert kilograms to pounds.");

System.out.println("5. Quit the program.");

//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:{

//Converting the miles to kilograms

double miles=UnitConverter.getInput("miles");

double kms=UnitConverter.milesToKilometers(miles);

System.out.printf("%.1f miles is %.1f kilometers ",miles,kms);

break;

}

case 2:{

//Converting the kilograms to miles

double kms=UnitConverter.getInput("kilometers");

double miles=UnitConverter.kilometersToMiles(kms);

System.out.printf("%.1f kilometers is %.1f miles ",kms,miles);

break;

}

case 3:{

//Converting the pounds to kilograms

double pounds=UnitConverter.getInput("pounds");

double kgs=UnitConverter.poundsToKilograms(pounds);

System.out.printf("%.1f pounds is %.1f kgs ",pounds,kgs);

break;

}

case 4:{

//Converting the kilograms to pounds

double kgs=UnitConverter.getInput("kgs");

double pounds=UnitConverter.kilogramsToPounds(kgs);

System.out.printf("%1f kgs is %.1f pounds ",kgs,pounds);

break;

}

case 5:{

System.out.println("** PROGRAM EXIT **");

break;

}

default:{

System.out.println("** Invalid Choice **");

}

}

}while(choice!=5);

}

}

___________________

Output:


Welcome to the Unit Converter.
1. Convert miles to kilometers.
2. Convert kilometers to miles.
3. Convert pounds to kilograms.
4. Convert kilograms to pounds.
5. Quit the program.

Enter Choice :1
Enter no of miles:50
50.0 miles is 80.5 kilometers

Welcome to the Unit Converter.
1. Convert miles to kilometers.
2. Convert kilometers to miles.
3. Convert pounds to kilograms.
4. Convert kilograms to pounds.
5. Quit the program.

Enter Choice :2
Enter no of kilometers:-50
** Must be greater than or equal to zero **
Re-Enter :50
50.0 kilometers is 31.1 miles

Welcome to the Unit Converter.
1. Convert miles to kilometers.
2. Convert kilometers to miles.
3. Convert pounds to kilograms.
4. Convert kilograms to pounds.
5. Quit the program.

Enter Choice :3
Enter no of pounds:50
50.0 pounds is 22.7 kgs

Welcome to the Unit Converter.
1. Convert miles to kilometers.
2. Convert kilometers to miles.
3. Convert pounds to kilograms.
4. Convert kilograms to pounds.
5. Quit the program.

Enter Choice :4
Enter no of kgs:50
50.000000 kgs is 110.2 pounds

Welcome to the Unit Converter.
1. Convert miles to kilometers.
2. Convert kilometers to miles.
3. Convert pounds to kilograms.
4. Convert kilograms to pounds.
5. Quit the program.

Enter Choice :5
** PROGRAM EXIT **

____________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote