Java Software Solutions > PP2.4 > Solution

Problem:

Create a version of the TempConverter application to convert
from Fahrenheit to Celsius. Read the Fahrenheit temperature
from the user.

Output:

Enter a Fahrenheit temperature: 45
Celsius Equivalent: 7.222222222222222

Solution:

import java.util.Scanner;

public class TempConverter2
{
   //-----------------------------------------------------------------
   //  Computes the Celsius equivalent of a Fahrenheit value read
   //  from the user. Uses the formula C = (5/9)(F - 32).
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      final int BASE = 32;
      final double CONVERSION_FACTOR = 5.0 / 9.0;

      double celsiusTemp, fahrenheitTemp;
   Scanner scan = new Scanner(System.in);

      System.out.print ("Enter a Fahrenheit temperature: ");
      fahrenheitTemp = scan.nextDouble();

      celsiusTemp = CONVERSION_FACTOR * (fahrenheitTemp - BASE);

      System.out.println ("Celsius Equivalent: " + celsiusTemp);
   }
}

1 comment:

  1. function start1() {
    setInstrument('violin');
    Set();
    Set();
    }


    function Pie_1() {
    play(0.25, 7);
    play(0.25, 8);
    play(0.25, 9);
    play(0.25, 7);
    }

    function Pie_4() {
    play(0.25, 7);
    play(0.25, 4);
    play(0.5, 7);
    }

    function start2() {
    setInstrument('piano');
    Rest();
    Set();
    Set();
    }


    function Set() {
    Pie_1();
    Pie_1();
    Pie_2();
    Pie_2();
    Pie_3();
    Pie_3();
    Pie_4();
    Pie_4();
    }

    function Rest() {
    rest(1);
    rest(1);
    }

    function Pie_2() {
    play(0.25, 9);
    play(0.25, 10);
    play(0.5, 11);
    }

    function start3() {
    setInstrument('trumpet');
    Rest();
    Rest();
    Set();
    Set();
    }


    function Pie_3() {
    play(0.125, 11);
    play(0.125, 12);
    play(0.125, 11);
    play(0.125, 10);
    play(0.25, 9);
    play(0.25, 7);
    }

    function start4() {
    setInstrument('drum');
    Rest();
    Rest();
    Rest();
    Set();
    Set();
    }

    ReplyDelete