SlideShare a Scribd company logo
How can I add multiple names and addresses? To the following code?
Tablea addressBook = new Tablea();
Scanner scanner = new Scanner(System.in);
String key, value;
// Insert a new entry into the address book
System.out.print("Enter a name: ");
key = scanner.nextLine();
System.out.print("Enter an address: ");
value = scanner.nextLine();
addressBook.insert(key, value);
// Lookup an entry in the address book
System.out.print("Enter a name to look up: ");
key = scanner.nextLine();
String result = addressBook.lookUp(key);
if (result != null) {
System.out.println("Address: " + result);
} else {
System.out.println("Name not found");
}
// Delete an entry from the address book
System.out.print("Enter a name to delete: ");
key = scanner.nextLine();
boolean deleted = addressBook.delete(key);
if (deleted) {
System.out.println("Address deleted");
} else {
System.out.println("Name not found");
}
// Update an entry in the address book
System.out.print("Enter a name to update: ");
key = scanner.nextLine();
System.out.print("Enter a new address: ");
value = scanner.nextLine();
boolean updated = addressBook.update(key, value);
if (updated) {
System.out.println("Address updated");
} else {
System.out.println("Name not found");
}
// Display all entries in the address book
System.out.println("All entries:");
addressBook.displayAll();
// Quit the program
System.out.println("Quitting...");
}
public class Node {
private String key;
private String value;
private Node next;
Node() {
// add here ..
}
Node(String key, String value) {
// add here ..
this.key = key;
this.value = value;
this.next = null;
}
public String getKey() {
// add here ..
return this.key;
}
public void setKey(String key) {
// add here ..
this.key = key;
}
public String getValue() {
// add here ..
return this.value;
}
public void setValue(String value) {
// add here ..
this.value = value;
}
public Node getNext() {
// add here ..
return this.next;
}
public void setNext(Node next) {
// add here ..
this.next = next;
}
}
import java.util.Scanner;
public class Tablea {
private Node mark;
public Node getMark() {
return this.mark;
}
public void setMark(Node mark) {
this.mark = mark;
}
public boolean insert(String key, String value) {
Node newNode= new Node(key,value);
if (this.mark== null) {
this.mark = newNode;
}else {
newNode.setNext(this.mark.getNext());
this.mark.setNext(newNode);
}
return true;
}
public String lookUp(String key) {
Node current = this.mark;
while(current!=null) {
if (current.getKey().equals(key)) {
return current.getValue();
}
current = current.getNext();
}
return null;
}
public boolean delete(String key) {
Node current = this.mark;
Node prev = null;
while (current!= null) {
if (current.getKey().equals(key)) {
if (prev == null) {
this.mark = current.getNext();
}else {
prev.setNext(current.getNext());
}
return true;
}
prev = current;
current = current.getNext();
}
return false;
}
public boolean update(String key, String newValue) {
Node current = this.mark;
while (current != null) {
if (current.getKey().equals(key)) {
current.setValue(newValue);
return true;
}
current = current.getNext();
}
return false;
}
public boolean markToStart() {
if (this.mark== null) {
return false;
}
this.mark = null;
return true;
}
public boolean advanceMark() {
if (this.mark== null) {
return false;
}
this.mark = this.mark.getNext();
return true;
}
public String keyAtMark() {
if (this.mark== null) {
return null;
}
return this.mark.getKey();
}
public String valueAtMark() {
if (this.mark== null) {
return null;
}
return this.mark.getValue();
}
public int displayAll() {
Node current = this.mark;
int count = 0;
while (current != null) {
System.out.println(current.getKey()+":"+ current);
}
return count;
}
}

More Related Content

PDF
How do you update an address according to this code- Currently- I fig.pdf
PDF
There is something wrong with my program-- (once I do a for view all t.pdf
PDF
How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdf
PDF
How to fix this error- Exception in thread -main- q- Exit java-lang-.pdf
PDF
Note             Given Code modified as required and required met.pdf
PDF
why will it not display all of the entries-- output -nMenu n-Add Entr.pdf
PDF
package singlylinkedlist; public class Node { public String valu.pdf
PDF
could you implement this function please, im having issues with it..pdf
How do you update an address according to this code- Currently- I fig.pdf
There is something wrong with my program-- (once I do a for view all t.pdf
How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdf
How to fix this error- Exception in thread -main- q- Exit java-lang-.pdf
Note             Given Code modified as required and required met.pdf
why will it not display all of the entries-- output -nMenu n-Add Entr.pdf
package singlylinkedlist; public class Node { public String valu.pdf
could you implement this function please, im having issues with it..pdf

Similar to How can I add multiple names and addresses- To the following code- Tab.pdf (20)

PDF
mainpublic class AssignmentThree {    public static void ma.pdf
PDF
can you add a delete button and a add button to the below program. j.pdf
PDF
in this assignment you are asked to write a simple driver program an.pdf
PDF
Javai have to make a method that takes a linked list and then retu.pdf
PDF
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
PDF
Sorted number list implementation with linked listsStep 1 Inspec.pdf
PDF
Given the following codepackage data1;import java.util.;p.pdf
PDF
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
PDF
TutorialII_Updated____niceupdateprogram.pdf
PDF
This is to test a balanced tree. I need help testing an unbalanced t.pdf
PDF
import java-util--- public class MyLinkedList{ public static void.pdf
PDF
6. Generics. Collections. Streams
PPTX
Link List Programming Linked List in Cpp
PDF
tested on eclipseDoublyLinkedList class.pdf
PDF
I dont know what is wrong with this roulette program I cant seem.pdf
PDF
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PDF
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
PDF
import java.util.Random;defines the Stock class public class S.pdf
PDF
1 OverviewFor this lab, you will implement the insert method and t.pdf
PDF
JAVA...With N.E.T_B.E.A.N.S___________________________________.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
can you add a delete button and a add button to the below program. j.pdf
in this assignment you are asked to write a simple driver program an.pdf
Javai have to make a method that takes a linked list and then retu.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
Given the following codepackage data1;import java.util.;p.pdf
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
TutorialII_Updated____niceupdateprogram.pdf
This is to test a balanced tree. I need help testing an unbalanced t.pdf
import java-util--- public class MyLinkedList{ public static void.pdf
6. Generics. Collections. Streams
Link List Programming Linked List in Cpp
tested on eclipseDoublyLinkedList class.pdf
I dont know what is wrong with this roulette program I cant seem.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
import java.util.Random;defines the Stock class public class S.pdf
1 OverviewFor this lab, you will implement the insert method and t.pdf
JAVA...With N.E.T_B.E.A.N.S___________________________________.pdf

More from ThomasXUMParsonsx (20)

PDF
How would you define a community- The beginning of government's major.pdf
PDF
How would you transform an organisation into a learning organisation u.pdf
PDF
How to fixed these provide listsort-asm assembly code as same as the l.pdf
PDF
How to measure effectiveness using 1- The goal approach2- The human-ba.pdf
PDF
How should a company evaluate a society's level of interdependence exp.pdf
PDF
How much would the force of gravity at the surface of the Earth differ.pdf
PDF
How is the human population changing globally- What general difference.pdf
PDF
How many different groups of two students can be formed out of 4 stud.pdf
PDF
How might environment might influence the total phenotypic variance in.pdf
PDF
How long will it take $100 to grow to $400 if invested at 4-5- compoun.pdf
PDF
How many samples should we collect for the following scenario- We want.pdf
PDF
How is of_type modeled- Not modeled into any relationship Relation and.pdf
PDF
How is Plane_service modeled- Relation and foreign key with single att.pdf
PDF
How has the Internet created a new marketing model- What are the main.pdf
PDF
How have human diseases changed from pre-1950 to the present- and how.pdf
PDF
How frequently are incidents of violence and-or corruption in policing.pdf
PDF
How does myosin II differ from myosin V- Myosin II assembles into bipo.pdf
PDF
How does Fogel justify using only one year of data in his social savin.pdf
PDF
How Effective Managers Use Information Systems Advances in computer-ba.pdf
PDF
How does bias affect human resource function How does bias affect huma.pdf
How would you define a community- The beginning of government's major.pdf
How would you transform an organisation into a learning organisation u.pdf
How to fixed these provide listsort-asm assembly code as same as the l.pdf
How to measure effectiveness using 1- The goal approach2- The human-ba.pdf
How should a company evaluate a society's level of interdependence exp.pdf
How much would the force of gravity at the surface of the Earth differ.pdf
How is the human population changing globally- What general difference.pdf
How many different groups of two students can be formed out of 4 stud.pdf
How might environment might influence the total phenotypic variance in.pdf
How long will it take $100 to grow to $400 if invested at 4-5- compoun.pdf
How many samples should we collect for the following scenario- We want.pdf
How is of_type modeled- Not modeled into any relationship Relation and.pdf
How is Plane_service modeled- Relation and foreign key with single att.pdf
How has the Internet created a new marketing model- What are the main.pdf
How have human diseases changed from pre-1950 to the present- and how.pdf
How frequently are incidents of violence and-or corruption in policing.pdf
How does myosin II differ from myosin V- Myosin II assembles into bipo.pdf
How does Fogel justify using only one year of data in his social savin.pdf
How Effective Managers Use Information Systems Advances in computer-ba.pdf
How does bias affect human resource function How does bias affect huma.pdf

Recently uploaded (20)

PPTX
Pharma ospi slides which help in ospi learning
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
01-Introduction-to-Information-Management.pdf
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Cell Structure & Organelles in detailed.
PPTX
Presentation on HIE in infants and its manifestations
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
master seminar digital applications in india
Pharma ospi slides which help in ospi learning
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
VCE English Exam - Section C Student Revision Booklet
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
GDM (1) (1).pptx small presentation for students
01-Introduction-to-Information-Management.pdf
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Microbial disease of the cardiovascular and lymphatic systems
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Cell Structure & Organelles in detailed.
Presentation on HIE in infants and its manifestations
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Final Presentation General Medicine 03-08-2024.pptx
human mycosis Human fungal infections are called human mycosis..pptx
Final Presentation General Medicine 03-08-2024.pptx
A systematic review of self-coping strategies used by university students to ...
Microbial diseases, their pathogenesis and prophylaxis
Chinmaya Tiranga quiz Grand Finale.pdf
master seminar digital applications in india

How can I add multiple names and addresses- To the following code- Tab.pdf

  • 1. How can I add multiple names and addresses? To the following code? Tablea addressBook = new Tablea(); Scanner scanner = new Scanner(System.in); String key, value; // Insert a new entry into the address book System.out.print("Enter a name: "); key = scanner.nextLine(); System.out.print("Enter an address: "); value = scanner.nextLine(); addressBook.insert(key, value); // Lookup an entry in the address book System.out.print("Enter a name to look up: "); key = scanner.nextLine(); String result = addressBook.lookUp(key); if (result != null) { System.out.println("Address: " + result); } else { System.out.println("Name not found"); } // Delete an entry from the address book System.out.print("Enter a name to delete: "); key = scanner.nextLine(); boolean deleted = addressBook.delete(key);
  • 2. if (deleted) { System.out.println("Address deleted"); } else { System.out.println("Name not found"); } // Update an entry in the address book System.out.print("Enter a name to update: "); key = scanner.nextLine(); System.out.print("Enter a new address: "); value = scanner.nextLine(); boolean updated = addressBook.update(key, value); if (updated) { System.out.println("Address updated"); } else { System.out.println("Name not found"); } // Display all entries in the address book System.out.println("All entries:"); addressBook.displayAll(); // Quit the program System.out.println("Quitting..."); } public class Node { private String key;
  • 3. private String value; private Node next; Node() { // add here .. } Node(String key, String value) { // add here .. this.key = key; this.value = value; this.next = null; } public String getKey() { // add here .. return this.key; } public void setKey(String key) { // add here .. this.key = key; } public String getValue() { // add here .. return this.value; } public void setValue(String value) { // add here .. this.value = value; } public Node getNext() { // add here .. return this.next; } public void setNext(Node next) { // add here .. this.next = next; } }
  • 4. import java.util.Scanner; public class Tablea { private Node mark; public Node getMark() { return this.mark; } public void setMark(Node mark) { this.mark = mark; } public boolean insert(String key, String value) { Node newNode= new Node(key,value); if (this.mark== null) { this.mark = newNode; }else { newNode.setNext(this.mark.getNext()); this.mark.setNext(newNode); } return true; } public String lookUp(String key) { Node current = this.mark; while(current!=null) { if (current.getKey().equals(key)) { return current.getValue(); } current = current.getNext(); } return null; } public boolean delete(String key) { Node current = this.mark; Node prev = null; while (current!= null) { if (current.getKey().equals(key)) { if (prev == null) { this.mark = current.getNext(); }else { prev.setNext(current.getNext()); }
  • 5. return true; } prev = current; current = current.getNext(); } return false; } public boolean update(String key, String newValue) { Node current = this.mark; while (current != null) { if (current.getKey().equals(key)) { current.setValue(newValue); return true; } current = current.getNext(); } return false; } public boolean markToStart() { if (this.mark== null) { return false; } this.mark = null; return true; } public boolean advanceMark() { if (this.mark== null) { return false; } this.mark = this.mark.getNext(); return true; } public String keyAtMark() { if (this.mark== null) { return null; } return this.mark.getKey(); }
  • 6. public String valueAtMark() { if (this.mark== null) { return null; } return this.mark.getValue(); } public int displayAll() { Node current = this.mark; int count = 0; while (current != null) { System.out.println(current.getKey()+":"+ current); } return count; } }