• We just launched and are currently in beta. Join us as we build and grow the community.

Converting Temperatures in Java

Gugucdm

Quest Master
G Rep
0
0
0
Rep
0
G Vouches
0
0
0
Vouches
0
Posts
142
Likes
111
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
This tutorial covers creating a calculator to convert between Celsius (centigrade), Kelvin, and Fahrenheit. The user is first given the choice of which temperature unit they would like to convert from, and then they are given the results of the conversion.

When different units are in use, things can get confusing. It is useful to have a calculator on hand for this sort of thing. Luckily, we can make one in Java that is able to do just that. Let's get right into the code.

  1. import

    java.util.Scanner

    ;

  2. public

    class

    temps
  3. {

  4. public

    static

    Scanner input =

    new

    Scanner(

    System

    .in

    )

    ;

  5. public

    static

    double

    c, k, f;

    //creates public variables for use in all methods

  6. public

    static

    void

    main(

    String

    [

    ]

    args)
  7. {

  8. System

    .out

    .println

    (

    "\n

    Please enter the number of the temperature you would like to convert from:\n

    \n

    1 - Celsius\n

    2 - Kelvin\n

    3 - Fahrenheit\n

    "

    )

    ;

  9. double

    userChoice =

    input.nextDouble

    (

    )

    ;
  10. input.nextLine

    (

    )

    ;

  11. if

    (

    userChoice ==

    1

    )

    //calls respective methods depending on user input
  12. celsius(

    )

    ;

  13. else

    if

    (

    userChoice ==

    2

    )
  14. kelvin(

    )

    ;

  15. else

    if

    (

    userChoice ==

    3

    )
  16. fahrenheit(

    )

    ;

  17. else

    //closes program if user does not enter a 1, 2, or 3
  18. {
  19. System

    .out

    .println

    (

    "That is not a valid choice. Exiting..."

    )

    ;
  20. System

    .exit

    (

    0

    )

    ;
  21. }


  22. }

  23. public

    static

    void

    celsius(

    )
  24. {

  25. System

    .out

    .print

    (

    "\n

    Enter the temperature in Celsius that you wish to convert: "

    )

    ;

  26. c =

    input.nextDouble

    (

    )

    ;
  27. k =

    c +

    273

    +

    0.15

    ;
  28. f =

    c *

    9

    /

    5

    +

    32

    ;

  29. System

    .out

    .println

    (

    "\n

    "

    +

    c +

    " Celsius is equal to "

    +

    k +

    " degrees Kelvin and "

    +

    f +

    " degrees Fahrenheit."

    )

    ;

  30. }

  31. public

    static

    void

    kelvin(

    )
  32. {

  33. System

    .out

    .print

    (

    "\n

    Enter the temperature in Kelvin that you wish to convert: "

    )

    ;

  34. k =

    input.nextDouble

    (

    )

    ;
  35. c =

    k -

    273.15

    ;
  36. f =

    c *

    9

    /

    5

    +

    32

    ;

  37. System

    .out

    .println

    (

    "\n

    "

    +

    k +

    " Kelvin is equal to "

    +

    c +

    " degrees Celsius and "

    +

    f +

    " degrees Fahrenheit."

    )

    ;
  38. }

  39. public

    static

    void

    fahrenheit(

    )
  40. {

  41. System

    .out

    .print

    (

    "\n

    Enter the temperature in Fahrenheit that you wish to convert: "

    )

    ;

  42. f =

    input.nextDouble

    (

    )

    ;
  43. c =

    (

    f -

    32

    )

    *

    5

    /

    9

    ;
  44. k =

    c +

    273

    +

    0.15

    ;

  45. System

    .out

    .println

    (

    "\n

    "

    +

    f +

    " Fahrenheit is equal to "

    +

    c +

    " degrees Celsius and "

    +

    k +

    " degrees Kelvin."

    )

    ;

  46. }

  47. }

As you can see, there is a decent amount of code dealt with here. However, it is nicely organized into four separate methods. This is where separate methods begin to really come in handy for organization. We have a main method, where the user makes their selection, and then three other methods for performing calculations; the method that is called depends upon the user's choice.

It is always useful to avoid errors to add code for what wouldn't be considered a valid input. In this case, we have
  1. else
  2. {
  3. System

    .out

    .println

    (

    "That is not a valid choice. Exiting..."

    )

    ;
  4. System

    .exit

    (

    0

    )

    ;
  5. }

This is the final else, only executing if neither the if nor the else ifs were satisfied.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top