Celsius to fahrenheit table

The problem

The formula for converting celsius temperature to fahrenheit is °C x 9/5 + 32 = °F. Write a program that displays temps up to 100 degrees fahrenheit.

Breaking it down

create a method that convert fahrenheit to celsius

/**
 * The celsius method converts a Fahrenheit temperature to Celsius.
 *
 * @param fahrenheit The Fahrenheit temperature.
 * @return The equivalent Celsius temperature.
 */
static double celsius(double fahrenheit) {
    return ((5.0 / 9.0) * (fahrenheit - 32));
}

Display table header

System.out.println("Fahrenheit\tCelsius");
System.out.println("====================");

Create a loop from 0 to 100

for (int x = 0; x <= 100; x++) {
    System.out.print(x);
    System.out.print("\t\t");
    System.out.print(celsius(x));
    System.out.println();
 }

Output

Fahrenheit  Celsius
====================
0       -17.77777777777778
1       -17.22222222222222
2       -16.666666666666668
3       -16.11111111111111
4       -15.555555555555557
5       -15.0

...

96      35.55555555555556
97      36.111111111111114
98      36.66666666666667
99      37.22222222222222
100     37.77777777777778

Unit tests

@Test
public void test_celsius () {
    assertEquals(35, CelsiusToFahrenheitTable.celsius(95), 0);
}

Level Up

  • ask user to input temp in fahrenheit and display celsius
  • tidy up formatting 1) format celsius output 2) look at the column header