Temperature conversion using objects in java - java

So i am currently writing a program using objects where the user enters an initial temperature, then the program has to compute it into Celsius, which would just be the user input, then to Fahrenheit and then to kelvin. The class also has a single constructor that accepts an initial temperature provided as a double argument. If this argument is < -273.15, set it to -273.15. I thought I was on the right track but when i compiled it, it wasn't doing what i wanted, any tips on how I can fix it?
With this code, the output gives me
Please enter the initial temperature: 20
The current temperature in Celsius is: 0.0
The current temperature in Fahrenheit is: 32.0
The current temperature in Kelvin is: 273.15
which isn't right... any tips?
//blueprint
public class TemperatureC{
private double temperatureC;
public TemperatureC(){
if(temperatureC<-273.15){
temperatureC = -273.15;}
else{}
}
public void setC(double c){
temperatureC = c;
}
public double getC(){return temperatureC;}
public double getF(){return (temperatureC * 1.8) + 32;}
public double getK(){return temperatureC + 273.15;}
}
//code
import java.util.Scanner;
public class TemperatureTester{
public static void main(String[] args){
TemperatureC temp = new TemperatureC();
double initialTemperature;
double celsius=temp.getC();
double fahrenheit=temp.getF();
double kelvin=temp.getK();
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the initial temperature: ");
initialTemperature = keyboard.nextDouble();
//TemperatureC temp = new TemperatureC();
System.out.println("The current temperature in Celsius is: " + celsius);
System.out.println("The current temperature in Fahrenheit is: "+fahrenheit);
System.out.println("The current temperature in Kelvin is: "+kelvin);
}
}

You are assigning the values of celsius, fahrenheit, and kelvin before you know the temperature value. You want your tester to look more like this
public static void main(String[] args) {
TemperatureC temp = new TemperatureC();
double initialTemperature;
Scanner keyboard = new Scanner(System.in);
initialTemperature = keyboard.nextDouble();
temp.setC(initialTemperature);
System.out.println("The current temperature in Celsius is: " + temp.getC());
System.out.println("The current temperature in Fahrenheit is: "+temp.getF());
System.out.println("The current temperature in Kelvin is: "+temp.getK());
}
So the operations are now done after the temperature of initialTemperature is set.

Related

How to combine two simple programs into one

How can I combine two small programs I created?
They are conversions of Farenheit to Celsius and vice versus. When I join the two together, I clearly have double/repeating variables. Not quite sure how/what to change.
The goal is to combine the two programs so it will ask the user to choose one, (F or C) and then direct the user to input an integer to convert. Not sure if I need to create these as two objects of my class? Or how to direct a choice, maybe using Switch?
Below is one conversion used, the formula is the same just inverse.
import java.util.Scanner;
public class FahrenheitToCelsius {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a degree in Fahrenheit: ");
double fahrenheit = input.nextDouble();
double celsius =(5.0 / 9) * (fahrenheit - 32);
System.out.println("Fahrenheit " + fahrenheit + " is " + celsius + " in Celsius") ;
}
}
I think you are right that we can simplify the code using a single switch statement - or even an if statement in this case.
Try maybe this:
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
String convertFrom = userInput.nextLine();
double C, F, convertedDegrees;
if (convertFrom.equals("F")) {
// Convert Fahrenheit to Celsius
} else if (convertFrom.equals("C")) {
// Convert Celsius to Fahrenheit
convertedDegrees = userInput.nextDouble();
F = (convertedDegrees * 1.8) + 32;
System.out.println(F);
} else {
System.out.println("Invalid input. Please type 'C' or 'F' to indicate whether you wish to convert Celsius or Fahrenheit degrees.");
}
}

When I place my doubles on top, the program doesn't run properly

Assignment: Variables
This program prompts the user to enter a temperature between -58°F and
41°F and a wind speed greater than or equal to 2 then displays then
displays the wind-chill temperature.
// Imports util.Scanner
import java.util.Scanner;
public class Windchill {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Tempurature
double temperature = input.nextDouble();
// Windspeed
double speed = input.nextDouble();
// Compute the wind chill tempurature
double windChill = 35.74 + 0.6215 * temperature -
35.75 * Math.pow(speed,
0.16) + 0.4275 * temperature *
Math.pow(speed, 0.16);
// Prompt the user to enter a temperature between -58F and 41F.
System.out.print("Enter the temperature in Fahrenheit " +
"between -58\u00b0F and 41\u00b0F: ");
// Prompt the user to enter the wind speed greter than or equal to 2.
System.out.print("Enter the wind speed (>= 2) in miles per hour: ");
// Display result
System.out.println("The wind chill tempurature is " + windChill);
}
}
This seems to be a school assignment. However, it seems like you have already completed the bulk of the work. Congratulations! Now, I feel like the issue here can be solved by explaining why your program does not work if "the doubles are on top". I hope that my answer can help you better understand the way java interprets your code!
Without further ado, programming languages of all types have variables. Java is no different. For example...
double number = 0.0; // Java variable declaration
number = 0.0 # Python variable declaration
var number = 0.0 // JavaScript variable declaration
Your code is going to be executed from the top down. An illustration of this would look like the following.
int money = 0;
System.out.println(money);
money = 10;
System.out.println(money);
money = 9000;
System.out.println("I have over " + money);
This will output
0
10
I have over 9000
However, if you wrote this code like the following
System.out.println(money);
int money = 0;
You will get an error! This is because the execution has not seen that money is even a thing yet! This would be like brushing your teeth without a tooth brush. You can't because you don't have a brush.
Therefore, the same applies to your program.
public static void main(String[] args) {
double temperature = input.nextDouble();
Scanner input = new Scanner(System.in);
// Prompt the user to enter a temperature between -58F and 41F.
System.out.print("Enter the temperature in Fahrenheit " +
"between -58\u00b0F and 41\u00b0F: ");
// Tempurature
// Prompt the user to enter the wind speed greter than or equal to 2.
System.out.print("Enter the wind speed (>= 2) in miles per hour: ");
// Windspeed
double speed = input.nextDouble();
// Compute the wind chill tempurature
double windChill = 35.74 + 0.6215 * temperature -
35.75 * Math.pow(speed,
0.16) + 0.4275 * temperature *
Math.pow(speed, 0.16);
// Display result
System.out.println("The wind chill tempurature is " + windChill);
}
Notice temperature above the scanner line. Input is a object you create to read in that double. If you try to use this before you create your input object the program has no idea what that input object is!
Just rearrange the code like below
// Imports util.Scanner
import java.util.Scanner;
public class Windchill {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt the user to enter a temperature between -58F and 41F.
System.out.print("Enter the temperature in Fahrenheit " +
"between -58\u00b0F and 41\u00b0F: ");
// Tempurature
double temperature = input.nextDouble();
// Prompt the user to enter the wind speed greter than or equal to 2.
System.out.print("Enter the wind speed (>= 2) in miles per hour: ");
// Windspeed
double speed = input.nextDouble();
// Compute the wind chill tempurature
double windChill = 35.74 + 0.6215 * temperature -
35.75 * Math.pow(speed,
0.16) + 0.4275 * temperature *
Math.pow(speed, 0.16);
// Display result
System.out.println("The wind chill tempurature is " + windChill);
}
}
but there is no problem related to double, :)
Thanks everyone. I got it.
/* Assignment: Variables
This program prompts the user to enter a temperature between -58°F and 41°F
and a wind speed greater than or equal to 2 then displays then displays the
wind-chill tempurature.
*/
// Imports util.Scanner
import java.util.Scanner;
public class Windchill {
public static void main(String[] args) {
// Declare variables
double temperature;
double windspeed;
double wind_chill;
// Create a Scanner object to read input
Scanner input = new Scanner(System.in);
// Prompt the user to enter a temperature between -58F and 41F.
System.out.print("Enter the temperature in Fahrenheit " +
"between -58\u00b0F and 41\u00b0F: ");
temperature = input.nextDouble();
// Prompt the user to enter the wind speed greter than or equal to 2.
System.out.print("Enter the wind speed (>= 2) in miles per hour: ");
windspeed = input.nextDouble();
// Display result
wind_chill = 35.74 + 0.6215 * temperature -
35.75 * Math.pow(windspeed, 0.16) +
0.4275 * temperature * Math.pow(windspeed, 0.16);
System.out.println("The wind chill temprature is " + wind_chill);
}
}

java program not desired outcome

import java.util.Scanner;
public class FtoC
{
public static void main(String[] args)
{
double DegreesF, DegreesC;
System.out.print("Enter temperature in Fahrenheit:");
` Scanner sc = new Scanner(System.in);
DegreesF = sc.nextInt();
DegreesC = 5*(DegreesF-32)/9;
System.out.print(DegreesF + " degrees Fahrenheit" + " is " + DegreesC + " degrees Celsius.");
}
}
i get the output :Enter temperature in Fahrenheit:72
72.0 degrees Fahrenheit is 22.22222222222222 degrees Celsius.
I need that 72 degrees Fahrenheit to be a whole number, without the decimal part. please help.
I need that 72 degrees Fahrenheit to be a whole number, without the
decimal part
You could store it initially as an int, and not a double:
int DegreesF;
double DegreesC;
Which will print:
72 degrees Fahrenheit is 22.0 degrees Celsius.
Or cast it as an int in your print statement:
System.out.print((int)DegreesF + " degrees...
Although better to go with the first way.
Also, you should then specify the Celsius value (perhaps to two decimal points):
One way would be to use DecimalFormat:
DecimalFormat df = new DecimalFormat(".##");
and print it like:
df.format(DegreesC)
Since you only Scan for an int value, simply define DegreesF as an int
int DegreesF;
rather than
double DegreesF;
since Integers don't have a decimal point you won't have the problem any more
to get correct Celcius value do this:
DegreesC = 5*((double)DegreesF-32)/9;

Exception in thread "main" java.lang.NumberFormatException: For input string: "0.06"

I have been trying to make a simple program in eclipse for a school project, but I keep getting this after I enter my interest rate. I am relatively new to coding and programming in general, and java is new to me as of this month so any help is appreciated. The code is this:
import java.util.Calendar;
import java.util.Locale;
import java.util.Scanner;
public class Interest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
//Input ============================
System.out.println("Initial loan total:"); //cost
String cost;
cost = in.nextLine();
System.out.println("Down payment:"); //down
String down;
down = in.nextLine();
System.out.println("Length of term:"); //term
String term;
term = in.nextLine();
System.out.println("Interest rate (decimal form):"); //rate
String rate;
rate = in.nextLine();
int principle1 = Integer.parseInt(cost) - Integer.parseInt(down);
String hundred;
hundred = "100";
int interest = Integer.parseInt(rate) * Integer.parseInt(hundred);
//Output ===========================
Calendar c = Calendar.getInstance();
System.out.format("%tB %td, %tY", c,c,c);
System.out.println("");
System.out.println("The initial cost of the loan is $" + cost);
System.out.println("The down payment is $" + down);
System.out.println("The principle is $" + principle1);
System.out.println("The term is " + term + " months");
System.out.println("The interest rate is " + interest + "%");
System.out.println("The monthly patments are $");
in.close();
}
}
When I run the program it lets me put in the initial loan, down payment and length of term but as soon as I put in 0.06 for the interest rate it gives me the error message. I would also like to point out that I have a limited understanding of how the math in my code works.
the problem is that you are trying to parse 0.06 to Integer and 0.06 is float.
use Float.parseFloat(rate); and your interest should be a float too float interest

Temperature conversion code in Java won't run?

Ok,so I'm a complete novice at programming and I just started coding in Java. I tried to write a code for temperature conversion (Celsius to Fahrenheit) and for some reason it simply won't run! Please, help me find out errors in this code(however silly it may be).
Here's the code:
package tempConvert;
import java.util.Scanner;
public class StartCode {
Scanner in = new Scanner(System. in );
public double tempInFarenheit;
public double tempInCelcius;
{
System.out.println("enter the temp in celcius");
tempInCelcius = in .nextDouble();
tempInFarenheit = (9 / 5) * (tempInCelcius + 32);
System.out.println(tempInFarenheit);
}
}
You forgot to write the main method which is the start point for a program to run. Let me modify your code.
import java.util.Scanner;
public class StartCode
{
Scanner in = new Scanner (System.in);
public double tempInFarenheit;
public double tempInCelcius;
public static void main (String[] args)
{
System.out.println("enter the temp in celcius");
tempInCelcius = in.nextDouble() ;
tempInFarenheit = (9/5)*(tempInCelcius+32);
System.out.println(tempInFarenheit);
}
}
I think this is going to work better for you:
import java.util.Scanner;
public class StartCode
{
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
double tempInFarenheit;
double tempInCelcius;
System.out.println("enter the temp in celcius");
tempInCelcius = in.nextDouble() ;
tempInFarenheit = 1.8*tempInCelcius+32;
System.out.println(tempInFarenheit);
}
}
You equation for Farenheit was incorrect. Integer division isn't for you, either.
You need a main method. I also suggest using an IDE such as Eclipse, which can generate the skeleton code for you (including the syntax of the main method).
import java.util.*;
public class DegreeToFahrenheit {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a temperature: ");
double temperature = input.nextDouble();
System.out.println("Enter the letter of the temperature type. Ex: C or c for celsius, F or f for fahrenheit.: ");
String tempType = input.next();
String C = tempType;
String c = tempType;
String F = tempType;
String f = tempType;
double celsius = temperature;
double fahrenheit = temperature;
if(tempType.equals(C) || tempType.equals(c)) {
celsius = (5*(fahrenheit-32)/9);
System.out.print("The fahrenheit degree " + fahrenheit + " is " + celsius + " in celsius." );
}
else if(tempType.equals(F) || tempType.equals(f)) {
fahrenheit = (9*(celsius/5)+32);
System.out.print("The celsius degree " + celsius + " is " + fahrenheit + " in fahrenheit." );
}
else {
System.out.print("The temperature type is not recognized." );
}
}
}

Categories