CISS-110 Project 7
Write a Java program to continue creating your user-defined methods and introduce some do-while
loops.
Learning outcomes
- Implementing user-defined methods.
- Working with
do
–while
loops. - Working with data validation.
- Confirmation program produces desired results.
Write two value-returning methods called farToCel()
and celToFar()
. These two methods will convert temperatures from Fahrenheit to Celsius and Celsius to Fahrenheit. They will each take a single int
parameter and return the converted value as an int
.
Write an additional value-returning method called displayMenu()
to display a three-item menu and read input from the user, which is their selection, and return that as a char
. Consider using the Character
class method toUpperCase()
to narrow the number of choices from six {F, f, C, c, Q, q} to three {F, C, Q}.
Note that displayMenu()
has no parameters. The displayMenu()
method MUST only return valid values.
Start by calling displayMenu()
within a do-while
loop in the main()
method, which will capture the return value of displayMenu()
into a char
variable. This value will determine the conversion type, and the loop will terminate when the user enters ‘Q’.
The other do-while loop is in the displayMenu()
method. We would stay in the displayMenu()
method until the user chooses a valid selection. By doing so, this method can never return bad selections to the main()
method.
Once you have mastered your displayMenu()
method, you can proceed to the other two. Also, note that displayMenu()
is not responsible for reading the temperatures; the main()
method will do this based on the choice made by the user.
The calculation for Fahrenheit to Celsius is:
( f - 32 ) * 5 / 9
The calculation for Celsius to Fahrenheit is:
c * 9 / 5 + 32
Your output should resemble:
Please select one of the following: F - To convert Fahrenheit to Celsius C - To convert Celsius to Fahrenheit Q - To Quit. Choice: f Enter the Fahrenheit temperature: 32 The Temperature 32 Fahrenheit is 0 Celsius. Please select one of the following: F - To convert Fahrenheit to Celsius C - To convert Celsius to Fahrenheit Q - To Quit. Choice: c Enter the Celsius temperature: -40 The Temperature -40 Celsius is -40 Fahrenheit. Please select one of the following: F - To convert Fahrenheit to Celsius C - To convert Celsius to Fahrenheit Q - To Quit. Choice: q
Submit the project to the Learning Management System as Project7_lastname.java.