Created
May 26, 2019 08:23
-
-
Save GrunclePug/f994252df57f601a15a0ed27602d8c22 to your computer and use it in GitHub Desktop.
COMP 1409 | Lab03
This file contains hidden or 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
/** | |
* Class to model a Person | |
* | |
* Lab 3 | |
* | |
* @author Chad | |
* @version 1.2 | |
*/ | |
public class Person | |
{ | |
private static int counter = 0; | |
//Default Values | |
private static final String DEFAULT_FIRST_NAME = "John"; | |
private static final String DEFAULT_LAST_NAME = "Doe"; | |
private static final int DEFAULT_AGE = 1; | |
private static final double DEFAULT_HEIGHT = 150.0; | |
//Maximum Values | |
private static final int MAXIMUM_AGE = 100; | |
private static final double MAXIMUM_HEIGHT = 200.0; | |
//Instance Variables | |
private String firstName; | |
private String lastName; | |
private int age = DEFAULT_AGE; | |
private double height = DEFAULT_HEIGHT; | |
/** | |
* Constructor for creating a Person instance with default values. | |
*/ | |
public Person() | |
{ | |
counter++; | |
firstName = DEFAULT_FIRST_NAME; | |
lastName = DEFAULT_LAST_NAME; | |
age = DEFAULT_AGE; | |
height = DEFAULT_HEIGHT; | |
} | |
/** | |
* Constructor for creating a Person instance with first and last name. | |
* | |
* @param _firstName First name of our Person | |
* @param _lastName Last name of our Person | |
*/ | |
public Person(String _firstName, String _lastName) | |
{ | |
counter++; | |
setFirstName(_firstName); | |
setLastName(_lastName); | |
age = DEFAULT_AGE; | |
height = DEFAULT_HEIGHT; | |
} | |
/** | |
* 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) | |
{ | |
counter++; | |
setFirstName(_firstName); | |
setLastName(_lastName); | |
setAge(_age); | |
setHeight(_height); | |
} | |
/** | |
* 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 MAXIMUM_AGE. | |
* | |
* @param _age The new age to set. | |
*/ | |
public void setAge(int _age) | |
{ | |
if(_age >= 0 && _age <= MAXIMUM_AGE) | |
{ | |
age = _age; | |
} | |
} | |
/** | |
* Sets the height of this Person. | |
* | |
* Must be positive and less than or equal to MAXIMUM_HEIGHT. | |
* | |
* @param _height The new height to set | |
*/ | |
public void setHeight(double _height) | |
{ | |
if(_height >= 0 && _height <= MAXIMUM_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; | |
} | |
/** | |
* See how many times a Person object has been created. | |
* | |
* @return A string showing the amount of times a person object was created. | |
*/ | |
public String getNumberOfTimesCreated() | |
{ | |
switch(counter) | |
{ | |
case 0: | |
return "none"; | |
case 1: | |
return "once"; | |
case 2: | |
return "twice"; | |
default: | |
return "too many times!"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment