In: Computer Science
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”);
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"));
}
}