Write a class called Person that has two data members - a string variable called
ID: 3869108 • Letter: W
Question
Write a class called Person that has two data members - a string variable called name and a double variable called age. It should have a constructor that takes two values and uses them to initialize the data members. It should have get methods for both data members (getName and getAge), but doesn't need any set methods.
Write a separate function (not part of the Person class) called stdDev that takes two parameters - an array of Persons and the size of the array - and returns the standard deviation of all the ages (the population standard deviation that uses a denominator of N, not the sample standard deviation, which uses a different denominator).
The files must be named Person.hpp, Person.cpp and stdDev.cpp.
Explanation / Answer
class Person {
public Person() {
name = "YetToBeNamed";
birthdayYear = 1999; // my default
}
public Person(String givenName, int yearOfBirth) {
name = givenName;
birthdayYear = yearOfBirth;
}
public String getName() {
return name;
}
public String changeName(String name) {
String aux;
aux = this.name;
this.name = name;
return aux;
}
public int getAgeInYears(int currentYear) {
return currentYear - birthdayYear;
}
private String name;
private int birthdayYear;
public static void main(String[] args) {
Person a = new Person();
Person b = new Person("Richard P. Feynman", 1918);
String name = a.changeName("The Next Richard Feynman");
System.out.println(
"Physicist " + name + " makes big " +
"discovery, touted as " + a.getName()
);
System.out.println(
b.getName() + " was " +
b.getAgeInYears(1945) +
" in 1945, in May. "
);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.