Question

Objective In this assignment, you are asked to implement a bag collection using a linked list and, in order to focus on the lClass Thing You may use the same Thing requirements are met class you created in Assignment 2 but make sure all the folloclass ThioaNede This class represents a node in the linked list. The node class should include the following: Two private i7. boanremove (int index): this method removes the element that is located at position index in the linked list where the firclass ThingLinkedBagDriver Write a driver class to test ALL the methods that you implemented in the ThingNode class and the T

***JAVA: Please make "Thing" movies.

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

Hi, Please find the solution and rate the answer.

public class FruitLinkedBagDriver {
    public static void main(String[] args) {
        MovieLinkedBag bag = new MovieLinkedBag();
        Movie movie1 = new Movie("Movie1", 1, 1994);
        bag.add(movie1);
        Movie movie2 = new Movie("Movie2", 2, 2000);
        bag.add(movie2);
        bag.add(new Movie("Movie3",3,2004));
        bag.add(new Movie("Movie4",4,2014));
        bag.add(new Movie("Movie5",5,2016));
        Movie movie6 = new Movie("Movie6", 6, 2018);
        bag.add(movie6);

        System.out.println(String.format("Maximum is %s",bag.getMax()));
        System.out.println(String.format("Minimum is %s", bag.getMin()));
        bag.set(2, new Movie("MovieX", 5, 2000));;
        System.out.println(bag.countRange(movie1,movie6));
        System.out.println(bag.grab(2));
    }
}
public class MovieLinkedBag {
    private MovieNode head;
    private int manyItems;

    public MovieLinkedBag() {
    }

    int size() {
        MovieNode temp = head;
        int count = 0;
        while (temp != null) {
            count++;
            temp = temp.next;
        }
        return count;
    }

    void display() {
        MovieNode temp = head;
        while (temp != null) {
            System.out.println(temp);
            temp = temp.next;
        }
    }

    void add(Movie f) {
        MovieNode temp = head;
        if (temp == null) {
            head = new MovieNode(f, null);
            return;
        }
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = new MovieNode(f, null);
    }

    void add(int index, Movie element) {
        if (index < 0)
            return;
        MovieNode temp = head;
        int count = 1;
        while (count != index && temp.next != null) {
            count++;
            temp = temp.next;
        }
        if (temp.next == null) {
            temp.next = new MovieNode(element, null);
        } else {
            MovieNode node = new MovieNode(element, null);
            node.next = temp.next;
            temp.next = node;
        }
    }

    boolean remove(Movie target) {
        MovieNode temp = head;
        if (head.getData().equals(target)) {
            head = head.next;
            return true;
        }
        while (temp.next != null && !temp.next.getData().equals(target)) {
            temp = temp.next;
        }
        if (temp.next == null) {
            return false;
        }
        temp.next = temp.next.next;//jumping a node to delete it
        return true;
    }

    boolean remove(int index) {//here head is index 1 (assumption)
        int count = 1;
        MovieNode temp = head;
        if (index == 1) {
            head = head.next;
        }
        while (temp != null && count != index) {
            count++;
            temp = temp.next;
        }
        if (temp == null) {
            return false;
        } else return remove(temp.getData());
    }

    int countRange(Movie start, Movie end) {
        int count = 0;
        MovieNode temp = head;
        while (temp != null) {
            if (temp.getData().compareTo(start) >= 0 && temp.getData().compareTo(end) <= 0) {
                count++;
            }
            temp = temp.next;
        }
        return count;
    }

    Movie grab(int index) {
        MovieNode temp = head;
        int count = 1;
        while (count != index && temp != null) {
            count++;
            temp = temp.next;
        }
        if (temp != null) {
            return temp.getData();
        }
        return null;
    }

    int indexOf(Movie target) {
        MovieNode temp = head;
        int count = 1;
        while (temp != null && !temp.getData().equals(target)) {
            count++;
            temp = temp.next;
        }
        if (temp != null) {
            return count;
        }
        return -1;
    }

    void set(int index, Movie element) {
        MovieNode temp = head;
        int count = 1;
        while (count != index && temp != null) {
            count++;
            temp = temp.next;
        }
        if (temp != null) {
            temp.setData(element);
            return;
        }
        return;
    }

    int totalValue() {
        MovieNode temp = head;
        int total = 0;
        while (temp != null) {
            total += temp.getData().getPrice();
        }
        return total;
    }

    MovieNode lessThan(Movie movie) {
        MovieLinkedBag bag = new MovieLinkedBag();
        MovieNode temp = head;
        while (temp != null) {
            if (temp.getData().compareTo(movie) < 0) {
                bag.add(temp.getData());
            }
            temp = temp.next;
        }
        return bag.head;
    }

    MovieNode greaterThan(Movie movie) {
        MovieLinkedBag bag = new MovieLinkedBag();
        MovieNode temp = head;
        while (temp != null) {
            if (temp.getData().compareTo(movie) > 0) {
                bag.add(temp.getData());
            }
            temp = temp.next;
        }
        return bag.head;
    }

    Movie getMax() {
        MovieNode max = head, temp = head;
        while (temp.next != null) {
            if (temp.next.getData().compareTo(max.getData()) > 0) {
                max = temp.next;
            }
            temp = temp.next;
        }
        if (max != null)
            return max.getData();
        else return null;
    }

    Movie getMin() {
        MovieNode max = head, temp = head;
        while (temp.next != null) {
            if (temp.next.getData().compareTo(max.getData()) < 0) {
                max = temp.next;
            }
            temp = temp.next;
        }
        if (max != null)
            return max.getData();
        else return null;
    }

}
public class MovieNode {
    private Movie movie;
    public MovieNode next;

