Write a complete Java program for the following two problems: Chapter 5, Program
ID: 3841181 • Letter: W
Question
Write a complete Java program for the following two problems: 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 for other methods. Test Data: see example given in the book. Use the same format. Submit: 1. Takehome Exam Question Paper 2. Algorithm 3. A complete Java Program with comments/documentation 4. OutputExplanation / Answer
import java.math.BigDecimal;
import java.util.Scanner;
import java.lang.*;
public class chegg_conver{
public float getInput()
{
System.out.println("Please enter the distance in meters:");
Scanner scanner = new Scanner(System.in);
float input=scanner.nextFloat();
return input;
}
public void TestData(float input)
{
if(input<0)
{
System.out.println("Cannot accept Negative numbers");
}
}
public int Menu()
{
System.out.println("1. Convert to Kilometers");
System.out.println("2. Convert to Inches");
System.out.println("3. Convert to Feet");
System.out.println("4. Quit");
System.out.println("Enter Your Choice:");
Scanner scanner = new Scanner(System.in);
int input=scanner.nextInt();
return input;
}
public float Convert2Kilometers(float distance)
{
float result;
result=(float) (distance*0.001);
return result;
}
public float Convert2Inches(float distance)
{
float result;
result=(float) (distance*39.37);
return result;
}
public float Convert2Feet(float distance)
{
float result;
result=(float) (distance*3.281);
return result;
}
public static void main(String[] args)
{
float distance;
int choice;
chegg_conver convert=new chegg_conver();
distance=convert.getInput();
convert.TestData(distance);
choice=convert.Menu();
switch(choice)
{
case 1: System.out.println("Distance in Kilometers " + convert.Convert2Kilometers(distance));
break;
case 2: System.out.println("Distance in Inches " + convert.Convert2Inches(distance));
break;
case 3: System.out.println("Distance in Feet " + convert.Convert2Feet(distance));
break;
case 4: System.exit(0);
break;
}
}
}
Output:
Please enter the distance in meters:
10
1. Convert to Kilometers
2. Convert to Inches
3. Convert to Feet
4. Quit
Enter Your Choice:
1
Distance in Kilometers 0.01
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.