Question

In NetBeans Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList)...

In NetBeans

Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList)

a) Create a DateTime class

1. Add day, month, year, hours, minutes as attributes

2. Add a constructor and a toString() method

3. Implement the Comparable interface, and add a CompareTo() method

4. Add methods to get and set all attributes.

b) Add to MyLinkedList class the following methods:

1. Insert a Node to a particular position in the List

2. Insert a Node in place, assuming that the list is ordered in ascending order.

3. Delete node in front

4. Delete last node

5. Delete a node given its position

6. Method to find the node with the minimum value in the list – getMinimum()

7. Method to merge two lists together and return the resulting linked list

8. Sort list in ascending order

9. Sort list in descending order

c) Create a Test3 class to do the following in the main method:

1. Create a new List (call it dateTimeList) of type MyLinkedList

2. Insert five DateTime objects numbers to a specific position in the list (not ordered).

3. Display all the DateTime objects in the List

4. Display the DateTime objects occurring after today

5. Insert a new DateTime elements to its appropriate position in the List and display it

6. Sort the List and display it in both ascending and descending

7. Display the smallest and the largest elements in the list

8. Display all DateTime objects with the time in the afternoon

9. Delete a DateTime object from the front of the list

10. Delete a DateTime object from the back of the list

11. Create two lists of DateTime objects, merge them together, and then print the resulting merged list

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

public class DateTime implements Comparable<DateTime> {
    public int day;
    public int year;
    public int months;
    public int minutes;
    public int hours;

    DateTime(int minutes, int hours, int day, int months, int year) {
        this.day = day;
        this.hours = hours;
        this.year = year;
        this.months = months;
        this.minutes = minutes;
    }


    public String toString() {
        return minutes + " " + hours + " " + months + " " + day + " " + year;
    }

    public static void main(String[] args) {
        DateTime dd = new DateTime(12, 02, 07, 02, 2018);
        System.out.println(dd);

    }

    public int getDay() {
        return day;
    }

    public int getYear() {
        return year;
    }

    public int getMonths() {
        return months;
    }

    public int getMinutes() {
        return minutes;
    }

    public int getHours() {
        return hours;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public void setMonths(int months) {
        this.months = months;
    }

    public void setHours(int hours) {
        this.hours = hours;
    }

    public void setMinutes(int minutes) {
        this.minutes = minutes;
    }


    @Override
    public int compareTo(DateTime dt) {
        if (day == dt.day)
            return 0;
        else if (day > dt.day)
            return 1;
        else
            return -1;

        if (hours == dt.hours)
            return 0;
        else if (hours > dt.hours)
            return 1;
        else
            return -1;

        if (year == dt.year)
            return 0;
        else if (year > dt.year)
            return 1;
        else
            return -1;

        if (months == dt.months)
            return 0;
        else if (months > dt.months)
            return 1;
        else
            return -1;

        if (minutes == dt.minutes)
            return 0;
        else if (minutes > dt.minutes)
            return 1;
        else
            return -1;
//        return 0;
    }
}

-----------------------------------------------Question Number 2------------------------------------------

import java.util.List;

public class MyLinkedList {
    static Node head;

    class Node {
        int data;
        Node next;

        Node(int d) {
            data = d;
            next = null;
        }
    }

    void sortedInsert(Node new_node) {
        Node current;
        if (head == null || head.data >= new_node.data) {
            new_node.next = head;
            head = new_node;
        } else {

            current = head;

            while (current.next != null &&
                    current.next.data < new_node.data)
                current = current.next;

            new_node.next = current.next;
            current.next = new_node;
        }
    }

    Node newNode(int data) {
        Node x = new Node(data);
        return x;
    }

    void printList(Node head) {
        Node temp = MyLinkedList.head;
        while (temp != null) {
            System.out.print(temp.data + " ");
            temp = temp.next;
        }

    }

    void deleteNode(int position) {

        if (head == null)
            return;


        Node temp = head;


        if (position == 0) {
            head = temp.next;
            return;
        }


        for (int i = 0; temp != null && i < position - 1; i++)
            temp = temp.next;


        if (temp == null || temp.next == null)
            return;


        Node next = temp.next.next;

        temp.next = next;
    }

    public static Integer findMin(List<Integer> list) {
        return list.stream()
                .mapToInt(v -> v)
                .min()
                .orElse(Integer.MAX_VALUE);
    }
    Node reverse(Node node) {
        Node prev = null;
        Node current = node;
        Node next = null;
        while (current != null) {
            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        }
        node = prev;
        return node;
    }


    public static void main(String args[]) {
        MyLinkedList llist = new MyLinkedList();
        Node new_node;
        new_node = llist.newNode(5);
        llist.sortedInsert(new_node);
        new_node = llist.newNode(10);
        llist.sortedInsert(new_node);
        new_node = llist.newNode(7);
        llist.sortedInsert(new_node);
        new_node = llist.newNode(3);
        llist.sortedInsert(new_node);
        new_node = llist.newNode(1);
        llist.sortedInsert(new_node);
        new_node = llist.newNode(9);
        llist.sortedInsert(new_node);
        System.out.println("Sorted List in ascending order");
        llist.printList(head);
        System.out.println("Delete the list node at 1st position");
        llist.deleteNode(0);
        llist.printList(head);
        System.out.println("Delete the list node at the last position");
        llist.deleteNode(5);
        llist.printList(head);
        System.out.println("Minimum element is " + findMin((List<Integer>) llist));
        llist.printList(head);
        head = llist.reverse(head);
        System.out.println("Reversed linked list ");
        llist.printList(head);

    }
}

-------------------------Question Number 3------------------------------

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

class Test3 {
    static MyLinkedList.Node head;

    class Node {
        int data;
        MyLinkedList.Node next;

        Node(int d) {
            data = d;
            next = null;
        }
    }

    void sortedInsert(MyLinkedList.Node new_node) {
        MyLinkedList.Node current;
        if (head == null || head.data >= new_node.data) {
            new_node.next = head;
            head = new_node;
        } else {

            current = head;

            while (current.next != null &&
                    current.next.data < new_node.data)
                current = current.next;

            new_node.next = current.next;
            current.next = new_node;
        }
    }

    MyLinkedList.Node newNode(int data) {
        MyLinkedList.Node x = new MyLinkedList.Node(data);
        return x;
    }

    void printList(MyLinkedList.Node head) {
        MyLinkedList.Node temp = MyLinkedList.head;
        while (temp != null) {
            System.out.print(temp.data + " ");
            temp = temp.next;
        }

    }

    void deleteNode(int position) {

        if (head == null)
            return;


        MyLinkedList.Node temp = head;


        if (position == 0) {
            head = temp.next;
            return;
        }


        for (int i = 0; temp != null && i < position - 1; i++)
            temp = temp.next;


        if (temp == null || temp.next == null)
            return;


        MyLinkedList.Node next = temp.next.next;

        temp.next = next;
    }

    public static Integer findMin(List<Integer> list) {
        return list.stream()
                .mapToInt(v -> v)
                .min()
                .orElse(Integer.MAX_VALUE);
    }
    MyLinkedList.Node reverse(MyLinkedList.Node node) {
        MyLinkedList.Node prev = null;
        MyLinkedList.Node current = node;
        MyLinkedList.Node next = null;
        while (current != null) {
            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        }
        node = prev;
        return node;
    }
    public static void main(String[] args) {
        LinkedList dateTimeList = new LinkedList<>();
        dateTimeList.add(19);
        dateTimeList.add(5);
        dateTimeList.add("today");
        dateTimeList.add(20);
        dateTimeList.add(60);
        System.out.println(dateTimeList);
        int ind = dateTimeList.indexOf("today");
        for (int i = ind; i >= ind; i++) {
            Iterator<String> itr = dateTimeList.iterator();
            while (itr.hasNext()) {
                System.out.println(itr.next());

            }
            dateTimeList.add(40);
            System.out.println(dateTimeList);

            MyLinkedList llist = new MyLinkedList();
            MyLinkedList.Node new_node;
            new_node = llist.newNode(5);
            llist.sortedInsert(new_node);
            System.out.println("Sorted List in ascending order");
            llist.printList(head);
            head = llist.reverse(head);
            System.out.println("Reversed linked list ");
            llist.printList(head);
            System.out.println("Smallest element in the list");
            llist.printList(0);
            System.out.println("Last Largest in the list");
            llist.printList(5);
        }
dateTimeList.add("afternoon");
Iterator<String> itr1 = dateTimeList.iterator();
while (itr1.hasNext()) {
System.out.println(itr1.next());
}
System.out.print("Delete the first element from the list");
llist.deleteNode(0);
llist.printList(head);
System.out.print("Delete the last element from the list");
llist.deleteNode(6);
llist.printList(head);
dateTimeList2.add(28);
dateTimeList2.add(55);
dateTimeList2.addAll(dateTimeList);
Iterator<String> itr2 = dateTimeList2.iterator();
while (itr2.hasNext()) {
System.out.println(itr2.next());

}
    }
}
Add a comment
Know the answer?
Add Answer to:
In NetBeans Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList)...
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
  • In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class...

    In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class (50 pts): Create node with public properties: Block type block and block ptr next. Create a linked list class that uses the node you generated without an add or delete method with a head and optional tail and counter. Make a driver that generates a node to test your implementation. Part 2 Add Method (30 pts): Create an add method in your linked list...

  • Create a linked list of at least 15 different values that are prompted for and entered...

    Create a linked list of at least 15 different values that are prompted for and entered while the program is running. Appending a node attaches that node to the end of the list while inserting a node places it in order maintaining a sorted list. Create a menu driven program where you have the options to appended, insert, display, and delete a node. Display the list after first appending data showing it in no specific order, delete all nodes and...

  • JAVA please Java problem that has to be in doubly linked list. It is game problem...

    JAVA please Java problem that has to be in doubly linked list. It is game problem that I have to keep of the players' scores. this should have a Java blueprint class named player   It should have two fields, the name and score and include the constructors, toString() method, and getters and setters. Scores must be kept in ascending order (smallest at the front ) in a doubly linked list. It has menu as well: Load original data Print the...

  • Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list -...

    Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list - Reference: chapter 18: Create an array of 15 student records that should not be sorted Create a liked list of 15 student record nodes. Each node is a node of one student record from the above unsorted array. The list of student records should be sorted by student ID. (Insert function without sort function to create a linked list.) (If you insert correctly, the...

  • // 1. Add methods to get and set, Data and Link. Data should be any Comparable...

    // 1. Add methods to get and set, Data and Link. Data should be any Comparable object. class Node { Integer data; // Integer is Comparable Node link; public Node(Integer data, Node link) { this.data = data; this.link = link; } public Integer getData() { return data; } public void setData(Integer data) { this.data = data; } public Node getLink() { return link; } public void setLink(Node link) { this.link = link; } } // b. Create MyLinkedList class and...

  • python Programming assignment: Let's think about doubly-linked lists. Define a class ListNode2, with three attributes: item,...

    python Programming assignment: Let's think about doubly-linked lists. Define a class ListNode2, with three attributes: item, left, and rightL. Left link points to the previous node in the list, right link points to the next node in the list. You can also add the display method to this class (like we did it in class for the ListNode class). Then test your class. For example, create a linked list of 5 values: 34,1, 23, 7, and 10. Display it. Then...

  • could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head,...

    could somone please help me to complete this ! public class MyLinkedList<AnyType> { private Node<AnyType> head, tail; private int size; public MyLinkedList() { this.head = null; this.tail = null; this.size = 0; } //1.Insert a node at the end of the list public void insert(AnyType data) { Node<AnyType> newNode = new Node(); newNode.data = data; if (head == null) { head = newNode; tail = newNode; head.next = null; tail.next = null; } else { tail.next = newNode; tail =...

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

  • You will need to first create an object class encapsulating a Trivia Game which INHERITS from...

    You will need to first create an object class encapsulating a Trivia Game which INHERITS from Game. Game is the parent class with the following attributes: description - which is a string write the constructor, accessor, mutator and toString methods. Trivia is the subclass of Game with the additional attributes: 1. trivia game id - integer 2. ultimate prize money - double 3. number of questions that must be answered to win - integer. 4. write the accessor, mutator, constructor,...

  • In 6A, you created an object class encapsulating a Trivia Game which INHERITS from Game. Now...

    In 6A, you created an object class encapsulating a Trivia Game which INHERITS from Game. Now that you have successfully created Trivia objects, you will continue 6B by creating a linked list of trivia objects. Add the linked list code to the Trivia class. Your linked list code should include the following: a TriviaNode class with the attributes: 1. trivia game - Trivia object 2. next- TriviaNode 3. write the constructor, accessor, mutator and toString methods. A TriviaLinkedList Class which...

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