Created
June 22, 2016 11:25
-
-
Save desrtfx/5937fcd95f378ba186efc4df85b5d83c to your computer and use it in GitHub Desktop.
Demonstration program for a simple Human class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Represents a single human being | |
* | |
* This class is basically only a storage | |
* there is no direct logic in this class | |
* | |
*/ | |
/** | |
* @author ATGEMET | |
* | |
*/ | |
public class Human { | |
/** | |
* Member variable for the Name of the Human | |
*/ | |
private String name; | |
/** | |
* Default constructor. | |
* | |
* Generates a new Human with an empty name (indicating that the Human is | |
* not named yet.) | |
*/ | |
public Human() { | |
name = ""; | |
} | |
/** | |
* Generates a new Human with the given name | |
* | |
* @param name the name for the new Human | |
*/ | |
public Human(String name) { | |
this.name = name; | |
} | |
/** | |
* Gives an existing Human a new name | |
* (think baptizing, marriage) | |
* | |
* @param name | |
*/ | |
public void setName(String name) { | |
this.name = name; | |
} | |
/** | |
* Returns the name of the Human | |
* | |
* @return the name of the Human | |
*/ | |
public String getName() { | |
return name; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see java.lang.Object#toString() | |
* | |
* This method is used when you want to print the | |
* information about a single Human | |
* | |
*/ | |
@Override | |
public String toString() { | |
return "My name is: " + name + "."; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
/** | |
* Stores several generated Human instances in an array | |
* | |
* | |
*/ | |
public class Humans { | |
/** | |
* The internal storage for the Human instances | |
*/ | |
private Human[] people; | |
/** | |
* A list of valid human names from which one will be picked | |
*/ | |
private static final String[] HUMAN_NAMES = { "Frank", "John", "Joe", | |
"Izobel", "June", "Camilla" }; | |
/** | |
* Just a random number generator - nothing special | |
*/ | |
private static Random generator = new Random(); | |
/** | |
* The default constructor for the Humans class | |
* | |
* In this constructor, a random number is generated which then is used to | |
* dimension an array of Human instances that consecutively will be filled | |
* with Human instances who are given random names. | |
*/ | |
public Humans() { | |
// generate a random amount between 1 and 3 | |
int numberOfHumans = Humans.generator.nextInt(3) + 1; | |
// dimension the storage for the new humans | |
people = new Human[numberOfHumans]; | |
// From here on, the actual Human instances are created | |
for (int i = 0; i < people.length; i++) { | |
// the following snippet explains the uncommented line in detailed | |
// steps: | |
// You can remove the block comment too activate the code | |
/* | |
* int randomNameIndex = | |
* Humans.generator(nextInt(Humans.HUMAN_NAMES.length); // generate | |
* a new random index inside the range of the HUMAN_NAMES array | |
* String randomName = Humans.HUMAN_NAMES[randomNameIndex]; // get a | |
* random name from the Array people[i] = new Human(); | |
* people[i].setName(randomName); | |
*/ | |
// the code in the line below condenses all four lines above into a | |
// single line | |
// When testing, make sure that either the above four lines, or the | |
// line below are active, not both! | |
people[i] = new Human( | |
Humans.HUMAN_NAMES[generator | |
.nextInt(Humans.HUMAN_NAMES.length)]); | |
} | |
} | |
/** | |
* @return the array of Human instances | |
*/ | |
public Human[] getpeople() { | |
return people; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see java.lang.Object#toString() | |
* | |
* This method prints a nice list of all the Human instances stored in the | |
* class | |
*/ | |
@Override | |
public String toString() { | |
// since it is bad to join String values with "+", I'll use a | |
// StringBuilder | |
// here | |
StringBuilder sb = new StringBuilder(); | |
// append some header information | |
sb.append("I have " + people.length | |
+ (people.length == 1 ? " person" : " people") + " in my list."); | |
sb.append("Let me introduce you to them:\n\n"); | |
// go through the array of people and let every member | |
// introduce themselves. | |
for (int i = 0; i < people.length; i++) { | |
sb.append("#" + i + " " + people[i] + "\n"); | |
} | |
// return the completed String | |
return sb.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This class only acts as a tester for the Humans class | |
* | |
* Basically, the main method here could be integrated in | |
* the Humans class - still it is not advisable to do so | |
* because if the class is to be reused, the main method | |
* could get in the way | |
* | |
*/ | |
public class HumanTester { | |
public static void main(String[] args) { | |
// create a new instance of the Humans | |
Humans people = new Humans(); | |
// print out its contents | |
System.out.println(people); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment