21 September, 2011

[TUT] Celcius To Fahrenheit Converter in Eclipse!

Note: This is my first program in java, and my first tut ever. (Yeah it's bad D:) :3

1. Create a new Java Project. (I use default settings)

[Image: jbbrtY7nU6JSoA.JPG]

2. Create 3 classes inside your project, name them like so in order -

"Celciustoferen" "Converter" and "FahrenheittoCelcius"
[Image: jOaRsZbpjXaqK.JPG]

3. Open up your Converter class and you will need to import the scanner.

Code:
import java.util.Scanner;


Then we will need to declare a bunch of things

Code:
Scanner input = new Scanner(System.in); - Our scanner to make possible inputing of info.
        Celciustoferen CelciustoferenObject = new Celciustoferen(); - Our Celciustoferen class so we can use it.
        FahrenheittoCelcius FahrenheittoCelciusObject = new FahrenheittoCelcius(); -Our FahrenheittoCelcius class to we can use it.
        String type; - A string that we will later use to determine if the user want's to convert F to C or C to F.
        double degrees; - How much F or C.

Then we need to get the info from the user
Code:
System.out.println("Enter Celcius or Fahrenheit: "); -Writes "Enter Celcius or Fahrenheit:"
        type = input.nextLine(); -Get's the input from the user using the scanner and put's it into type as a string.


System.out.println("Enter " + type); - Writes "Enter" + Whatever we got from the user input before using the scanner.
        degrees = input.nextDouble(); -Get's the number from the user using the scanner and put's it into degrees as a double.

Lastly we will need to determine what class to call. If it's celcius then call our Celciustoferen if it's fahrenheit then call FahrenheittoCelcius.
Code:
if(type.contains("c") || type.contains("C")){ - If type contain's C(Theres no C in fehrenheit and a c in celcius)
            CelciustoferenObject.Celciustoferen(degrees, degrees); - Call the Celciustoferen method inside Celciustoferen class and give it the number's we have collected.
        }else{FahrenheittoCelciusObject.FahrenheittoCelcius(degrees, degrees);} - If it doesn't contain a C it must be fahrenheit. So, call the FahrenheittoCelciusinside method inside the FahrenheittoCelcius class and give it the number's we have collected.
[Image: jbujHBOzdSIvPC.JPG]



4. Open up Celciustoferen class.


5. Make a new method that requires 2 doubles "double x, double a"

Code:
public void Celciustoferen(double x, double a){

}

6. Convert Celcius(x) to fahrenheit.
Code:
x = x * 9 / 5 + 32;

And then deliver the result to the user
Code:
System.out.println("The conversion to from "+ a + " Celcius to Fahrenheit equals to = " + x);
[Image: jbl1bOgBOBatRG.JPG]


7. Open up FahrenheittoCelcius class Make a new method that requires 2 doubles "double x, double a"

Code:
public void FahrenheittoCelcius(double x, double a){

}

Then convert Fahrenheit to Celcius.
Code:
x = x - 32 * 5 / 9;

And deliver the result to the user
Code:
System.out.println("The conversion to from "+ a + " Fahrenheit to Celcius equals to = " + x);
[Image: jVvKZOviab53c.JPG]




And you are done.


Challenges:

Make the program run over again after the result has been given.

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home