Question
I THINK I'VE FINISHED THE MAIN JAVA CODE CORRECTLY, BUT I AM HAVING TROUBLE COMING UP WITH THE LAST 5 LINES OF CODE IN THE
I THINK I'VE FINISHED THE MAIN JAVA CODE CORRECTLY, BUT I AM HAVING TROUBLE COMING UP WITH THE LAST 5 LINES OF CODE IN THE TESTER JAVA CODE.
MAIN JAVA CODE:
/**
* A class for a Person object that stores first name and last name.
*
* You should create the following methods:
* - Create a constructor that takes in a first name and last name
* - Create a getFullName method which returns the full name of the person
* - Create a getInitials method which returns the initials of the person
* - Create an equals method which returns whether two Person objest are equal
*/
public class Person
{
private String firstName;
private String familyName;
// Create a Constructor Method with two arguments to set the
// firstName and familyName instance variables
//-----------Start below here. To do: approximate lines of code = 3
//
public Person(String firstName, String familyName)
{
this.firstName = firstName;
this.familyName = familyName;
}
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
// Create a public method called getFullName() that returns a string containing
// the first name followed by a blank space followed by the family name
//-----------Start below here. To do: approximate lines of code = 2
//
public String getFullName()
{
return firstName + " " + familyName;
}
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
// Create a public method call getInitials() that returns a string containing the
// first letter of the first name followed by the first letter of
// the family name. Hint: use the substring method of class String.
//-----------Start below here. To do: approximate lines of code = 2
//
public String getInitials()
{
return firstName.substring(0,1) + familyName.substring(0,1);
}
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
// Create a method boolean equals(Person other) - **using this exact method signature!!** -
// this method will return true if and only if the two person objects have the same first
// and last name.
//
// Don't forget you must use the equals method of class String when comparing familyName strings
// That means that the equals method of class Person will be calling the equals method of class String using
// the familyName variable but these are two different equals methods!!
//-----------Start below here. To do: approximate lines of code = 3
//
public boolean equals(Person other)
{
if(firstName == familyName)
{
return true;
}
else
{
return false;
}
}
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
TESTER JAVA CODE:
import java.util.ArrayList;
/**
* This class tests the Person class. You should complete the findPerson method
*/
public class PersonTester
{
// Create a static method that returns true if a person with the given first name and last name
// is in the given ArrayList of Person objects
//
// HINT: Use the equals method you defined in Person
public static boolean findPerson(ArrayList
{
//-----------Start below here. To do: approximate lines of code = 5
//
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
public static void main(String[] args)
{
Person p1 = new Person("Tom","O'Brien");
Person p2 = new Person("Alireza","Sadeghian");
Person p3 = new Person("Timothy","O'Brien");
System.out.println("Name = " + p1.getFullName());
System.out.println("Initials = " + p1.getInitials());
System.out.println("Name = " + p2.getFullName());
System.out.println("Initials = " + p2.getInitials());
System.out.println("Expected: Name = Tom O'Brien Initials = TO Name = Alireza Sadeghian Initials = AS");
ArrayList
if (findPerson(people, "Alireza", "Sadeghian"))
System.out.println("Alireza Sadeghian is in the array list");
else
System.out.println("Alireza Sadeghian is not in the array list");
System.out.println("Expected: Alireza Sadeghian is in the array list");
people.add(p1);
people.add(p2);
people.add(p3);
if (findPerson(people, "Rick", "Valenzano"))
System.out.println("Rick Valenzano is in the array list");
else
System.out.println("Rick Valenzano is not in the array list");
System.out.println("Expected: Rick Valenzano is not in the array list");
if (findPerson(people, "Alireza", "Sadeghian"))
System.out.println("Alireza Sadeghian is in the array list");
else
System.out.println("Alireza Sadeghian is not in the array list");
System.out.println("Expected: Alireza Sadeghian is in the array list");
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started