Results 1 to 5 of 5
  1. #1
    casta is offline Member
    Join Date
    Apr 2016
    Posts
    27
    Rep Power
    0

    Default How to use one class' objects as variable for other class

    Hi guys. I think I have a fairly easy question on objects and variables. I got three classes, one main class (class with main method) and the two other classes called Region and Train.
    The second class is named is Region. Each Region-object has its own variables, and some variables I will use to make calculations with. Examples are the variables averageSpeedInRegion, averageNumberOfCustomers, etc.
    In the class with my main method I want to assign trains to specific regions. Trains are assigned to specific zones, and based on their regions, they will take a specific time to go from one location to the other (based on their average speed).

    How do I assign a train to a specific Region, and thus give it an average speed based on this region?

    Example:
    Region A1
    Averagespeed = 40 mph

    Train T1 rides a track in region A1. Average speed is 40 mph in this region, thus averagespeed of train T1 is 40 mph.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: How to use one class' objects as variable for other class

    I would say the Train should know what region it is in, so should have a reference to the Region.
    When a Train is added to a Region it would (in my mind) be added to a Set<Train> in the Region, and the Region would setRegion on the Train, passing itself in.
    The Train would then be able to request the average speed off the Region.

    This is off the top of my head, so treat with suspicion!
    :)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to use one class' objects as variable for other class

    Quote Originally Posted by casta View Post
    How do I assign a train to a specific Region, and thus give it an average speed based on this region?
    You don't! You assign a region to a train (like Toll's said). Then the train simply interrogates its region object reference to
    determine how it is to operate in that region.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    casta is offline Member
    Join Date
    Apr 2016
    Posts
    27
    Rep Power
    0

    Default Re: How to use one class' objects as variable for other class

    Okay. How would I do that? In my mind, I would do something like this:


    Java Code:
    public class Region {
    
    	String regionName;
    
    	double speedInRegion; 
    
    	
    	static void initiateRegions(){
    		Region CaW = new Zone();
    		CaW.regionName = "Carolina West";
    		CaW.speedInRegion = 40.00;
    How would I refer to the correct region? What I wrote below certainly does not work
    Java Code:
    		Region.initiateRegions(); //Create all regions with their corresponding speeds
    		Train t1 = new Train();
    		t1.regionActive = CaW;

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to use one class' objects as variable for other class

    Let me amend what I said. Let's say you have a region class. You can either add the train to a specific region and then within that region, let the train obtain an instance of the region. Or you can just add an instance or the region to the train. Either way you need to know ahead of time which train will operate in which region.

    So your train class would need a method which take a region object as an argument.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 14
    Last Post: 03-25-2015, 10:20 AM
  2. Replies: 2
    Last Post: 01-08-2014, 06:09 PM
  3. Replies: 5
    Last Post: 12-18-2011, 05:06 AM
  4. Replies: 0
    Last Post: 10-23-2011, 11:49 PM
  5. Replies: 5
    Last Post: 01-26-2011, 07:38 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
  •