Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-27-2007, 06:55 PM
Member
 
Join Date: Dec 2007
Posts: 5
beserie is on a distinguished road
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 ?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-05-2008, 11:59 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-06-2008, 01:25 PM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
It ain't ruby !!
You can achieve this using call back, mess of codes ..

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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-06-2008, 07:49 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-07-2008, 05:12 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
use of onclick function m4tt New To Java 1 02-16-2008 05:03 AM
Need a little help with a function! Nuluvius New To Java 3 02-08-2008 01:33 AM
I want to add function romina New To Java 1 08-07-2007 07:25 AM
function name osval Advanced Java 1 08-06-2007 10:56 PM
Help with rebound function fernando New To Java 1 08-06-2007 09:12 AM


All times are GMT +3. The time now is 01:28 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org