Reputation: 137
I'm currently making a depression-simulation program where I created Human objects that have multiple parameters which alter the way the depression will affect them.
My issue is that I am able to create singular human objects, such as
Human steve = new Human("Steve", 25, 'm', 97000, 1, 3, false, false, 1, Human.happiness);
but I do not know how to create Human arrays that include the parameters that a single Human has. For example, I can create a Human array Human[] test = new Human[3];
with 3 Humans in it, but I do not know how to provide those Humans with the parameters.
import java.util.ArrayList;
public class Human {
private String name; // your name
private int age; // how old you are
private int wealth; // your average yearly income
private int amtFriendsFamily; // how many friends/family members you are
// close to
private int levelOfEducation; /*
* 1 = no education, 2 = high school
* education, 3 = college education, 4 =
* upper level college education
*/
private boolean married;
private boolean seeksDoctor; // true = seeks a doctor, false = does not seek
// a doctor
private char sex; // m = male, f = female
private int race; // 1 = white, 2 = black, 3 = hispanic, 4 = asian, 5 =
// mideast
static int happiness = 75;
private String sSex, sMarried, sDoct, sRace, sEdu;
private String[] names = { "Sally", "Karl", "Steven", "Emily" };
public Human(String name1, int age1, char sex1, int wealth1, int amtFriendsFamily1, int levelOfEducation1,
boolean married1, boolean seeksDoctor1, int race1, int happiness1) {
name = name1;
age = age1;
wealth = wealth1;
amtFriendsFamily = amtFriendsFamily1;
levelOfEducation = levelOfEducation1;
married = married1;
seeksDoctor = seeksDoctor1;
sex = sex1;
race = race1;
happiness = happiness1;
if (sex == 'm')
sSex = "male";
else if (sex == 'f')
sSex = "female";
else
sSex = "foreign gender";
if (married == false)
sMarried = "is not married, ";
else
sMarried = "is married, ";
if (seeksDoctor == false)
sDoct = "does not seek doctors.";
else
sDoct = "seeks doctors.";
if (race == 1)
sRace = "White";
else if (race == 2)
sRace = "Black";
else if (race == 3)
sRace = "Hispanic";
else if (race == 4)
sRace = "Asian";
else if (race == 5)
sRace = "Middle Eastern";
if (levelOfEducation == 1)
sEdu = " has no formal education, ";
else if (levelOfEducation == 2)
sEdu = " has a high school education, ";
else if (levelOfEducation == 3)
sEdu = " has a college education, ";
else if (levelOfEducation == 4)
sEdu = " has an upper-college level or higher education, ";
System.out.println(name + " is a " + age + " year old " + sRace + " " + sSex + " who makes $" + wealth
+ " per year," + " has " + amtFriendsFamily + " friends and family members combined," + sEdu + sMarried
+ "and " + sDoct);
}
}
Upvotes: 0
Views: 64
Reputation: 50809
You can do something like this
// doesn't need size in initialization, the compiler can count the number of new Human instances
Human[] test = new Human[] {
new Human(...), new Human(...), new Human(...)
}
Or use indexes
Human[] test = new Human[3];
test[0] = new Human(...);
Upvotes: 2
Reputation: 1232
I think what you're looking for is along these lines:
Human[] test = new Human[3];
Human temp = new Human("Steve", 25, 'm', 97000, 1, 3, false, false, 1, Human.happiness);
test[0] = temp;
Just redefine temp
for each object in the array and then set one of the objects in the array equal to temp
. Of course, it would be preferable to do this in a loop, but in this case that would only be possible if the program grabs user/file input for each Human
object.
Upvotes: 1