Results 1 to 10 of 10
  1. #1
    ararar is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default Need help with a programming exercise

    So I'm very new to java and i dont know too much but i cant figure out how to make this program work correctly. I am supposed to take a unit of measurement and that the user inputs and convert it to any of several units that they desire using if statements. this is the class i have to far

    Java Code:
    public class UnitConverter
    {
     /**
      *Constructs a class that converts measurements to different units
      */
     public UnitConverter()
     {
      unitTo= null;
      unitFrom= null;
      val=0;
     }
     /**
      *Constructs a class that converts measurements to different units
      *@param inUnitFrom the unit to convert
      */
     public UnitConverter(String inUnitFrom, String inUnitTo, double inVal)
     {
      unitFrom = inUnitFrom;
      unitTo= inUnitTo;
      inVal = val;
     }
     public double getConversion()
     {
     	double finalVal=0;
     	double valInMeters=0;
      {
      if (unitFrom.equalsIgnoreCase("m"))
      valInMeters = val;
      else if (unitFrom.equalsIgnoreCase("ft"))
      valInMeters = val*M_IN_FT;
      else if (unitFrom.equalsIgnoreCase("mm"))
      valInMeters = val*M_IN_MM;
      else if (unitFrom.equalsIgnoreCase("cm"))
      valInMeters = val*M_IN_CM;
      else if (unitFrom.equalsIgnoreCase("in"))
      valInMeters = val*M_IN_IN;
      else if (unitFrom.equalsIgnoreCase("mi"))
      valInMeters = val*M_IN_MI;
      else if (unitFrom.equalsIgnoreCase("km"))
      valInMeters = val*M_IN_KM;
      else
      	valInMeters=0;
      }
      {
      	if (unitTo.equalsIgnoreCase("m"))
      	finalVal = valInMeters;
      else if (unitTo.equalsIgnoreCase("ft"))
      	finalVal = valInMeters/ M_IN_FT;
      else if (unitTo.equalsIgnoreCase("mm"))
      	finalVal = valInMeters/ M_IN_MM;
      else if (unitTo.equalsIgnoreCase("cm"))
      	finalVal = valInMeters/ M_IN_CM;
      else if (unitTo.equalsIgnoreCase("in"))
      	finalVal = valInMeters/ M_IN_IN;
      else if (unitTo.equalsIgnoreCase("mi"))
      	finalVal = valInMeters/ M_IN_MI;
      else if (unitTo.equalsIgnoreCase("km"))
      	finalVal = valInMeters/ M_IN_KM;
      	else
      		finalVal=0;
      }
      return finalVal;
     }
     private String unitTo;
     private String unitFrom;
     private double val;
    
     public static final double M_IN_FT = .3048;
     public static final double M_IN_MM = .001;
     public static final double M_IN_CM = .01;
     public static final double M_IN_IN = .0254;
     public static final double M_IN_MI = 1609.344;
     public static final double M_IN_KM = 1000;
    
    }
    
    
    and this is the tester
    import java.util.Scanner;
    public class UnitConverterTester23
    {
     public static void main (String[]args)
     {
      Scanner scan = new Scanner (System.in);
      System.out.println("Converting from:");
      String from = scan.next();
      System.out.println("Converting to:");
      String to = scan.next();
      System.out.println("Enter the value of the measurement");
      double val= scan.nextInt();
      UnitConverter conv1 = new UnitConverter(from, to, val);
      UnitConverter conv2 = new UnitConverter (to, from, val);
      System.out.println(val+ " " +from+ " = " +fjfjf+ " " +to);
     }
    }
    any help would be greatly appreciated
    Last edited by Eranga; 11-15-2010 at 02:22 AM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    25

    Default

    Lets start from the the beginning.

    So if I'm correct in your application, user can define units convert from-to with the value. I've collect that information using a scanner. Fine.

    But think about this. Use has to enter the unit, and sometimes them may confusing with it. So it's better to print kind of a legend that units you can handle from your application. What you think?

  3. #3
    ararar is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    okay yes that sounds like a good idea to me

  4. #4
    ararar is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    my problem is that everytime i go to convert it gives me 0.0 as the conversion and i do not know how to fix this. the instructions said something about defining two objects of the class that convert between meters and a given unit, but I'm not sure how to go about that

  5. #5
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    10

    Default

    Does this one compile?
    How did you know that it gives you zero value as result? Because I did not see in your code that you print the result. Also, I did not see that you call getConversion() method. And what is fjfjf? Did not see its declaration.

  6. #6
    ararar is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    yeah i was messing with the code the bottom should be

    UnitConverter conv1 = new UnitConverter(from, to, val);
    UnitConverter conv2 = new UnitConverter (to, from, val);
    System.out.println(val+ " " +from+ " = " +conv1.getConversion()+ " " +to);
    }
    }
    /*Output:
    --------------------Configuration: 5.14 - JDK version 1.6.0_21 <Default> - <Default>--------------------
    Converting from:
    cm
    Converting to:
    mm
    Enter the value of the measurement
    10
    10.0 cm = 0.0 mm

    Process completed.*/

  7. #7
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    10

    Default

    { public UnitConverter()
    {
    unitTo= null;
    unitFrom= null;
    val=0;
    }

    public UnitConverter(String inUnitFrom, String inUnitTo, double inVal)
    {
    unitFrom = inUnitFrom;
    unitTo= inUnitTo;
    inVal = val;
    }
    What is the use of val? You declared with a FIX value of ZERO then you passed it to inVal
    which makes it ZERO also.

    valInMeters = val*M_IN_FT;
    Remember that val has a value of zero and you multiply it to M_IN_FT. That makes the result ZERO as always

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    25

    Default

    My suggestion is to step down the process. Start from one conversion at a time. Take a piece of paper and write down your design, may be in a simple flow chart. Then move to the coding. Think about the user aspects as well.

  9. #9
    ararar is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    yeah i fixed it thanks alot

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,370
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Have I done this exercise right?
    By ccie007 in forum New To Java
    Replies: 7
    Last Post: 09-28-2010, 05:54 PM
  2. Exercise for java 3d
    By armiri in forum Java Software
    Replies: 3
    Last Post: 05-13-2010, 11:13 PM
  3. Prob with an exercise
    By jhetfield18 in forum New To Java
    Replies: 4
    Last Post: 02-15-2008, 06:11 PM
  4. I/O exercise
    By Feldom in forum New To Java
    Replies: 1
    Last Post: 10-28-2007, 04:48 PM
  5. help with exercise
    By e_as're in forum New To Java
    Replies: 3
    Last Post: 09-25-2007, 10:14 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •