Implement the print method in the doublylinkedlist class

Assignment Help JAVA Programming
Reference no: EM131862590

Assignment

Objectives

The objectives of this assignment are:

• To review using doubly-linked lists
• To implement a List using Generics
• To implementation several methods for a doubly-linked list
• To implement a Java iterator

The Lab

Java Files:

• List.java specifies the interface for a list using Generics and includes the printBackwards method.
• DoublyLinkedListTest.java is a set of tests that test your implementation of the DoublyLinkedList.
• DoublyLinkedList.java is a partially completed implementation of the List interface using doubly- linked lists.

When you examine DoublyLinkedList.java, you will notice several methods that are not complete (with a comment //TODO), so they only compile, but are lacking a proper implementation. You will implement all methods, and test them using functions that you will add to DoublyLinkedListTest.java main function.

Step 1. Print an empty list.

Implement the print method in the DoublyLinkedList class. You should start from the head, and traverse by the next references.

Within the main DoublyLinkedListTest.java, finish the function called testPrintEmptyListForward that:

• creates a DoublyLinkedList object
• calls print on the empty DoublyLinkedList.

Run your main method and make sure it does not crash.

Step 2. Print an empty list backwards.

Implement the printBackwards method in the DoublyLinkedList class. You should start from the tail and traverse by the previous references.

Within the main DoublyLinkedListTest.java, finish the function called testPrintEmptyListForward that:

• creates a DoublyLinkedList object
• calls printBackwards on the empty DoublyLinkedList.

Run your main method and make sure it does not crash.

Step 3. Get from the doubly-linked list

The get method has an integer parameter that is the position in the list. position start from 0 for the first element. You should return the T data (not the Node) at that position. If the position does not exist, you should return null.

Begin by handling the cases where you should return null.

After you are sure the position is valid, write a loop that only traverse as far as the position parameter. Again, you should return the T data (not the Node) at that position.

Within the main DoublyLinkedListTest.java, write a function called testEmptyGet that works en empty list. Of course, it should return null no matter what position you pass as an argument, since the list is empty.

Step 4a. Prepend to the doubly-linked list: drawing and pseudocode

Draw on paper what it will look like to add a new generic object to the beginning of a doubly-linked list. You should draw three examples:

• What happens when you prepend to an empty doubly-linked list?
• What happens when you prepend to a doubly-linked list with one item in it?
• What happens when you prepend to a doubly-linked list with three items in it?

For each example:

1. Draw the original doubly-linked list, including (a) the head of the doubly-linked list, (b) the tail of the doubly-linked list, (c) the number of elements, (d) every node in the doubly-linked list, each with the data, next and previous.

2. Draw the new Node that needs to be created for the new generic object under your original doubly- linked list. Be sure to draw the data, next and previous fields in the Node.

3. Draw the new arrows necessary for the new doubly-linked list after appending the new Node. Number each of these arrows. Each new arrow will require an assignment in your Java code.

4. Double-check that the instance variables (the head, tail and the number of elements) are set correctly. If they aren't, you'll need to modify them.

Step 4b. Prepend to the doubly-linked list: Java code.

Implement the addFirst method in the DoublyLinkedList class. The addFirst method adds a new generic object to the start of the doubly linked list.

If you implement addFirst correctly, you should be able to get the output of testPrependAndGet correct by uncommenting it in the main function. If passed, uncomment testPrependForwards and testPrependBackwards one at a time, to see if you get the list printed in the right order. If not, (or if you get NullPointerException), it is likely that you have corrupted your previous references.

Step 5a. Append to the doubly-linked list: drawing and pseudocode.

Draw on paper what it will look like to append a new generic object to a doubly-linked list. You should draw three examples:

• What happens when you append to an empty doubly-linked list?
• What happens when you append to a doubly-linked list with one item in it?
• What happens when you append to a doubly-linked list with three items in it?

Step 5b. Append to the doubly-linked list: Java code.

Now implement the addLast method. Be sure your code will handle all three situations that you drew in the previous example.

Same way as in 5, uncomment testAddLastAndGet(), testAddLastForwards(), testAddLastBackwards() one at a time, and verify you get the right output.

Step 6. Implement getLength and isEmpty.

Write the getLength() and isEmpty() methods. Since these don't modify the doubly-linked list, they should be very similar to the same methods in the linked list seen in class. Uncomment testIsEmpty() and testGetLength() to verify your code.

Step 7a. Remove from the doubly-linked list: drawing.

First draw on paper what it will look like to remove an object in a doubly-linked list. You will need to search the entire doubly-linked list for the item you want to remove. If the item is found, you should remove the first version of that item. You should draw five examples (again, have each person in your group should draw at least one example):

• Assuming you have a doubly-linked list with three items in it, what happens when you remove the first item in the doubly-linked list?
• Assuming you have a doubly-linked list with three items in it, what happens when you remove the middle item in the doubly-linked list?
• Assuming you have a doubly-linked list with three items in it, what happens when you remove the last item in the doubly-linked list?
• Assuming you have a doubly-linked list with three items in it, what happens if you do not find the item?
• Assuming you have a doubly-linked list with one item in it, what happens when you remove that item from the doubly-linked list?

Go through the same four-step process as in Step 4a.

Step 7b. Remove from the doubly-linked list: Java code.

Now implement the remove method. Be sure your code will handle all four situations that you drew in the previous example.

Then you can run the following tests:

- testRemoveFromEmptyList();
- testRemoveFromListWithOneElementNegative();
- testRemoveFromListWithOneElementPositive();
- testRemoveFromListWithTwoElementNegative();
- testRemoveFromListWithTwoElementPositive();
- testRemoveFromListWithThreeElementNegative();
- testRemoveFromListWithThreeElementPositive();

and see if they pass the tests properly.

Step 8: Iterator for DoublyLinkList

Add an iterator class definition for the DoublyLinkList. Follow the example of the slides. Then finish the testIterator method that create a list, obtain an iterator, and then uses it to print the content of the list.

Reference no: EM131862590

Questions Cloud

Stock worth to investor who has required rate of return : what is the stock worth to an investor who has a required rate of return of 12%?
Why integrate technology into the curriculum : Discuss how well you believe the students and teachers are utilizing technology to meet the following four key components of learning.
Calculate the npv of proposal. assumption : Calculate the NPV of this proposal. Assumption: From three produced whisky barrels, one is sold as 9-year, one is sold as 12-year,
What is required rate of return for envoy common stock : The expected return on the market is 12.8 percent. What is the required rate of return for Envoy common stock which has a beta of 1.35?
Implement the print method in the doublylinkedlist class : Implement the print method in the DoublyLinkedList class. You should start from the head, and traverse by the next references.
How long does it take the ball to travel this distance : It is 60.5 feet from the mound to the plate. How long does it take the ball to travel this distance?
Roofer drops a nail that hits the ground : A roofer drops a nail that hits the ground at 23 m/s. How fast was it traveling 2 seconds before it hit the ground?
Did your actual return equal your expected return : The yield to maturity on your new bond is 5.5% with a 10-year remaining life. Did your actual return equal your expected return? Explain.
Raise the temperature of the water : How much heat is needed to raise the temperature of the water from room temperature (20 degrees C) to its boiling point (100 degrees C)?

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd