Question

In: Computer Science

OOPDA in java project. in eclipse Instructions: Create a class called Person with the properties listed:...


OOPDA in java project. in eclipse

Instructions:

  1. Create a class called Person with the properties listed: int id, String firstName, String middleName, String lastName, String email, String ssn, int age.
    The Person class should have getters and setters for all properties
    other than id. (You may use Eclipse to help you auto-generate these.)

    1. Person class should also have a getter for id

    2. Create a no-arg constructor for Person

    3. In addition to these getters and setters, Person should have the
      following instance methods:

  • String toString() – returns full name, like "Edgar Allen Poe"

  • String getEmailDomain() – returns the domain portion of an email (after
    the @), as in "gmail.com"

  • String getLast4SSN() – returns the last 4 digits of the Social Security
    Number

  • Add validation checking methods

    • static boolean validAge(String testAge)

  1. You will have to convert the testAge String to an int to store it after validation

static boolean validEmail(String testEmail)

static boolean validSSN(String testSSN)

for the age, email and ssn.You may have done this via the setters in the past. However, this time, let's make them class-level (static) methods to validate these fields as best you can.

  • Give some thought to what advantages this approach would have.

  1. Notes on validation.

    1. Here is a list of validation checks you should perform. Some are easier to implement than others, and there are many ways to correctly validate these aspects of the fields. Implement as many of these checks as you can.

  • You may accept any value for the names

    • Extra Credit: Is there a good way to validate a name? Do a little research on topic and include your answer. If there is, include your algorithm.

  • Age entered by user  

    • The String entered must be an integer greater than 16

    • If the String is greater than 100 you should log an info message

  • Email address string entered by user

    • String must contain a '@' and at least one '.’

    • A '.' must follow the '@’

    • String must only contain one '@'

    • The domain name (the part after ‘@’ but before the first ‘.’ )must be at least one character in length

Clearly there are far more restrictions on email addresses than those listed above. See if you can think of any others that might be applicable. I will give out extra credit for especially clever email validation tests. Make sure to document and comment your ideas.

Social Security Number string entered by user

  • String must contain two '-' characters in the correct position

  • The length of the SSN must be 11 total characters

  • String must contain only digits or numbers

If you encounter invalid data for any of these fields, log it using the logging API of Java. (Do not use System.out.println(); )

Static variables: Keep track of the highest age entered for a Person in a static variable.

Create a driver program PersonApp to test the class

  1. Prompt user for all Person properties except id. You can hardcode any id you like, but use the id as the HashMap key

  2. Accept any value for the name field

  3. Only accept valid values for age, email and ssn

  4. Create a Person with the valid input

Create subclasses for Instructor and Student

  1. Instructors should have a Department

  2. Students should have a Major

  3. Generate getters and setters

Add the Person, the Student and the Instructor to a HashMap.

  1. In the Driver, create a Student & an Instructor (Hardcoded, do not prompt for Input)

  2. Add each of these three instances (the hardcoded Student & Instructor, and previously created Person ) to a HashMap using the id as the key

Perform the following after creating each person in the Driver:

  1. Loop through the HashMap and display

  • The person's full name and what kind of Person are they

  • The person's email domain

  • The last 4 digits of the person's Social Security Number

  • Whether the person is the oldest in the collection

  • The major for the Student, the department for the Instructor

Here is some sample output, to help you understand the end goal of the exercise (your prompts may look different than mine, don’t worry about that as long as they are clear)


Enter person's first name
Julie
Enter person's middle name
Theresa
Enter person's last name
Collins
Enter person's email address
[email protected]
Enter person's SSN in ###-##-#### format
123-45-6789
Enter person's age
47


Julie Theresa Collins (Person)
acme.com
6789
Oldest


Jane Marie Doe (Student)
students.rowan.edu
4444
not oldest

Computer Science


Stephen J. Hartley (Instructor)
rowan.edu
5555
not oldest
Math/Science

Solutions

Expert Solution

/*************************Person.java**********************/

package person;

// TODO: Auto-generated Javadoc
/**
* The Class Person.
*/
public class Person {

   /** The id. */
   private int id;

