How to Add strings to this code?

Anonymous
timer Asked: Jun 8th, 2013
account_balance_wallet $5

Question Description

How to Add strings to this code?


Hi, I already have the code below but i have to do the following with it. I am making a Unit convertor program where I can convert CM to Inch and Fahrenheit to Celsius in same program.
I want to use strings. For example:- User will have different options for the unit converter they want to use.

Such as:

Option A: cm to inch
Option B: Fahrenheit to Celsius

If user enters letter “B” than program will convert Fahrenheit to Celsius. User will be asked to enter a value in Fahrenheit and once the value is entered it will convert it to Celsius.

Here is the code

CM to Inch


import java.util.Scanner;

public class FinalProject {
public static void main (String args[]){
// 2.54cm is 1 inch
Scanner cm = new Scanner(System.in); //Get INPUT from pc-Keyboard
System.out.println("Enter the CM:"); // Write input
//double
double centimeters = cm.nextDouble();
double inches = centimeters/2.54;
System.out.println(inches + " Inch Is " + centimeters + " centimeters");


}
}

Fahrenheit to Celsius



double celsius=0, fahrenheit=0;
Scanner scan = new Scanner(System.in);

System.out.println("Enter the amount of Fahrenheit to be converted: ");
fahrenheit = scan.nextDouble();

celsius = (fahrenheit-32)*5/9;

System.out.println("The entered amount of Fahrenheit is equal to " + celsius + " degrees Celsius.");



}
}

Tutor Answer

Codorna
School: UC Berkeley

It would be something like this :

public class FinalProject {

Public static Class Convert
{
public static double CmToIn(Double Value)
{
CmToIn= Value / 2.52;
}

public static double FarToCel(Double Value)
{
FarToCel = (Value-32) * 5/9;
}
}


public static void main (String args[]){
Scanner cm = new Scanner(System.in); //Get INPUT from pc-Keyboard
System.out.println("Enter 1 for cm to inch, 2 for far to celcius"); // Write input

if (cm.nextdouble() == 1)
{
result = Convert.CmToIn(cm.NextDouble());
System.out.println("The entered amount of centimeters is equal to " + result + " inches.");
}
else
{
result = Convert.FarToCel(cm.NextDouble());,
System.out.println("The entered amount of Fahrenheit is equal to " + result + " degrees Celsius.")

}

I've never worked with java, it probably needs some adjustments

flag Report DMCA
Review

Anonymous
Excellent job

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors