Question

In: Computer Science

In a BestFriendSimulation driver class, define and initialize a reference variable called myBestFriends referencing an ArrayList...

In a BestFriendSimulation driver class, define and initialize a reference variable called myBestFriends referencing an ArrayList ofBestFriend objects. Then, create a menu that will continue to display itself, and process the requests of the user, until the user requests to exit the menu (loop).

The menu should have 5 menu options:

1.) Add a BestFriend to the arrayList called myBestFriends; 2.) Change a BestFriend in the arrayList; 3.) Remove a BestFriend from the arrayList; 4.) Display all the objects in the myBestFriends arrayList. 5.) Exit

For each of these menu options, be sure to prompt the user for the necessary information such as BestFriend's first name, last name, nickname, and cell phone number.

Hint: In order to be able to change a BestFriend's information or remove a BestFriend from the arrayList, you will have to create a loop that will search the myBestFriends arrayList from beginning to end, and store the index position of the BestFriend that will be updated or removed. If the desired BestFriend is not found, a -1 will be returned from the search, or a not found flag will be set. If the BestFriend is found, use that index position to either change or remove that object from the myBestFriends arrayList.

Make sure you test each of your menu options, and then select the option that prints the table immediately afterwards, to ensure the PhoneBook looks correctly.

1. Create the BestFriend Domain Class
Define and/or instantiate the private static and non-static fields.

Create the constructor

public class BestFriend

{ private static int friendNumber = 0;

     private int friendIdNumber;

     private String firstName;

     private String lastName;

     private String nickName;

     private String cellPhoneNumber;

     public BestFriend (String aFirstName, String aLastName, String aNickName, String aCellPhoneNumber)

    {    firstName = aFirstName;

         lastName = aLastName;

         nickName = aNickName;

         cellPhoneNumber = aCellPhoneNumber;
friendNumber++;

friendIdNumber = friendNumber;

    }

    public String toString( )