   /** The first name. */
   private String firstName;

   /** The middle name. */
   private String middleName;

   /** The last name. */
   private String lastName;

   /** The email. */
   private String email;

   /** The ssn. */
   private String ssn;

   /** The age. */
   private int age;

   /** The max age. */
   public static int maxAge = 0;

   /**
   * Instantiates a new person.
   */
   public Person() {

       this.id = 0;
       this.firstName = "";
       this.middleName = "";
       this.lastName = "";
       this.email = "";
       this.ssn = "";
       this.age = 0;
   }

   /**
   * Instantiates a new person.
   *
   * @param id the id
   * @param firstName the first name
   * @param middleName the middle name
   * @param lastName the last name
   * @param email the email
   * @param ssn the ssn
   * @param age the age
   */
   public Person(int id, String firstName, String middleName, String lastName, String email, String ssn, int age) {
       super();
       this.id = id;
       this.firstName = firstName;
       this.middleName = middleName;
       this.lastName = lastName;
       this.email = email;
       this.ssn = ssn;
       if (age > maxAge) {
           maxAge = age;
       }
       this.age = age;
   }

   /**
   * Gets the first name.
   *
   * @return the first name
   */
   public String getFirstName() {
       return firstName;
   }

   /**
   * Sets the first name.
   *
   * @param firstName the new first name
   */
   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }

   /**
   * Gets the middle name.
   *
   * @return the middle name
   */
   public String getMiddleName() {
       return middleName;
   }

   /**
   * Sets the middle name.
   *
   * @param middleName the new middle name
   */
   public void setMiddleName(String middleName) {
       this.middleName = middleName;
   }

   /**
   * Gets the last name.
   *
   * @return the last name
   */
   public String getLastName() {
       return lastName;
   }

   /**
   * Sets the last name.
   *
   * @param lastName the new last name
   */
   public void setLastName(String lastName) {
       this.lastName = lastName;
   }

   /**
   * Gets the email.
   *
   * @return the email
   */
   public String getEmail() {
       return email;
   }

   /**
   * Sets the email.
   *
   * @param email the new email
   */
   public void setEmail(String email) {
       this.email = email;
   }

   /**
   * Gets the ssn.
   *
   * @return the ssn
   */
   public String getSsn() {
       return ssn;
   }

   /**
   * Sets the ssn.
   *
   * @param ssn the new ssn
   */
   public void setSsn(String ssn) {
       this.ssn = ssn;
   }

   /**
   * Gets the age.
   *
   * @return the age
   */
   public int getAge() {
       return age;
   }

   /**
   * Sets the age.
   *
   * @param age the new age
   */
   public void setAge(int age) {
       if (age > maxAge) {
           maxAge = age;
       }
       this.age = age;
   }

   /**
   * Gets the id.
   *
   * @return the id
   */
   public int getId() {
       return id;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return firstName + " " + middleName + " " + lastName;
   }

   /**
   * Gets the email domain.
   *
   * @return the email domain
   */
   public String getEmailDomain() {

       return "@g m ail. c om ";//rearrange this
   }

   /**
   * Gets the last 4 SSN.
   *
   * @return the last 4 SSN
   */
   public String getLast4SSN() {

       return ssn.substring(ssn.length() - 4, ssn.length());
   }

   /**
   * Valid age.
   *
   * @param testAge the test age
   * @return true, if successful
   */
   public static boolean validAge(String testAge) {

       int age = Integer.parseInt(testAge);

       if (age > 16 && age < 120) {
           if (age > 100) {

               System.out.println("Person age is above 100");
           }
           return true;
       } else {

           return false;
       }
   }

   /**
   * Valid email.
   *
   * @param testEmail the test email
   * @return true, if successful
   */
   public static boolean validEmail(String testEmail) {

       String validator = "[A-Z]+[a-zA-Z_]+@\b([a-zA-Z]+.){2}\b?.[a-zA-Z]+";
       if (testEmail.matches(validator)) {

           return true;
       } else {

           return false;
       }
   }

   /**
   * Valid SSN.
   *
   * @param testSSN the test SSN
   * @return true, if successful
   */
   public static boolean validSSN(String testSSN) {
       /*
       * ^\\d{3}: Starts with three numeric digits. [- ]?: Followed by an optional "-"
       * \\d{2}: Two numeric digits after the optional "-" [- ]?: May contain an
       * optional second "-" character. \\d{4}: ends with four numeric digits.
       */
       String validator = "^\\d{3}[- ]?\\d{2}[- ]?\\d{4}$";
       if (testSSN.matches(validator)) {

           return true;
       } else {

           return false;
       }
   }

}
/*********************Driver.java*********************/

package person;

import java.util.HashMap;
import java.util.Scanner;


/**
* The Class Driver.
*/
public class Driver {

   /**
   * The main method.
   *
   * @param args the arguments
   */
   public static void main(String[] args) {

       HashMap<Integer, Person> hm = new HashMap<Integer, Person>();
       Scanner scan = new Scanner(System.in);
       hm.put(101, new Person(101, "Jane", "Marie", "Doe", "students.rowan.edu", "111-22-4444", 25));
       hm.put(102, new Person(102, "Stephen", "J.", "Hartley", "rowan.edu", "222-33-5555", 35));

       System.out.println("Enter person's first name");
       String firstName = scan.nextLine();
       System.out.println("Enter person's middle name");
       String middleName = scan.nextLine();
       System.out.println("Enter person's last name");
       String lastName = scan.nextLine();
       System.out.println("Enter person's email address");
       String testEmail = scan.nextLine();
       System.out.println("Enter person's SSN in ###-##-#### format");
       String testSSN = scan.nextLine();
       System.out.println("Enter person's age");
       String testAge = scan.nextLine();
       int age = 0;
       String email = null;
       String ssn = null;
       if (Person.validAge(testAge)) {

           age = Integer.parseInt(testAge);
       }
       if (Person.validEmail(testEmail)) {

           email = testEmail;
       }
       if (Person.validSSN(testSSN)) {

           ssn = testSSN;
       }

       hm.put(103, new Person(103, firstName, middleName, lastName, email, ssn, age));

       for (int key : hm.keySet()) {
          
           String ageFlag = "";
           if(hm.get(key).getAge()==Person.maxAge) {
              
               ageFlag = "Oldest";
           }
           else {
              
               ageFlag = "Not oldest";
           }
           System.out.println(hm.get(key).toString()+"\n"+hm.get(key).getEmail()+"\n"+hm.get(key).getLast4SSN()+"\n"+ageFlag);
           System.out.println();
       }

   }
}
/******************output********************/

Enter person's first name
Julie
Enter person's middle name
Theresa
Enter person's last name
Collins
Enter person's email address
[email protected]
Enter person's SSN in ###-##-#### format
123-45-6789
Enter person's age
47
Jane Marie Doe
students.rowan.edu
4444
Not oldest

Stephen J. Hartley
rowan.edu
5555
Not oldest

Julie Theresa Collins
null
6789
Oldest

Please let me know if you have any doubt or modify the answer, Thanks :)


Related Solutions

JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign5Test.java. Copy your code from Assignment 4 into the Student.java and StudentList.java Classes. Assign5Test.java should contain the main method. Modify StudentList.java to use an ArrayList instead of an array. You can find the basics of ArrayList here: https://www.w3schools.com/java/java_arraylist.asp In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign4. Create 3 Java Classes...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign4. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign4Test.java. Copy your code from Assignment 3 into the Student.java and StudentList.java. Assign4Test.java should contain the main method. In Student.java, add a new private variable of type Integer called graduationYear. In Student.java, create a new public setter method setGraduationYear to set the graduationYear. The method will have one parameter of type Integer. The method will set...
Using eclipse IDE, create a java project and call it applets. Create a package and call...
Using eclipse IDE, create a java project and call it applets. Create a package and call it multimedia. Download an image and save it in package folder. In the package, create a java applet where you load the image. Use the drawImage() method to display the image, scaled to different sizes. Create animation in Java using the image. In the same package folder, download a sound. Include the sound in your applets and make it repeated while the applet executes....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT