*Add javadoc to code/ comments package bestfriend; import java.util.*; public cl
ID: 3741909 • Letter: #
Question
*Add javadoc to code/ comments
package bestfriend;
import java.util.*;
public class BestFriend {
private static int number = 0;
private int iD;
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;
number++;
iD = number;
}
public String toString() {
return iD + " " + firstName + " " + lastName + " " + cellPhoneNumber + " "+ ". " + nickName;
}
void setFirstName(String first) {
firstName = first;
}
void setLastName(String last) {
lastName = last;
}
void setNickName(String nick) {
nickName = nick;
}
void setCellPhoneNumber(String phone) {
cellPhoneNumber = phone;
}
public boolean equals(Object another) {
if (another instanceof BestFriend) {
BestFriend anotherBFF = (BestFriend) another;
if (firstName.equalsIgnoreCase(anotherBFF.firstName)
&& lastName.equalsIgnoreCase(anotherBFF.lastName)) {
return true;
}
}
return false;
}
}
*****************************************
package bestfriend;
import java.util.*;
class BestFriendSimulation {
private static ArrayList<BestFriend> best;
public static void displayMenu() {
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 " + " ";
System.out.println(MainMenu);
}
public static void main(String arg[]) {
Scanner scanner = new Scanner(System.in);
BestFriendHelper helper = new BestFriendHelper();
int option = 0;
while (true) {
displayMenu();
option = scanner.nextInt();
if (option <= 0 || option > 5) {
System.out.println("Invalid Input! Enter input between 1 to 5");
continue;
}
if (option == 1) {
helper.add();
} else if (option == 2) {
helper.change();
} else if (option == 3) {
helper.remove();
} else if (option == 4) {
helper.display();
} else if (option == 5) {
System.out.println("Exiting");
break;
}
}
}
}
*******************************************************
package bestfriend;
import java.util.ArrayList;
import java.util.Scanner;
public class BestFriendHelper {
private ArrayList<BestFriend> best;
public BestFriendHelper() {
best = new ArrayList<BestFriend>();
}
public void add() {
Scanner scanner = new Scanner(System.in);
System.out.println("Please Enter First Name");
String first = scanner.next();
System.out.println("Please Enter Last Name");
String last = scanner.next();
System.out.println("Please Enter Nick Name");
String nick = scanner.next();
System.out.println("Please Enter Cell Phone Number");
String number = scanner.next();
best.add(new BestFriend(first, last, nick, number));
}
public void change() {
Scanner scanner = new Scanner(System.in);
int AS = 0;
System.out.println("Enter First Name Of Friend You Want To Change");
String first = scanner.next();
System.out.println("Enter Last Name Of Friend You Want To Change");
String last = scanner.next();
BestFriend BF = new BestFriend(first, last, "", "");
AS = best.size();
if (AS == 0) {
System.out.print("List is Empty");
return;
}
for (int i = 0; i < AS; i++) {
if (best.get(i).equals(BF)) {
System.out.println("Please Enter New First Name");
first = scanner.next();
System.out.println("Please Enter New Last Name");
last = scanner.next();
System.out.println("Please Enter New Nick Name");
String nick = scanner.next();
System.out.println("Please Enter New Cell Phone Number");
String number = scanner.next();
best.get(i).setFirstName(first);
best.get(i).setLastName(last);
best.get(i).setNickName(nick);
best.get(i).setCellPhoneNumber(number);
break;
} else if (i == AS - 1) {
System.out.println("No Such Friend Exists");
break;
}
}
}
public void remove() {
int AS = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter First Name Of Friend You Want To Remove");
String first = scanner.next();
System.out.println("Enter Last Name Of Friend You Want To Remove");
String last = scanner.next();
BestFriend BF = new BestFriend(first, last, "", "");
AS = best.size();
if (AS == 0) {
System.out.println("List is Empty");
return;
}
for (int i = 0; i < AS; i++) {
if (best.get(i).equals(BF)) {
best.remove(i);
break;
} else if (i == AS - 1) {
System.out.println("No Such Friend Exists");
break;
}
}
}
public void display() {
int AS = best.size();
if (AS == 0) {
System.out.println("List is Empty");
return;
}
for (int i = 0; i < AS; i++) {
System.out.println(best.get(i).toString());
}
}
}
Explanation / Answer
package bestfriend;
import java.util.*;
public class BestFriend
{
private static int number = 0;
private int iD;
private String firstName;
private String lastName;
private String nickName;
private String cellPhoneNumber;
// constructor
public BestFriend(String aFirstName, String aLastName, String aNickName, String aCellPhoneNumber) {
firstName = aFirstName;
lastName = aLastName;
nickName = aNickName;
cellPhoneNumber = aCellPhoneNumber;
number++;
iD = number;
}
// orerride the toString() function which returns the String if the object is printed
public String toString() {
return iD + " " + firstName + " " + lastName + " " + cellPhoneNumber + " "+ ". " + nickName;
}
// setter methods
// set the first name
void setFirstName(String first) {
firstName = first;
}
// set the last name
void setLastName(String last) {
lastName = last;
}
// set the nick name
void setNickName(String nick) {
nickName = nick;
}
// set the cell phone number
void setCellPhoneNumber(String phone) {
cellPhoneNumber = phone;
}
public boolean equals(Object another) {
// if another is an object of class BestFriend
if (another instanceof BestFriend)
{
// type cast another to BestFriend class
BestFriend anotherBFF = (BestFriend) another;
// if the objects are equal
if (firstName.equalsIgnoreCase(anotherBFF.firstName) && lastName.equalsIgnoreCase(anotherBFF.lastName)) {
return true;
}
}
// if the objects are not equal
return false;
}
}
===========================================================
package bestfriend;
import java.util.*;
class BestFriendSimulation {
private static ArrayList<BestFriend> best;
// function to display the menu to the user
public static void displayMenu()
{
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 " + " ";
System.out.println(MainMenu);
}
// main method
public static void main(String arg[]) {
// create a Scanner class object to get user input
Scanner scanner = new Scanner(System.in);
// create an object of class BestFriendHelper
BestFriendHelper helper = new BestFriendHelper();
int option = 0;
// infinite loop
while (true) {
// display the menu
displayMenu();
option = scanner.nextInt();
// if the entered option is invalid
if (option <= 0 || option > 5) {
System.out.println("Invalid Input! Enter input between 1 to 5");
// go to the beginning of the loop
continue;
}
// if user enetered 1
if (option == 1) {
// call the add() method
helper.add();
// if user enetered 2
} else if (option == 2) {
// call the helper() method
helper.change();
// if user enetered 3
} else if (option == 3) {
// call the remove method
helper.remove();
// if user enetered 4
} else if (option == 4) {
// call the display method
helper.display();
// if user enetered 5
} else if (option == 5) {
System.out.println("Exiting");
// break out of the loop
break;
}
}
}
}
=================================================================
package bestfriend;
import java.util.ArrayList;
import java.util.Scanner;
public class BestFriendHelper {
// create an object to store the objects fo type BestFriend
private ArrayList<BestFriend> best;
// constructor
public BestFriendHelper() {
// initialize arraylist
best = new ArrayList<BestFriend>();
}
// function to add data to ArrayList
public void add() {
// create Scanner object to get user input
Scanner scanner = new Scanner(System.in);
System.out.println("Please Enter First Name");
// read the next element
String first = scanner.next();
System.out.println("Please Enter Last Name");
// read the next element
String last = scanner.next();
System.out.println("Please Enter Nick Name");
// read the next element
String nick = scanner.next();
System.out.println("Please Enter Cell Phone Number");
// read the next element
String number = scanner.next();
// create new BestFriend object and add to best object
best.add(new BestFriend(first, last, nick, number));
}
public void change() {
// create Scanner object to get user input
Scanner scanner = new Scanner(System.in);
int AS = 0;
System.out.println("Enter First Name Of Friend You Want To Change");
// read the next element
String first = scanner.next();
System.out.println("Enter Last Name Of Friend You Want To Change");
// read the next element
String last = scanner.next();
// create an object of class bestfriend
BestFriend BF = new BestFriend(first, last, "", "");
// store the size
AS = best.size();
// if there is no element in BF
if (AS == 0) {
System.out.print("List is Empty");
return;
}
for (int i = 0; i < AS; i++) {
if (best.get(i).equals(BF)) {
System.out.println("Please Enter New First Name");
// read next element
first = scanner.next();
System.out.println("Please Enter New Last Name");
// read next element
last = scanner.next();
System.out.println("Please Enter New Nick Name");
// read next element
String nick = scanner.next();
System.out.println("Please Enter New Cell Phone Number");
// read next element
String number = scanner.next();
// update the fields of i th element in best
best.get(i).setFirstName(first);
best.get(i).setLastName(last);
best.get(i).setNickName(nick);
best.get(i).setCellPhoneNumber(number);
// break out of loop
break;
}
// if the element is not found
else if (i == AS - 1) {
System.out.println("No Such Friend Exists");
break;
}
}
}
public void remove() {
int AS = 0;
// create Scanner object to get user input
Scanner scanner = new Scanner(System.in);
System.out.println("Enter First Name Of Friend You Want To Remove");
// read next element
String first = scanner.next();
System.out.println("Enter Last Name Of Friend You Want To Remove");
// read next element
String last = scanner.next();
// create object of type BestFriend class
BestFriend BF = new BestFriend(first, last, "", "");
// store the size
AS = best.size();
// if there is no element in BF
if (AS == 0) {
System.out.println("List is Empty");
return;
}
for (int i = 0; i < AS; i++)
{
// if the current element is the required element to be removed
if (best.get(i).equals(BF)) {
// remove the element
best.remove(i);
// break out of loop
break;
}
// if the element is not found
else if (i == AS - 1) {
System.out.println("No Such Friend Exists");
// break out of loop
break;
}
}
}
public void display() {
int AS = best.size();
// if the list is empty
if (AS == 0) {
System.out.println("List is Empty");
return;
}
for (int i = 0; i < AS; i++) {
System.out.println(best.get(i).toString());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.