Question

In: Computer Science

A company would like to implement its inventory of smartphones as a doubly linked list, called...

A company would like to implement its inventory of smartphones as a doubly linked list, called

MobileList.

1. Write a Mobile node node class, called MobileNode, to hold the following information about a

smartphone:

• code (as a String)

• brand (as a String)

• model (as a String)

• price (as int)

MobileNode should have constructors and methods (getters, setters, and toString()) to manage

the above information as well as the link to next and previous nodes in the list.

2. Write the MobileList class to hold objects of the class MobileNode. This class should define:

• Two instance variables first and last to keep the reference (address) of the first and last

nodes of the list.

• The MobileList class should implement the following interface:

public interface MList {

public boolean isEmpty();

// returns true if the list is empty, false otherwise

public int size();

// returns the number of items in the list

public MobileNode getNodeAt(int index);

//returns the MobileNode object at the specified index

public void addFirst(MobileNode item);

// adds a Mobile node at the front of the list

public void addLast(MobileNode item);

// adds a Mobile node at the end of the list

public void addAt(int index, MobileNode item);

// adds a Mobile node to the list at the given index

public String removeAt(int index);

// removes the Mobile node from the list that has the given

// index

public String remove(MobileNode item);

// removes the first item in the list whose data equals

// the given item data

public MobileNode[] searchPriceGreaterThan(int p);

//search and return an array of the set of MobileNode items

//having a price greater than p

public double averagePrice();

// return the average price of the mobile nodes in the list

public double averageBrandPrice(String brand);

// return the average price of the mobile nodes in the list

// from the brand given as a parameter (e.g., “Samsung” or

// “samsung”)

@Override

public String toString();

// implement a toString() method that prints the list in the

// format:

//[ size: the_size_of_the_list

// Mobile node1,

// Mobile node2,

//.... ]

}

3. Write a TestMobileList class to test the class MobileList. This class should have a main method

in which you perform the following actions:

• Create a MobileList object,

• Insert 10 MobileNode objects into the created list (from some brands like “Apple”,

“Samsung”, “Huwaei”, “Sony”),

• Print the content of your 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 ist,

• Print out the average price of all mobiles in the list

• Print out the average price of all mobiles in the list from “Apple”.

For each operation above, print a small message that describes the operation you are doing.

For example:

System.out.println(“Insertion of 10 Mobile nodes in the list”);

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.LinkedList;

public class MobileNode {
        private String code;
        private String brand;
        private String model;
        private int price;

        public MobileNode(String code, String brand, String model, int price) {
                super();
                this.code = code;
                this.brand = brand;
                this.model = model;
                this.price = price;
        }

        @Override
        public String toString() {
                return "MobileNode [code=" + code + ", brand=" + brand + ", model=" + model + ", price=" + price + "]";
        }

        public String getCode() {
                return code;
        }

        public void setCode(String code) {
                this.code = code;
        }

        public String getBrand() {
                return brand;
        }

        public void setBrand(String brand) {
                this.brand = brand;
        }

        public String getModel() {
                return model;
        }

        public void setModel(String model) {
                this.model = model;
        }

        public int getPrice() {
                return price;
        }

        public void setPrice(int price) {
                this.price = price;
        }

        @Override
        public int hashCode() {
                final int prime = 31;
                int result = 1;
                result = prime * result + ((brand == null) ? 0 : brand.hashCode());
                result = prime * result + ((code == null) ? 0 : code.hashCode());
                result = prime * result + ((model == null) ? 0 : model.hashCode());
                result = prime * result + price;
                return result;
        }

        @Override
        public boolean equals(Object obj) {
                if (this == obj)
                        return true;
                if (obj == null)
                        return false;
                if (getClass() != obj.getClass())
                        return false;
                MobileNode other = (MobileNode) obj;
                if (brand == null) {
                        if (other.brand != null)
                                return false;
                } else if (!brand.equals(other.brand))
                        return false;
                if (code == null) {
                        if (other.code != null)
                                return false;
                } else if (!code.equals(other.code))
                        return false;
                if (model == null) {
                        if (other.model != null)
                                return false;
                } else if (!model.equals(other.model))
                        return false;
                if (price != other.price)
                        return false;
                return true;
        }
        
}

class MobileList implements MList {
        private LinkedList<MobileNode> mobileNodes;
        private MobileNode first;
        private MobileNode last;

        public MobileList() {
                mobileNodes = new LinkedList<>();
        }

        @Override
        public boolean isEmpty() {
                if(mobileNodes.size()>0)
                        return true;
                return false;
        }

        @Override
        public int size() {
                return mobileNodes.size();
        }

        @Override
        public MobileNode getNodeAt(int index) {
                return mobileNodes.get(index);
        }

        @Override
        public void addFirst(MobileNode item) {
                mobileNodes.addFirst(item);
        }

        @Override
        public void addLast(MobileNode item) {
                mobileNodes.addLast(item);
        }

        @Override
        public void addAt(int index, MobileNode item) {
                mobileNodes.add(index, item);
        }

        @Override
        public String removeAt(int index) {
                return mobileNodes.remove(index).toString();
        }

        @Override
        public String remove(MobileNode item) {
                if(mobileNodes.remove(item)){
                        return item.toString();
                }
                return null;
        }

        @Override
        public MobileNode[] searchPriceGreaterThan(int p) {
                ArrayList<MobileNode> mobileNodes2=new ArrayList<>();
                for (MobileNode mobileNode : mobileNodes) {
                        if(mobileNode.getPrice()>p){
                                mobileNodes2.add(mobileNode);
                        }
                }
                return mobileNodes2.toArray(new MobileNode[mobileNodes2.size()]);
        }

        @Override
        public double averagePrice() {
                double avg=0;
                if(mobileNodes.size()>0){
                        for (MobileNode mobileNode : mobileNodes) {
                                avg+=mobileNode.getPrice();
                        }
                        avg=avg/mobileNodes.size();
                }
                return avg;
        }

        @Override
        public double averageBrandPrice(String brand) {
                double avg=0;
                int sizeTemp=0;
                for (MobileNode mobileNode : mobileNodes) {
                        if(mobileNode.getBrand().equalsIgnoreCase(brand)){
                                avg+=mobileNode.getPrice();
                                sizeTemp++;
                        }
                }
                if(sizeTemp>0){
                        return avg/sizeTemp;
                }
                return 0;
        }

}

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

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

        // returns the MobileNode object at the specified index
        public MobileNode getNodeAt(int index);

        // adds a Mobile node at the front of the list
        public void addFirst(MobileNode item);

        // adds a Mobile node at the end of the list
        public void addLast(MobileNode item);

        // adds a Mobile node to the list at the given index
        public void addAt(int index, MobileNode item);

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

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

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

        // return the average price of the mobile nodes in the list
        public double averagePrice();

        // return the average price of the mobile nodes in the list from the brand given as a parameter (e.g., “Samsung” or “samsung”)
        public double averageBrandPrice(String brand);

        @Override
        public String toString();

}

class TestMobileList{
        public static void main(String[] args) {
                MobileList mobileList=new MobileList();
                System.out.println("Insertion of 10 Mobile nodes in the list");
                mobileList.addLast(new MobileNode("1", "Apple", "Iphone6", 1000));
                mobileList.addLast(new MobileNode("2", "Apple", "Iphone7", 3100));
                mobileList.addLast(new MobileNode("3", "Apple", "Iphone8", 3200));
                mobileList.addLast(new MobileNode("4", "Samsung", "S10", 1300));
                mobileList.addLast(new MobileNode("5", "Samsung", "Note10", 1400));
                mobileList.addLast(new MobileNode("6", "Huwaei", "H50", 900));
                mobileList.addLast(new MobileNode("7", "Huwaei", "H90", 850));
                mobileList.addLast(new MobileNode("8", "Sony", "Xperia Z", 700));
                mobileList.addLast(new MobileNode("9", "Sony", "Xperia X", 930));
                mobileList.addLast(new MobileNode("10", "Sony", "Xperia XC", 3950));
                for (int i=0;i<mobileList.size();i++){
                        System.out.println(mobileList.getNodeAt(i).toString());
                }
                MobileNode[] mobileNodes=mobileList.searchPriceGreaterThan(3000);
                System.out.println("MobileNode with price greater than 3000:");
                for (MobileNode mobileNode : mobileNodes) {
                        System.out.println(mobileNode.toString());
                }
                mobileList.removeAt(0);
                mobileList.removeAt(3);
                System.out.println("MobileNode Updated List:");
                for (int i=0;i<mobileList.size();i++){
                        System.out.println(mobileList.getNodeAt(i).toString());
                }
                System.out.println("Avg Price of All Mobiles: "+mobileList.averagePrice());
                System.out.println("Avg Price of Apple Mobiles: "+mobileList.averageBrandPrice("Apple"));
        }
}


Related Solutions

Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy...
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy node. This challenge simulates an operating system’s window manager. Requirements Write the following struct struct Window { string appname; Window *next; Window *prev; }; Create a class called WindowManager. In this class, create a private variable Window * head. This will keep track of the location of the head node. Create private variables Window * current, * dummy. current will keep track of the...
In JAVA: Create a circular doubly linked list. It need not be generic. Implement addToStart and...
In JAVA: Create a circular doubly linked list. It need not be generic. Implement addToStart and addToEnd methods, as well as printList method. Implement delete(Node n) method that deletes a node n, if n is in the linked list. Make no assumptions about n. Test your linked list.
(Implement a doubly linked list) The MyLinkedList class used in Listing 24.5 is a one-way directional...
(Implement a doubly linked list) The MyLinkedList class used in Listing 24.5 is a one-way directional linked list that enables one-way traversal of the list. Modify the Node class to add the new data field name previous to refer to the previous node in the list, as follows : public class Node { E element; Node next; Node previous; public Node(E e) { element = e; } } Implement a new class named TwoWayLinkedList that uses a doubly linked list...
Write an efficient java program to implement an integer doubly linked list Dequeue which insertion and...
Write an efficient java program to implement an integer doubly linked list Dequeue which insertion and deletion can be than at either end. You have to write 6 methods: add front, delete front, add rear, delete rear, print forward (left to right) and print backward (right to left). After each addition or deletion dequeue elements are printed forward or backward respectively. Your main method should be as follow: public static void main(String args[]) { xxxxx dq = new xxxxx ();...
Using java: Implement a basic doubly-linked list that implements a priority system sorting the elements that...
Using java: Implement a basic doubly-linked list that implements a priority system sorting the elements that are inserted. Sort based on the speed of the warrior. Driver code: public class LinkedListDriver { public static void main(String[] args) { LinkedList list = new SortedDoublyLinkedList(); System.out.println(list); Warrior krogg = new Warrior("Krogg", 30, 50, 200); list.insert(krogg); System.out.println(list); Warrior gurkh = new Warrior("Gurkh", 40, 45, 180); list.insert(gurkh); System.out.println(list); Warrior brynn = new Warrior("Brynn", 45, 40, 190); list.insert(brynn); System.out.println(list); Warrior dolf = new Warrior("Dolf", 20,...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
write a java code to implement a linked list, called CupList, to hold a list of...
write a java code to implement a linked list, called CupList, to hold a list of Cups. 1.Define and write a Cup node class, called CupNode, to hold the following information about a cup: •number (cup number) •capacity (cup capacity in ml) •Write a method size() that returns the number of elements in the linkedlist CupList. •Write a method getNodeAt() that returns the reference to cup node object at a specific position given as a parameter of the method. •Write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT