Question

In: Computer Science

1. Chapter 5, Programming Challenge #8, Conversion Program (page 314). Program Name: FinalExamConversion. Write a program...

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

Solutions

Expert Solution

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 ;

}

}


Related Solutions

In Programming Challenge 12 of Chapter 3, you were asked to write a program that converts...
In Programming Challenge 12 of Chapter 3, you were asked to write a program that converts a Celsius temperature to Fahrenheit. Modify that program so it uses a loop to display a table of the Celsius temperatures 0–20, and their Fahrenheit equivalents. Display table on screen and it a .txt file. c++
java from control structures through objects 6th edition, programming challenge 5 on chapter 16 Write a...
java from control structures through objects 6th edition, programming challenge 5 on chapter 16 Write a boolean method that uses recursion to determine whether a string argument is a palindrome. the method should return true if the argument reads the same forward amd backword. Demonstrate the method in a program, use comments in the code for better understanding. there must be a demo class and method class also .. generate javadocs through eclipse compiler. And make a UML diagram with...
java programming write a program with arrays to ask the first name, last name, middle initial,...
java programming write a program with arrays to ask the first name, last name, middle initial, IDnumber and 3 test scores of 10 students. calculate the average of the 3 test scores. show the highest class average and the lowest class average. also show the average of the whole class. please use basic codes and arrays with loops the out put should look like this: sample output first name middle initial last name    ID    test score1 test score2...
This assignment assumes you have completed Programming Challenge 1 of Chapter 9 ( Employee and ProductionWorker...
This assignment assumes you have completed Programming Challenge 1 of Chapter 9 ( Employee and ProductionWorker Classes). Modify the Employee and ProductionWorker classes so they throw exceptions when the following errors occur: ● The Employee class should throw an exception named InvalidEmployeeNumber when it receives an employee number that is less than 0 or greater than 9999. ● The ProductionWorker class should throw an exception named InvalidShift when it receives an invalid shift. ● The ProductionWorker class should throw an...
Using the Prelude to Programming book, complete Programming Challenge 2 on page 463. Save your file...
Using the Prelude to Programming book, complete Programming Challenge 2 on page 463. Save your file as LASTNAME_FIRSTNAME_M07HW2. (worth 15 points) Create a program that allows the user to input a list of first names into one array and last names into a parallel array. Input should be terminated when the user enters a sentinel character. The output should be a list of email addresses where the address is of the following form: [email protected]. please help!!!!
3. Complete programming project 5 in Chapter 5 of the Hanly/Koffman Problem Solving & Program Design...
3. Complete programming project 5 in Chapter 5 of the Hanly/Koffman Problem Solving & Program Design in C book. All input should be read from a file and output should be written to a file. Make sure that you design a function greatest_common_divisor() which calculates and returns the greatest common divisor of two integer numbers. Also, develop functions to read data from a file and to write data to a file. Problem Statement: The greatest common divisor (gcd) of two...
Write a program that implements the FIFO, LRU, and Optimal page replacement algorithms presented in chapter...
Write a program that implements the FIFO, LRU, and Optimal page replacement algorithms presented in chapter 8 of your text. First generate a random page-reference string (this should be 20 entries long) where page numbers range from 0 to 9. Apply the random page-reference string to each algorithm, and record the number of page faults incurred by each algorithm. Implement the replacement algorithms so that the number of page frames goes from 1 to 7 and you must compute the...
Chapter 8 Programming exercise 6 "Days of each month" Original Exercise: Design a program that displays...
Chapter 8 Programming exercise 6 "Days of each month" Original Exercise: Design a program that displays the number of days in each month. The program’s output should be similar to this: January has 31 days. February has 28 days. March has 31 days. April has 30 days. May has 31 days. June has 30 days. July has 31 days. August has 31 days. September has 30 days. October has 31 days. November has 30 days. December has 31 days. The...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
Programming C: Write a program for a Rolodex of contact information (e.g., name, phone number, email)...
Programming C: Write a program for a Rolodex of contact information (e.g., name, phone number, email) implemented as a linked list. The program will ask the user to enter a new contact information, retrieve/print a person’s contact information, and to delete a person. It will maintain the linked list in alphabetical order by last name. It will also allow the user to search for a person’s contact information by last name. Assume that all last names are unique.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT