Question

PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

==========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 "";
   }
}

Add a comment
Know the answer?
Add Answer to:
PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In java Write a method public void printReverse() that prints the elements of a doubly linked...

    In java Write a method public void printReverse() that prints the elements of a doubly linked list in reverse. Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting. In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the...

  • In java Write a method public void printReverse() that prints the elements of a doubly linked...

    In java Write a method public void printReverse() that prints the elements of a doubly linked list in reverse. Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting. In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to...

    CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to implement its inventory of computing machines as a linked list, called ComputerList. Write a Computer node class, called ComputerNode, to hold the following information about a Computer: • code (as a String) • brand (as a String) • model (as a String) • price (as double) • quantity (as int) ComputerNode should have constructors and methods (getters, setters, and toString()) to manage the above...

  • can you solve it in java please Create the following: 1. Class Invoice ( the node...

    can you solve it in java please Create the following: 1. Class Invoice ( the node ) that includes three instance variables:     int No; // the Invoice No             String CustName; // the Customer name             int Amount; // the Invoice Amount Invoice next; // points to the next Invoice Default and overloaded constructors 2. Class Shop that includes three instance variables: Invoice head; Invoice Tail; Your class should have the following: • A method that initializes the instance variables....

  • Parts B is about our linked list implementation. avvut our linked list implementation. Recall that the...

    Parts B is about our linked list implementation. avvut our linked list implementation. Recall that the declaration of a list node is. class DListNode { public int key; public DistNode previous; public DListNode next; de ollos Salon You can assume that this is an inner class within the full MyLinked List class. B. Write a method that will add a new element to the front of a doubly linked list. Assume that head and tail point to the head and...

  • Using Java You are given a Node class and a List class: public class Node {...

    Using Java You are given a Node class and a List class: public class Node {    int   data;     Node next; } public class List {     Node first; } You are also given a Stack class. The following functions are available for use: public class Stack { public boolean isEmpty(){}; public void push(int n){}; public int pop(){};} Write a Java method snglyRevToStck that pushes the data found in a linked list t in reverse order into the stack...

  • Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element...

    Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element stored at this node */ private int element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n...

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

  • Assignment Description. In this assignment, you will design a class to represent a book of recipes...

    Assignment Description. In this assignment, you will design a class to represent a book of recipes using a doubly linked list. Your recipe book will support methods to add a recipe and get a recipe at a position; your main will use these to print all the names of all the recipes in the book. Create a C++ class called RecipeBook. Your class must implement the following variables, constructors, and methods: Create a private inner class called Node for implementing...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT