Hey guys so I am a Java student I have the rest of the code working except for 1 line and not exactly sure whats not working. The error I am getting is a "cannot find symbol". I am very grateful for the help ahead of time!!!!
/**
* NAME: Mitchell Noble
* DATE: November 3, 2015
* FILE: lab10
* COMMENTS: This program displays a loop using a celcius to fahrenheit conversion formula
*/
public class lab10
{
public static void main(String[] args)
{
// declare variables
double fahrenheit;
double celsius;
value.setPrecision(2);
celsius = 0;
System.out.print("Celsius Fahrenheit");
while (celsius <= 15)
{
fahrenheit = 9 / 5 * celsius + 32;
System.out.print(celsius + " " + fahrenheit);
celsius = celsius + 1;
}
} // close main
}
Are you trying to do floating point math with arbitrary precision? If so, you need to look at java's BigDecimal class:
http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html
and do something like this:
BigDecimal celsiusValue = new BigDecimal("18");
BigDecimal farenheit = celsiusValue.multiply(new BigDecimal("1.8")).add(new BigDecimal("32"));
If you're just trying to format it to print out with two decimals, you can do it like this:
String toPrint = new DecimalFormat("#.##").format(celsius);
value is an undefined variable. In order to set the precision for your local variables:
fahrenheit.setPrecision(2);
celsius.setPrecision(2);
That will provide you with the desired precision on both the double variables.
Related
I have a problem with this program. I get no compile errors, but when I run it didn’t display the decimal point for Celsius output.
Here is my code:
public class TempLoops {
public static void main(String args[]) {
int fahrenheit = 0;
System.out.println("Fahrenheit Celsius");
for ( fahrenheit = 0; fahrenheit <= 300; fahrenheit+= 20) {
System.out.printf("%5d ",fahrenheit);
double Celsius = (fahrenheit-32.0) * (5.0/9.0); // formula for celsius to fahrenheit conversion
System.out.printf("%5d", (int)Celsius );
System.out.println();
}
}
}
How do I get digits behind the decimal point for Celsius output?
Here is the sample output for what it suppose to look like
sample out
Change this
System.out.printf("%5d", (int)Celsius );
to this
System.out.printf("%0.2f", Celsius );
This will print centigrades with 2 decimal places.
The main reason why you are getting integer output is because you are casting float(ing point number like x.abcd) into integer number, cutting of what is left as fracture part (resulting in x alone).
Use DecimalFormat to solve your problem.
Working Code :
import java.text.DecimalFormat;
public class TempLoops {
public static void main(String args[]) {
DecimalFormat f = new DecimalFormat("##.00"); // this will helps you to always keeps in two decimal places
int fahrenheit = 0;
System.out.println("Fahrenheit Celsius");
for ( fahrenheit = 0; fahrenheit <= 300; fahrenheit+= 20) {
System.out.printf(" "+fahrenheit);
double Celsius = (fahrenheit-32.0) * (5.0/9.0); // formula for celsius to fahrenheit conversion
System.out.printf("\t\t"+f.format(Celsius));
System.out.println();
}
}
}
Kindly refer to Java Docs for more information about DecimalFormat
I've been working on this code and everything seems to be working, but when MyProgrammingLab actually runs my code it says there is a problem with my standard output.
Here is the problem:
Write a Temperature class that will hold a temperature in Fahrenheit and provide methods to get the temperature in Fahrenheit, Celsius, and Kelvin. The class should have the
following field:
• ftemp: a double that holds a Fahrenheit temperature.
The class should have the following methods :
• Constructor : The constructor accepts a Fahrenheit temperature (as a double ) and stores it in the ftemp field.
• setFahrenheit: The set Fahrenheit method accepts a Fahrenheit temperature (as a double ) and stores it in the ftemp field.
• getFahrenheit: Returns the value of the ftemp field as a Fahrenheit temperature (no conversion required)
• getCelsius: Returns the value of the ftemp field converted to Celsius. Use the following formula to convert to Celsius:
Celsius = (5/9) * (Fahrenheit - 32)
• getKelvin: Returns the value of the ftemp field converted to Kelvin. Use the following formula to convert to Kelvin:
Kelvin = ((5/9) * (Fahrenheit - 32)) + 273
Demonstrate the Temperature class by writing a separate program that asks the user for a
Fahrenheit temperature. The program should create an instance of the Temperature class ,
with the value entered by the user passed to the constructor . The program should then
call the object 's methods to display the temperature in the following format (for example,
if the temperature in Fahrenheit was -40):
The temperature in Fahrenheit is -40.0
The temperature in Celsius is -40.0
The temperature in Kelvin is 233.0
And now here is my code:
import java.io.*;
import java.util.Scanner;
public class Temperature
{
private double ftemp;
public Temperature(double ftemp)
{
this.ftemp = ftemp;
}
public void setFahrenheit(double ftemp)
{
this.ftemp = ftemp;
}
public double getFahrenheit()
{
return ftemp;
}
public double getCelsius()
{
return (5.0/9.0) * (ftemp - 32.0);
}
public double getKelvin()
{
return (5.0/9.0) * ((ftemp - 32.0) + 273.0);
}
}
class myTemperature
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
double input;
System.out.print("Enter a Fahrenheit temperature:");
input = keyboard.nextDouble();
Temperature temp1 = new Temperature(input);
System.out.println("The temperature in Fahrenheit is " + temp1.getFahrenheit());
System.out.println("The temperature in Celsius is " + temp1.getCelsius());
System.out.println("The temperature in Kelvin is " + temp1.getKelvin());
}
}
These are the errors it gives me:
http://imgur.com/gallery/0D2RkW7/new
I don't have enough rep to post images, sorry!
I really just don't understand what the problem could be, any help would be greatly appreciated.
public double getKelvin()
{
return ((5.0/9.0) * (ftemp - 32.0)) + 273.0;
}
Note the changes in ()
This is my code as of right now: http://ideone.com/PfXNQc
import java.util.Scanner;
public class Temperature
{
public static double convertFtoC(double F)
{
return (F-32)*5/9;
}
public static void main(String[] args)
{
double F; //Stores Farenheit
double C; //Stores Celsius
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a temperature in Fahrenheit: "); //Enter in Farenheit
F = keyboard.nextDouble(); //Stores Farenheit
System.out.println("The temperature in Celsius is: " + convertFtoC(F)); //Displays Celsius (call convertFtoC(F) where celcuis conversion needed)
F = 0;
System.out.println("Fahrenheit\t Celsius");
while (F <= 100)
{
// Display the F and C in increments of 10.
System.out.println(F + "\t\t" + convertFtoC(F));
// Increment for the next day.
F++;
}
}
}
I would like the loop for conversions to go up in increments of 10 rather than by 1.
Right now it is outputting this:
But I would like it to be
0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
what if you write as follows.
while (F <= 90) //Last number before 100 is 90
{
// Display the F and C in increments of 10.
System.out.println(F + "\t\t" + convertFtoC(F));
// Increment for the next day.
F=F+10;
}
Additionally,why you use capitals for variables, per conventions you should start variable names with lower case letters, objects names are started with capitals.
Modern Idiomatic Professional Approach
Q32835243.java
package com.stackoverflow;
import java.util.Scanner;
public class Q32835243
{
private static double farhenheitToCelsius(final double fahrenheit) {return (fahrenheit - 32) * 5 / 9;}
public static void main(final String[] args)
{
final Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a temperature in Fahrenheit: ");
if (keyboard.hasNextDouble())
{
final double fahrenheit = keyboard.nextDouble();
final double celsius = farhenheitToCelsius(fahrenheit);
System.out.format("The temperature in Celsius is: %8.4f%n", celsius);
System.out.format("Fahrenheit\t Celsius%n");
for (int i=0; i < 100; i+=10)
{
System.out.format("%8.4f\t%8.4f%n", fahrenheit + i, farhenheitToCelsius(fahrenheit + i));
}
}
else
{
System.err.format("%s is not a double!%nPlease enter a double value.", keyboard.next());
System.exit(1);
}
}
}
Link to this solution as a fork of what you did.
I put this working code on ideone.com so you could easily see the differences and how to make this trival code bullet proof as possible.
Things to take note of in this solution:
Always make as many things final as possible. It eliminates two of the most common classes of program errors NullPointerException and side effects.
Always limit the scope of variable references and visibility of methods as much as possible. You want to define variables when you use them and make as many things private as you can.
Always name your variables in the context that you are using them so that you do not need to put comments explaining what they are.
Comments should only be in clean code when you are deviating from the expected. All your comments were useless tautology and violate the DRY principle as well. If things change now you have to update the code and the comments. More to maintain, easier to just name things that are self explanatory.
Using single letter variable names in short functions is fine. In the context of the block f and c would be acceptable names for fahrenheit and celsius if they were object references. But in the case of homework explicit is always better for communicating exactly what you are thinking.
Always follow the style guide when using lowerCamelCase and UpperCamelCase. Know when to use each in the correct context.
Always check your inputs and handle invalid data. Never try and fix it, if it is wrong stop the program, explain to the user where they went wrong and end the program. Defensive programming is terrible.
Always use the features of the different objects to your advantage. Use the String.format() and PrintStream.format() to eliminate formatting issues and making things more maintainable.
Learn to use the Scanner class correctly. the .hasNextXxx() methods are there for a reason, use them!
The scanner class has a terrible API and is the source of more frustration to use programmers than just about anything else.
For enhanced readability:
Don't use Capitalized identifiers for the variables as they are meant for the constants in Java.
Use meaningful identifier names.
Here is the code:
import java.util.Scanner;
public class Temperature
{
public static double convertFarenheitToCelsius(double farenheit)
{
return (farenheit-32)*5/9;
}
public static void main(String[] args)
{
double farenheit; //Stores Farenheit
double celsius; //Stores Celsius
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a temperature in Fahrenheit: "); //Enter in Farenheit
if (keyboard.hasNextDouble()){
F = keyboard.nextDouble(); //Stores Farenheit
System.out.println("The temperature in Celsius is: " + convertFarenheitToCelsius(farenheit)); //Displays Celsius (call convertFarenheitToCelsius(farenheit) where celcius conversion needed)
farenheit = 0;
System.out.println("Fahrenheit\t Celsius");
while (farenheit <= 100){
System.out.println(farenheit + "\t\t" + convertFarenheitToCelsius(farenheit));
farenheit += 10;
}
} else {
System.err.format("%s is not a double!%nPlease enter a double value.", keyboard.next());
System.exit(1);
}
}
}
Set F+=10 instead of F++ in the loop.
I think in your case use FOR is a better choice.
public static void main(String[] args) {
double fahrenheit = 50;
double increment = 10;
for(;fahrenheit <= 100; fahrenheit += increment){
System.out.println(fahrenheit);
}
}
So, i'm supposed to display a table that gives the Fahrenheit to Celsius conversion from 94F to 104F by 0.5 increments using a for loop and a celsius method that accepts a Fahrenheit temperature as an argument.
(we are just learning about methods this week)
why am I getting this error? and is this code headed in the right direction?
FahrenheitToCelsius.java:28: error: cannot find symbol
fhdeg, celdeg(fhdeg) );
^
symbol: method celdeg(double)
location: class FahrenheitToCelsius
1 error
FahrenheitToSelsius.java
/* PC 5.6 - Fahrenheit to Celsius
-------------------------
Programmer: xxxxxxxxxxx
Date: xxxxxxxxxxxxxxxxx
-------------------------
This program will convert Fahrenheit to Celsius and display the
results.
*/
// ---------1---------2---------3---------4---------5---------6---------7
// 1234567890123456789012345678901234567890123456789012345678901234567890
public class FahrenheitToCelsius
{
public static void main(String[] args)
{
System.out.println("Temperature Conversion Table");
System.out.println("____________________________");
System.out.println(" Fahrenheit Celsius ");
}
public static double celsius(double fhdeg)
{
for (fhdeg = 0; fhdeg >=94; fhdeg+=0.5)
{
double celdeg = 0;
celdeg = 5.0/9 * fhdeg-32;
System.out.printf( " %5.1d %4.1d\n",
fhdeg, celdeg(fhdeg) );
}
}
}
The expression celdeg(fhdeg) means that you are calling a method called celdeg passing an argument called fhdeg. You get the error because there is no method called celdeg().
By the statement of the problem, though, I guess you don't need to create such method. Instead, you just need to iterate over a range of degrees in Fahrenheit and display the equivalent value in Celsius. Your for loop could be something like this:
public static void main(String[] args) {
System.out.println("Temperature Conversion Table");
System.out.println("____________________________");
System.out.println(" Fahrenheit Celsius ");
// From 94F to 104F, with increments of 0.5F
for (double fhdeg = 94.0; fhdeg < 104.5; fhdeg += 0.5) {
// Calculate degree in Celsius by calling the celsiusFromFahrenheit method
double celdeg = celsiusFromFahrenheit(fhdeg);
// Display degrees in Fahrenheit and in Celsius
System.out.printf( " %.2f %.2f\n", fhdeg, celdeg);
}
}
static double celsiusFromFahrenheit(double fhdeg) {
return (5. / 9.) * fhdeg - 32;
}
Currently have project for school I am working on, here are the requirements:
Write a Temperature class that will hold a temperature in Fahrenheit
and provide methods to get the temperature in Fahrenheit, Celsius,
and Kelvin. The class should have the following field:
ftemp: a double that holds a Fahrenheit temperature.
The class should have the following methods:
Constructor: The constructor accepts a Fahrenheit temperature (as a double) and stores it in the ftemp field.
setFahrenheit: The set Fahrenheit method accepts a Fahrenheit temperature (as a double) and stores it in the ftemp field.
getFahrenheit: Returns the value of the ftemp field as a Fahrenheit temperature (no conversion required)
getCelsius: Returns the value of the ftemp field converted to Celsius. Use the following formula to convert to Celsius: Celsius = (5/9) * (Fahrenheit - 32)
getKelvin: Returns the value of the ftemp field converted to Kelvin. Use the following formula to convert to Kelvin: Kelvin = ((5/9) * (Fahrenheit - 32)) + 273
Demonstrate the Temperature class by writing a separate program tha asks the user for a Fahrenheit temperature. The program should create an instance of the Temperature class, with the value entered by the user passed to the constructor . The program should then call the object's methods to display the temperature in the following format (for example, if the temperature in Fahrenheit was -40):
The temperature in Fahrenheit is -40.0
The temperature in Celsius is -40.0
The temperature in Kelvin is 233.0
Here is my current code:
Temperature(Class)
public class Temperature {
private double ftemp;
public Temperature(double temp)
{
ftemp = temp;
}
public void setFahrenheit(double t)
{
ftemp = t;
}
public double getFahrenheit()
{
return ftemp;
}
public double getCelsius()
{
return (5/9) * (ftemp - 32);
}
public double getKelvin()
{
return ((5/9) * (ftemp - 32)) + 273;
}
}
MPL1(Main source code)
import java.util.Scanner;
public class MPL1 {
public static void main(String[] args)
{
double input;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a Fahrenheit temperature: ");
input = keyboard.nextDouble();
Temperature myTemp = new Temperature(input);
System.out.println("The temperature in Fahrenheit is " +
myTemp.getFahrenheit());
System.out.println("The temperature in Celsius is " +
myTemp.getCelsius());
System.out.println("The temperature in Kelvin is " +
myTemp.getKelvin());
}
}
When executed, it performs almost everything correctly, however, using getCelsius and getKelvin give me results of 0.0 and 273.0 regardless of what number is entered.
5/9 is integer division, and since 9 divides into 5 zero times, 5/9 = 0. Cast it to a double instead: ((double)5/9).
5/9 == 0 in Java because both 5 and 9 are int literals. You need to explicitly use floating point literals:
return (5.0/9.0) * (ftemp - 32.0);
return ((5.0/9.0) * (ftemp - 32.0)) + 273.15;
See Primitive Data Types