In: Computer Science
1. Chapter 5, Programming Challenge #8, Conversion Program (page 314). Program Name: FinalExamConversion. Write a program that asks the user to enter a distance in meters. The program will then present the following menu of selection:
1. Convert to Kilometers
2. Convert to Inches
3. Convert to Feet
4. Quit the Program The program will convert the distance to kilometers, inches or feet, depending on the user’s selection.
Write the following methods:
• getInput: This method prompts user to enter a distance in meters. Returns input to the caller.
• TestData: Cannot accept negative numbers.
• Menu: This method does not accept any arguments, but returns a selection to the caller.
• Convert2Kilometers: This method receives a parameter, converts to kilometers (meters * 0.001) and returns the value to the caller.
• Convert2Inches: This method receives a parameter, converts to inches (meters * 39.37) and returns the value to the caller.
• Convert2Feet: This method receives a parameter, converts to feet (meters * 3.281) and returns the value to the caller.
• DisplayData: This method receives the input and the converted value.
See p. 314 in Tony Gaddis - Starting Out with Java_ From Control Structures through Objects-Pearson (2015) for other methods.
HINT: Use switch statement for menu selection! Test Data: see example given in the book. Use the same format.
Submit:
1. Algorithm
2. A complete Java Program with comments/documentation
3. Output
Code:
import java.lang.Math; // headers MUST be above the first class
import java.util.Scanner;
// one class needs to have a main() method
public class HelloWorld
{
public static double getInput()
{
System.out.println("Enter the distance in meters");
Scanner sc=new Scanner(System.in);
double dist = sc.nextDouble();
return dist;
}
public static boolean test(double no)
{
if(no<0)
return false;
else
return true;
}
public static double convert2km(double no)
{
return no*0.001;
}
public static double convert2inch(double no)
{
return no*39.37;
}
public static double convert2feet(double no)
{
return no*3.281;
}
public static void displayvalue(double in,double output)
{
System.out.println("value in meters:"+in);
System.out.println("desired output:"+output);
}
public static double menu()
{
System.out.println("Enter your choice:");
System.out.println("1)convert to kilometer:");
System.out.println("2)convert to inch:");
System.out.println("3)convert to feet:");
System.out.println("4)quit the program");
Scanner sc=new Scanner(System.in);
double choice = sc.nextDouble();
return choice;
}
// arguments are passed using the text field below this editor
public static void main(String[] args)
{
double dist =getInput();
if(test(dist)==false)
System.out.println("invalid value");
double ch = menu();
if(ch==1)
{
displayvalue(dist,convert2km(dist));
}
else if (ch==2)
displayvalue(dist,convert2inch(dist));
else if(ch==3)
displayvalue(dist,convert2feet(dist));
else
return ;
}
}
//*******************************************************************
// Dear CompileJava users,
//
// CompileJava has been operating since 2013 completely free. If you
// find this site useful, or would otherwise like to contribute, then
// please consider a donation (link in 'More Info' tab) to support
// development of the new CompileJava website (stay tuned!).
//
// Most sincerely, Z.
//*******************************************************************
import java.lang.Math; // headers MUST be above the first class
import java.util.Scanner;
// one class needs to have a main() method
public class HelloWorld
{
public static double getInput()
{
System.out.println("Enter the distance in meters");
Scanner sc=new Scanner(System.in);
double dist = sc.nextDouble();
return dist;
}
public static boolean test(double no)
{
if(no<0)
return false;
else
return true;
}
public static double convert2km(double no)
{
return no*0.001;
}
public static double convert2inch(double no)
{
return no*39.37;
}
public static double convert2feet(double no)
{
return no*3.281;
}
public static void displayvalue(double in,double output)
{
System.out.println("value in meters:"+in);
System.out.println("desired output:"+output);
}
public static double menu()
{
System.out.println("Enter your choice:");
System.out.println("1)convert to kilometer:");
System.out.println("2)convert to inch:");
System.out.println("3)convert to feet:");
System.out.println("4)quit the program");
Scanner sc=new Scanner(System.in);
double choice = sc.nextDouble();
return choice;
}
// arguments are passed using the text field below this editor
public static void main(String[] args)
{
double dist =getInput();
if(test(dist)==false)
System.out.println("invalid value");
double ch = menu();
if(ch==1)
{
displayvalue(dist,convert2km(dist));
}
else if (ch==2)
displayvalue(dist,convert2inch(dist));
else if(ch==3)
displayvalue(dist,convert2feet(dist));
else
return ;
}
}