Question

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.

  1. 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 information as well as the link to next node in the list.

  1. Write the ComputerList class to hold objects of the class ComputerNode. This class should define:

• Two instance variables head and size. (size should be updated during insertions and removals from the list)

The constructor of the class is: public ComputerList(){ head = null; size = 0; }

• The ComputerList class should implement the following interface:

public interface CList { public boolean isEmpty(); // returns true if the list is empty, false otherwise

public int listSize(); // returns the number of items in the list     

public ComputerNode getAt(int index); //returns the ComputerNode object at the specified index or null if the list is empty

public void addFirst(ComputerNode item); // adds a Computer at the front of the list

public void addLast(ComputerNode item); // adds a Computer at the end of the list

public void addAt(int index, ComputerNode item); // adds a Computer to the list at the given index

public ComputerNode search(String code); // search and return the Computer with the given code

public ComputerNode[] searchPriceGreaterThan(int p); //search and return an array of the set of ComputerNode items //having a price greater than p

public String removeAt(int index); // removes the Computer from the list that has the given index

public String remove(ComputerNode item); // removes the first item in the list whose data equals // the given item data

public void removeDuplicates(); // removes the duplicates of every Computer in the list (nodes with the same data)

@Override

public String toString(); // implement a toString() method that prints the list in the // format: //[ size: the_size_of_the_list // Computer1, // Computer2, //.... ] }

3.Write a TestComputerList class to test the class ComputerList. This class should have a main method in which you perform the following actions:

• Create a ComputerList object,

• Insert 10 ComputerNode objects into the created Computer List,

• Print the content of your Computer list,

• Find out in the list the items that have a price greater than 3000. Print them out.

• Remove the first element of the list

• Remove the item at index 3

• Print again the content of your Computer list,

• Insert some duplicate Computers in the list

• Print again the content of your Computer list,

• Remove duplicates

• Print again the content of your Computer list,

For each operation above, print a small message that describes the operation you are doing. For example: System.out.println(“Insertion of 10 Computers in the list”);

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

ANSWER:-

CODE:-

import java.util.*;
interface CList{
   public boolean isEmpty();  
}
class ComputerNode{
   String code, brand, model;
   double price;
   int quantity;
   ComputerNode next;
   ComputerNode(String code, String brand, String model, double price, int quantity){
       this.code = code;
       this.brand = brand;
       this.model = model;
       this.price = price;
       this.quantity = quantity;
       this.next = null;
   }
   ComputerNode(){
       this.code = "";
       this.brand = "";
       this.model = "";
       this.price = 0.0;
       this.quantity = 0;
       this.next = null;
   }
   void setCode(String code){
       this.code = code;
   }
   void setBrand(String brand){
       this.brand = brand;
   }
   void setModel(String model){
       this.model = model;
   }
   void setPrice(double price){
       this.price = price;
   }
   void setQuantity(int quantity){
       this.quantity = quantity;
   }
   void setnext(ComputerNode next){
       this.next = next;
   }
   String getCode(){
       return this.code;
   }
   String getBrand(){
       return this.brand;
   }
   String getModel(){
       return this.model;
   }
   double getPrice(){
       return this.price;
   }
   int getQuantity(){
       return this.quantity;
   }
   public String toString(){
       return "Code: "+this.getCode()+"\n"
       + "Brand: "+this.getBrand()+"\n"
       + "Model: "+this.getModel()+" \n"
       + "Price: "+this.getPrice()+"\n"
       + "Quantity: "+this.getQuantity()+"\n";
   }
}
class ComputerList{
   ComputerNode head;
   int size;
   ComputerList(){
       this.head = null;
       this.size = 0;
   }
   public boolean isEmpty(){
       return head == null;
   }
   public int listSize(){
       return this.size;
   }
   public ComputerNode getAt(int index){
       if(head == null) return null;
       ComputerNode it = head;
       int i=0;
       while(it!=null){
           if(i == index){
               return it;
           }
           i++;
           it = it.next;
       }
       return null;
   }
   public void addFirst(ComputerNode item){
       item.next = head;
       head = item;
       this.size++;
   }
   public void addLast(ComputerNode item){
       ComputerNode it = head;
       if(head == null){
           head = item;
       }else{
           while(it.next!=null){
               it = it.next;
           }
           it.next = item;
       }
       this.size++;
   }
   public void addAt(int index, ComputerNode item){
       if(index==0){
           this.addFirst(item);
       }else if(index == this.size){
           this.addLast(item);
       }else{
           if(head == null){
               return ;
           }else{
               ComputerNode it = head;
               ComputerNode prev = head;
               int i=0;
               while(it!=null){
                   if(i == index){
                       prev.next = item;
                       item.next = it;
                       break;
                   }
                   i++;
                   prev = it;
                   it = it.next;
               }
           }
           this.size++;

       }
   }
   public ComputerNode search(String code){
       ComputerNode it = head;
       while(it!=null){
           if(code.equals(it.getCode())){
               return it;
           }
           it = it.next;
       }
       return null;
   }
   public ComputerNode[] searchPriceGreaterThan(int p){
       ArrayList<ComputerNode> list = new ArrayList<ComputerNode>();
       ComputerNode it = head;
       while(it!=null){
           if(it.getPrice() > p){
               list.add(it);
           }
           it = it.next;
       }
       ComputerNode[] com = new ComputerNode[list.size()];
       for(int i=0;i<list.size();i++){
           com[i] = list.get(i);
       }
       return com;
   }
   public String removeAt(int index){
       if (head == null) return null;
       String removed = null;
       if(index == 0){
           removed = head.toString();
           head = head.next;
       }else{
           ComputerNode it = head;
           ComputerNode prev = head;
           int i=0;
           while(it!=null){
               if(i == index){
                   removed = it.toString();
                   prev.next = it.next;
                   break;
               }
               i++;
               prev = it;
               it = it.next;
           }
       }
       this.size--;
       return removed;
   }
   public String remove(ComputerNode item){
       if (head == null) return null;
       String code = item.getCode();
       String brand = item.getBrand();
       String model = item.getModel();
       double price = item.getPrice();
       int quantity = item.getQuantity();
       String removed = null;
       if(head.getCode() == code && head.getBrand()==brand && head.getModel() == model && head.getPrice() == price && head.getQuantity() == quantity){
           removed = head.toString();
           head = head.next;
       }else{
           ComputerNode it = head;
           ComputerNode prev = head;
           while(it!=null){
               if(it.getCode() == code && it.getBrand()==brand && it.getModel() == model && it.getPrice() == price && it.getQuantity() == quantity){
                   removed = head.toString();
                   prev.next = head.next;
                   break;
               }
               prev = it;
               it = it.next;
           }
       }
       this.size--;
       return removed;
   }
   public boolean contains(ArrayList<ComputerNode> list, ComputerNode item){
       String code = item.getCode();
       String brand = item.getBrand();
       String model = item.getModel();
       double price = item.getPrice();
       int quantity = item.getQuantity();
       for (int i=0;i<list.size() ; i++) {
           ComputerNode it = list.get(i);
           if(it.getCode() == code && it.getBrand()==brand && it.getModel() == model && it.getPrice() == price && it.getQuantity() == quantity){
               return true;
           }
       }
       return false;
   }
   public void removeDuplicates(){
       ArrayList<ComputerNode> list = new ArrayList<ComputerNode>();
       ComputerNode it = head;
       while(it!=null){
           if(!this.contains(list,it)){
               list.add(it);
           }
           it = it.next;
       }
       this.head = null;
       for(int i=0;i<list.size();i++){
           ComputerNode com = list.get(i);
           com.next = null;
           this.addLast(list.get(i));
       }
       this.size = list.size();
   }
   public String toString(){
       String str = "Size: "+this.size+"\n\n";
       ComputerNode it = head;
       while (it!=null) {
           str += it.toString()+"\n";
           it = it.next;
       }
       return str;
   }
}
public class TestComputerList{
   public static void main(String[] args) {
       ComputerList computers = new ComputerList();
       System.out.println("Insertion of 10 Computes in the list:");
       computers.addLast(new ComputerNode("C1","Lenovo","L321",2000,1));
       computers.addLast(new ComputerNode("C2","Lenovo","L432",900,2));
       computers.addFirst(new ComputerNode("C3","ASUS","SUS2.3",5000,1));
       computers.addFirst(new ComputerNode("C4","ASUS","S2.0",899,3));
       computers.addLast(new ComputerNode("C5","HP","H234",2000,1));
       computers.addLast(new ComputerNode("C6","HP","H897",35000,1));
       computers.addAt(4,new ComputerNode("C7","Lenovo","L865",2999,1));
       computers.addAt(7,new ComputerNode("C8","Apple","L321",85000,4));
       computers.addAt(0,new ComputerNode("C9","Apple","L321",95000,3));
       computers.addLast(new ComputerNode("C10","Apple","L321",199999,2));
       System.out.println("Computers in the list:");
       System.out.println(computers);
       ComputerNode[] computersGreater = computers.searchPriceGreaterThan(3000);
       System.out.println("Computers whose price is greater than 3000:");
       for(int i=0;i<computersGreater.length;i++){
           System.out.println(computersGreater[i].toString());
       }
       computers.removeAt(0);
       computers.removeAt(3);
       System.out.println("List after removing first and 3rd indices:");
       System.out.println(computers);
       computers.addFirst(new ComputerNode("C5","HP","H234",2000,1));
       computers.addFirst(new ComputerNode("C6","HP","H897",35000,1));
       System.out.println("List after inserting duplicates:");
       System.out.println(computers);
       computers.removeDuplicates();
       System.out.println("List after removing duplicates:");
       System.out.println(computers);
   }
}

NOTE:- If you need any modifications in the code,please comment below.Please give positive rating.THUMBS UP.

            THANK YOU!!!!

OUTPUT:-

Add a comment
Know the answer?
Add Answer to:
CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to...
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
  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

  • JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public...

    JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public E previous() { // Returns the previous Element return null; } Explanation: We have this class with these two implemented inferfaces: The interfaces are: package edu.ics211.h04; /** * Interface for a List211. * * @author Cam Moore * @param the generic type of the Lists. */ public interface IList211 { /** * Gets the item at the given index. * @param index the index....

  • In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> {...

    In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> { /** Adds one element to the top of this stack. * @param element element to be pushed onto stack */ public void push (T element);    /** Removes and returns the top element from this stack. * @return T element removed from the top of the stack */ public T pop(); /** Returns without removing the top element of this stack. * @return T...

  • Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface:...

    Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> {      /**       * Append an item to the end of the list       *       * @param item – item to be appended       */ public void append(E item);      /**       * Insert an item at a specified index position       *       * @param item – item to be...

  • Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to under...

    Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...

  • In a file named LLBag.java, write a class called LLBag that implements the Bag interface using...

    In a file named LLBag.java, write a class called LLBag that implements the Bag interface using a linked list instead of an array. You may use a linked list with or without a dummy head node. Bag interface code: /* * Bag.java * * Computer Science 112, Boston University */ /* * An interface for a Bag ADT. */ public interface Bag { /*    * adds the specified item to the Bag. Returns true on success    * and...

  • Assignment: A set is a collection of items without repetitions. The goal of this assignment is...

    Assignment: A set is a collection of items without repetitions. The goal of this assignment is to implement a set as a data structure. Do the following. 1. (30 points) Starting with the template attached, implement a generic class Set(T) that maintains a set of items of generic type T using the class ArrayList(T) in the Java API. Your Set class must provide the following instance methods: add: that adds a new item. (Ignore if already in the set.) remove:...

  • Complete an Array-Based implementation of the ADT List including a main method and show that the...

    Complete an Array-Based implementation of the ADT List including a main method and show that the ADT List works. Draw a class diagram of the ADT List __________________________________________ public interface IntegerListInterface{ public boolean isEmpty(); //Determines whether a list is empty. //Precondition: None. //Postcondition: Returns true if the list is empty, //otherwise returns false. //Throws: None. public int size(); // Determines the length of a list. // Precondition: None. // Postcondition: Returns the number of items in this IntegerList. //Throws: None....

  • 117% Question 1: Linked Lists Create an IntLinkedList class, much like the one presented in class....

    117% Question 1: Linked Lists Create an IntLinkedList class, much like the one presented in class. It should implement a linked list that will hold values of type int. It should use an IntNode class to implement the nodes in the linked list. The linked list must have the following methods: . A constructor with no parameters which creates an empty list. . void add (int data) -adds data to the front of the list. .A constructor IntlinkedList(int[]) which will...

  • 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...

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