2 Replies - 3042 Views - Last Post: 28 March 2010 - 03:06 PM Rate Topic: -----

#1 Guest_Shonda*


Reputation:

Help : Creating a simple JAVA program with temperatures.

Posted 28 March 2010 - 02:20 PM

Hello forum,

I am fairly new to creating JAVA programs. It is a hard task for me, but I am trying to learn and understand the basics. I am attempting to create a simple program that converts a given temperature Fahrenheit to centigrade. To begin here is my program thus far:

 import java.util.Scanner 
  public class Temp{
  {
  public static void main (String [] args){
 //Create a Scanner
 Scanner reading = new Scanner(System.in);
 //Have the user to enter a temperature in fahrenheit
    {
    System.out.println ("Enter a temperature");


I know it is not much, but if I could get someone to pull me through the steps and assist me where to begin I believe I can understand. Problems I am having is where to begin entering in the variables, the formula and the input of numbers. Also how to concert centigrade..I know I use the expressions float number; and scanner reading. In addition, int n;. Again I do not know where to place these expressions.

Thanks in advance for all suggestions and comments.


Is This A Good Question/Topic? 0

Replies To: Help : Creating a simple JAVA program with temperatures.

#2 zim1985   User is offline

  • Grand Inquisitor
  • member icon

Reputation: 75
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Help : Creating a simple JAVA program with temperatures.

Posted 28 March 2010 - 02:49 PM

When you use the Scanner to get user input, you usually (or I do at least) do something along these lines:
// Declare a new Scanner
Scanner input = new Scanner(System.in);

// Prompt user for input
// Use "print" so user input
// appears on the sane line

System.out.print("Enter some number here:  ");

// Use the Scanner class's
// nextInt() method to assign
// a variable the inputted value.
int num = input.nextInt();


I'm going to direct you to the Scanner class in the API as a guideline for getting other types of data.

Now for your conversion. The formula for converting from Fahrenheit to Celsius is:
Celsius = 5/9 * (Fahrenheit - 32)

Remember you are using floats, so your input might need to be in float format as well.

As for declaring variables, it really all depends on how much of the program you want to access it. For example:
public class Example
{
    // Class Scope
    int num;
    public Example()
    {
        num = 0;
    }

    public void exMeth(int num2) // <-- This only has Local Scope
    {
        int num3 = num2;
        // Local Scope as well
        // If you try to access these variables elsewhere
        // You will receive an error
    }
}


I hope that clears up some of your questions.
Was This Post Helpful? 0
  • +
  • -

#3 MadScientist305   User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 44
  • Joined: 26-December 09

Re: Help : Creating a simple JAVA program with temperatures.

Posted 28 March 2010 - 03:06 PM

The Scanner class has a method called nextFloat() which will read the next float from InputStream(System.In in your case). This method returns the float read, so you'd need to store that into a variable.

To do the conversion, you'd need to define a new method that takes a float as its input, and returns another float. In that method, you do the conversion. If the input is always in Fahrenheit, then you'd assume the input to the method is in Fahrenheit,convert it to Celsius, and return that value.

Regarding where to do all this, you need to get the input from the user after the "Enter a temperature" message, and once you have the user's input, you can pass that input to the conversion method you created.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1