Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that tests whether two given persons are neighbor of each o

ID: 668517 • Letter: W

Question

Write a Java program that tests whether two given persons are neighbor of each other, if a person lives in the same city as another person. This program contains a class called person which presents a person's name and place of residence. The structure of this class can be presented as below: /** Constructs a default Person object that has an empty name and city. */ /** Constructs a Person object that has a given name and city. /** Sets a person's last-name and city. /** Sets a person's first-name and last-name and city. /** Gets a person's name. /** Gets a person's city of residence. /** Gets a person's name and city as a string. /** Tests whether a person lives in the same city as another person.

Explanation / Answer

import java.util.Scanner; public class Person { private String name; private String phoneNumber; private String city; public Person() { } public Person(String name) { this.name = name; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public String getName() { return name; } public void setName(String name) { this.name = name; } public static boolean checkSameCity(Person first, Person second) { // checks if the cities stored in both Person instances are equal return (first.getCity().equals(second.getCity())); } public static Person registerChild (Person parent) { // creates new Person instance with a new name and the same phones number and city as another instance Person child = new Person(); child.setName("Abc"); child.setCity(parent.getCity()); child.setPhoneNumber(parent.getPhoneNumber()); return child; } public static void main(String[] args) { Scanner in = new Scanner(System.in); Person firstPerson = new Person(); Person secondPerson = new Person(); // prompts user for two Persons' instance info System.out.println("Please enter the first person's name: "); firstPerson.setName(in.next()); System.out.println("Please enter the first person's phone number: "); firstPerson.setPhoneNumber(in.next()); System.out.println("Please enter the first person's city: "); firstPerson.setCity(in.next()); System.out.println(" Stored! "); System.out.println("Please enter the second person's name: "); secondPerson.setName(in.next()); System.out.println("Please enter the second person's phone number: "); secondPerson.setPhoneNumber(in.next()); System.out.println("Please enter the second person's city: "); secondPerson.setCity(in.next()); // compares the cities of the two Person instances created above using the checkSameCity() method. if (checkSameCity(firstPerson, secondPerson)) { System.out.println(" " + firstPerson.getName() + " and " + secondPerson.getName() + " live in the same city!"); } else { System.out.println(" " +firstPerson.getName() + " and " + secondPerson.getName() + " are not in the same city."); } System.out.println(" Hey, " + firstPerson.getName() + " had a baby named " + registerChild(firstPerson).getName() + "!"); System.out.println(registerChild(firstPerson).getName() + "'s number is " + registerChild(firstPerson).getPhoneNumber() + " and (s)he lives in " + registerChild(firstPerson).getCity() + ". Just like " + firstPerson.getName() + "!"); } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote