Write a program that simulates an address book. Driver main method should be as
ID: 3545443 • Letter: W
Question
Write a program that simulates an address book.
Driver main method should be as shown below. Add comments to explain functionality
import java.util.ArrayList;
public class LastFirstChapter10
//Replace LastFirst with your Last Name and First Name
{
public static void main(String [] args)
{
ArrayList<LiFiAddressBook> aBook = new ArrayList<LiFiAddressBook>();
//Replace LiFi with Last Initial First Initial (for all instances)
for (int count = 0; count < 1; count++)
{
//****************************
//add code here to add new entry to ArrayList
//call addEntry
//print blank line
//****************************
}
int foundIndex = LiFiAddressBook.search(aBook);
System.out.println();
if (foundIndex > -1)
aBook.get(foundIndex).display();
else
System.out.println("No Entry Found");
}
}
25%
Output should be as shown in example at bottom.
LiFiAddressBook.java class
Instance variables:
First Name (string)
Last Name (integer)
Street Address (string)
City State (string)
Zip Code (string)
addEntry method:
Get input for variables above. See sample in example at bottom.
search method:
Receive ArrayList as argument
Output Search Menu (see example at bottom)
Utilize a switch and search ArrayList for field specified.
Return index number if entry found or -1 if not found
display method:
Print results as shown in example at bottom.
Explanation / Answer
public static void main(String args[]) {
//creating LinkedList with 5 elements including head
LinkedList linkedList = new LinkedList();
LinkedList.Node head = linkedList.head();
linkedList.add( new LinkedList.Node("1"));
linkedList.add( new LinkedList.Node("2"));
linkedList.add( new LinkedList.Node("3"));
linkedList.add( new LinkedList.Node("4"));
//finding middle element of LinkedList in single pass
LinkedList.Node current = head;
int length = 0;
LinkedList.Node middle = head;
while(current.next() != null){
length++;
if(length%2 ==0){
middle = middle.next();
}
current = current.next();
}
if(length%2 == 1){
middle = middle.next();
}
System.out.println("length of LinkedList: " + length);
System.out.println("middle element of LinkedList : " + middle);
}
}
class LinkedList{
private Node head;
private Node tail;
public LinkedList(){
this.head = new Node("head");
tail = head;
}
public Node head(){
return head;
}
public void add(Node node){
tail.next = node;
tail = node;
}
public static class Node{
private Node next;
private String data;
public Node(String data){
this.data = data;
}
public String data() {
return data;
}
public void setData(String data) {
this.data = data;
}
public Node next() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public String toString(){
return this.data;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.