Topic Doubly List for Name card Java write a class NameCardj
Topic: Doubly List for Name card (Java)
write a class NameCard.java which has
•Data fields: name (String), age(int), company(String)
•Methods:
•Public String getName();
•Public int getAge();
•Public String getCom();
•Public void setName(String n);
•Public void setAge(int a);
•Public void setCom(String c);
•toString(): \\\\ can be used to output information on this name card
--------------------------------------------------------------------------------------------------------------------------------------------
write a class DoublyNameCardList.java, which is a doubly linked list and each node of the list a name card.
In the DoublyNameCardList.java:
•Data field:
•Reference data type: head, which always references the first node of the doubly linked list
•Reference data type: tail, which aways references the last node of the doubly linked list
•int count, which is used to record the number of name cards in the list
•Method:
•public DoublyNameCardList(); // A constructor which can initialize the list:
•public void addEnd(NameCard x); // A method that allows you to place a value at the end of the list
•public void addHead(NameCard x); // A method that allows you to place a value at the begin of the list
•public void add(int index, NameCard x); //A method that allows you to place a name card after the given location
• public NameCard get(int index); //A method that allows you to retrieve a name card from a given location
•public int size(); // A method that allows you to return the number of elements that is currently in the list
•public void delete (index n); //A method that will remove a name card at the given index
•Public boolean exist(NameCard n); // test if there has a name card in the list which has same content with n
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Now, write a driver program (the class with the public static void main(String[] args) method) name test.java to test the data structure you just created:
• Fill in the list with 5 name card objects
• print the list on the screen two times:
•From head to tail
•From tail to head
•Call the method in the DoublyNameCardList class one by one, each method you called,
•Print out a message to show what have you done
•print out all the name cards in the list
Solution
NameCard.java :
package org.chegg;
public class NameCard {
String name;
int age;
String company;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getCom() {
return company;
}
public void setName(String n) {
this.name = n;
}
public void setAge(int a) {
this.age = a;
}
public void setCom(String c) {
this.company = c;
}
public String toString(){
return name+\" \"+age+\" \"+company;
}
}
DoublyNameCradList.java :
package org.chegg;
import java.util.ArrayList;
import java.util.List;
public class DoublyNameCardList{
List<NameCard> list = new ArrayList<NameCard>();
int head;
int tail;
int count;
public DoublyNameCardList(List<NameCard> list){
this.list = list;
tail = 4;
head = 0;
count = list.size();
}
public void addEnd(NameCard x){
list.add(x);
tail = list.size()-1;
head = 0;
count = list.size();
}
public void addHead(NameCard x){
list.add(0,x);
tail = list.size()-1;
head = 0;
count = list.size();
}
public void add(int index,NameCard x){
list.add(index,x);
tail = list.size()-1;
head = 0;
count = list.size();
}
public NameCard get(int index){
return list.get(index);
}
public int size(){
return list.size();
}
public void delete(int index){
list.remove(index);
tail = list.size()-1;
head = 0;
count = list.size();
}
public boolean exist(NameCard n){
if(list.contains(n))
return true;
else
return false;
}
}
test.java :
package org.chegg;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
List<NameCard> li = new ArrayList<NameCard>();
Scanner sc = new Scanner(System.in);
DoublyNameCardList dl = new DoublyNameCardList(li);
for(int i=0;i<5;i++){
NameCard nm = new NameCard();
System.out.println(\"Enter name, age & company fo NameCard \"+ (i+1)+\" :\");
String n = sc.next();
int a = sc.nextInt();
String c = sc.next();
nm.setName(n); nm.setAge(a); nm.setCom(c);
li.add(nm);
}
System.out.println(\"List from Head to Tail:\");
for(int i = dl.head;i<dl.size();i++){
NameCard cd = dl.get(i);
System.out.println(cd.name +\" \"+cd.age+\" \"+cd.company);
}
System.out.println(\"List from Tail to Head:\");
for(int i = dl.tail;i> 0 ;i--){
NameCard cd = dl.get(i);
System.out.println(cd.name +\" \"+cd.age+\" \"+cd.company);
}
NameCard x = new NameCard();
NameCard y = new NameCard();
NameCard z = new NameCard();
x.setName(\"Jack\");x.setAge(20);x.setCom(\"tata\");
y.setName(\"Jane\");x.setAge(21);x.setCom(\"zeal\");
x.setName(\"James\");x.setAge(19);x.setCom(\"indigo\");
dl.addHead(x);
dl.add(4,y);
dl.addEnd(z);
dl.delete(2);
System.out.println(dl.size());
System.out.println(dl.exist(z));
NameCard c = dl.get(3);
System.out.println(c.name +\" \"+c.age+\" \"+c.company);
System.out.println(\"Final List:\");
for (NameCard n : li){
System.out.println(n.name +\" \"+n.age+\" \"+n.company);
}
}
}
Sample Input & Output :
Enter name, age & company fo NameCard 1 :
lakshman 29 kpg
Enter name, age & company fo NameCard 2 :
mahesh 32 pph
Enter name, age & company fo NameCard 3 :
manoj 21 hkp
Enter name, age & company fo NameCard 4 :
kumar 43 esp
Enter name, age & company fo NameCard 5 :
sravan 32 rth
List from Head to Tail:
lakshman 29 kpg
mahesh 32 pph
manoj 21 hkp
kumar 43 esp
sravan 32 rth
List from Tail to Head:
sravan 32 rth
kumar 43 esp
manoj 21 hkp
mahesh 32 pph
lakshman 29 kpg
Name Card Status(if found means true else false):true
Jane 21 zeal
Final List:
James 19 indigo
lakshman 29 kpg
manoj 21 hkp
Jane 21 zeal
kumar 43 esp
sravan 32 rth
Jack 20 tata





