Converting celsius to fahrenheit using methods - java

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.

Related

Beginner, basic trig table in increments of 5 degrees

For a class that I am taking I am trying to create a program that produces a table of sin(), cos(), and tan() values for angles from 0 to 180 degrees in steps of 5 degrees.
!http://i65.tinypic.com/14ahliq.jpg
So far I have the following code, which produces an introduction and the first two lines of the table, but I cannot figure out how to get it to repeat.
import java.util.*;
public class Angles {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("This program computes the");
System.out.println("sin(), cos(), and tan() values");
System.out.println("for angles from 0 to 180 degrees");
System.out.println("in steps of 5 degrees.");
System.out.println("");
System.out.println("Angle\tSin()\tCos()\tTan()");
double Anglex = 0;
for(double i = 5;i <= Anglex;i += 5) {
Anglex = 0 + i;
}
double Sinx = Math.sin(Math.toRadians(Anglex));
double Cosx = Math.cos(Math.toRadians(Anglex));
double Tanx = Math.tan(Math.toRadians(Anglex));
System.out.println(Anglex + "\t" + Sinx + "\t" + Cosx + "\t" + Tanx);
}
}
Is not really ok that you ask people on forums to solve your assignments.
Otherwise, several issues with your little program (didn't test, please do it yourself).
anglex should start at 0 and stop at 180. So for(int anglex=0; anglex<=180; anglex+=5). Use anglex instead of i, inside the loop.
the calculations for sinx, cosx, tanx and the printing of the new line should be inside the curlies {}. As your code is right now, the only thing inside the loop is the increment of anglex.
Sorry for not providing the full solution, pretty sure you can do it.
Recast your for loop to
for (double Anglex = 0; Anglex <= 180; Anglex += 5){
Note well the opening brace to enclose multiple subsequent statements. Don't forget to balance it with a closing }; probably after the println call.
Using a double as a loop index is not to everyone's taste (you can get yourself into trouble if you are not using whole numbers), but this is fine in this instance, particularly also as you are using <= as the stopping condition.
Starting variable names with an upper case letter is also to be discouraged in Java as it's unconventional.
Your for loop only applies on the Anglex = 0+i line.
Add {} to the whole section that should be repeated.
public static void main(String[] args) {
System.out.println("This program computes the");
System.out.println("sin(), cos(), and tan() values");
System.out.println("for angles from 0 to 180 degrees");
System.out.println("in steps of 5 degrees.");
System.out.println("");
System.out.println("Angle\tSin()\tCos()\tTan()");
double maxAngleX = 180.0;
for (double angleX = 5; angleX <= maxAngleX; angleX += 5) {
double Sinx = Math.sin(Math.toRadians(angleX));
double Cosx = Math.cos(Math.toRadians(angleX));
double Tanx = Math.tan(Math.toRadians(angleX));
System.out.println(angleX + "\t" + Sinx + "\t" + Cosx + "\t" + Tanx);
}
}
for(double i = 5;i <= Anglex;i += 5) {
Anglex = 0 + i;
double Sinx = Math.sin(Math.toRadians(Anglex));
double Cosx = Math.cos(Math.toRadians(Anglex));
double Tanx = Math.tan(Math.toRadians(Anglex));
}
Enclose the above statements inside a { and }. The for loop applies for only the first statement in your code.

How to calculate circumference with random numbers?

I need to print the circumference with Math.random() * Math.Pi; but i'm doing something wrong or missing something. Each random generated number equals the radius of the circle. My idea was to calculate Pi in the getRandomNumberInRange method but when I do, I get error:
Bad operand for type double
import java.util.Random;
import java.util.Scanner;
final static double PI = 3.141592564;
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
//ask the player to enter a number less than or equal to 18 and higher to 9.
System.out.println(" Please enter a number less than or equal to 18 and above 9: ");
int random = sc.nextInt ();
//send error message if bad input
if (random < 9 || random > 18) {
System.out.println(" Error. Unauthorized entry . You need to enter a number less than or equal to 18 and above 9 ");
} else
//If the answer is yes , generate nine different random numbers from 0.
for (int i = 0; i < 9; i++) {
double surface = PI * (random * 2);
System.out.println(getRandomNumberInRange(9, 18) + " : " + " The circumference is : " + surface );
}}
The method called:
private static int getRandomNumberInRange(int min, int max) {
Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}
You call getRandomNumberInRange() in the for loop, but don't assign it to anything, or use it.
This is probably closer to what you want:
for (int i = 0; i < 9; i++) {
int r2 = getRandomNumberInRange(9, 18);
double surface = PI * (r2 * 2);
System.out.println(r2 + " : " + " The circumference is : " + surface);
}

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--;
}
}
}

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

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 ");
}

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

Categories

Resources