Reputation:
Long story I can't figure out how to load the jet data jetid
and currentLocation
into j
. Any help is appreciated. I apologize before hand if I referred to things wrongly. Thank you.
public class Corporation {
public Jet[] jets = new Jet[14];
public Corporation()//RUN A CONSTRUCTOR TO CREATE 14 NEW JETS, IN DIFFERENT LOCATIONS
{
//create 14 new jets
for(int i =0; i < jets.length; i++)
{
//make a new jet here
Jet j = new Jet();
j.jetid =
}
}
}
The second class the one I'm trying to pull jetid
and currentLocation
from is:
public class Jet {
public int jetid;
public Delivery deliveryArray[];
public String currentLocation; //where it's currently sitting
public Jet()
{
int random = (int)(Math.random()*10000);
this.jetid = random;
this.currentLocation = setCurrentLocation();
System.out.println("NEW JET IS ONLINE WITH JET ID: " + this.jetid + " AT LOCATION " + this.currentLocation);
}
private String setCurrentLocation()
{
//set up a random # that determines which city the plane is based in
double random = (Math.random());
String location = " ";
if(random < .1 && random > 0)
location = "PHX";
else if(random > .1 && random < .2)
location = "SAN";
else if(random > .2 && random <.3)
location = "LAX";
else if(random > .3 && random < .4)
location = "POR";
else if(random > .4 && random < .5)
location = "SEA";
else if(random > .5 && random <.6)
location = "SAF";
else
location = "DAL";
return location;
}
}
Upvotes: 1
Views: 78
Reputation:
Thanks everyone for the input. Other problems aside I settled on this code shortly after posting this question:
Jet j = new Jet();
jets[i]=j;
And from there I've been able to figure out all other issues. Thanks again everyone for your input
Upvotes: 1
Reputation: 1123
If i have understood your question correctly, you want to set jetid
and currentLocation
, than you need to change your Jet
constructor like the following:
public Jet(int jetid, String currentLocation)
{
this.jetid = jetid;
this.currentLocation = currentLocation;
}
Upvotes: 0
Reputation: 8036
Not sure what error you are getting, I assume, your constructor:
public Corporation()//RUN A CONSTRUCTOR TO CREATE 14 NEW JETS, IN DIFFERENT LOCATIONS
{
//create 14 new jets
for(int i =0; i < jets.length; i++)
{
//make a new jet here
Jet j = new Jet();
j.jetid =
}
}
Should be the following, if you declared a local j[]
in your Corporation class
public Corporation()//RUN A CONSTRUCTOR TO CREATE 14 NEW JETS, IN DIFFERENT LOCATIONS
{
//create 14 new jets
for(int i =0; i < jets.length; i++)
{
//make a new jet here
Jet j[i] = new Jet();
//j.jetid = no need to set this
}
}
The other thing wrong is the j.jetid =
. But you do not need to set it, as it is already set in the Jet
constructor to this.jetid = random;
. Happens every time you instantiate a new Jet
in Jet j[i] = new Jet();
Upvotes: 0
Reputation: 263
you need to pass the variables to the jet class constructor!
Jet jet = new Jet(string _jetID, string _currentLocation)
Upvotes: 0
Reputation: 11710
You probably want to pass things in through the constructor:
public Jet(int jetid, String currentLocation)
{
this.jetid = jetid;
this.currentLocation = currentLocation;
}
Now you can write the following in your corporation class:
for(int i =0; i < jets.length; i++)
{
//make a new jet here
int randomId = (int)(Math.random()*10000);
String randomLocation = randomLocation();
jets[i] = new Jet(randomId, randomLocation);
}
That randomLocation
is just your old setCurrentLocation
method, from your Jet class. You should rename it and move it into your Corporation class, since Corporation is now responsible for making up random locations. Or you could rename it and leave it in Jet, and make it public static, and you could implement it in just 2 lines:
public static String randomLocation() {
// "DAL" is twice as likely as the others:
String[] array = {"ABC", "DEF", "DAL", "DAL"};
return array[new Random().nextInt(array.length)];
}
and then invoke it as follows:
String randomLocation = Jet.randomLocation();
Upvotes: 0
Reputation: 7753
The corporation object will have an array with 14 references to the Jet object.
public class Corporation {
private Jet[] jets = new Jet[14];
// Constructor
// RUN A CONSTRUCTOR TO CREATE 14 NEW JETS, IN DIFFERENT LOCATIONS
public Corporation() {
//create 14 new jets
for (int i =0; i < jets.length; i++)
{
//make a new jet here
Jet j = new Jet();
//save the reference to array
jets[i] = j;
// more Jet intialization
j.id = ...
}
}
// accessor
public Jet [] getJets() {
...
}
}
Upvotes: 0
Reputation: 153
The code appears to create 14 jets, all of which initialise their own data (e.g. they already have jetId and current location). The data's already there, the Corporation doesn't need to set it.
If you're saying you want the corp to create it, then create a new constructor, Jet(int jetId, String currentLocation) and you can pass the data in.
Upvotes: 0