Reputation: 45
My assignment is to make a simple Celsius to Fahrenheit or Fahrenheit to Celsius converter. My lab teacher kind of walked me through most of it, but did a bad job at explaining things. I know what most of it does and how it interacts, and the code has 0 errors but does not properly do the conversions. I will post the code, and will //comment on the parts that I donot understand, but what I really need is for someone to explain the parts that I do not understand, and it'd be awesome to fix the script so that it performs properly. I will comment on the parts I do not understand.
import java.util.Scanner;
public class ExerciseOne
{
public static void main( String[] args )
{
int choice;
System.out.println( "What would you like to do? \n 1 - Fahnrenheit to Celsius \n 2 - Celsius to Fahrenheit \n 3 - Quit" );
Scanner dylan = new Scanner(System.in);
choice = dylan.nextInt();
if (choice == 1)
{
System.out.println( "What number do you want to convert to Celsius?" );
float input = dylan.nextFloat();
float output = ftoC(input); // I'm kind of confused as to why she told me to put the variable inside of the ();
System.out.println(ftoC(input)); //Same here with the variables :(
}
if (choice == 2)
{
System.out.println( "What number do you want to convert to Fahrenheit?" );
float input = dylan.nextFloat();
float output = ctoF(input);
System.out.println(ctoF(output));
}
if (choice == 3)
{
System.out.println( "Exiting application.");
}
}
public static float ftoC(float f) //This is a method line, but why is float f inside the parenthesis?
{
float celsius = (f-32)*(5/9);
return celsius;
}
public static float ctoF(float c) //Same thing goes for here
{
float fahrenheit = (c)*(9/5) + 32;
return fahrenheit;
}
}
Upvotes: 0
Views: 2684
Reputation: 425083
Taking one question at a time...
float output = ftoC(input); // I'm kind of confused as to why she told me to
put the variable inside of the ();
Here ftoC
is a method and input
is being passed to it
System.out.println(ftoC(input)); //Same here with the variables :(
You are calling the method again. Pointless. Just print the result from the previous call:
System.out.println(output);
Further, I would change these lines:
float input = dylan.nextFloat();
float output = ftoC(input);
System.out.println(output);
To just:
System.out.println(ftoC(dylan.nextFloat()));
You don't need those local variables, just pass each return value straight into the next method.
public static float ftoC(float f) //This is a method line, but why is float f inside the parenthesis?
Parameters (if any) are declared in brackets, as a type followed by a parameter name. Here we have a float
parameter called f
But there are several bugs. In java, integer division results in an integer result, which means truncating the non-whole number part, so 5/9
is the int
0
. But to fix it, just remove the brackets!:
public static float ftoC(float f) {
return (f-32)*5/9;
}
By removing the brackets, the problem is fixed because in java, if one of the operands is float
and the other int
, the result is float
. This calculation first subtracts an int
from a float
- giving a float
. It then multiplies by the int
5
- giving a float
. It then divides by the int
9
- giving a float
!
Notice too that I didn't bother with the local variable - just return the calculation. Less code is good.
Upvotes: 2
Reputation: 298948
For starters this doesn't do what you expect:
(f-32)*(5/9);
5/9 is performed as an integer division, so it equates to 0. anything multiplied by 0 is also 0. You need to cast the 5 and / or the 9 to float:
(f-32)*(5f/9f);
Upvotes: 1
Reputation:
float output = ftoC(input); // I'm kind of confused as to why she told me to put the variable inside of the ();
This is so you can pass the variable to the function ftoC
.
System.out.println(ftoC(input)); //Same here with the variables :(
Same as above. How else will the data get to the function or method?
public static float ftoC(float f) //This is a method line, but why is float f inside the parenthesis?
Because you have to tell the program what your data type is so it knows how to lay it out in memory. Otherwise, it has no idea how big you want your data type to be in memory. Also, if you were to pass an int
to a method that expects float
, the program would exhibit different behavior.
Upvotes: 1