I need to convert Fahrenheit to Celsius in a Java program with methods, a for loop statement, and no user input. It is supposed to display Fahrenheit temperatures 0 through 20 and it's Celsius conversions. Any solutions?
import java.util.Scanner;
public class celsiusTempTable
{
public static void main(String[] args)
{
System.out.println("Fahrenheit to Celsius Conversion Table");
double tempC = celsiusConversion(tempC);
int tempF = fahrenheit(tempF);
displayData(tempF, tempC);
}
public static int fahrenheit(int F)
{
for(F = 0; F <= 20; F++)
{
return F;
}
}
public static double celsiusConversion(double C)
{
Scanner input = new Scanner(System.in);
for(int F = 0; F <= 20; F++)
{
C = (5.0/9.0) * (F - 32);
return C;
}
}
public static void displayData(int F, double C)
{
for(F = 0; F <= 20; F++)
{
System.out.println("\nThe temperature in Fahrenheit is: " + F);
System.out.println("The temperature in Celsius is: " + C);
}
}
}
I'll only give hints.
Without user input. So forget about using a Scanner and System.in.
You need to understand what method arguments and method return values are. Arguments are usually the inputs of a method. And the return value is the output of the method. You're being asked to translate a temperature in fahrenheit degrees to a temperature in celsius degrees. This is a perfect situation where a method is useful. The input of the translation method is thus a unique integer value (the temperature in fahrenheit degrees), and the output is another single integer value (the temperature in celsius degrees).
You must do that 21 times. Once with 0 as input, once with 1 as input, etc. until 20. This means you need a loop, and that at each iteration, you will translate the current temperature (0, 1, 2, etc.) into celsius degrees by calling the translation method, and print the result. Read your text book about for loops. This part should be in the main method.
A quick search and have found the following:
Error: celsius cannot be resolved to a variable
Java Fahrenheit to Celsius loop (with methods)
Fahrenheit to Celsius conversion
Celsius/Fahrenheit conversion error
Related
My program so far:
import java.util.Scanner;
public class Temperature
{
public static void main(String[] args)
{
int count = 20;
double fahrenheit;
double celsius;
String input;
// Scanner object for keyboard input.
Scanner kb = new Scanner(System.in);
// Get the information.
System.out.print("Enter your starting temperature in Fahrenheit: ");
fahrenheit = kb.nextDouble();
// Display the results in a table.
System.out.println("Fahrenheit Celsius");
System.out.println("-----------------------");
for (fahrenheit = fahrenheit; fahrenheit <= count; fahrenheit += 5)
{
// Calculate
celsius = (fahrenheit - 32)*(5.0 / 9.0);
System.out.printf("%,.2f \t\t\t %,.2f\n", fahrenheit, celsius);
}
}
}
Now what I need it do is to produce a table of 20 temperature conversions from Fahrenheit to Celsius.
If the User entered 0, here is a sample of the first 3 lines of what your output could look like:
FAHRENHEIT: 0 CELSIUS: -17.78
FAHRENHEIT: 5 CELSIUS: -15.00
FAHRENHEIT: 10 CELSIUS: -12.22
etc...
The problem is that it won't loop the correct amount of times if the input is more than 20.
Here is an example of what would happen if the user input 5:
Output:
Enter your starting temperature in Fahrenheit: 5
Fahrenheit Celsius
5.00 -15.00
10.00 -12.22
15.00 -9.44
20.00 -6.67
And then that is where the output ends.
Does anyone know how I can make it so that the program will display 20 F to C equivalents no matter what the user inputs?
Your for loop is all messed up: for (fahrenheit = fahrenheit; fahrenheit <= count; fahrenheit += 5) it is comparing tempratures to a count (fahrenheit <= count).
If you always want 20 steps then something like:
for (int i = 0; i < count; i++) {
...
fahrenheit += 5;
}
would do it.
Option 1, add a counter and use it.
for (int i = 0; i < count; i++, fahrenheit += 5)
{
// Calculate
celsius = (fahrenheit - 32)*(5.0 / 9.0);
System.out.printf("%,.2f \t\t\t %,.2f\n", fahrenheit, celsius);
}
Option 2, decrement count (and test against zero) while you loop.
for (; count > 0; count--, fahrenheit += 5)
{
// Calculate
celsius = (fahrenheit - 32)*(5.0 / 9.0);
System.out.printf("%,.2f \t\t\t %,.2f\n", fahrenheit, celsius);
}
What you need to do is keep a separate index that starts at 0 and goes up to 20. That means that there would be 2 variables being changed each time, index and fahrenheit, and the for-loop would look as follows:
int index = 0
for (fahrenheit = fahrenheit; index < count; fahrenheit += 5) {
...
index++;
}
I am very new to Java and I have to write a program that takes from -40 to 120 Fahrenheit and convert it to Celsius and then display them in two columns with increments of 5 and accurate to one decimal place. I have most of the code finished and I have the right output for the Fahrenheit but I can't figure out how to get the Celsius to print accurate to one decimal place. Anything would be helpful!
package 3;
public class prog2 {
public static void main(String[] args) {
System.out.println("Fahrenheit to Celsius converter from -40F below to 120F");
for(double temp = -40.0; temp <= 120; temp += 5)
{
System.out.printf("%10.1f", temp);
double sum = (temp -32) * (5.0/9.0);
System.out.printf("%5d",(int) sum );
System.out.println();
}
}
}
Change the printing of sum to this:
System.out.printf(" %10.1f", sum );
Formatting is same as for the first column and don't cast the calculated value to int.
you can use DecimalFormat class by sending in the constructor a formating string. In your exemple '#' means one digits and the '.' means decimal point.
check this link for more https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html
you can do something like that :
public static void main(String[] args) {
System.out.println("Fahrenheit to Celsius converter from -40F below to 120F");
DecimalFormat df = new DecimalFormat("###.#");
for (double temp = -40.0; temp <= 120; temp += 5) {
System.out.printf("%sC°", df.format(temp));
double sum = (temp - 32) * (5.0 / 9.0);
System.out.printf("= %sF°", df.format(sum));
System.out.println();
}
}
This question already has answers here:
Division in Java always results in zero (0)? [duplicate]
(3 answers)
Closed 7 years ago.
This is a strange issue:
So when I choose 1 for Farenheit it will prompt me to enter the Farenheit value.
It will then go the FarenHeitToCelcius method. For some reason the celcius is always returning 0.0
Any kind of help would be greatly appreciated
package Exercises;
import java.util.Scanner;
public class TemperatureCOnversion {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Enter 1 to return Celcius or 2 for Farenheit: ");
System.out.println("Your CHOICE: ");
int choice = input.nextInt();
switch(choice){
case 1:
System.out.println("Enter the Fareheit for celcius calculation: ");
double farenheit = input.nextDouble();
System.out.println(farenheit);
double celcius_result = FarenHeitToCelcius(farenheit);
System.out.println(celcius_result);
break;
case 2:
System.out.println("Enter the Celcius for Farenheit calculation: ");
double celcius1 = input.nextDouble();
double farenheit_result = CelciusToFarenheit(celcius1);
break;
default:
System.out.println(" **********Invalid Entry ******");
break;
}
}
public static double FarenHeitToCelcius(double f){
//double celcius = 0;
double celcius = (5/9)*(f -32);
System.out.println(" The equivalent celcius temperature is: " + celcius);
return celcius;
}
public static double CelciusToFarenheit(double c){
double farenheit =( (9/5*c) + 32);
System.out.println(" The equivalent fareheit temperature is: " + farenheit);
return farenheit;
}
}
5/9 will do integer division, which returns 0. You need to cast one of these integers to double, like
double celcius = ((double)5/9)*(f -32);
or
double celcius = (5.0/9)*(f -32);
This will first convert 5 to a double, and then divide this double to 9, which gives the correct result of the division.
In the CelciusToFarenheit method, there is no problem, since, in the line:
double farenheit =( (9/5*c) + 32);
9/5*c is evaluated as 9/(5*c), where c is a double, so 5*c first returns a double and then 9 is divided to this double. So there is no integer division there.
I think (5/9) results 0 (both numbers are integer – result is integer), replace it with (5.0 / 9.0) or (5d / 9d).
The Problem lies in this line:
double celcius = (5/9)*(f -32);
5/9 will be interpreted as int 5 /int 9 - it will always return 0 since 9 does fit in 5 0 times.
since you want to operate with double, this would work:
double celcius = (5.0 / 9.0) * (f - 32);
I need to create a program that will output a formatted two column list converting Celsius to Fahrenheit ending at the temperature 40 degrees from the starting temperature, which is entered by the user. It's supposed to look like this: (except it counts up from whatever the user enters as Celsius)
I've been working at this for hours and I have no idea how to fix it. Not only is the Celsius starting at 1 (which I think is that (cel=1;) bit), but I have no idea why Fahrenheit is not calculating correctly.
Here's my current source:
import java.util.Scanner;
public class TempTable {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
float cel;
double column;
double Fahrenheit;
final double C_2_F = (9.0 / 5.0);
System.out.println("Enter your city's temperature in Celsius.");
cel = kb.nextFloat();
System.out.println("Here is the conversion from " + cel);
System.out.printf("%2s%12s%n", "Celcius", "Fahrenheit");
for (cel = 1; cel <= 10; cel++) {
column = cel;
for (Fahrenheit = 1; Fahrenheit <= 10; Fahrenheit++) {
Fahrenheit = cel * C_2_F + 32;
System.out.printf("%2.0f%12.0f%n", cel, Fahrenheit);
}
}
kb.close();
}
}
Fix your loop to not reset 'Fahrenheit' each time through.
for ( Fahrenheit = 1; Fahrenheit <=10; Fahrenheit++) {
Fahrenheit = cel * C_2_F + 32;
System.out.printf("%2.0f%12.0f%n",cel,Fahrenheit);
}
To
for ( Fahrenheit = 1; Fahrenheit <=10; Fahrenheit++) {
double f = cel * C_2_F + 32;
System.out.printf("%2.0f%12.0f%n",cel,f);
}
Would be one way. Also rename 'Fahrenheit' to 'fahrenheit'
Actually, that's only one issue. That's just going to print out the same value 10 times since 'cel' and 'C_2_F' don't change during the loop...
I have to convert a temperature in degrees Celsius to Fahrenheit. However when I print the temperature in Celsius I get the wrong answer ! Please help ! (The formula is c = (5/9) * (f -32). When I type in 1 for degrees farenheit I get c = -0.0. I have no idea what is wrong :s
Here is the code
import java.io.*; // import/output class
public class FtoC { // Calculates the temperature in Celcius
public static void main (String[]args) //The main class
{
InputStreamReader isr = new InputStreamReader(System.in); // Gets user input
BufferedReader br = new BufferedReader(isr); // manipulates user input
String input = ""; // Holds the user input
double f = 0; // Holds the degrees in Fahrenheit
double c = 0; // Holds the degrees in Celcius
System.out.println("This program will convert the temperature from degrees Celcius to Fahrenheit.");
System.out.println("Please enter the temperature in Fahrenheit: ");
try {
input = br.readLine(); // Gets the users input
f = Double.parseDouble(input); // Converts input to a number
}
catch (IOException ex)
{
ex.printStackTrace();
}
c = ((f-32) * (5/9));// Calculates the degrees in Celcius
System.out.println(c);
}
}
You are doing integer division, and hence 5 / 9 will give your 0.
Change it to floating point division: -
c = ((f-32) * (5.0/9));
or, do the multiplication first (Remove the brackets from division): -
c = (f-32) * 5 / 9;
Since, f is double. Numerator will be double only. I think this way is better.
use this rather:
c = (int) ((f-32) * (5.0/9));// Calculates the degrees in Celcius
as it involves division and you should not use only ints to get proper division
Use this
System.out.println((5F / 9F) * (f - 32F));
You should try using double instead of int as this would lead to loss of precision. Instead of using the whole formula, use one calculation at one time
Example: Use appropriate casting
Double this = 5/9
F - Double 32
Unless explicitly specified otherwise, Java treats all numbers as integers. Since integers cannot store fractional parts of numbers, when integer division is performed, the remainder is discarded. Therefore: 5/9 == 0
Rohit's solution c = (f-32) * 5 / 9; is probably the cleanest (though the lack of explicit types may cause a bit of confusion).