I'm supposed to create methods, but I don't know how to :(. All inputs and outputs should be done in the main method and calls to the methods should be done for computation. Can someone please show me how? Thanks.
I have to do it on this program:
(Actually there's more programs, but I'll just apply what ya'll post here on the other ones}

``````import java.io.*;
public class Temperature   {
public static void main (String[] args)throws IOException    {
double celsius=0.0, fahrenheit=0.0;
String TextInput;
System.out.print ("Input a temperature in Celsius: ") ;
celsius=Integer.parseInt (TextInput) ;
fahrenheit=celsius*9/5+32;
System.out.println ("Fahrenheit is: "+fahrenheit) ;
}
}``````
5
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by laupkram

So sorry, but I'm not still quite not sure how to do this.

Show.Some.Effort.

Show us what you have written so far - no one is going to code for you.

There is so many great sources to assist you in learning the Java programming language available via google.com. I'll post a sample class using a method below and explain it to the best of my ability to help set you on the right track.

Here is a simple example of using a method to add two numbers together:

``````import java.io.BufferedReader;
import java.io.IOException;

// this class is used to add two numbers together
{
public static void main(String[] args) throws IOException
{
// BufferedReader is used to read the user input from  the console

// stores the two numbers input by the user to add together
int number1, number2;

// stores the input line as a String
String input;

// ask user to input the first number
System.out.print("Input the first number: ");

// parse the first number from the input String and store it in the variable number1
number1 = Integer.parseInt(input);

// ask user to input the second number
System.out.print("Input the second number: ");

// parse the second number from the input String and store it in the variable number2
number2 = Integer.parseInt(input);

// output the sum by using the add() method!
System.out.println("The sum of the two numbers is: " + add(number1, number2));
}

// method to add two numbers together
public static int add(int num1, int num2)
{
return num1 + num2;
}
}``````

Let's take a closer look at the method declaration:

``public static int add(int num1, int num2)``

This piece of code is called the declaration. The declaration describes the methods visibility, persistence (for lack of a better word), return type, name, and parameters.

The word public represents its visibility type. There are four visibility types available: public, private, protected, and package-private. For now we will only talk about public and private. Essentially public means that any other class you make will be able to call the method inside of this class. Private is just the opposite. When the visibility type is set to private only the class and instances of the class has access to the method. For the purpose of this method we will just use public. Note that it is written in lowercase!

The second word, static, is used to describe whether the method belongs to the entire class or is independent to each instance of the class. For now just ignore this and just assume you need the word static for the method in your temperature class.

The third word, int, is the return type of the method. A method with any return type other then void will return the type of data specified. Since the add() method adds two integers together, it should return the sum of the two integers as an integer. You must always specify the return type when you create your methods! If no data is going to be returned you should use the keyword "void" (without the quotes).

The next word, add, is the word I chose to describe what the method is intended to do. This is called the method name. The method name should contain no spaces and start with a word in lowercase, and each additional word should start with an uppercase letter. Some examples: getTemperature, convert, convertToFahrenheit.

The next bit of text inside of the parenthesis are called parameters. Parameters are separated by commas and contain two parts. The first part is the data type of the parameter. Since we want to add numbers together, the data type of each number is int. The second part is the name of the parameter.

Parameters are essentially the data that the method will need in order to work. In order for our method add to add together two numbers we need to specify two numbers that it will add. I have specified these two numbers as num1 and num2. num1 and num2 will change depending on the actual numbers I specify when I call the method.

Calling a method, or making the code run in the method we defined, is simple. To add, lets say 3 and 5, we would simply type:

``add(3, 5);``

inside of the main method.

After the declaration of the method comes the method body, or the code within { and }.
In this example the method body simply calculates num1 + num2 and returns the result!

I hope this helped you a little bit, and didn't confuse you too much. Perhaps a more experienced programmer could provide a simpler/easier to understand explanation. You should definitely talk to your teacher if you continue to have trouble! Good luck!

Awesome link James, it looks like that should help get him on the right track!

Study well dude! I remember myself on you before
by the way... If you declared the variables like celsius in double be sure that you will parse the inputs in double ^_^

``````import java.io.*;
public class Temperature   {
public static void main (String[] args)throws IOException    {
double celsius=0.0, fahrenheit=0.0;
String TextInput;
System.out.print ("Input a temperature in Celsius: ") ;
celsius=Double.parseDouble (TextInput) ;
//TextInput=dataIn.readLine () ; //I removed this extra input code...
System.out.println ("Fahrenheit is: "+ reFarenheit(celsius).toString()) ;
}
public static double retFarenheit(double celsius){ //method for processing the inputs
double farenheit = celsius*9/5+32;
return farenheit;
}

}``````

test it... because Ive never compiled it yet...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.