Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Person {
- private int age;
- private String name;
- public Person(int age, String name) {
- // WHAT HAPPENS IF AGE ENTERED IS NEGATIVE?!?!
- //this.age = (age > 0) ? age : 0;
- setAge(age);
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = Math.max(age, 0);
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return "Name : " + getName() +
- "Age: "+ getAge();
- }
- public int addAges(Person anotherPerson) {
- return anotherPerson.getAge() + this.getAge();
- }
- public static void main(String[] args) {
- Person person1 = new Person(23, "Robin");
- Person person2 = new Person(22, "Thomas");
- System.out.println(person1.toString());
- System.out.println(person2.toString());
- System.out.printf("The sum of ages: %,d\n",person1.addAges(person2));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment