I am working on a unit converter i have got my code to start working but now i a
ID: 3683617 • Letter: I
Question
I am working on a unit converter i have got my code to start working but now i am having problems figuring out the best way to implement the rejection of imcompatible conversions. this is the code i have so far:
import java.util.Scanner;
/**
This class converts between two units.
*/
public class ConversionCalculator
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Convert from:");
String fromUnit = in.nextLine();
System.out.println("Convert to: ");
String toUnit = in.nextLine();
UnitConverter from = new UnitConverter(fromUnit);
UnitConverter to = new UnitConverter(toUnit);
System.out.println("Value:");
double val = in.nextDouble();
double meters = from.toMeters(val);
double converted = to.fromMeters(meters);
System.out.println(val + " " + fromUnit + " = " + converted + " " + toUnit);
}
}
this is the UnitConverter:
public class UnitConverter
{
static double INCHES = 0.0254001;
static double FEET = 0.3048;
static double MILES = 1609.35;
static double MILLIMETERS = 0.001;
static double CENTIMETERS = 0.01;
static double METERS = 1;
static double KILOMETERS = 1000;
private double val ,meters ,converted;
String afromUnit;
public UnitConverter(String fromUnit)
{
afromUnit = fromUnit;
}
public double toMeters(double val)
{
if(afromUnit.equals("in"))
{
meters = (val*INCHES);
}
else if(afromUnit.equals("ft"))
{
meters = (val*FEET);
}
else if(afromUnit.equals("mi"))
{
meters = (val*MILES);
}
else if(afromUnit.equals("mm"))
{
meters = (val*MILLIMETERS);
}
else if(afromUnit.equals("cm"))
{
meters = (val*CENTIMETERS);
}
else if(afromUnit.equals("m"))
{
meters = (val*METERS);
}
else
{
meters = (val*KILOMETERS);
}
return meters;
}
public double fromMeters(double meters)
{
if(afromUnit.equals("in"))
{
converted = Math.round(meters*39.369923740457715);
}
else if(afromUnit.equals("ft"))
{
converted = Math.round(meters*3.280839895013123);
}
else if(afromUnit.equals("mi"))
{
converted = Math.round(meters*0.0006213688756330196);
}
else if(afromUnit.equals("mm"))
{
converted = Math.round(meters*1000);
}
else if(afromUnit.equals("cm"))
{
converted = Math.round(meters*100);
}
else if(afromUnit.equals("m"))
{
converted = Math.round(meters*1);;
}
else
{
converted = Math.round(meters*0.001);
}
return converted;
}
}
Explanation / Answer
Note:I have integrated two classes
This code is succesfully working for all conversions
ConversionCalculator.java
import java.util.Scanner;
/**
This class converts between two units.
*/
public class ConversionCalculator
{//main class
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);//key board inputting
System.out.println("Convert from:");//enter from unit name
String fromUnit = in.nextLine();
System.out.println("Convert to: ");//enter to unit name
String toUnit = in.nextLine();
UnitConverter from = new UnitConverter(fromUnit);//creating object and passing argument
UnitConverter to = new UnitConverter(toUnit);//creating object and passing argument
System.out.println("Value:");
double val = in.nextDouble();
double meters = from.toMeters(val);
double converted = to.fromMeters(meters);
System.out.println(val + " " + fromUnit + " = " + converted + " " + toUnit);
}
}
class UnitConverter
{
static double INCHES = 0.0254001;
static double FEET = 0.3048;
static double MILES = 1609.35;
static double MILLIMETERS = 0.001;
static double CENTIMETERS = 0.01;
static double METERS = 1;
static double KILOMETERS = 1000;
private double val ,meters ,converted;
String afromUnit;
public UnitConverter(String fromUnit)
{//constructor with arguments
afromUnit = fromUnit;
}
public double toMeters(double val)
{//method with returns double and one argument
//which converts meters into other converisons
if(afromUnit.equals("in"))
{//which converts inches into meters
meters = (val*INCHES);
}
else if(afromUnit.equals("ft"))
{//which converts feet into meters
meters = (val*FEET);
}
else if(afromUnit.equals("mi"))
{//which converts miles into meters
meters = (val*MILES);
}
else if(afromUnit.equals("mm"))
{//which converts millimeters into other meters
meters = (val*MILLIMETERS);
}
else if(afromUnit.equals("cm"))
{//which converts centimeterss into meters
meters = (val*CENTIMETERS);
}
else if(afromUnit.equals("m"))
{
meters = (val*METERS);
}
else if(afromUnit.equals("km"))
{//km into meters
meters = (val*KILOMETERS);
}
else
{
System.out.println("INcompetable conversion");
System.exit(1);
}
return meters;
}
public double fromMeters(double meters)
{ //which converts meters into other converisons
if(afromUnit.equals("in"))
{//inches into any other unit
converted = Math.round(meters*39.369923740457715);
}
else if(afromUnit.equals("ft"))
{//feet into any other unit
converted = Math.round(meters*3.280839895013123);
}
else if(afromUnit.equals("mi"))
{//miles into any ther unit
converted = Math.round(meters*0.0006213688756330196);
}
else if(afromUnit.equals("mm"))
{//millimeters into any other unit
converted = Math.round(meters*1000);
}
else if(afromUnit.equals("cm"))
{//cm into any other unit
converted = Math.round(meters*100);
}
else if(afromUnit.equals("m"))
{//meters into meters
converted = Math.round(meters*1);;
}
else if(afromUnit.equals("km"))
{
converted = Math.round(meters*0.001);
}
else
{
System.out.println("Incompetable conversion");
System.exit(1);
}
return converted;
}
}
output
run:
Convert from:
gal
Convert to:
m
Value:
1
INcompetable conversion
Java Result: 1
BUILD SUCCESSFUL (total time: 7 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.