    {   

         return friendIdNumber + ". " + nickName + " "  + firstName + " " + lastName + " " + cellPhoneNumber;

    }


Create the set methods (setters)
Create the get methods (getters)

public boolean equals(Object another)
{
      if (another instanceof BestFriend )
     {


     BestFriend anotherBFF = (BestFriend) another;
      if (firstName.equalsIgnoreCase(anotherBFF.firstName) &&
          lastName.equalsIgnoreCase(anotherBFF.lastName)
   //     && nickname.equalsIgnoreCase(anotherBFF.nickName) &&
   //      cellphone.equalsIgnoreCase(anotherBFF.cellPhone))
              return true;
}

         return false;

}





2. Create the BestFriendSimulation Driver Class:
Instantiate an arrayList  called myBFFs

Create a Do …… While loop to create a menu of 5 choices (add, change, remove, display, exit)
Allow the user to input the choice.
Use an if statement (or switch) to execute the user’s choice.

There are many ways to accomplish this task:

Technique #1.) Create a Helper class that defines the arrayList of BestFriends in its constructor, and then also defines the add, change, display, remove, and error methods as instance methods. Instantiate the Helper class from the driver (main) class. Also in the driver class, create the loop to display and process the menu options. When any specific menu option is selected, call the method from the Helper class.

Technique #2.) Instantiate the driver class in the main method (instantiate its own self). Then, define instance methods in the driver class for the add, change, display, remove, and error methods. Call them from the menu loop.

Technique #3.) In the driver class, create static methods for the add, change, display, remove, and error methods. Call them from the menu loop.

In any of the above 3 techniques, the arrayList of BestFriends can be defined as a global variable, so that it does not have to be passed as a parameter to each of the add, change, display, remove, and error methods. Similarly, the Scanner keyboard object should also be defined as a global variable too.

The alternative to defining the arrayList and Scanner keyboard as global variables is to pass these as parameters to each method call. For example:

addBFF(myBFFs, keyboard);

Solutions

Expert Solution


/* setFirstName(String) , setLastName(String) , setNickName(String) , setCellPhoneNumber(String)*/

/* BestFriendSimulation contains the main function * /
public import java.util.*;

class BestFriend
{ private static int friendNumber = 0;
private int friendIdNumber;
private String firstName;
private String lastName;
private String nickName;
private String cellPhoneNumber;

public BestFriend (String aFirstName, String aLastName, String aNickName, String aCellPhoneNumber)
{ firstName = aFirstName;
lastName = aLastName;
nickName = aNickName;
cellPhoneNumber = aCellPhoneNumber;
friendNumber++;
friendIdNumber = friendNumber;
}

public String toString( )
{   
return friendIdNumber + ". " + nickName + " " + firstName + " " + lastName + " " + cellPhoneNumber;
}
  
//Create the set methods (setters)
//Create the get methods (getters)

void setFirstName(String FN)
{
firstName = FN;
}
void setLastName(String LN)
{
lastName = LN;
}
void setNickName(String NN)
{
nickName = NN;
}
void setCellPhoneNumber(String CPN)
{
cellPhoneNumber = CPN;
}

public boolean equals(Object another)
{
if (another instanceof BestFriend )
{

BestFriend anotherBFF = (BestFriend) another;
if (firstName.equalsIgnoreCase(anotherBFF.firstName) &&
lastName.equalsIgnoreCase(anotherBFF.lastName))
// && nickname.equalsIgnoreCase(anotherBFF.nickName) &&
// cellphone.equalsIgnoreCase(anotherBFF.cellPhone))
return true;
}
return false;
}
}

class BestFriendSimulation
{
private static ArrayList<BestFriend> ABF ;
  
public static void main(String arg[])
{
Scanner scanner = new Scanner(System.in);
ABF = new ArrayList<BestFriend>();
int Option = 0 ;
String FN , LN , NN ,CPN ;
String MainMenu = "Please Enter Option Number Of your Choice"+"\n"+"1.) Add a BestFriend to the arrayList called myBestFriends; " +"\n"+"2.) Change a BestFriend in the arrayList;"+"\n"+ "3.) Remove a BestFriend from the arrayList;"+"\n"+"4.) Display all the objects in the myBestFriends arrayList."+"\n"+"5.) Exit "+"\n";
int AS = 0;
while(Option!=5)
{
System.out.print(MainMenu);
Option = scanner.nextInt();
switch(Option)
{
case 1 : {
System.out.println("Please Enter First Name");
FN = scanner.next();
System.out.println("Please Enter Last Name");
LN = scanner.next();
System.out.println("Please Enter Nick Name");
NN = scanner.next();
System.out.println("Please Enter Cell Phone Number");
CPN = scanner.next();
ABF.add(new BestFriend(FN,LN,NN,CPN));
break;
}
case 2 : {
System.out.println("Enter First Name Of Friend You Want To Change");
FN = scanner.next();
System.out.println("Enter Last Name Of Friend You Want To Change");
LN = scanner.next();
BestFriend BF = new BestFriend(FN,LN,"","");
AS = ABF.size();
if(AS==0)
{
System.out.print("List is Empty");
break;
}
for(int i=0;i<AS;i++)
{
if(ABF.get(i).equals(BF))
{
System.out.println("Please Enter New First Name");
FN = scanner.next();
System.out.println("Please Enter New Last Name");
LN = scanner.next();
System.out.println("Please Enter New Nick Name");
NN = scanner.next();
System.out.println("Please Enter New Cell Phone Number");
CPN = scanner.next();
ABF.get(i).setFirstName(FN);
ABF.get(i).setLastName(LN);
ABF.get(i).setNickName(NN);
ABF.get(i).setCellPhoneNumber(CPN);
break;
}
else if(i==AS-1)
{
System.out.print("No Such Friend Exists");
break;
}
}
break;
}
case 3 : {
System.out.println("Enter First Name Of Friend You Want To Remove");
FN = scanner.next();
System.out.println("Enter Last Name Of Friend You Want To Remove");
LN = scanner.next();
BestFriend BF = new BestFriend(FN,LN,"","");
AS = ABF.size();
if(AS==0)
{
System.out.print("List is Empty");
break;
}
for(int i=0;i<AS;i++)
{
if(ABF.get(i).equals(BF))
{
ABF.remove(i);
break;
}
else if(i==AS-1)
{
System.out.print("No Such Friend Exists");
break;
}
}
break;
}
case 4 : {
AS = ABF.size();
if(AS==0)
{
System.out.print("List is Empty");
break;
}
for(int i=0;i<AS;i++)
{

System.out.print(ABF.get(i).toString());
break;
}
break;
}
case 5 : {
break;
}
}
}
}
}


Related Solutions

***Given a class called Student and a class called Course that contains an ArrayList of Student....
***Given a class called Student and a class called Course that contains an ArrayList of Student. Write a method called dropStudent() as described below. Refer to Student.java below to learn what methods are available.*** Course.java import java.util.*; import java.io.*; /****************************************************** * A list of students in a course *****************************************************/ public class Course{ /** collection of Students */ private ArrayList<Student> roster; /***************************************************** Constructor for objects of class Course *****************************************************/ public Course(){ roster = new ArrayList<Student>(); } /***************************************************** Remove student with the...
Define a class called Counter whose internal "count" variable is a basic integer counter. This class...
Define a class called Counter whose internal "count" variable is a basic integer counter. This class track that count from its instantiation in the constructor (can be set to any positive integer, or 0). The count should never be allowed to be negative. Include methods that will set the counter to 0, increase the count by 1, and decrease the count by 1. Include an accessor method that returns the current count value (getCount()). There should be no input /...
A) Declare a String variable called myString0 and initialize it to "Java". B) Declare a String...
A) Declare a String variable called myString0 and initialize it to "Java". B) Declare a String variable called myString1 and initialize it to "Programming". C) Declare a String variable called myString2 and initialize it to "Language". D) Print the concatenation of myString0 + " is a " + myString1 + " " + myString2 + ".". E) Print the sum of the lengths of myString1 and myString2 (must call the length() method). F) Print the 2nd, 4th, and 7th character...
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance...
Step 4: Create a class called BabyNamesDatabase This class maintains an ArrayList of BabyName objects. Instance Variables Declare an ArrayList of BabyName objects. Constructor public BabyNamesDatabase () - instantiate the ArrayList of BabyName objects. You will not insert the items yet. This method will be one line of code. Mutator Methods public void readBabyNameData(String filename) - open the provided filename given as input parameter, use a loop to read all the data in the file, create a BabyName object for...
(JAVA) Referencing the class Oven, complete the Constructor and set method for the instance variable temp....
(JAVA) Referencing the class Oven, complete the Constructor and set method for the instance variable temp. Constructor checks if the temperature is between 0 and 500. If the temperature does not fit the requirement, then set the temperature to be zero. Otherwise, set it to be the value passed to the constructor. Set the power to be off. Set the String to be the color passed to the constructor. Use the same requirement for the constructor for the set temperature...
9.9 LAB: Artwork label (classes/constructors) Define the Artist class with a constructor to initialize an artist's...
9.9 LAB: Artwork label (classes/constructors) Define the Artist class with a constructor to initialize an artist's information and a print_info() method. The constructor should by default initialize the artist's name to "None" and the years of birth and death to 0. print_info() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Define the Artwork class with a constructor to initialize an artwork's information and a print_info() method. The constructor should by...
We have created an ArrayList of Person class. write a method called push that pushes all...
We have created an ArrayList of Person class. write a method called push that pushes all the people with the even length last name to the end of the ArrayList Content of the ArrayList before push [alex Bus, Mary Phillips, Nik Lambard, Rose Rodd, Esa khan, Jose Martinex, Nik Patte] content of the ArrayList after the push method [alex Bus, Nik Lambard, Nik Patte, Mary Phillips, Rose Rodd, Esa khan, Jose Martinex] import java.util.*; class Person { private String name;...
Written in C++ Define a class for a type called Fraction. This class is used to...
Written in C++ Define a class for a type called Fraction. This class is used to represent a ratio of two integers. Include mutator functions that allow the user to set the numerator and denominator. Also include a member function that returns the values of the numerator divided by the denominator as a double. Include an additional member function that outputs the value of the fraction reduced to lowest terms. This will require finding the greatest common divisor for the...
In APA referencing, how do you reference a lab manual?
In APA referencing, how do you reference a lab manual?
Write a class called Animal that contains a static variable called count to keep track of...
Write a class called Animal that contains a static variable called count to keep track of the number of animals created. Your class needs a getter and setter to manage this resource. Create another variable called myCount that is assigned to each animal for each animal to keep track of its own given number. Write a getter and setter to manage the static variable count so that it can be accessed as a class resource
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT