Skip to content

Instantly share code, notes, and snippets.

@GrunclePug
Created May 26, 2019 08:26
Show Gist options
  • Save GrunclePug/80d4bfbcf39b3e1f6426698af0726adc to your computer and use it in GitHub Desktop.
Save GrunclePug/80d4bfbcf39b3e1f6426698af0726adc to your computer and use it in GitHub Desktop.
COMP 1409 | Lab04
/**
* Class to model a Person
*
* Lab 4
*
* @author Chad
* @version 1.3
*/
public class Person
{
private String firstName = "John";
private String lastName = "Doe";
private int age = 1;
private double height = 150.0;
public static final int MAX_HEIGHT = 200;
public static final double MAX_AGE = 100;
private static int counter = 0;
/**
* Default constructor.
*/
public Person()
{
counter++;
}
/**
* Constructor for creating a Person instance with just first name and last name.
*
* @param _firstName The first name of our Person
* @param _lastName The first last of our Person
*/
public Person(String _firstName, String _lastName)
{
setFirstName(_firstName);
setLastName(_lastName);
counter++;
}
/**
* Constructor for creating a Person instance.
*
* @param _firstName The first name of our Person
* @param _lastName The last name of our Person
* @param _age The age of our Person
* @param _height The height of our Person
*/
public Person(String _firstName, String _lastName, int _age, double _height)
{
setFirstName(_firstName);
setLastName(_lastName);
setAge(_age);
setHeight(_height);
counter++;
}
/**
* Sets the first name of this person.
*
* @param _firstName The new first name.
*/
public void setFirstName(String _firstName)
{
firstName = _firstName;
}
/**
* Sets the last name of this person.
*
* @param _lastName The new last name.
*/
public void setLastName(String _lastName)
{
lastName = _lastName;
}
/**
* Sets the age of this person.
*
* Must be positive and less than or equal to 100.
*
* @param _age The new age to set.
*/
public void setAge(int _age)
{
if(_age > 0 && _age < MAX_AGE)
{
age = _age;
}
}
/**
* Sets the height of this person
*
* Must be positive and less than or equal to 200.
*
* @param _height The new height to set.
*/
public void setHeight(double _height)
{
if(_height > 0 && _height <= MAX_HEIGHT)
{
height = _height;
}
}
/**
* Get the first name of this person.
*
* @return The first name of this person.
*/
public String getFirstName()
{
return firstName;
}
/**
* Get the last name of this person.
*
* @return The last name of this person.
*/
public String getLastName()
{
return lastName;
}
/**
* Get the age of this person.
*
* @return The age of this person.
*/
public int getAge()
{
return age;
}
/**
* Get the height of this person.
*
* @return The height of this person.
*/
public double getHeight()
{
return height;
}
/**
* Grab details of this person
* @return A string showing the full name and age of this person.
*/
public String getDetails()
{
return "Hello my name is " + firstName + " " + lastName + " and my age is " + age;
}
/**
* @return The number of times a Person object was instantiated.
*/
public String getNumberOfTimesCreated()
{
String howMany = "too many times!";
switch(counter)
{
case 0:
howMany = "none";
break;
case 1:
howMany = "once";
break;
case 2:
howMany = "twice";
break;
}
return howMany;
}
}
/**
* A class representing a playground containing people.
*
* Lab 4
*
* @author Chad
* @version 1.0
*/
public class Playground
{
//Constants
public static final int AGE_OF_SENIORITY = 65;
public static final int NEW_AGE = 73;
public static final String NEW_FIRST_NAME = "Oliver";
public static final String NEW_LAST_NAME = "Tree";
public static final double NEW_HEIGHT = 120.0;
/**
* Add people to the playground
*/
public void run()
{
Person person1 = new Person();
String defaultDetails = "DEFAULT VALUES" +
"\nAge: " + person1.getAge() +
"\nName: " + person1.getFirstName() + " " + person1.getLastName() +
"\nHeight: " + person1.getHeight() + "cm";
System.out.println(defaultDetails);
person1.setAge(NEW_AGE);
person1.setFirstName(NEW_FIRST_NAME);
person1.setLastName(NEW_LAST_NAME);
person1.setHeight(NEW_HEIGHT);
String isOld = "Person is young!";
if(person1.getAge() > AGE_OF_SENIORITY)
{
isOld = "Person is old!";
}
String newDetails = "\nNEW VALUES" +
"\nAge: " + person1.getAge() +
"\nName: " + person1.getFirstName() + " " + person1.getLastName() +
"\nHeight: " + person1.getHeight() + "cm" +
"\nStatus: " + isOld;
System.out.println(newDetails);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment