import java.util.Scanner;
public class tempconverter {
public static void main(String[] args) {
final int Base = -32;
final double CONVERSION_FACTOR = 5.0 / 9.0;
int farenheit;
int CelsiusTemp;
Scanner scan = new Scanner (system.in);
System.out.print("Enter the farenheit temperature: ");
farenheit = scan.nextInt();
CelsiusTemp = farenheit * CONVERSION_FACTOR + Base;
System.out.println("Celsius Equivalent: " + CelsiusTemp);
}
}
temp converisonHi, Im trying to create a program that asks the user to enter a code i
Page 1 of 1
5 Replies - 316 Views - Last Post: 19 November 2009 - 06:05 PM
#1
temp converison
Posted 18 November 2009 - 07:03 PM
Replies To: temp converison
#2
Re: temp converison
Posted 18 November 2009 - 07:04 PM
Put your question in the body of the post please.
#3
Re: temp converison
Posted 18 November 2009 - 07:07 PM
KYA, on 18 Nov, 2009 - 06:04 PM, said:
Put your question in the body of the post please.
sorry im still getting used to this site. but when I try to run this code i keep getting this error: Exception in thread "main" java.lang.Error: Unresolved compilation problems:
system cannot be resolved
Type mismatch: cannot convert from double to int
at tempconverter.main(tempconverter.java:14)
and i have no clue how to resolve it
#4
Re: temp converison
Posted 18 November 2009 - 08:06 PM
CelsiusTemp = farenheit * CONVERSION_FACTOR + Base;
CelsiusTemp is an int
CONVERSION_FACTOR a double
In Java if the left side of a = contains one double variable all operations are "upgraded to double
you cannot put a double into a int
let say:
int x;
double y = 1.0E100;
x = y; // you cannot store 1000000000000000000000...00000 a hundred times into a int
now if you know for sure that your double is convertibele to a int (behing in the int range) you can always cast
CelsiusTemp = (int) farenheit * CONVERSION_FACTOR + Base;
CelsiusTemp is an int
CONVERSION_FACTOR a double
In Java if the left side of a = contains one double variable all operations are "upgraded to double
you cannot put a double into a int
let say:
int x;
double y = 1.0E100;
x = y; // you cannot store 1000000000000000000000...00000 a hundred times into a int
now if you know for sure that your double is convertibele to a int (behing in the int range) you can always cast
CelsiusTemp = (int) farenheit * CONVERSION_FACTOR + Base;
#5
Re: temp converison
Posted 19 November 2009 - 01:54 PM
pbl, on 18 Nov, 2009 - 07:06 PM, said:
CelsiusTemp = farenheit * CONVERSION_FACTOR + Base;
CelsiusTemp is an int
CONVERSION_FACTOR a double
In Java if the left side of a = contains one double variable all operations are "upgraded to double
you cannot put a double into a int
let say:
int x;
double y = 1.0E100;
x = y; // you cannot store 1000000000000000000000...00000 a hundred times into a int
now if you know for sure that your double is convertibele to a int (behing in the int range) you can always cast
CelsiusTemp = (int) farenheit * CONVERSION_FACTOR + Base;
CelsiusTemp is an int
CONVERSION_FACTOR a double
In Java if the left side of a = contains one double variable all operations are "upgraded to double
you cannot put a double into a int
let say:
int x;
double y = 1.0E100;
x = y; // you cannot store 1000000000000000000000...00000 a hundred times into a int
now if you know for sure that your double is convertibele to a int (behing in the int range) you can always cast
CelsiusTemp = (int) farenheit * CONVERSION_FACTOR + Base;
vishalpiratla, on 19 Nov, 2009 - 12:49 PM, said:
pbl, on 18 Nov, 2009 - 07:06 PM, said:
CelsiusTemp = farenheit * CONVERSION_FACTOR + Base;
CelsiusTemp is an int
CONVERSION_FACTOR a double
In Java if the left side of a = contains one double variable all operations are "upgraded to double
you cannot put a double into a int
let say:
int x;
double y = 1.0E100;
x = y; // you cannot store 1000000000000000000000...00000 a hundred times into a int
now if you know for sure that your double is convertibele to a int (behing in the int range) you can always cast
CelsiusTemp = (int) farenheit * CONVERSION_FACTOR + Base;
CelsiusTemp is an int
CONVERSION_FACTOR a double
In Java if the left side of a = contains one double variable all operations are "upgraded to double
you cannot put a double into a int
let say:
int x;
double y = 1.0E100;
x = y; // you cannot store 1000000000000000000000...00000 a hundred times into a int
now if you know for sure that your double is convertibele to a int (behing in the int range) you can always cast
CelsiusTemp = (int) farenheit * CONVERSION_FACTOR + Base;
thanks man that was really helpful. now I am encountering another problem. My program runs but its not giving me the correct answer. for example when i enter 32 degrees Fahrenheit i get a response of -14 degrees Celsius. i fiddled around with the code to make it work but i couldnt figure it out. here is the updated code, see if anyone can figure out where i am going wrong.
import java.util.Scanner;
public class tempconverter {
public static void main(String[] args) {
final int Base = -32;
final double CONVERSION_FACTOR = 5.0 / 9.0;
int farenheit;
double CelsiusTemp;
Scanner scan = new Scanner (System.in);
System.out.print("Enter the farenheit temperature: ");
farenheit = scan.nextInt();
CelsiusTemp = (int)farenheit * CONVERSION_FACTOR + Base;
System.out.println("Celsius Equivalent: " + CelsiusTemp);
}
}
#6
Re: temp converison
Posted 19 November 2009 - 06:05 PM
This formula is wrong
farenheit * CONVERSION_FACTOR + Base
the Base should be multiply by 5
the original formula is
C / 5 = (F - 32) / 9
so
C = (F - 32) / 9 / 5
the Base should be subtract before the CONVERSION_FACTOR is applied
farenheit * CONVERSION_FACTOR + Base
the Base should be multiply by 5
the original formula is
C / 5 = (F - 32) / 9
so
C = (F - 32) / 9 / 5
the Base should be subtract before the CONVERSION_FACTOR is applied
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|