I am working on a program that should convert between in, ft, mi, mm, cm, m, and km. The question is:

Write a unit conversion program that asks users to identify the unit from which they want to convert and the unit to which they want to convert. Legal units are in, ft, mi, mm, cm, m, and km. Declare two objects of a class UnitConverter that convert between meters and a given unit.

Convert from:
in
Convert to:
mm
Value:
10
10 in = 254 mm

Use the following class as your main class:

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

You need to supply the following class in your solution:

UnitConverter

Me and some of my fellow partners have been working on this for a good while and this is what we have come up with for the UnitConvert class.

public class UnitConverter 
{
 static double INCHES = 39.37;
 static double FEET = 3.28;
 static double MILES = 0.00062;
 static double MILLIMETERS = 1000;
 static double CENTIMETERS = 100;
 static double METERS = 1;
 static double KILOMETERS = 0.001;
 private double val ,meters ,converted;
 String fromUnit, toUnit;
 
 public UnitConverter(String afromUnit)
 {
  fromUnit = afromUnit;
  toUnit = afromUnit;
 }
	
 public double toMeters(double val) 
 {
  if(toUnit.equals("in"))
  {
   meters = (1/INCHES);
  }
  else if(toUnit.equals("ft"))
  {
   meters = (1/FEET);
  }
  else if(toUnit.equals("mi"))
  {
   meters = (1/MILES);
  }
  else if(toUnit.equals("mm"))
  {
   meters = (1/MILLIMETERS);
  }
  else if(toUnit.equals("cm"))
  {
   meters = (1/CENTIMETERS);
  }
  else if(toUnit.equals("m"))
  {
   meters = (1/METERS);
  }
  else
  {
   meters = (1/KILOMETERS);
  }
  return meters;
 }
 
 public double fromMeters(double meters) 
 {
  if(fromUnit.equals("in"))
  {
   converted = Math.round(meters*100*val);
  }
  else if(fromUnit.equals("ft")) 
  {
   converted = Math.round(meters*100*val);
  }
  else if(fromUnit.equals("mi"))
  {
   converted = Math.round(meters*100*val);
  }
  else if(fromUnit.equals("mm")) 
  {
   converted = Math.round(meters*100*val);
  }
  else if(fromUnit.equals("cm")) 
  {
   converted = Math.round(CENTIMETERS*val);
  }
  else if(fromUnit.equals("m")) 
  {
   converted = Math.round(METERS*val);
  }
  else 
  {
   converted = Math.round(KILOMETERS*val);
  }
  return converted;
 }	
}

The code compiles and runs but when you try to do the inputs is just returns 10.0 in = 0.0 mm. The ConversionCalculator cannot be modified. Any help would be appreciated if you need more information please let me know I tried to supply everything I could think of.

Recommended Answers

All 15 Replies

Try debugging the code by adding printlns to show the values of variables and expressions as the code executes. The print out will show you where the program is not doing what you want it to do.

Copy and paste here the console from when you execute the program.

Try debugging the code by adding printlns to show the values of variables and expressions as the code executes. The print out will show you where the program is not doing what you want it to do.

Copy and paste here the console from when you execute the program.

I have debugged the program and it turns out for some reason the program is working up until it gets to the fromMeters in which the val variable is equal to 0. I have tryed many different things and I can not get the variable to carry over and equal anything but 0.

I can not get the variable to carry over

Where is the variable val given a value?
Where do you try to access the variable?

Are there multiple copies of the variable, each in a different instance of the class?

Where is the variable val given a value?
Where do you try to access the variable?

Are there multiple copies of the variable, each in a different instance of the class?

The variable val is given a value here when the user gives the input from the ConversionCalculator.

System.out.println("Value:");
double val = in.nextDouble();

I need to access the variable on the UnitConverter class at the fromMeters method. I tryed adding a line of code where I create a new variable called VALUE and set it equal to val but I guess i don't totally understand how to pass the variable from the ConversionCalculator to the UnitConverter. I can set the val = 10 on the UnitConverter and it will give the outcome I am needing but I need the val to be set from the users input. Sorry If im making this harder than it should be I have alot of time and head ache in this problem lol its starting to wear me out.

double val = in.nextDouble();

That variable val is local to the method it is in. It is not the same variable as any other variable named: val defined in any other classes and methods.

Where do you have the value that you want to use in the method that is doing the conversion?
If you read it in from the user, then you need to pass it to the method that is to use it in the conversion.

Member Avatar for rnlds.walfrets

try to fix this part of your code..

UnitConverter from = new UnitConverter(fromUnit);
 UnitConverter to = new UnitConverter(toUnit);

and

public UnitConverter(String afromUnit)
 {
  fromUnit = afromUnit;
  toUnit = afromUnit;
 }

which value, stores in which variable??

Member Avatar for rnlds.walfrets

solved it already??

I am stuck on the same problem. I tried passing the value entered into each instance of the fromUnit method by multiplying each case by val and still get 0.0 as well.

try to fix this part of your code..

UnitConverter from = new UnitConverter(fromUnit);
 UnitConverter to = new UnitConverter(toUnit);

The above code was provided and is not to be changed, so I assume it is correct.

public UnitConverter(String afromUnit)
 {
  fromUnit = afromUnit;
  toUnit = afromUnit;
 }

which value, stores in which variable??

here I think toUnit = afromUnit; doesn't need to be here. even with removing it, the result is still the same when running the program.

Thanks everyone for the help and speedy replies when I needed it. I finally got the program working and I am posting the code so in case someone either just wants it or needs it I hope it can help someone else out in the future.

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

How do you deal with precision?

Convert from:
cm
Convert to:
m
Value:
10

10.0 cm = 0.0 m

Which isn't accurate as it should return 0.1m.

Hi Az, welcome to DaniWeb.
This thread has been dead for a year now, so it's not a good idea to hijack it.
Please start a new thread for your question, and someone will help.

so much else if,maybe you can use switch instead

what is the parpose of unit Convrter project eplain me in breif for the purpose....

Well, the answer is right there in the original (solved) post from four years ago which states quite clearly: "asks users to identify the unit from which they want to convert and the unit to which they want to convert."

Was there actually something else you wanted to ask? If so, you would be well advised to start a new thread to do so.

All the best
Davey

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.