Results 1 to 10 of 10
- 11-15-2010, 02:01 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
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
any help would be greatly appreciatedJava 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); } }Last edited by Eranga; 11-15-2010 at 02:22 AM. Reason: code tags added
- 11-15-2010, 02:25 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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?
- 11-15-2010, 02:29 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
okay yes that sounds like a good idea to me
- 11-15-2010, 02:57 AM #4
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
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
- 11-15-2010, 03:17 AM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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.
- 11-15-2010, 03:32 AM #6
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
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.*/
- 11-15-2010, 03:56 AM #7
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
What is the use of val? You declared with a FIX value of ZERO then you passed it to inVal{ public UnitConverter()
{
unitTo= null;
unitFrom= null;
val=0;
}
public UnitConverter(String inUnitFrom, String inUnitTo, double inVal)
{
unitFrom = inUnitFrom;
unitTo= inUnitTo;
inVal = val;
}
which makes it ZERO also.
Remember that val has a value of zero and you multiply it to M_IN_FT. That makes the result ZERO as alwaysvalInMeters = val*M_IN_FT;
- 11-15-2010, 03:59 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 11-16-2010, 01:48 AM #9
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
yeah i fixed it thanks alot
- 11-19-2010, 05:57 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You are welcome. :)
If you've solved the problem, then please mark the thread solved from tools menu.
Similar Threads
-
Have I done this exercise right?
By ccie007 in forum New To JavaReplies: 7Last Post: 09-28-2010, 05:54 PM -
Exercise for java 3d
By armiri in forum Java SoftwareReplies: 3Last Post: 05-13-2010, 11:13 PM -
Prob with an exercise
By jhetfield18 in forum New To JavaReplies: 4Last Post: 02-15-2008, 06:11 PM -
I/O exercise
By Feldom in forum New To JavaReplies: 1Last Post: 10-28-2007, 04:48 PM -
help with exercise
By e_as're in forum New To JavaReplies: 3Last Post: 09-25-2007, 10:14 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks