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
Related
import java.util.*;
class TempConver {
public static void main(String[] args) {
double temperature;
Scanner in = new Scanner(System.in);
System.out.printf("Enter Fahrenheit Temperature: ");
temperature = in.nextInt();
temperature = (temperature - 32) * 5 / 9;
System.out.printf("Censius Temperatre is = " + temperature);
}
}
I've to write program using information given below.
output formatting - "printf()" instead of print() or println()
Do While loops - repeat a question until a user gives you a valid response
Scanner.HasNextDouble() - method to ascertain whether or not the next item the scanner is about to read works as a double data type
Please help me how to write output in 2 place decimal using do while loop. !!
You have all the answers you need in the hints.
Do While loops - repeat a question until a user gives you a valid response And Scanner.hasNextDouble() - method to ascertain whether or not the next item the scanner is about to read works as a double data type
Here you are telling the user, while the input is not a double, then execute the code. Which will be asking the user for input until you get a double
while (!in.hasNextDouble()) {
// code here
}
To round to two decimals you can use Math.round() to round a value to the nearest integer, and multiply temperature by 100 then divide by 100.
int round = (int) Math.round(temperature*100);
temperature = round / 100.0;
Full code:
public static void main(String[] args) {
double temperature;
Scanner in = new Scanner(System.in);
System.out.printf("Enter Fahrenheit Temperature: ");
// As long as it is not a double ask for another input
while (!in.hasNextDouble()) {
System.out.printf("Please enter a valid number:");
in.next();
}
temperature = in.nextDouble();
temperature = (temperature - 32) * 5 / 9;
// Use only 2 decimals
int round = (int) Math.round(temperature*100);
temperature = round / 100.0;
System.out.printf("Censius Temperatre is = " + temperature);
}
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.
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 ()
I have to allow the user to choose which conversion to do, and what degree to start at, then print the conversion starting at that degree up to the next 9 (10 conversions). And I have to write this using a callback function. Also there is a menu in the beginning. I have an "else without if" at line . Also my callback function is not calculating numbers correctly. How do I resolve this error and fix my callback function?
import java.util.Scanner;
public class FahrToCel {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
double choice = 0;
double fahrenheit = 0;
double celsius = 0;
double answer = 0;
//print the welcome message and the menu options for the user
System.out.printf("Welcome to Candace's Fahrenheit and Celsius "
+ "convertor. ");
System.out.printf("Please choose one of the following options: \n"
+ " Press 0 to exit \n Press 1 to to convert to Celsius \n"
+ " Press 2 to convert to Fahrenheit > ");
choice = input.nextInt();
//print what happens when the user hits a button
if (choice == 0){
// the user is going to exit the game
System.out.printf("Well hopefully you can play again soon! :) ");
} else {
//perform code for the operation
for ( double i = fahrenheit; i >-100; i-=10 ){
if (choice == 1) {
// it will convert from fahrenheit to celsius
System.out.printf("Please pick a number in Fahrenheit: > ");
fahrenheit = input.nextDouble();
celsius = FahrToCel();
System.out.printf("%f ", i);
}
}
for ( double i = celsius; i >-10; i-=10 ){
if (choice == 2){
// it will convert from celsius to fahrenheit
System.out.printf("Please pick a number in celsius: > ");
celsius = input.nextDouble();
fahrenheit = CelToFahr();
System.out.printf("%f ", i);
}
}
} else {
System.out.printf("I'm sorry, I did not ask you to enter that "
+ "number. ");
}
}
public static double CelToFahr(){
double celsius = 0;
double fahrenheit = 0;
celsius = (fahrenheit - 32) * (5.0/9.0);
return celsius;
}
public static double FahrToCel( ){
//perform the conversion for fahrenheit to celsius
double celsius = 0;
double fahrenheit = 0;
fahrenheit = ((9.0/5.0) + 32) * celsius;
return fahrenheit;
}
}
You are looking for a structure like this
if (choice == 0){
// ...
}
else if (choice == 1) {
for ( double i = fahrenheit; i >-100; i-=10 ){
// ...
}
}
else if (choice == 2) {
for ( double i = celsius; i >-10; i-=10 ){
// ...
}
}
else {
// ...
}
Right now you have some if statements inside of an else and your scope is getting confused. Your loops would then go inside those else if statements. Also note that you are doing the conversion many times, but printing the initial value entered each time, not the converted temperature.
Among other things, (EDIT) ONE OF your functions is not quite right. Since they involve math, I'm offering my revisions instead of hints.
I don't know if you want to pass in the value you're converting from, but doing so would make it better.
You definitely don't want to set both celsius and fahrenheit to zero in both functions.
public static double FahrToCel(double fahrenheit ){
double celsius;
celsius = (fahrenheit - 32) * (5.0/9.0);
return celsius;
}
public static double CelToFahr( double celsius){
//perform the conversion for fahrenheit to celsius
double fahrenheit;
fahrenheit = ((9.0/5.0) ) * celsius + 32;
return fahrenheit;
This would mean that inside your for loops, the calls to the functions would have to be something like:
celsius = input.nextDouble();
fahrenheit = CelToFahr(celsius);
But I'm not sure you should input the next value of celsius, if I'm reading the instructions and intent of the for loops correctly.
Maybe you want this instead of inputting:
celsius = i;
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;
}