Creating a table converting from F to C with increments of 3 - java

My task is to write a program that displays a table of 20 temperature conversions from Fahrenheit to Celcius and to increment he value in 3 degrees.
This is what I made
public class FahrenheitToCelsius {
public static void main(String[] args) {
for(int f = 20; f < 78; f = f+3) {
System.out.println(f + " degrees Fahrenheit is " + (5/9) * (f-32) + " degrees celsius ");
}
}
}
Yet when I print it is keeps saying the degrees is 0 for all of them, do I have my order of operations wrong or something?

You are working with int so (5/9) * (f-32) evaluates to 0 * (f-32) because integer maths stays integer and 5/9 as an integer is zero. 5.0/9.0 = 0.55555555 which, when converted to an int becomes 0.
Switch to using float.
for (float f = 20; f < 78; f = f + 3) {
System.out.println(f + " degrees Fahrenheit is " + (5.0 / 9.0) * (f - 32) + " degrees celsius ");
}

Related

Converting celsius to fahrenheit using methods

So I am having a problem figuring out this celsius to fahrenheit with methods, Ive done tried to figure this problem out myself for days and ive thrown in the towel and need help.
Here is the code:
public class ctof {
public statice void main(string[] args) {
for (double i = 40; i >= 30; i--) {
double j = 130;
double x = celsiusToFahrenheit(0)
double y = fahrenheitToCelsius(0)
System.out.println(i + " " + (x + i) + " | " + (j + i) + " " + (y + i)
}
public static double celsiusToFahrenheit(double celsius) {
double fahrenheit = (5.0 / 9) * celsius + 32;
return fahrenheit;
}
public static double fahrenheitToCelsius(double fahrenheit) {
double celsius = (9.0 / 5) * fahrenheit - 32;
return celsius;
}
}
Note that for the numbers to decrement in the loop 1234 exe I have to group them in parentsies (lettet + letter and if I dont group them or attach them to an I they dont increment but stay stagnant.
Note 2: When I go to increment them by 10 they dont increment but only add 10 to where they start so instead of starting with 70 they do 60 or 70, not 70, 60 50... the way I need them to
When I do them in a non method everything is fine.
Can somebody tell me what im doing wrong and how to correct it, thank you.

Java Fahrenheit=Celsius Program Issue

[EDIT] #Ryan Thanks for the solution! However, now I am receiving the error "c defined in first method" when I did not redefine it in the second.
public class cf {
public static void methodOne (double c, double f) {
double c = 40;
double f;
System.out.println("Celsius Fahrenheit");
while (c >= 30) {
f = c * 9/5 +32;
System.out.println((c) + " "+Math.round(f*100.0)/100.0);
c--;
}
}
public static void methodTwo (double ce, double fa) {
double ce;
double fa = 120;
System.out.println("Fahrenheit Celsius");
while (fa >= 30) {
ce = fa * 5/9 -32;
System.out.println((fa) + " "+Math.round(ce*100.0)/100.0);
fa--;
}
}
}
Your root problem is obviously that the conversion from Celsius to Fahrenheit is implemented incorrectly within the loop. I would approach this problem by extracting the "beef" of your application, i.e. the temperature conversion (formula of which is in Wikipedia), into a method of its own:
/**
* Converts the input Celsius temperature into Fahrenheit degrees, using the
* formula:
*
* <pre>
* (degreesCelsius * 1.8) + 32 = degreesFahrenheit
* </pre>
*
* #param degreesCelsius
* temperature in Celsius degrees
* #return the temperature in Fahrenheit degrees
*/
private static float celsiusToFahrenheit(float degreesCelsius) {
return (degreesCelsius * 1.8f) + 32.0f;
}
You should separate the calculation from the rest of the code because it:
Improves the readability of your code
Separates the concerns of iterating a range and calculating the conversion, which in turn makes
your application modular
implementing changes simpler
testing the conversion simpler
reusing the conversion possible
After you have done the above, the rest of the code only handles the initialization of the range and iterating over it:
// define the range
final int cMin = 30;
final int cMax = 40;
// run the conversion
for (int i = cMax; i >= cMin; i--) {
float degreesCelsius = (float) i;
float degreesFahrenheit = celsiusToFahrenheit(degreesCelsius);
System.out.println(String.format("%.1f\t|\t%.1f", degreesCelsius,
degreesFahrenheit));
}
Note that I've declared the Celsius degree range as ints because the requirement was an increment of one degree between each conversion. The values are cast into floats before the calculation.
You should avoid magic numbers in your code, which is why the range is defined as a pair of final variables (which you could also parse out from the args array, if you want to accept user input). The range could also be defined as static final fields if you don't expect it to change between runs of the program.
Finally, utility class Formatter is used for outputting the data through String.format(). This makes it easy to change the precision of the float values in the output.
public static void main(String[] args) {
double c=40;
double f;
while(c >= 30){
f = c * 9/5 +32; //°C x 9/5 + 32 = °F
System.out.println(c + "|" + f);
c--;
}
}
This should do it.
System.out.println(String.format("%-10s %-10s","Celsius ","Fahrenheit"));
double f = 30;
double c;
double i = 1;
while (f <= 120) {
f += i * 1 + f;
c = (5.0 / 9.0) * (f - 32);
System.out.println(String.format("%-10s %-10s",(double) Math.round((c / c * 100) * 10) / 10,(double) Math.round((f / f * 100) * 10) / 10));
i++;
}
This will format your code how you like it. It will give you your output as requested.
public class Far {
public static void main(String[] args) {
double c = 40;
double f;
System.out.println("Celsius Fahrenheit");
while (c >= 30) {
f = c * 9/5 +32;
System.out.println((c) + " "+Math.round(f*100.0)/100.0);
c--;
}
}
}

Need Help Finding the intersection of two equations in Java

I saw someone else posted this problem but didn't word it properly so he didn't end up receiving any help, so I figured I would try and be more direct.
Here is the problem proposed to us :
// Write and submit your code in a file called Intersect.java. Use the IO module to read inputs. Use System.out.println() to print your answer.
Write a program that calculates the intersection between 2 equations:
a degree-2 (quadratic) polynomial i.e.
y = dx^2 + fx + g
where d, f, and g are constants
and a degree-1 (linear) equation i.e.
y = mx + b
where m is the slope and b is a constant
The above is just text, not code that could appear in a Java program.
Ask the user for the constant values in each equation. Output the intersection(s) as ordered pair(s) (x,y), or "none" if none exists. Below is an example run.
java Intersect
Enter the constant d:
5
Enter the constant f:
-3
Enter the constant g:
2
Enter the constant m:
1
Enter the constant b:
3
The intersection(s) is/are:
(1,4)
(-0.20,2.8)
//
The main problem is essentially asking us to write a code that asks the user to imput the individual constants and the slope of the two equations, one being a quadratic polynomial and the other being a linear equation in point-slope.
I understand that we have to use the quadratic equation in the code, however I have no idea how to actually code this in.
Once we have had the user imput the constants, in this case 5 (d,f,g,m,b; with m being slope) we need to have the code run the calculations to imput those constants into the above example ( y = dx^2 + fx + g | y = mx + b) and return either "none" if there is no intersection, or if it does intersect, the ordered pair at which it does intersect (x,y).
I know already that if 0 is entered as the constants it returns (NaN,NaN) which I also know needs to be re-written to None.
so far I only have the following:
public class Intersect {
public static void main(String[] args) {
System.out.println("Enter the constant d:");
int d = IO.readInt();
System.out.println("Enter the constant f:");
int f = IO.readInt();
System.out.println("Enter the constant g:");
int g = IO.readInt();
System.out.println("Enter the constant m:");
int m = IO.readInt();
System.out.println("Enter the constant b:");
int b = IO.readInt();
If anyone can shed some light on this that would be fantastic, thanks!
EDIT1 :
So far i've changed the code to the following, however, I still don't know how to get it to return to me an answer:
public class Intersect {
public static void main(String[] args) {
System.out.println("Enter the constant d:");
int d = IO.readInt();
System.out.println("Enter the constant f:");
int f = IO.readInt();
System.out.println("Enter the constant g:");
int g = IO.readInt();
System.out.println("Enter the constant m:");
int m = IO.readInt();
System.out.println("Enter the constant b:");
int b = IO.readInt();
//y = dx^2 + fx + g
//y = mx + b
//mx + b = dx^2 + fx + g
//x^2 * (d) + x * ( f - m ) + ( g - b )
int A = d;
int B = f - m;
int C = g - b;
double x1 = - B + Math.sqrt( B^2 - 4 * A * C ) / (2 * A);
double x2 = - B - Math.sqrt( B^2 - 4 * A * C ) / (2 * A);
double y1 = m * x1 + b;
double y2 = m * x1 + b;
}
Also, eclipse is telling me that x2,y1, and y2 aren't used at all.
I know I need to use System.out.println() however I don't understand what I can put there to make the answer an ordered pair. Also, I tried setting an If statement to have the answer return None instead of NaN however it instead returns, NaN None.
There are a lot of special cases you have to take into account.
I hope I've got them all right.
So after the initialization of the values you can put this:
// calculating some useful values.
double t = -(f - m) / (2.0 * d);
double u = t * t - (g - b) / (double) d;
// the first polynomial is linear, so both terms are.
if (d == 0) {
// both linear functions have the same slope.
if (f == m) {
// both functions are shifted the same amount along the y-Axis.
if (g == b)
// the functions lie on top of each other.
System.out.println("There is an infinite amount intersections");
// the functions are shifted different amounts along the y-Axis.
else
// the lines are parallel.
System.out.println("There are no intersections");
}
// both linear functions have different slopes.
else {
// solve linear equation.
double x = (b - g) / (double) (f - m);
double y = m * x + b;
System.out.println("The intersection is: (" + x + "," + y + ")");
}
}
// the functions do not cross each other.
else if (u < 0)
System.out.println("There are no intersections");
// the linear function is a tangent to the quadratic function.
else if (u == 0) {
// solve equation.
double x = t;
double y = m * x + b;
System.out.println("The intersection is: (" + x + "," + y + ")");
}
// the linear function intersects the quadratic function at two points.
else {
// solve quadratic equation.
double x1 = t + Math.sqrt(u);
double x2 = t - Math.sqrt(u);
double y1 = m * x1 + b;
double y2 = m * x2 + b;
System.out.println("The intersections are: (" + x1 + "," + y1 + ") (" + x2 + "," + y2 + ")");
}
i guess....
//y = dx^2 + fx + g
//y = mx + b
//mx + b = dx^2 + fx + g
//x^2 * (d) + x * ( f - m ) + ( g - b )
A = d
B = f - m
C = g - b
x1 = - B + sqr( B^2 - 4 * A * C ) / (2 * A)
x2 = - B - sqr( B^2 - 4 * A * C ) / (2 * A)
y1 = m * x1 + b
y2 = m * x1 + b

Celsius to Fahrenheit Loop with user input

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...

Rounding int to 1 decimal place?

So in the following set of code I don't understand why "%.1f" will not round y to 1 decimal, I get the following when running the program:
123 F = Exception in thread "main"
java.util.IllegalFormatConversionException: f != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4045)
at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2761)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2708)
at java.util.Formatter.format(Formatter.java:2488)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at Check03A.main(Check03A.java:17)
I also tried Math.round(y * 10) / 10 but it gives me for example 29.0 instead of 28.9
import java.util.*;
import java.io.*;
import java.lang.*;
import type.lib.*;
public class Check03A
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
PrintStream print = new PrintStream(System.out);
print.println("Enter the temperature in Fahrenheit");
int x = scan.nextInt();
int y = 5 * (x - 32) / 9;
print.printf(x + " F = " + "%.1f", y + " C");
}
}
The problem is here:
print.printf(x + " F = " + "%.1f", y + " C");
There are two arguments to this method:
x + " F = " + "%.1f" (the format string),
y + "C" (the argument)
Condensing the first argument, the statement becomes:
print.printf(x + "F = %.1f", y + "C");
The problem: y + "C". Why? Well, one argument of the + operator is a String; therefore, this + becomes a string concatenation operator and what the argument will ultimately be is String.valueOf(y) + "C".
But the format specification in the format string is%.1f, which expects a float or double, or their boxed equivalents. It does not know how to handle a String argument.
Hence the error.
There is also the problem that you are doing an integer division, but this is not the topic of this question. Provided that all numeric problems are solved, your printing statement will ultimately be:
print.printf("%.1f F = %.1f C", x, y);
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
PrintStream print = new PrintStream(System.out);
print.println("Enter the temperature in Fahrenheit");
int x = scan.nextInt();
//change it
float y = 5 * (x - 32) / 9;
print.printf(x + " F = " + "%.1f", y).print(" C");
}
Make y as float if you want to use %f
float y = 5 * (x - 32) / 9;
print.printf(x + " F = " + "%.1f", y ).print("C");

Categories

Resources