How to combine two simple programs into one - java

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

Related

Creating Java conversion table from Fahrenheit to Celsius

I am new to this site and new to programming I just started a week a go. I've been tasked with making a conversion table from degrees Fahrenheit to Celsius. the table needs to start at 0 Celsius and stop at 100 degrees Celsius and go by increments of 5.
I am really trying to make it work but I can't seem to get the for loop working correctly. Can someone explain to me what I am doing wrong and how I can finish this program to work the way I need it?
public class Table
{
public static void main(String[] args)
{
System.out.println("Conversion Table");
final int TOTAL = 100;
for (int c = 0; c <= TOTAL; c+=5)
{
System.out.println((c*9/5)+32 +"\t");
{
System.out.println();
}
}
}
https://ideone.com/llmOER
Currently you are only printing the values of the Fahrenheit to standard out. If the idea is that you want to print to standard out the table then you probably want to add the Celsius value as well.
Add something like
System.out.println(c + "\t" + ((c*9/5)+32) +"\t");
to your output and you'll be sweet.
1-You have one less closing brace.
2-For making conversion table you have to show both Fahrenheit and Celsius value. Code for this would be.
public class A
{
public static void main(String[] args)
{
System.out.println("Conversion Table");
final int TOTAL = 100;
System.out.println("Fahrenheit"+"\t Celsius");
for (int c = 0; c <= TOTAL; c+=5)
{
System.out.println((c*9/5)+32 +"\t \t" + c);
{
System.out.println();
}
}
}
}

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;

Java User input Validation

I am trying to figure out how to validate the input of a user. I want the user to enter a double but if the user enters a string I want the question repeated until a double is entered. Iv'e searched but I couldn't find anything. Below is my code so far any help is appreciated. I have to use a do while loop I am stuck on what to put in the while loop to make sure the input is a double and not a string.
public class FarenheitToCelsius {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
double fahrenheit, celsius;
Scanner in = new Scanner(System.in);
do{
System.out.printf("Enter a fahrenheit degree: ");
fahrenheit = in.nextDouble();
}
while();
celsius = ((fahrenheit - 32)*5)/9;
System.out.println("Celsius value of Fahrenheit value " + fahrenheit + " is " + celsius);
One trick you can use here is to read the entire user input as a string, which would allow any type of input (string, double, or anything else). Then, use Double#parseDouble() to try to convert that input to a bona-fide double value. Should an exception occur, allow the loop to continue, otherwise, end the loop and continue with the rest of your program.
Scanner in = new Scanner(System.in);
boolean isValid;
do {
System.out.printf("Enter a fahrenheit degree: ");
isValid = false;
String input = in.nextLine();
try {
fahrenheit = Double.parseDouble(input);
isValid = true;
}
catch (Exception e) {
// do something
}
} while(!isValid);
celsius = ((fahrenheit - 32) * 5) / 9;

Trying to combine multiple programs together

I am trying to take 3 different programs I have created and put them under a single class. My professor has stated I must do this and I have no clue on how to. I am not looking for a hand out here, just some how I can do this quickly and efficiently. I am also trying to figure out how to call from the same scanner for each program or if I should just make multiple ones.
import java.util.Scanner;
public class AssignmentOneFahrenheit {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello, I can convert Fahrenheit to Celsius!");
System.out.println("Please enter the degrees Fahrenheit you want converted.");
double degreesF;
double degreesC;
Scanner keyboard = new Scanner(System.in);
degreesF = keyboard.nextDouble(); //Allows user to input decimal number
keyboard.close();
System.out.println("The temperature in Degrees Celsius is: ");
degreesC = 5*(degreesF - 32)/9;
System.out.printf("%.2f", degreesC);
}
import java.util.Scanner;
public class AssignmentOneHate {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a line containing 'hate'.");
String text = keyboard.nextLine();
System.out.println("I have changed that line to read: ");
System.out.println(text.replaceFirst("hate", "love"));
keyboard.close();
}
import java.util.Scanner;
public class AssignmentOneVerticalDisplay {
public static void main(String[] args) {
// TODO Auto-generated method stub
int userInput;
System.out.println("Please enter a 4 digit integer.");
Scanner keyboard = new Scanner(System.in);
userInput = keyboard.nextInt();
System.out.println(userInput / 1000);
userInput = userInput % 1000;
System.out.println(userInput / 100);
userInput = userInput % 100;
System.out.println(userInput / 10);
System.out.println(userInput % 10);
keyboard.close();
}
}
I basically just copied and pasted 2 programs I created. If anybody can help guide me in the correct direction here that would be great.
You can use the Double.parseDouble(String string); function together with a try-Catch to check if it was a number or a string in the input.
(...)
String text = keyboard.nextLine();
try {
//We try and assume that it is a number
Double number = Double.parseDouble(text);
/**
* Do stuff with the number like in the 1st program
*/
}
catch (NumberFormatException e)
{
//The input turned out not to be a number.
/**
* Do stuff here with the string like the 2nd program
*/
}
Am not really certain what your trying to accomplish, but if it's really necessary you combine all three classes together try using Java Inner Classes
I think that your professor is looking for a more object oriented solution. You can create a class that contains the three programs as separated methods like this
import java.util.scanner;
public class AssignmentScanner {
public double convertToCelsius() {
Scanner keyboard = new Scanner(System.in);
double degreesF = keyboard.nextDouble();
keyboard.close();
return 5*(degreesF - 32)/9;
}
public String replaceHate() {
Scanner keyboard = new Scanner(System.in);
String text = keyboard.nextLine();
String replacedText = text.replaceFirst("hate", "love");
keyboard.close();
return replacedText;
}
public int oneVerticalDisplay() {
Scanner keyboard = new Scanner(System.in);
int userInput = keyboard.nextInt();
System.out.println(userInput / 1000);
userInput = userInput % 1000;
System.out.println(userInput / 100);
userInput = userInput % 100;
System.out.println(userInput / 10);
System.out.println(userInput % 10);
keyboard.close();
}
}
You still need to create a main program that use this object like this:
public class AssignmentMain {
public static void main(String[] args) {
AssignmentScanner assignmentScanner = new AssignmentScanner();
System.out.println("Hello, I can convert Fahrenheit to Celsius!");
System.out.println("Please enter the degrees Fahrenheit you want converted.");
double degreesC = assignmentScanner.convertToCelsius();
System.out.println("The temperature in Degrees Celsius is: ");
System.out.printf("%.2f", degreesC);
System.out.println("Please enter a line containing 'hate'.");
String replacedText = assignmentScanner.replaceHate();
System.out.println("I have changed that line to read: ");
System.out.println(replacedText);
System.out.println("Please enter a 4 digit integer.");
assigmentScanner.oneVerticalDisplay();
}
}
This way your main program only knows about the AssignmentScanner and its three methods. This make the main program easier to read and maintain. There is still room for improvement but i think it's OK for a first approach.

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