Answer to Question #282552 in Java | JSP | JSF for koi

Question #282552

2. Write a simple java application that will declare the class shown in the UML diagram below.

- The getAge() method should calculate and return the present age of a person. Hint:

Use dob (date of birth). The printDetails() method should display the full name of

the person together with the gender and age in separate lines.

- Create two objects from this class. Initialize the first one with the first constructor

and, the second one with the second constructor. Use the setGender() and setDob()

methods to initialize the gender and dob of the first object.

- Display all the details of the two objects.


1
Expert's answer
2021-12-25T01:47:52-0500
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class Main {

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat dobDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        TimeZone timeZone = TimeZone.getTimeZone("UTC");
        dobDateFormat.setTimeZone(timeZone);
        Person person1 = new Person("John", "Smith", dobDateFormat.parse("2000-01-01"), "male");
        System.out.println(person1.printDetails());

        Person person2 = new Person();
        person2.setFirstName("Angelina");
        person2.setLastName("Jolie");
        person2.setDob(dobDateFormat.parse("1975-06-04"));
        person2.setGender("female");
        System.out.println(person2.printDetails());
    }

    public static class Person {

        private String firstName;
        private String lastName;
        /** Date of birth*/
        private Date dob;
        private String gender;

        public Person (String firstName, String lastName, Date dob, String gender) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.dob = dob;
            this.gender = gender;
        }

        public Person() {
            this.firstName = "";
            this.lastName = "";
            this.dob = new Date();
            this.gender = "";
        }

        public String printDetails() {
            return new String(
              "Person:\n" +
              "\t\tfirstName: " + firstName + ",\n"+
              "\t\tlastName: " + lastName + ",\n" +
              "\t\tgender: " + gender + ",\n" +
              "\t\tage: " +getAge()
            );
        }

        public void setFirstName (String firstName) {
            this.firstName = firstName;
        }
        public String getFirstName() {
            return firstName;
        }

        public void setLastName (String lastName) {
            this.lastName = lastName;
        }
        public String getLastName() {
            return lastName;
        }

        public void setDob(Date dob) {
            this.dob = dob;
        }
        public Date getDob() {
            return dob;
        }

        public void setGender(String gender) {
            this.gender = gender;
        }
        public String getGender() {
            return gender;
        }

        public int getAge () {
            return (int)((new Date().getTime() - dob.getTime()) / 1000 / 31536000);
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog