Results 1 to 12 of 12

Thread: Program help

  1. #1
    kevzspeare is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default Program help

    I am making a GUI program that has a class named TravellingPackage with data fields for age, travelling method, and travelling fare. This class has a constructors for the age and travelling method. Here is my original code with several problems displayed as comments:

    Java Code:
    public class TravellingPackage
    {
    	int CustomerID;
    	int Age;
    	int travelmethod;
    	double xfare;
    	InsuredPackage iota = new InsuredPackage();
    	String XName="";
    	public TravellingPackage(int id, String name, int tmethod, int age)
    	{
    		CustomerID=id;
    		XName=name;
    		travelmethod=tmethod;
    		Age=age;
    		calculateTravelFare(age, tmethod);
    	}
    	public void display()
    	{
    		//need to display all values in the fields (customer ID, name, travelmethod, age, total fare)
    	}
    	public int GetCustomerID()
    	{
    		return CustomerID;
    	}
    	public int Age()
    	{
    		return Age;
    	}
    	public String GetXName()
    	{
    		return XName;
    	}
    	public double GetInsurance()
    	{
    		return iota.addAdditionalCost(xfare);
    	}
    	public int calculateTravelFare(int age, int zmethod)
    	{
    		double cost = 0;
    		if(age == 1)
    		{
    			if (zmethod == 1)
    			{
    				cost = 0.00;
    			}
    			else if (zmethod == 2)
    			{
    				cost = 0.00;
    			}
    			else if (zmethod == 3)
    			{
    				cost = 100.00;
    			}
    		}
    		if(age == 2)
    		{
    			if (zmethod == 1)
    			{
    				cost = 40.00;
    			}
    			else if (zmethod == 2)
    			{
    				cost = 55.00;
    			}
    			else if (zmethod == 3)
    			{
    				cost = 700.00;
    			}
    		}
    		if(age == 3)
    		{
    			if (zmethod == 1)
    			{
    				cost = 60.00;
    			}
    			else if (zmethod == 2)
    			{
    				cost = 85.00;
    			}
    			else if (zmethod == 3)
    			{
    				cost = 1400.00;
    			}
    		}
    		return cost;//possible loss of precision (since cost is a double, and must be expressed with two decimal places)
    	}
    	
    }
    
    class InsuredPackage extends TravellingPackage
    {
    	public double addAdditionalCost(double cost)
    	{
    		double addCost = 0;
    		if(cost >= 0.01 && cost <= 39.99)
    			addCost=15.00;
    		else if(cost >= 40.00 && cost <= 100.00)
    			addCost=25.50;
    		else if(cost > 100.00)
    			addCost=45.50;
    		return addCost;
    	}
    }
    Last edited by kevzspeare; 03-23-2009 at 03:00 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What specifically is your question? Also, did you way you were developing a GUI? So far I don't see any GUI code. So without a GUI to display things in, the display method is a bit out of place and there really is no answer to the comment there. Regarding precision, many will tell you not to do financial calculations using double or any floating point type of number. Also, there's a big difference between a number and a String representation of a number. If you want to display currency, I suggest you look into using a NumberFormat object currency instance which you get by calling the NumberFormat.getCurrencyInstance() method. Best of luck.
    Last edited by Fubarable; 03-23-2009 at 03:03 AM.

  3. #3
    kevzspeare is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    Question: possible loss of precision, since i want the numbers to have two decimal places (example: 9.99)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Again, you can use a currency instance of a NumberFormat object. This will display the number plus a dollar sign. Other options include use of a DecimalFormat object or String.format(...). All of them will display a floating point number with 2 decimal places if used properly.

  5. #5
    kevzspeare is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    And then in the InsuredPackage, I get this error: TravellingPackage(int,java.lang.String,int,int) in TravellingPackage cannot be applied to ()
    class InsuredPackage extends TravellingPackage

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    The first thing the pops out is the following:
    Java Code:
    String travelmethod;
    .
    .
    .
    if (travelmethod == 1)
    In the above, you can't compare a String to an int. Either you make travelmethod an int or you make the number your comparing a string (if you do the later you will have to use the equals() method for the comparation).

    Java Code:
    public [B][COLOR="Blue"][B]int[/B][/COLOR][/B] calculateTravelFare(int age)
    	{
    	  [B][COLOR="red"]double[/COLOR][/B] cost = 0;
                   .
                   .
                   .
    	  return [B][COLOR="red"]cost[/COLOR][/B];	}
    The problem with the above is because you're trying to return a double (in red) when the method is saying it returns an int (in blue). Make the method indicate that it's returning a double.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    kevzspeare is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    it must be indicated as a float and how to do it in 2 decimal places?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    1) Never use float unless you have a compelling reason to do so. Instead if you need floating point numbers, use a double which is much more accurate at little cost.

    2) Don't worry about 2 decimal places until you are ready to display the number as a String. Really. Just ignore the issue until its time to display. Then again use one of the classes I mentioned above to format the displayed String for 2 decimal places.

    3) Another option, (to confuse the issue -- and probably is a better technique) though is to yes, worry about it now, and instead of using floating point numbers use ints or longs as to represent your currency as cents not dollars. Thus if you want to represent $25.40, use int value = 2540. When it comes time to display the value, simply divide by 100 and round the String by the methods that I mentioned above.
    Last edited by Fubarable; 03-23-2009 at 03:32 AM.

  9. #9
    kevzspeare is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    i solved the double/int/float problem, with the calculateTravelFare as a float instead of int.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Again, DON'T USE FLOAT. Thank you.

  11. #11
    kevzspeare is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    Okay. I changed the function into a double. Now on to the problem with thread #5 (about the InsuredPackage).

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'll bet the derived class doesn't directly call the super(....) constructor in its constructor. If it doesn't do that, Java will automatically call a default constructor on the super class TravellingPackage(). But there is no default constructor present with no parameters. Solution: let the first call of the derived class's constructor be a call to the super's constructor with super(...) and use proper parameters to match the parameters of the super's constructor definition.

    edit: your child class doesn't even have a constructor, and so Java will give it a default constructor and in this default constructor will call the super's default constructor. Solution: first and foremost, give you child class a constructor!

Similar Threads

  1. Execute A program from a Program!
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2009, 04:17 PM
  2. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 08:24 AM
  3. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  4. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

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