Demonstrate programming fundamentals – decisions, loops, arithmetic

 

Objectives

  1. Demonstrate programming fundamentals –  decisions, loops, arithmetic operations, arrays, use of objects,  constructing and using methods, parameter passing, etc.
  2. Demonstrate console I/0
  3. Demonstrate text file input
  4. Demonstrate ability to follow a program specification

Outcome

  1. Produce a program that converts temperature values between 4 different scales -Fahrenheit, Celsius, Kelvin, Rankine.
  2. A  starting program (TemperatureConversion.java) with required methods to  complete is provided below. No credit is given if this starting file is  not used. and must be used to receive credit 

Requirements

  1. Produce the four methods defined in the provided starting program TemperatureConversion.java:

public double fahrenheitToOther (double value, char scale)

public double celsiusToOther (double value, char scale)

public double kelvinToOther (double value, char scale)

public double rankineToOther (double value, char scale)

Each  of these takes two parameters, a double value and a target scale. The  scale values will be one of these characters: ‘F’, ‘C’, ‘K’, ‘R’. For  example, fahrenheitToOther assumes that the given value is in Fahrenheit  and converts that value to the new scale and returns the converted  value.

For example, the method below shows celsiusToOther in  use. An original value and a new scale are passed to it. As the name of  the method celsiusToOther implies, the original value is of scale  Celsius. The celsiusToOther method must contain the code to convert the  given value to the given scale.

public double doConversion (double originalValue, char originalScale, char newScale)

{

double newValue = 0.0;

if (OriginalScale == CELSIUS)

  newValue = celsiusToOther(originalValue, newScale);

return newValue;

}

Do  not change the signatures of any of the methods. You are required to  use the char type to assess the problem. Use defined constants where  needed.

  1. Complete the doConversion method that is  defined in TemperatureConversion.java. Notice that it only allows for  conversion from Celsius. Decisions and calls for the other conversion  methods must be added to use the other three xxxToOther methods.  Remember that to use a class, an object of that class type must be  created.

Conversion factors for translating between the  different scales are readily available on various sites on the internet.  Use defined constants whenever possible stylistically.

3: Add console input

Modify  the main program in TemperatureConversion.java to allow for user input  of single conversions. Loop and solicit console input until a user  indicates that he/she is finished. Your program should prompt for a  value, starting scale, and target scale then perform the conversion and  output the result. 

  • Make sure that your program works for both upper and lower case input.
  • Make  sure that you can recover from user input of an incorrect scale. Your  program should continue and not terminate with incorrect alpha  characters.
  • Check for negative Kelvin and Rankine input  temperatures. Your program should continue and not terminate with the  input of a negative Rankine or Kelvin temperature.
  • Be sure to test at least three of each conversion. See the test data spreadsheet included on canvas.

4: Add File input

Prompt  for the name of a file that contains a series of conversions and  perform those conversions. The file should be a text file with the  following format:

             There is one set of conversion data per line of the file.

             Each line of the file contains an original value, an original scale, and the target scale, for example,

23 F C

18.5 K R

You  will use the text file temptest.txt, which I created in notepad. The  file is in canvas. Note that in case of an error in the file a zero is  displayed for the conversion along with a message. A complete run of the  program output is at the end of this document. You may want to test  your program with no error data and then test it with the error data to  make sure you meet the specifications.

  1. Use some Error Handling

Your  program should handle errors with a console message and return 0.0 when  any conversion is not possible. For example, if a user enters ‘V’  instead of ‘C’, a console error message is displayed and the conversion method returns 0.0

Sample Program Run

Here is the sample run of a program that uses console input and file input. User input is in green.

 Enter a number to choose one of the following options:

  1. Convert temperatures one at a time.
  2. Convert temperature from a list of value in a .txt file.
  3. End the program

1

 You selected 1

Enter temperature:  25.5

Enter scale (F, C, K, R): 

c

Enter scale to convert to (F, C, K, R): 

f

25.50 C is 77.90 F

Would you like to doanother conversion? (y/n): 

y

Enter temperature:  100

Enter scale (F, C, K, R): 

c

Enter scale to convert to (F, C, K, R): 

k

100.00 C is 373.15 K

Would you like to doanother conversion? (y/n): 

n

Enter a number to choose one of the following options:

  1. Convert temperatures one at a time.
  2. Convert temperature from a list of value in a .txt file.
  3. End the program

2

 You selected 2

Enter name of inputfile

temptest.txt

60.00 F is 288.71 K

70.00 F is 529.67 R

30.00 F is -1.11 C

50.50 C is 122.90 F

60.50 C is 333.65 K

70.50 C is 618.57 R

20.00 K is -423.67 F

30.00 K is -243.15 C

40.00 K is 72.00 R

20.50 R is 480.17 F

30.50 R is -256.21 C

40.50 R is 22.50 K

error invalid original scale specified D

0.00 D is 0.00 F

0.00 C is 32.00 F

error invalid original scale specified A

25.30 A is 0.00 K

error invalid conversion scale specified A

27.00 F is 0.00 A

54.00 F is 12.22 C

20.00 K is -253.15 C

End of file processing.

Enter a number to choose one of the following options:

  1. Convert temperatures one at a time.
  2. Convert temperature from a list of value in a .txt file.
  3. End the program

3

 You selected 3

Goodbye