    public MovieNode(Movie fruit, MovieNode next) {
        this.movie = fruit;
        this.next = next;
    }

    public Movie getData() {
        return movie;
    }

    public MovieNode getLink() {
        return next;
    }

    public void setData(Movie f) {
        movie = f;
    }

    public void setLink(MovieNode next) {
        this.next = next;
    }
    //what to do in addNodeAfter and removeNodeAfter not given

    public Movie getMovie() {
        return movie;
    }

    public void setMovie(Movie movie) {
        this.movie = movie;
    }
}
import java.awt.*;
import java.util.Objects;

public class Movie implements Comparable{
    int price;
    private String name;
    int releaseYear;

    public Movie(String thing, int val,int year) {
        this.name = thing;
        this.price = val;
        this.releaseYear = year;
    }

    public int getPrice() {
        return price;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Movie fruit = (Movie) o;
        return name.equals(fruit.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }

    @Override
    public String toString() {
        return "Fruit{" +
                "price=" + price +
                ", thing='" + name + '\'' +
                ", year=" + releaseYear +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        if(this==o)return 0;
        if(o instanceof Movie){
            Movie f = (Movie)o;
            return this.name.compareTo(f.getName());
        }
        else throw new IllegalArgumentException("BAD");
    }
}

Sample Out:

/Library/Java/JavaVirtua Machines/jdki.8._144.jdk/Contents/Home/bin/java ... objc[2829]: Class JavaLaunchHelper is implementeMovieNode.javaX MovieLinked Bag.java Movie.java FruitLinkedBag Driver.java public class Fruit LinkedBagD river{ 1 public statMovie.javaX MovieNode.javaX MovieLinked Bag.javaX FruitLinkedBag Driver.javax public class Movie Lin ked Bag private MovieNodMovie.java X MovieLinkedBag.javax FruitLinkedBag Driver.java MovieNode.javax public class MovieNode 1 private Movie movie; 2MovieLinkedBag.java Movie.java MovieNode.javaX FruitLinked Bag Driver.java import java.awt.* eimport java.util. Obj ects ; 1

Output is perfect thanks. Please rate.

Add a comment
Know the answer?
Add Answer to:
***JAVA: Please make "Thing" movies. Objective In this assignment, you are asked to implement a bag...
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
  • Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following...

    Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following methods to the LinkedCollection class, and create a test driver for each to show that they work correctly. Code each of these methods by accessing the internal variables of the LinkedCollection, not by calling the previously defined methods of the class.String toString() creates and returns a string that correctly represents the current collection. 1. Such a method could prove useful for testing and debugging...

  • Modify the LinkedCollection class to be a SortedLinkedCollecton class and see how that effects our implementation...

    Modify the LinkedCollection class to be a SortedLinkedCollecton class and see how that effects our implementation for adding and removing items. You should reference the SortedArrayCollection class provided for how these algorithms should be implemented. What needs to change here? Is it a lot of code or not much? Include a toString method that creates and returns a string that correctly represents the current collection. Include a test driver application that demonstrates your class correctly. //--------------------------------------------------------------------------- // LinkedCollection.java // //...

  • In the first exercise, you will override the add method in a subclass in order to...

    In the first exercise, you will override the add method in a subclass in order to improve its performance. In the second exercise, you will implement an Iterator for your new linked list class. Exercise 1 The add method of the linked list class discussed during lecture performs in O(N) time. What can be done to improve its performance to O(1)? What are the boundary cases? Define a new class that inherits from the CS20bLinkedList class introduced during the lecture....

  • please explain how to code using java -. Implement the ListNode and LinkedIntList as we did...

    please explain how to code using java -. Implement the ListNode and LinkedIntList as we did in class (you can add other methods if needed). You can reference the coxle in powerpoint slides of LinkedList and your textbook. 2. Implement a LinkedIntList class with the following public operations. a. add(int value) - Adds the given value to the end of the list b.get(int index) - Retums value in list at given index. C.add( int index, int value) - Inserts the...

  • Exercise 1 Adjacency Matrix In this part, you will implement the data model to represent a graph. Implement the followi...

    Exercise 1 Adjacency Matrix In this part, you will implement the data model to represent a graph. Implement the following classes Node.java: This class represents a vertex in the graph. It has only a single instance variable of type int which is set in the constructor. Implement hashCode() and equals(..) methods which are both based on the number instance variable Node - int number +Node(int number); +int getNumberO; +int hashCode() +boolean equals(Object o) +String toString0) Edge.java: This class represents a...

  • In this assignment, you are asked to implement a Java class named HugeInt for storing huge...

    In this assignment, you are asked to implement a Java class named HugeInt for storing huge integers and performing mathematical operations on them. This class must have a private variable of type LinkedList〈Byte〉 which stores digits of a huge integer and ten of the following public methods (choose ten methods out of 12): public void setValue (int value): gets an integer (like 1234) as input parameter and stores its digits in the linked list in the same order as they...

  • Complete two of the List methods: SinglyLinkedList::size() and SinglyLinkedList::get(). import java.util.AbstractList; public class SinglyLinkedList<E> extends AbstractList<E>...

    Complete two of the List methods: SinglyLinkedList::size() and SinglyLinkedList::get(). import java.util.AbstractList; public class SinglyLinkedList<E> extends AbstractList<E> { /** Reference to the first node in our linked list. This will be null if the list is empty. */ private Entry head; /** * Creates an empty list. */ public SinglyLinkedList() { reset(); } /** * This method, which is only used within the SinglyLinkedList class, returns the instance to its initial state. This * call will reset the head to be...

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

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

  • 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