Write a program that contains the following two methods:
public static double celsiusToFahrenheit(double cels)
public static double fahrenheitToCelsius(double fahr)
Both of these methods will return a converted temperature. The main method will prompt a user for the number of conversions they would like to make. Use that number for the counter control of a for loop. Make sure that the numeric output has one decimal place.
I have half the program with the converting but I do not know how to ask the user how many conversions that they want and how to lay them out.
import java.util.Scanner;
public class TempConverter{
public static void main(String[] args)
{
double fahrenheit;
double celsius;
Scanner input = new Scanner(System.in);
System.out.println("Enter the Degrees in Fahrenheit");
fahrenheit = input.nextDouble();
celsius = (5.0/9.0)*(fahrenheit - 32);
System.out.println("The number of degrees of Fahrenheit: " + fahrenheit);
System.out.println("Converted to Celsius is: " + celsius);//end convert to celsius
System.out.println("Enter the Degrees in Celsius");
celsius = input.nextDouble();
fahrenheit = (9.0/5.0)* celsius + 32;
System.out.println("The number of degrees of Celsius: " + celsius);
System.out.println("Converted to Fahrenheit is: " + fahrenheit);
}
}
Take number of times as input using the scanner. Run a loop for that number and inside ask for temperature using scanner for Fahrenheit and Celsius.
Or run an infinite loop and ask to enter Fahrenhei and Celsius using scanner. If user enters END/QUIT you end the loop and exit.
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'm trying to do temperature conversion and I know I need to invoke convertTemp in main, but I just don't know what I'm doing. Can someone take a look at this and help me out?
import java.util.Scanner;
public class TemperatureConverter {
public static void convertTemp() {
Scanner keyboard = new Scanner(System.in);
double temperature;
String temperatureScale = " ";
if (temperatureScale.equals("f"))
{
// code that converts from Fahrenheit to Celsius
temperature = (5/9)*(keyboard.nextDouble() - 32);
// and prints the result to the screen
System.out.println("The temperature is " + temperature + "degrees celsius");
}
//
else if (temperatureScale.equals("c"))
{
// code that converts from Celsius to Fahrenheit
temperature = 32.0 +(keyboard.nextDouble() * 1.8);
System.out.println("The temperature is " + temperature + "degrees fahrenheit");
// and prints the result to the screen
}
else
{
// code that outputs a message indicating that an incorrect
System.out.println("Error! A valid temperature was not chosen!");
// option was selected
}
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("What temeprature number are you trying to find out?");
double keyboardInput = keyboard.nextDouble();
System.out.println("Type f for Fahrenheit or c for Celsius.");
String keyboardTempLetter = keyboard.next();
}
}
//}
I would suggest instead of calling scanner in the convertTemp method, pass the character or string and the number in as an argument. then you'll just need to call the function in your main like this:
convertTemp(double keyboardInput, String keyboardTempLetter);
You need to send the user input to the method convertTemp so the method checks it then returns the result or prints it like you did .
Problem is you initialized temperatureScale and temperature in your method by adding
double temperature;
`String temperatureScale = " "; `
So the method used them instead of what the user inserted.
Second problem convertTemp method doesn't receive the userinput
convertTemp(NOTHING IS RECIVED HERE).
Third problem you didn't even send the input from main
So first your method should have:
convertTemp(String temperatureScale,double temperature)
Then remove the initialization of temperatureScale and temperature as I said above also remove
` Scanner keyboard = new Scanner(System.in);`
From convertTemp since you initialized it in main
Finally call the method in your main method this way :-
convertTemp(keyboardInputkeyboardInput,keyboardTempLetter);
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);
}
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 ()
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