субота, 10 січня 2015 р.

Java task OOP: calculate the age of person

 Add class Person to the project.
 Class Person should consists of
 a) two fields: name and birthDate.
 b) two properties for access to these fields
 c) constructor for initialisation of these fields
 d) method Age(), which may calculate the age of person
 In the method Main() create 2 objects of Person type: p1 and p2;
 Change name and birthDate these persons by properties.
 Output ages (by colling method Age()) of these persons.



package LessonsFromC.Homework2.person;

public class Person {
private String name;
private int birthDate;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return birthDate;
}

public void setAge(int birthDate) {
this.birthDate = birthDate;
}

public Person(String theName, int theBirthDate) {
this.name = theName;
this.birthDate = theBirthDate;
}

public Person(String theName) {
this.name = theName;
}

public Person() {

}

public int Age() {
int realAge = 2015 - this.birthDate;
return realAge;
}

public String toString() {
return (this.getName() + " " + this
.getAge());
}

}

_____________________

package LessonsFromC.Homework2.person;

public class TestPerson {

public static void main(String[] args) {
Person p1 = new Person();
p1.setName("Ivan");
p1.setAge(1982);
System.out.println(p1.Age());

Person p2 = new Person("Mary", 2014);
String name = p2.getName();
int birthday = p2.getAge();

System.out.println(p2.Age());
System.out.println(name);
System.out.println(birthday);

}

}

Немає коментарів:

Дописати коментар