Results 1 to 5 of 5
  1. #1
    beserie is offline Member
    Join Date
    Dec 2007
    Posts
    5
    Rep Power
    0

    Default Function in a variable

    Here is my problem :

    I am trying to model a growing city having different types of buildings.
    The only deference between the buildings is the formula to calculate the cost necessary to go up one level.

    in extremely simple it makes something like this :
    class Building {
    private int initialCost;
    private << fucntion to calculate next level's cost>>;
    private currentLevel;
    public Building( int initialCost, ...
    ...
    public int grow() // sould return the cost of the new constructiion and increment the level.

    I would like to use the same class to model all the buildings. using the constructor to passe the growing function to the object

    the growing function could be somthing like :
    cost = initialCost * (level+2)
    --or--
    cost = initialCost ^level
    --or--
    anything like that. involving initialCost, the level, and possibly a few constants.


    how whoul I define that variable ? how do I use it ? is this possible? how else could I do ?

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Hello beserie.

    If I understand your problem correctly, you want to create a class to represent a building where buildings only differ by their formula. I would create another class to represent formulas. One that can create and solve them. The problem is that this could be difficult and time consuming.

    In one of the languages I used, it was possible to create a delegate object from an existing method. That delegate object can call a method no matter what it is, as long as it has the correct signature. That means that you can program the formulas you need yourself and "pass" them to the constructor of your building class. That constructor can then use these methods as the formulas that it needs and do calculations. I know this sounds funny, but C# supported this.

    If this is possible in java and someone on Java Forums would like to help, then your problem could be very easy to solve. Unfortunatly, I'm new to java and I can't do this type of programmng using java. :p
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    It ain't ruby !!
    You can achieve this using call back, mess of codes ..

    Java Code:
    public class Main {
    	double initialCost ;
    	CostCalculator cost1 ;
    	CostCalculator cost2 ;
    	
    	Main(){
    		cost1 = new CostCalculator(){
    			@Override
    			public double Calculate(double initialCost) {
    				return initialCost * 2 ;
    			}
    			
    		};
    		
    		cost2 = new CostCalculator(){
    			@Override
    			public double Calculate(double initialCost) {
    				return initialCost * 3 ; // What ever
    			}			
    		};	
    	}
    	
    	public double calculate(){
    		return cost1.Calculate(initialCost); // Plus extra stuffs if you have
    	}
    }
    If i have understood your problem correctly, this is one of the option you have got.
    dont worry newbie, we got you covered.

  4. #4
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Okay

    Quote Originally Posted by roots View Post
    It ain't ruby !!
    I looked into this. Looks like delegates are not supported by Java. I found a web page with a delegate class that can be used in Java. It's a bit complicated for me, but here is the link:
    Alex Winston's Blog: Strongly Typed Java Delegates
    Thank you for answering me roots. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  5. #5
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Quote Originally Posted by tim View Post
    Looks like delegates are not supported by Java.
    If you are talking about Delegation pattern - Wikipedia, the free encyclopedia it is object oriented design pattern rather than a language feature. Ruby and Java script supports passing variables directly unlike java.
    dont worry newbie, we got you covered.

Similar Threads

  1. use of onclick function
    By m4tt in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 03:03 AM
  2. Need a little help with a function!
    By Nuluvius in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 11:33 PM
  3. I want to add function
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:25 AM
  4. function name
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 08:56 PM
  5. Help with rebound function
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:12 AM

Posting Permissions

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