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("[email protected]");
  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: [email protected] 
  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 {...

  • 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...

  • 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:      [email protected] Enter phone:      800-221-5528 -------------------------------------------- ---- Current Contact ----------------------- -------------------------------------------- Name:           Mike Murach Email Address: [email protected] Phone Number:   800-221-5528 -------------------------------------------- Continue? (y/n): n Specifications Use a class named Contact to store the data...

  • 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;    }   ...

  • 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...

  • 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...

  • "Function does not take 0 arguments". I keep getting this error for my last line of...

    "Function does not take 0 arguments". I keep getting this error for my last line of code: cout << "First Name: " << employee1.getfirstName() << endl; I do not understand why. I am trying to put the name "Mike" into the firstName string. Why is my constructor not working? In main it should be set to string firstName, string lastName, int salary. Yet nothing is being put into the string. #include<iostream> #include<string> using namespace std; class Employee {    int...

  • The method m() of class B overrides the m() method of class A, true or false?...

    The method m() of class B overrides the m() method of class A, true or false? class A int i; public void maint i) { this.is } } class B extends A{ public void m(Strings) { 1 Select one: True False For the following code, which statement is correct? public class Test { public static void main(String[] args) { Object al = new AC: Object a2 = new Object(); System.out.println(al); System.out.println(a): } } class A intx @Override public String toString()...

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
ADVERTISEMENT