Mobile PhoneCreate a program that implements a simple mobile phone with the following capabilities.1. Implement the master class MobilePhone, that holds the ArrayList of Contacts, with the following attributes: - Two fields, a String called myNumber and an ArrayList of type Contact called myContacts. - A constructor that takes a String (the phone number) and initialises myNumber and instantiates myContacts. - And seven methods, they are (their functions are in their names): - addNewContact(), has one parameter of type Contact and returns a boolean. Returns true if the contact doesn't exists, or false if the contact already exists. - updateContact(), has two parameters of type Contact (the old contact that will be updated with the new contact) and returns a boolean. Returns true if the contact exists and was updated successfully, or false if the contact doesn't exists. - removeContact(), has one parameter of type Contact and returns a boolean. Returns true if the contact exists and was removed successfully, or false if the contact doesn't exists. - findContact(), has one parameter of type Contact and returns an int. The returned value is it's position in the ArrayList, it will either be -1 (doesn't exists) or a value greater than or equal to 0 (does exists). - findContact(), same as above, only it has one parameter of type String. - queryContact(), has one parameter of type String and returns a Contact. Use the String to search for the name and then return the Contact. Return null otherwise. - printContacts(), has no parameters and doesn't return anything. Print the contacts in the following format:Contact List:1. Bob -> 314159262. Alice -> 161803393. Tom -> 112358134. Jane -> 235711132. Implement the Contact class with the following attributes: - Two fields, both String, one called name and the other phoneNumber. - A constructor that takes two Strings, and initialises name and phoneNumber. - And Three methods, they are: - getName(), getter for name. - getPhoneNumber(), getter for phoneNumber. - createContact(), has two parameters of type String (the persons name and phone number) and returns an instance of Contact. This is the only method that is static.TIP: In MobilePhone, use findContact() in the other methods (except printContacts()) to check if it exists before proceeding.TIP: Two Contact objects are equal if they have the same name.TIP: Be extremely careful about spaces in the printed message.NOTE: All fields are private.NOTE: Constructors should be defined as public.NOTE: All methods should be defined as public (except for the two findContact() methods which are private).NOTE: Do not add a main method to the solution code.NOTE: Classes that are not in the java.lang package should be manually imported.NOTE: If you get an error from the Evaluate class, it's most likely the constructor. Check if you've added a constructor or if the constructor has the right arguments.
Question
Mobile PhoneCreate a program that implements a simple mobile phone with the following capabilities.1. Implement the master class MobilePhone, that holds the ArrayList of Contacts, with the following attributes: - Two fields, a String called myNumber and an ArrayList of type Contact called myContacts. - A constructor that takes a String (the phone number) and initialises myNumber and instantiates myContacts. - And seven methods, they are (their functions are in their names): - addNewContact(), has one parameter of type Contact and returns a boolean. Returns true if the contact doesn't exists, or false if the contact already exists. - updateContact(), has two parameters of type Contact (the old contact that will be updated with the new contact) and returns a boolean. Returns true if the contact exists and was updated successfully, or false if the contact doesn't exists. - removeContact(), has one parameter of type Contact and returns a boolean. Returns true if the contact exists and was removed successfully, or false if the contact doesn't exists. - findContact(), has one parameter of type Contact and returns an int. The returned value is it's position in the ArrayList, it will either be -1 (doesn't exists) or a value greater than or equal to 0 (does exists). - findContact(), same as above, only it has one parameter of type String. - queryContact(), has one parameter of type String and returns a Contact. Use the String to search for the name and then return the Contact. Return null otherwise. - printContacts(), has no parameters and doesn't return anything. Print the contacts in the following format:Contact List:1. Bob -> 314159262. Alice -> 161803393. Tom -> 112358134. Jane -> 235711132. Implement the Contact class with the following attributes: - Two fields, both String, one called name and the other phoneNumber. - A constructor that takes two Strings, and initialises name and phoneNumber. - And Three methods, they are: - getName(), getter for name. - getPhoneNumber(), getter for phoneNumber. - createContact(), has two parameters of type String (the persons name and phone number) and returns an instance of Contact. This is the only method that is static.TIP: In MobilePhone, use findContact() in the other methods (except printContacts()) to check if it exists before proceeding.TIP: Two Contact objects are equal if they have the same name.TIP: Be extremely careful about spaces in the printed message.NOTE: All fields are private.NOTE: Constructors should be defined as public.NOTE: All methods should be defined as public (except for the two findContact() methods which are private).NOTE: Do not add a main method to the solution code.NOTE: Classes that are not in the java.lang package should be manually imported.NOTE: If you get an error from the Evaluate class, it's most likely the constructor. Check if you've added a constructor or if the constructor has the right arguments.
Solution
This is a Java programming task. Here is a step-by-step guide on how to implement the MobilePhone and Contact classes as described:
- Create the Contact class:
public class Contact {
private String name;
private String phoneNumber;
public Contact(String name, String phoneNumber) {
this.name = name;
this.phoneNumber = phoneNumber;
}
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public static Contact createContact(String name, String phoneNumber) {
return new Contact(name, phoneNumber);
}
}
- Create the MobilePhone class:
import java.util.ArrayList;
public class MobilePhone {
private String myNumber;
private ArrayList<Contact> myContacts;
public MobilePhone(String myNumber) {
this.myNumber = myNumber;
this.myContacts = new ArrayList<Contact>();
}
public boolean addNewContact(Contact contact) {
if(findContact(contact.getName()) >=0) {
return false;
}
myContacts.add(contact);
return true;
}
public boolean updateContact(Contact oldContact, Contact newContact) {
int foundPosition = findContact(oldContact);
if(foundPosition <0) {
return false;
}
this.myContacts.set(foundPosition, newContact);
return true;
}
public boolean removeContact(Contact contact) {
int foundPosition = findContact(contact);
if(foundPosition <0) {
return false;
}
this.myContacts.remove(foundPosition);
return true;
}
private int findContact(Contact contact) {
return this.myContacts.indexOf(contact);
}
private int findContact(String contactName) {
for(int i=0; i<this.myContacts.size(); i++) {
Contact contact = this.myContacts.get(i);
if(contact.getName().equals(contactName)) {
return i;
}
}
return -1;
}
public Contact queryContact(String name) {
int position = findContact(name);
if(position >=0) {
return this.myContacts.get(position);
}
return null;
}
public void printContacts() {
System.out.println("Contact List:");
for(int i=0; i<this.myContacts.size(); i++) {
System.out.println((i+1) + ". " +
this.myContacts.get(i).getName() + " -> " +
this.myContacts.get(i).getPhoneNumber());
}
}
}
This code creates two classes, Contact and MobilePhone, with the specified fields, constructors, and methods. The MobilePhone class uses an ArrayList to store Contact objects, and provides methods to add, update, remove, find, and print contacts. The Contact class provides a static method to create a new Contact object.
Similar Questions
Describe the algorithm used by a telephone to place a phone call. (You maygive your description in either English or a pseudocode, whichever you findmore convenient.)
Imagine you need the information below. Follow the instructions and write the necessary question / response.Ask me about two phone brands. (Use as … as)
Write A Java Program to define a class Mobile having 3 members manufacture namebrandpriceDefine 3 constructors1. by default "apple" "i5" "125000"2 .use to enter 3 information3. copy constructor4. only parameter (name,price )and model is same as brand name create the objects for the suitable constructors and use toString() method to display.
list 5 rules about mobile phone usuage ?
Multiple Choice QuestionA cell phone that allows you to surf the web is called a(n) ___. This is actually a powerful computer capable of accepting input, processing data into information, providing data, and storing data.Multiple choice question.desktop2-in-1laptopsmartphone
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.