I have the java code for two programs (ContactNode.java and ContactList.java) th
ID: 3572586 • Letter: I
Question
I have the java code for two programs (ContactNode.java and ContactList.java) that work together to recreate the expected output and would appreciate any help altering the code to meet the requirements.
1. Unit Test: Tests that insertAfter() correctly inserts Juan Alberto Jr.'s ContactNode after Roxanne Hughes's ContactNode and getNext() correctly returns Juan Alberto Jr.'s ContactNode.
Your Output: java.lang.NullPointerException
2. Compare Output
Input: Roxanne Hughes
443-555-2864
Juan Alberto Jr.
410-555-9385
Rachel Phillips
310-555-6610
Your Ouput: Person 1
Enter name: Enter phone number: You entered: Roxanne Hughes, 443-555-2864
Person 2
Enter name: Enter phone number: You entered: Juan Alberto Jr., 410-555-9385
Person 3
Enter name: Enter phone number: You entered: Rachel Phillips, 310-555-6610
Name: Roxanne Hughe
Expected Output: Person 1
Enter name:
Enter phone number:
You entered: Roxanne Hughes, 443-555-2864
Person 2
Enter name:
Enter phone number:
You entered: Juan Alberto Jr., 410-555-9385
Person 3
Enter name:
Enter phone number:
You entered: Rachel Phillips, 310-555-6610
3. Compare Output
Input: David Frederick
240-555-2104
Bernard Green
802-555-4986
Hillary Jones
212-555-9467
Your Ouput: Person 1
Enter name: Enter phone number: You entered: David Frederick, 240-555-2104
Person 2
Enter name: Enter phone number: You entered: Bernard Green, 802-555-4986
Person 3
Enter name: Enter phone number: You entered: Hillary Jones, 212-555-9467
Name: David Frede
Expected Output: Person 1
Enter name:
Enter phone number:
You entered: David Frederick, 240-555-2104
Person 2
Enter name:
Enter phone number:
You entered: Bernard Green, 802-555-4986
Person 3
Enter name:
Enter phone number:
You entered: Hillary Jones, 212-555-9467
ContactNode.java
public class ContactNode {
private String contactName;
private String contactPhoneNumber;
private ContactNode nextNodePtr;
public ContactNode(String contactName, String contactPhoneNumber) {
this.contactName = contactName;
this.contactPhoneNumber = contactPhoneNumber;
this.nextNodePtr = null;
}
public String getName() {
return contactName;
}
public String getPhoneNumber() {
return contactPhoneNumber;
}
public ContactNode getNext() {
return nextNodePtr;
}
public void printContactNode(){
System.out.println("Name: " + contactName);
System.out.println("Phone Number: " + contactPhoneNumber);
}
public void insertAfter(ContactNode prev){
prev.nextNodePtr = this;
}
}
ContactList.java
import java.util.Scanner;
public class ContactList {
public static void main(String[] args){
String name, number;
Scanner in = new Scanner(System.in);
System.out.println("Person 1");
System.out.print("Enter name: ");
name = in.nextLine();
System.out.print("Enter phone number: ");
number = in.nextLine();
System.out.println("You entered: " + name + ", " + number);
ContactNode node1 = new ContactNode(name, number);
System.out.println("Person 2");
System.out.print("Enter name: ");
name = in.nextLine();
System.out.print("Enter phone number: ");
number = in.nextLine();
System.out.println("You entered: " + name + ", " + number);
ContactNode node2 = new ContactNode(name, number);
System.out.println("Person 3");
System.out.print("Enter name: ");
name = in.nextLine();
System.out.print("Enter phone number: ");
number = in.nextLine();
System.out.println("You entered: " + name + ", " + number);
ContactNode node3 = new ContactNode(name, number);
node3.insertAfter(node2); // insert node3 after node2
node2.insertAfter(node1); // insert node2 after node1
ContactNode temp = node1;
while (temp != null){
temp.printContactNode();
temp = temp.getNext();
}
}
}
Explanation / Answer
public class ContactNode {
private String contactName;
private String contactPhoneNumber;
private ContactNode nextNodePtr;
public ContactNode(String contactName, String contactPhoneNumber) {
this.contactName = contactName;
this.contactPhoneNumber = contactPhoneNumber;
this.nextNodePtr = null;
}
public String getName() {
return contactName;
}
public String getPhoneNumber() {
return contactPhoneNumber;
}
public ContactNode getNext() {
return nextNodePtr;
}
public void printContactNode(){
System.out.println("Name: " + contactName);
System.out.println("Phone Number: " + contactPhoneNumber);
}
public void insertAfter(ContactNode prev){
prev.nextNodePtr = this;
}
}
//everything is working perfectly, place both files in same path
ouput:-
run:
Person 1
Enter name:
David Frederick
240-555-2104
Bernard Green
802-555-4986
Hillary Jones
212-555-9467
Enter phone number:
You entered: David Frederick, 240-555-2104
Person 2
Enter name:
Enter phone number:
You entered: Bernard Green, 802-555-4986
Person 3
Enter name:
Enter phone number:
You entered: Hillary Jones, 212-555-9467
Name: David Frederick
Phone Number: 240-555-2104
Name: Bernard Green
Phone Number: 802-555-4986
Name: Hillary Jones
Phone Number: 212-555-9467
Enter name:
Enter phone number:
You entered: David Frederick, 240-555-2104
Enter name:
Enter phone number:
You entered: Bernard Green, 802-555-4986
Enter name:
Enter phone number:
You entered: Hillary Jones, 212-555-9467
BUILD SUCCESSFUL (total time: 3 seconds)