Question

Complete the Person class: For setEmail, setFirstName, and setSurname: if the method is passed an empty...

Complete the Person class:

For setEmail, setFirstName, and setSurname: if the method is passed an empty string, it should do nothing; but otherwise it should set the appropriate field.

For setMobile: if the method is passed a valid mobile phone number, it should set the appropriate field; otherwise it should do nothing. A string is a valid mobile phone number if every character in it is a digit from 0 to 9.

Hints:

To convert a string so you can process it in a for-each loop you can use the String.toCharArray method. You do not need to know any array operations for this task.

To test whether a character is a digit, you can use the Character.isDigit method.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Complete the printContactDetails method:

The method should print, in order, 3 lines showing the name, mobile and email of the person, prefaced by the strings "name: ", "mobile: " and "email: ", respectively. The name of a person is their first name and surname, separated by a space.

For each of the social media accounts the person has, the method should print one line containing the website name, the userID, and the website URL, separated by commas.

For example, the following code will construct a Person object, and add details to it:

  Person p = new Person("Diana", "Prince");
  p.setMobile("0409670123");
  p.setEmail("diana.prince@inscom.mil");
  p.addSocialMediaAccount("@dianap", "Twitter", "http://twitter.com/", 50);
  p.addSocialMediaAccount("diana.prince", "Facebook", "http://facebook.com/", 90);

Calling the printContactDetails method on the object p should print the following:

  name: Diana Prince
  mobile: 0409670123 
  email: diana.prince@inscom.mil 
  Twitter,@dianap,http://twitter.com/
  Facebook,diana.prince,http://facebook.com/

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.util.ArrayList;
/**
* Person details including contacts and social media accounts
*
*/
public class Person {
private String firstName;
private String surname;
private String mobile;
private String email;
private ArrayList socialMediaAccounts;

/**
   * Create a Person with the first name and surname given by
   * the parameters.
   */
public Person(String firstName, String surname) {
    this.firstName = firstName;
    this.surname = surname;
    mobile = null;
    email = null;
    this.socialMediaAccounts = new ArrayList();
}

/**
   * @return the person's first name
   */
public String getFirstName() {
    return ""; // replace this line with your own code
}

/**
   * Set the person's first name
   * unless the parameter is an empty string.
   */
public void setFirstName(String firstName) {
    // replace this line with your own code
}


/**
   * Return the person's surname
   */
public String getSurname() {
    return ""; // replace this line with your own code
}

/**
   * Set the person's surname
   * unless the parameter is an empty string.
   */
public void setSurname(String surname) {
     // replace this line with your own code
}


/**
   * Return the person's mobile phone number
   */
public String getMobile() {
    return ""; // replace this line with your own code
}

/**
   * Set the person's mobile phone number
   */
public void setMobile(String mobile) {
   this.mobile = mobile;
}

/**
   * Return the person's email address
   */
public String getEmail() {
    return ""; // replace this line with your own code
}

/**
   * Set the person's email address
   */
public void setEmail(String email) {
    // replace this line with your own code
}

/**
   * Create a new SocialMediaAccount object, and add it to the socialMediaAccounts list.
   */
public void addSocialMediaAccount(String userID, String websiteName, String websiteURL, int activityLevel) {
    // replace this line with your own code
}

/**
   * Search the socialMediaAccounts list for an account on the website specified by the websiteName
   * parameter, and return the userID for that account. If no such account can be found, return
   * null.
   */
public String getSocialMediaID(String websiteName) {
    return ""; // replace this line with your own code
}

/** Print the person's contact details in the format given in the
   * project specifications.
   */
public void printContactDetails() {
    // replace this line with your own code
}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
abc

package com.abc;

public class SocialMediaAccount {
   private String userID;
   private String websiteName;
   private String websiteURL;
   private int activityLevel;
  
  
  
   public SocialMediaAccount(String userID, String websiteName, String websiteURL, int activityLevel) {
       super();
       this.userID = userID;
       this.websiteName = websiteName;
       this.websiteURL = websiteURL;
       this.activityLevel = activityLevel;
   }
   public String getUserID() {
       return userID;
   }
   public void setUserID(String userID) {
       this.userID = userID;
   }
   public String getWebsiteName() {
       return websiteName;
   }
   public void setWebsiteName(String websiteName) {
       this.websiteName = websiteName;
   }
   public String getWebsiteURL() {
       return websiteURL;
   }
   public void setWebsiteURL(String websiteURL) {
       this.websiteURL = websiteURL;
   }
   public int getActivityLevel() {
       return activityLevel;
   }
   public void setActivityLevel(int activityLevel) {
       this.activityLevel = activityLevel;
   }
}

package com.abc;

import java.util.ArrayList;
/**
* Person details including contacts and social media accounts
*
*/
public class Person {
   private String firstName;
   private String surname;
   private String mobile;
   private String email;
   private ArrayList<SocialMediaAccount> socialMediaAccounts;

   /**
   * Create a Person with the first name and surname given by
   * the parameters.
   */
   public Person(String firstName, String surname) {
       this.firstName = firstName;
       this.surname = surname;
       mobile = null;
       email = null;
       this.socialMediaAccounts = new ArrayList<SocialMediaAccount>();
   }
   /**
   * @return the person's first name
   */
   public String getFirstName() {
       return this.firstName;
   }

   /**
   * Set the person's first name
   * unless the parameter is an empty string.
   */
   public void setFirstName(String firstName) {
       if(!firstName.equals("")){
           this.firstName=firstName;
       }
   }


   /**
   * Return the person's surname
   */
   public String getSurname() {
       return this.surname;
   }

   /**
   * Set the person's surname
   * unless the parameter is an empty string.
   */
   public void setSurname(String surname) {
       if(!surname.equals("")){
           this.surname = surname;
       }
   }


   /**
   * Return the person's mobile phone number
   */
   public String getMobile() {
       return this.mobile;
   }

   /**
   * Set the person's mobile phone number
   */
   public void setMobile(String mobile) {
       boolean isValid = true;
       for(char ch:mobile.toCharArray()){
           if(!Character.isDigit(ch)){
               isValid = false;
           }
       }
       if(isValid)
           this.mobile = mobile;
   }

   /**
   * Return the person's email address
   */
   public String getEmail() {
       return this.email;
   }

   /**
   * Set the person's email address
   */
   public void setEmail(String email) {
       if(!email.equals("")){
           this.email = email;
       }
   }

   /**
   * Create a new SocialMediaAccount object, and add it to the socialMediaAccounts list.
   */
   public void addSocialMediaAccount(String userID, String websiteName, String websiteURL, int activityLevel) {
       SocialMediaAccount socialMediaAccount = new SocialMediaAccount(userID, websiteName, websiteURL, activityLevel);
       socialMediaAccounts.add(socialMediaAccount);
   }

   /**
   * Search the socialMediaAccounts list for an account on the website specified by the websiteName
   * parameter, and return the userID for that account. If no such account can be found, return
   * null.
   */
   public String getSocialMediaID(String websiteName) {
       String userId = null;
       for(SocialMediaAccount acccount:socialMediaAccounts){
           if(acccount.getWebsiteName().equals(websiteName)){
               userId = acccount.getUserID();
               break;
           }
       }

       return userId;
   }
   /** Print the person's contact details in the format given in the
   * project specifications.
   */
   public void printContactDetails() {
       System.out.println("name: "+getFirstName()+" "+getSurname());
       System.out.println("mobile: "+getMobile());
       System.out.println("email: "+getEmail());
       for(SocialMediaAccount acccount:socialMediaAccounts){
           System.out.println(acccount.getWebsiteName()+","+acccount.getUserID()+","+acccount.getWebsiteURL());
       }
   }
}

Add a comment
Know the answer?
Add Answer to:
Complete the Person class: For setEmail, setFirstName, and setSurname: if the method is passed an empty...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • USING THIS CODE: #include <iostream> #include <string> using namespace std; class Contact { //this class cannot...

    USING THIS CODE: #include <iostream> #include <string> using namespace std; class Contact { //this class cannot be viewed from outside of the private class. //only the class functions within the class can access private members. private: string name; string email; string phone; //the public class is accessible from anywhere outside of the class //however can only be within the program. public: string getName() const { return name; } void setName(string name) { this->name = name; } string getEmail() const {...

  • Write in C++ 1. Use the following Person class (Person.cpp, Person.h). You will be writing Person...

    Write in C++ 1. Use the following Person class (Person.cpp, Person.h). You will be writing Person objects to a random access file. --------------------- Person.cpp--------------------------------- // Class Person stores customer's credit information. #include <string> #include "Person.h" using namespace std; // default Person constructor Person::Person( int idValue, string lastNameValue, string firstNameValue, int AgeValue ) { setID( idValue ); setLastName( lastNameValue ); setFirstName( firstNameValue ); setAge( AgeValue ); } // end Person constructor // get id value int Person::getID() const { return id;...

  • Public class Person t publie alass EmployeeRecord ( private String firstName private String last ...

    public class Person t publie alass EmployeeRecord ( private String firstName private String last Nanei private Person employee private int employeeID publie EnmployeeRecord (Person e, int ID) publie Person (String EName, String 1Name) thia.employee e employeeID ID setName (EName, 1Name) : publie void setName (String Name, String 1Name) publie void setInfo (Person e, int ID) this.firstName- fName this.lastName 1Name this.employee e employeeID ID: publio String getFiritName) return firstName public Person getEmployee) t return employeei public String getLastName public int getIDO...

  • Language: C++ Create an abstract base class person. The person class must be an abstract base...

    Language: C++ Create an abstract base class person. The person class must be an abstract base class where the following functions are abstracted (to be implemented in Salesman and Warehouse): • set Position • get Position • get TotalSalary .printDetails The person class shall store the following data as either private or protected (i.e., not public; need to be accessible to the derived classes): . a person's name: std::string . a person's age: int . a person's height: float The...

  • I need to create file called EvilPerson.java. This implements a class that inherits from Person. The...

    I need to create file called EvilPerson.java. This implements a class that inherits from Person. The EvilPerson class doesn’t want you to be able to set a phone number for a user, so it always stores the text “n/a” in the phone attribute. You should define a constructor for EvilPerson that takes in a name and a phone number, stores the name correctly but throws away the phone number values and stores “n/a” there instead. The setPhone() method from Person...

  • A class called Author is designed as shown in the class diagram. (JAVA)

    in javaA class called Author is designed as shown in the class diagram. Author name: String email: String gender: char +Author (name :String, email: String, gender char) +getName ():String +getEmail() String +setEmail (email: String) :void +getGender(): char +toString ():String It contains  Three private instance variables: name (String), email (String), and gender (char of either 'm' or 'f),  One constructor to initialize the name, email and gender with the given values; public Author (String name, String email, char gender) f......)  public getters/setters: getName(), getEmail setEmail and getGender(); (There are no setters for name and gender,...

  • I'm struggling to figure out how to do this problem in Java, any help would be...

    I'm struggling to figure out how to do this problem in Java, any help would be appreciated: Create an application that uses a class to store and display contact information. Console: Welcome to the Contact List application Enter first name: Mike Enter last name: Murach Enter email:      mike@murach.com Enter phone:      800-221-5528 -------------------------------------------- ---- Current Contact ----------------------- -------------------------------------------- Name:           Mike Murach Email Address: mike@murach.com Phone Number:   800-221-5528 -------------------------------------------- Continue? (y/n): n Specifications Use a class named Contact to store the data...

  • Task 3: Main Program Create a main program class that: Creates three or more Nurse instances...

    Task 3: Main Program Create a main program class that: Creates three or more Nurse instances assigned to different shifts. Creates three or more Doctor instances. Creates three or more Patient instances with pre-determined names and manually assigned physicians chosen from the pool of Doctor instances previously created. Generates another 20 Patient instances using randomly generated names and randomly assigns them physicians chosen from the pool of Doctor instances previously created. Prints the toString() values for all employees. Prints the...

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person...

    Introduction In this lab, you will be working with three classes: Person, Student, and Roster. Person and Student represent individuals with a first and last name as String class variables, and Student has an additional int class variable that represents a ID number. The Roster class represents a group of people that are objects of either the Person or Student class. It contains an ArrayList. The Person class has been completed for you with class variables, a constructor, and a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT