Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Write the VideoList class to hold objects of the class VideoNode. This class

ID: 3597820 • Letter: 2

Question

2. Write the VideoList class to hold objects of the class VideoNode. This class should define: • Two instance variables head and size. The default constructor of the class is: public VideoList(){ head = null; size = 0; } • The VideoList class should implement the following interface List // a list interface public interface List { public boolean isEmpty(); // returns true if the list is empty, false otherwise public int size(); // returns the number of items in the list public VideoNode get(int index); //returns the VideoNode object at the specified index or //null if the list is empty public void addFirst(VideoNode item); // adds an item at the front of the list public void addLast(VideoNode item); // adds an item at the end of the list public void add(int index, VideoNode item); // adds an item to the list at the given index public void removeFirst(); // removes the first item in the list public void remove(int index); // removes the item from the list that has the given index public void remove(VideoNode item); // removes the first item in the list whose data equals // the given item data public VideoNode[] searchPriceGreaterThan(double p); //search and retun an array contenant the VideoNode items // having a price greater than p @Override public String toString(); // implement a toString() method that prints the list in the // format: //[ size: the_size_of_the_list // item1, // item2, //.... ] }

Explanation / Answer

VideoNode.java:

public class VideoNode implements Comparable<VideoNode>{

private String title;

private double price;

private VideoNode next = null;

public VideoNode(String title, double price, VideoNode next) {

this.title = title;

this.next = next;

this.price = price;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

public VideoNode(String title) {

this.title = title;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public VideoNode getNext() {

return next;

}

public void setNext(VideoNode next) {

this.next = next;

}

@Override

public int compareTo(VideoNode other) {

return title.compareTo(other.getTitle());

}

@Override

public String toString() {

return title + " [$" + price + "]";

}

}

VideoList.java:

import java.util.ArrayList;

public class VideoList implements List{

private VideoNode front;

private int count;

// constructor

public VideoList() {

front = null;

count = 0;

}

// add a node to the front of the linked list

public void addFirst(VideoNode n) {

n.setNext(front);

front = n;

count++;

}

// get the current size of the list

public int size() {

return count;

}

// check if the list is empty

public boolean isEmpty() {

if (front == null)

return true;

else

return false;

}

// remove front node

public void removeFirst() {

if (front == null) {

System.out.println("Empty list");

} else {

front = front.getNext();

count--;

}

}

// add a node to the end

public void addLast(VideoNode n) {

n.setNext(null);

VideoNode curr = front;

if (front == null)

front = n;

else {

while (curr.getNext() != null)

curr = curr.getNext();

curr.setNext(n);

}

count++;

}

// remove last node

public void removeLast() {

if (front == null) {

System.out.println("Empty list");

} else if (front.getNext() == null) {

front = null;

count--;

} else {

VideoNode curr = front;

while (curr.getNext().getNext() != null)

curr = curr.getNext();

curr.setNext(null);

count--;

}

}

// search for a given data and return the index of the node (first

// occurrence)

// return -1 if not found

public int contains(VideoNode n) {

VideoNode curr = front;

boolean found = false;

int index = -1;

while (curr != null && !found) {

index++;

if (curr.equals(n))

found = true;

curr = curr.getNext();

}

if (!found)

return -1;

else

return index;

}

// add a node at a given index

public void add(int index, VideoNode n) {

if (index < 0 || index > size())

System.out.println("Can't add. Index out of bounds");

else {

if (index == 0)

addFirst(n);

else {

VideoNode curr = front;

for (int i = 0; i < index - 1; i++)

curr = curr.getNext();

n.setNext(curr.getNext());

curr.setNext(n);

count++;

}

}

}

// remove a node at a given index

public void remove(VideoNode item) {

int index = contains(item);

if(index != -1) {

remove(index);

}

}

public void remove(int index) {

if (index < 0 || index >= size())

System.out.println("Can't remove. Index out of bounds");

else if (index == 0)

removeFirst();

else if (index == size() - 1)

removeLast();

else {

VideoNode curr = front;

for (int i = 0; i < index - 1; i++)

curr = curr.getNext();

curr.setNext(curr.getNext().getNext());

count--;

}

}

// get a node data given an index

public VideoNode get(int index) {

VideoNode curr = front;

int i = 0;

while (curr != null && i != index) {

curr = curr.getNext();

i++;

}

return (curr);

}

@Override

public VideoNode[] searchPriceGreaterThan(double p) {

ArrayList<VideoNode> list = new ArrayList<VideoNode>();

VideoNode curr = front;

while (curr != null) {

if(curr.getPrice() > p) {

list.add(curr);

}

curr = curr.getNext();

}

return (VideoNode[]) list.toArray();

}

@Override

public String toString() {

String output = "size: " + size() + " ";

VideoNode curr = front;

while (curr != null) {

output += curr.toString() + ", ";

curr = curr.getNext();

}

return output;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote