I am a beginner programmer
I have an assignment which is
Write a program that converts a Fahrenheit degree to Celsius using formula:
Celsius = (5/9)(Fahrenheit - 32)
The problem is I am always getting the same value -17.78 whatever I give value in input.
Here down there is my code !!!
package com.temperatureconversion;
import java.util.Scanner;
public class TemperatureConversion {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double F = 0.0; // Temperature in Fahrenheit
double C = 0.0; // Temperature in celsius
C = 5.0 /9 * (F - 32);
System.out.print("Enter temperature in fahrenheit: ");
F = input.nextDouble();
System.out.printf("The celsius value of %10.2f is %2.2f", F, C);
}
}
What's wrong with the above code ?
Your F value is always the same 0.0, because you're asking for its value after your calculations, so you need to move the getting of F value before doing calculation.
double F = 0.0; // Temperature in Fahrenheit
double C = 0.0; // Temperature in celsius
//ASK for value.
System.out.print("Enter temperature in fahrenheit: ");
F = input.nextDouble();
// Do your calculations.
C = 5.0 /9 * (F - 32);
Try this , ask first and then calculate
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double F = 0.0; // Temperature in Fahrenheit
double C = 0.0; // Temperature in celsius
System.out.print("Enter temperature in fahrenheit: ");
F = input.nextDouble();
C = 5.0 /9 * (F - 32);
System.out.printf("The celsius value of %10.2f is %2.2f", F, C);
}
Related
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 2 years ago.
This is the problem: Write a program that prompts for the lengths of the sides of a triangle and reports the three angles.
I have written the following code for it:
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.print("Please input length of side A: ");
int sideA = console.nextInt();
System.out.print("Please input length of side B: ");
int sideB = console.nextInt();
System.out.print("Please input length of side C: ");
int sideC = console.nextInt();
System.out.println();
System.out.println("The angle between A and B is: " + calculateAngle(sideA, sideB, sideC));
System.out.println("The angle between B and C is: " + calculateAngle(sideB, sideC, sideA));
System.out.println("The angle between C and A is: " + calculateAngle(sideC, sideA, sideB));
}
public static double calculateAngle(int a, int b, int c) {
return Math.toDegrees(Math.acos((a * a + b * b - c * c) / (2 * a * b)));
}
Here is a sample output from my code above:
Please input length of side A: 55
Please input length of side B: 22
Please input length of side C: 76
The angle between A and B is: 90.0
The angle between B and C is: 90.0
The angle between C and A is: 90.0
No matter what values I input for the sides, the only angles I ever get are 90 or 180 degrees, never the actual correct angle that can be calculated from the cosine rule. What is wrong with my code?
Just cast the calculation in Math.acos to double :
return Math.toDegrees(Math.acos((double)(a * a + b * b - c * c) / (2 * a * b)));
As you could see in comments, when computing between multiple int types, the integer arithmetic is used and then casted to double.
Also it's worth noting, that int always round down, meaning:
int i = 0.9999999; // i = 0
According to documentation they are expecting a double value as their parameter for acos method JavaSE7 Math doc
so re-arrange your code like this
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.print("Please input length of side A: ");
double sideA = console.nextDouble();
System.out.print("Please input length of side B: ");
double sideB = console.nextDouble();
System.out.print("Please input length of side C: ");
double sideC = console.nextDouble();
System.out.println();
System.out.println("The angle between A and B is: " + calculateAngle(sideA, sideB, sideC));
System.out.println("The angle between B and C is: " + calculateAngle(sideB, sideC, sideA));
System.out.println("The angle between C and A is: " + calculateAngle(sideC, sideA, sideB));
}
public static double calculateAngle(double a, double b, double c) {
return Math.toDegrees(Math.acos((a * a + b * b - c * c) / (2 * a * b)));
}
My code is supposed to take the inputted dew point and temperature data in order to produce the Relative Humidity. For example, a dew point of 46 and a temperature of 100-degrees Fahrenheit will give a relative humidity of around 16. Unfortunately, no matter what your input for these values the answer always comes out to 100.
Here is my code (not finished):
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Input the dew point and temperature to get the relative humidity
System.out.print("Enter dewpoint: ") ;
// change dewpoint into celcius
double Td = input.nextDouble() ;
Td = (5.0 / 9.0) * (Td - 32) ;
System.out.print("Enter temperature: ") ;
// change temperature to celcius
double T = input.nextDouble() ;
T = (5.0 / 9.0) * (T - 32) ;
double Rh = 100*(Math.exp((17.625 * Td) / (243.04 + Td)) / Math.exp ((17.625 * T) / (243.04 + T))) ;
System.out.print("The % Relative Humidity = " + Rh) ;
}
Please help if you can
There is nothing at all wrong with the way you calculated the humidity. But as of Java 8 you can use lambda expressions. These can be use to simplify expressions without resorting to full fledged methods. It also helps in reducing errors. Here is an example using your code.
UnaryOperator<Double> toC = f -> 5 / 9. * (f - 32);
UnaryOperator<Double> fnc = t -> Math.exp((17.625 * t) / (243.04 + t));
BinaryOperator<Double> humidity =
(t, td) -> 100 * fnc.apply(td) / fnc.apply(t);
Scanner input = new Scanner(System.in);
// Input the dew point and temperature to get the relative humidity
System.out.print("Enter dewpoint: ");
// change dewpoint into celcius
double Td = input.nextDouble();
Td = toC.apply(Td);
System.out.print("Enter temperature: ");
// change temperature to celcius
double T = input.nextDouble();
T = toC.apply(T);
double Rh = humidity.apply(T, Td);
System.out.print("The % Relative Humidity = " + Rh);
They can be combined in a variety of ways. They can also be passed to methods so your passing around a function. I would recommend you learn them as you as you continue to learn about Java.
Working on a problem and got the Fahrenheit list that I need but I don't really know how to add another list next to it and have it do the math as well. Please help, Thank you.
The Problem:
A program that generates a two-column table showing Fahrenheit temperatures from -40F to 120F and their equivalent Celsius temperatures. Each line in the table should be 5 degrees F more than the previous one. Both the Fahrenheit and Celsius temperatures should be accurate to 1 decimal place.
My Attempt:
package chapter5;
public class Assignment2 {
public static void main(String[] args) {
//T(°C) = (T(°F) - 32) × 5/9
int F = 120;
int count = 0;
int C = (F - 32) * (5/9);
while (F >= -40 && F <= 120 ){
if (F % 5 == 0){
System.out.printf("%-5d",F);
count++;
}
if (count == 1){
System.out.println();
count = 0;
}
F--;
}
}
}
/* package whatever; // don't place package name! */
package chapter5;
/* Name of the class has to be "Main" only if the class is public. */
public class Assignment2 {
public static void main(String[] args) {
//T(°C) = (T(°F) - 32) × 5/9
final String DEGREE = "\u00b0";
int F = -40;
float C = 0.0F;
while (F <= 120 ){
C = (F - 32)*(0.5556F);
System.out.printf("%d%sF \t\t\t%.1f%sC\n", F, DEGREE, C, DEGREE);
F = F + 5;
}
}
}
Output
-40°F -40.0°C
-35°F -37.2°C
-30°F -34.4°C
-25°F -31.7°C
-20°F -28.9°C
-15°F -26.1°C
-10°F -23.3°C
-5°F -20.6°C
0°F -17.8°C
5°F -15.0°C
10°F -12.2°C
15°F -9.4°C
20°F -6.7°C
25°F -3.9°C
30°F -1.1°C
35°F 1.7°C
40°F 4.4°C
45°F 7.2°C
50°F 10.0°C
55°F 12.8°C
60°F 15.6°C
65°F 18.3°C
70°F 21.1°C
75°F 23.9°C
80°F 26.7°C
85°F 29.4°C
90°F 32.2°C
95°F 35.0°C
100°F 37.8°C
105°F 40.6°C
110°F 43.3°C
115°F 46.1°C
120°F 48.9°C
Look at printf format specification to print out in a tab mode both the F and C temperatures.
Also you need to do your calculations in a float or a double.
And you can write your loop in increments of 5 instead of looping through everything and checking for %5.
I used System.out.printf to format output in eclipse console.
public static void main(String[] args) {
double F = -40;
for (; F < 120; F += 5) {
double C = (F - 32) * 5 / 9;
C = BigDecimal.valueOf(C).setScale(1,RoundingMode.CEILING).doubleValue();
System.out.printf("%-10s %-10s \n", F, C);
}
}
This program takes one Farenheit value converts it to celsium, prints both in two columns and increments farenheit value by 5 and converts it to celsium again.
It uses java.lang.BigDecimal to round double value to 1 decimal place. This is done in method setScale(1,RoundingMode.CEILING)
I just have a for loop where I decrement by five, so I don't have to check if it could be divided by five.
In my solution, I convert from fahrenheit directly before printing.
Suggested by #Scary Wombat I used %.1f%n to display the celsius temperature with one decimal line place and a line separator.
Update: Added "\u00b0F" and "\u00b0C" to add the Strings °F and °C
public static void main(String[] args) {
//T(°C) = (T(°F) - 32) × 5/9
for (int F = 120;F >= -40; F-=5){
System.out.printf("%-3d\u00b0F %.1f\u00b0C%n",F, (double) ((F - 32d) * 5/9));
}
}
How about this:
// Each Celsius degree is calculated in the cycle.
double CasDouble = (F - 32.) * (5./9.);
// The double value is rounded to an integer
int C = (int) CasDouble;
// The application prints the Fahrenheit and Celsius degrees
System.out.printf("%-5d\t%-5d",F,C);
So:
package chapter5;
public class Assignment2 {
public static void main(String[] args) {
// T(°C) = (T(°F) - 32) × 5/9
int F = 120;
int count = 0;
while (F >= -40 && F <= 120) {
if (F % 5 == 0) {
// Each Celsius degree is calculated in the cycle.
double CasDouble = (F - 32.) * (5. / 9.);
// The double value is rounded to an integer
int C = (int) CasDouble;
// The application prints the Fahrenheit and Celsius degrees
System.out.printf("%-5d\t%-5d", F, C);
count++;
}
if (count == 1) {
System.out.println();
count = 0;
}
F--;
}
}
}
I am working on a function for the quadratic formula in java eclipse mars, and when I compile the code it outputs NaN when mathematically this answer is possible and i should get 2.0 please help
import java.util.Scanner;
public class Quadradic1 {
public static void main(String[] args) {
double a;
double b;
double c;
double x;
System.out.print("Input A B C: ");
Scanner input = new Scanner(System.in);
a = input.nextDouble();
b = input.nextDouble();
c = input.nextDouble();
x = (-b + Math.sqrt(b * b + 4 * a * c))/(2 * a);
System.out.println("Quadratic1 " + x);
}
}
Sorry the values I entered are a=1 b=2 and c=-8
For your inputs
b * b + 4 * a * c evaluates to -28. There is not such thing as the square root of a negative number
Here is my code.
import java.util.Scanner;
import java.util.ArrayList;
public class SysQ {
public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in);
System.out.print("Enter A: ");
double a = keyboard.nextDouble();
System.out.print("Enter B: ");
double b = keyboard.nextDouble();
System.out.print("Enter C: ");
double c = keyboard.nextDouble();
System.out.print("Enter X: ");
double x = keyboard.nextDouble();
System.out.print("Enter Y: ");
double y = keyboard.nextDouble();
System.out.print("Enter Z: ");
double z = keyboard.nextDouble();
double fixedequation2 = x * b;
double fixedequation3 = x * c;
double fixedequation5 = a * y;
double fixedequation6 = a * z;
double final1 = fixedequation2 - fixedequation5;
double final2 = fixedequation3 - fixedequation6;
double yanswer = final2/final1;
double x1 = c - (b * yanswer);
double xanswer = x1/a;
System.out.println("X is " + xanswer);
System.out.println("Y is " + yanswer);
So the code works for most numbers for a 2 x 2 equation, but when I put in the values 5,5,5,5,5,5 for a,b,c,x,y,z, I get "NaN" I just want to know does "NaN" mean infinitely many solutions or No Solution..
NaN means "not a number". You get that solution because you are doing
(25-25)/(25-25) = 0/0, the result of which is undefined, or "Not a number"