PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a cla
PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN
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
2, 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
==========Test.java=============
public class Test {
   public static void main(String[] args) {
        System.out.print(\"ok\");
        DoublyNameCardList list = new DoublyNameCardList();
         list.addHead(new NameCard(\"loknath\",28,\"oracle\"));
         list.addEnd(new NameCard(\"loknath\",2,\"zen\"));
         list.addEnd(new NameCard(\"loknath\",7,\"zen\"));
         list.addEnd(new NameCard(\"loknath\",27,\"zen\"));
         list.addEnd(new NameCard(\"loknath\",27,\"zen\"));  
         list.addEnd(new NameCard(\"loknath\",27,\"zen\"));
         list.add(2, new NameCard(\"ok\", 88,\"test\"));
         System.out.println(list);
    }
 }
======================= NameCard.java ==================
 public class NameCard {
    private String name;
    private int age;
    private String company;
    private NameCard next;
    private NameCard pre;
    public NameCard(){
       
    }
    public NameCard(String n,int a,String c){
        name = n;
        age = a;
        company = c;
        next=null;
        pre=null;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCompany() {
        return company;
    }
    public void setCompany(String company) {
        this.company = company;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
   
    @Override
    public String toString(){
        return \" (\" + name+\", \"+age+\", \"+company+\" ) \";
    }
    public NameCard getNext() {
        return next;
    }
    public void setNext(NameCard next) {
        this.next = next;
    }
    public NameCard getPre() {
        return pre;
    }
    public void setPre(NameCard pre) {
        this.pre = pre;
    }
 }
=======================DoublyNameCardList.java============
 public class DoublyNameCardList {
    NameCard head;
    NameCard tail;
    int count;
   
    public DoublyNameCardList(){
        head = new NameCard();
        tail = new NameCard();
        tail.setPre(head);
        head.setNext(tail);
        count=0;
    }
    public void addHead(NameCard x){
        x.setNext(head.getNext());
        x.getNext().setPre(x);
        head = x;
    }
    public void addEnd(NameCard x){
        x.setPre(tail.getPre());
        x.setNext(tail);
        x.getPre().setNext(x);
        tail.setPre(x);
    }
    public void add(int index, NameCard x){
        // fix the position
        if (index < 0) {
            index = 0;
        }
        if (index > count) {
            index = count;
        }
       // if the list is empty, make it be the only element
        if (head == null) {
            head = x;
            tail = head;
        }
        // if adding at the front of the list...
        else if (index == 0) {
            x.setNext(head);
            head = x;
        }
        // else find the correct position and insert
        else {
            NameCard temp = head;
            for (int i=1; i<index; i+=1) {
                temp = temp.getNext();
            }
                   
            x.setNext(x.getNext());
            x.setPre(temp);
            x.getNext().setPre(x);
            temp.setNext(x);
        }
        // the list is now one value longer
        count += 1;
    }
    public void delete (int index) {
        // fix position
        if (index < 0) {
            index = 0;
        }
       
        if (index >= count) {
            index = count-1;
        }
       
        // if nothing in the list, do nothing
        if (head == null)
            return;
       
        // if removing the head element...
        if (index == 0) {
            head = head.getNext();
            if (head == null)
                tail = null;
        }
        // else advance to the correct position and remove
        else {
            NameCard temp = head;
            for (int i=1; i<index; i+=1) {
                temp = temp.getNext();
            }
            temp.getNext().setPre(temp.getPre());
            temp.getPre().setNext(temp.getNext());
        }
        // reduce the length of the list
        count -= 1;
    }
    public int size(){
        return count;
    }
   
    public NameCard get(int index){
        int i=0;
        NameCard tempCard=head;
        while(i<index){
            if(tempCard==null){
                System.out.println(\"Index out of bond! Unable to get\");
                return null;
            }
            tempCard = tempCard.getNext();  
            i++;
        }
        return tempCard;
    }
    public String toString(){
        NameCard temp = head;
        while(temp!=null){
            System.out.println(temp);
            temp =temp.getNext();
        }
        return \"\";
    }
 }





