Take the Driver class of the previous assignment and change
Take the Driver class of the previous assignment, and change it to use an array of BestFriend objects, instead of an arrayList of BestFriend objects. Do all of the requirements of the assignment, but simulate \"removing\" the last element by moving nulls to the last element of the array.
Below is the code for Arraylist but switch it to Array of
/* 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\"+\"\ \"+\"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 \"+\"\ \";
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;
}
}
}
}
}
Solution
Please find the required program and output below: Please find the comments against each line for the description:
------------------------------------------------------------------
OUTPUT:
Please Enter Option Number Of your Choice
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
4
List is Empty
Please Enter Option Number Of your Choice
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
1
Please Enter First Name
john
Please Enter Last Name
abraham
Please Enter Nick Name
ja
Please Enter Cell Phone Number
123456
Please Enter Option Number Of your Choice
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
1
Please Enter First Name
Donald
Please Enter Last Name
Trump
Please Enter Nick Name
dt
Please Enter Cell Phone Number
904231
Please Enter Option Number Of your Choice
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
4
1. ja john abraham 123456
2. dt Donald Trump 904231
Please Enter Option Number Of your Choice
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
5




