Java代写:CSE214 Road Trips

基础Java数据结构作业,写一个主菜单来操作单链表。相比普通单链表作业,这次的单链表作业工作量略大。

Reminders

  • Be sure your code follows the coding style for CSE214.
  • You are not allowed to use LinkedList, ArrayList, Vector or any other Java API Data Structure classes to implement this assignment except where noted.
  • You may use Scanner, InputStreamReader, or any other class that you wish for keyboard input.

Requirement

In this homework assignment you will be building a trip planner for road trips. You will be modelling a road trip as a linked list of stops, with each stop having a distance to travel, a location, and an activity. There are two itineraries (so that you can plan two trips, or make two versions of the same trip), and they should be completely independent (ie: modifying one should not cause the other to change). This means that a deep copy must be made each time the itinerary is copied from one itinerary to the other.

NOTE: All exceptions explicitly thrown in Required Classes except for IllegalArgumentException are custom exceptions that need to be made by you.

Required Classes

TripPlanner (Driver Class)

  • public static void main(String[] args)
    • Runs the User interface menu as shown in Required Functions
    • Can be changed as necessary for GUI/Android App
    • This method will contain two Itinerary objects for cloning.

Itinerary

  • private TripStopNode head
  • private TripStopNode tail
  • private TripStopNode cursor

The above variables will hold links into a doubly linked list of TripStopNodes where each node links to the previous and next ones, or nulls if there are none.

  • public Itinerary()
    • Constructor: initializes an empty itinerary with no elements – head, tail, and cursor are set to null.
  • public int getStopsCount()
    • Returns the total number of TripStopNodes in the Itinerary.
    • This method must run in O(1) time.
  • public int getTotalDistance()
    • Returns the sum of distances over all TripStopNodes.
    • This method must run in O(1) time.
  • public TripStop getCursorStop()
    • Returns a reference to the TripStop wrapped by the TripStopNode that cursor points to.
    • If cursor is null, this returns null.
  • public void resetCursorToHead()
    • Returns the cursor to the start of the list.
    • Postconditions:
      • If head is not null, the cursor now references the first TripStopNode in this list.
      • If head is null, the cursor is set to null as well (there are no TripStop objects in this list).
  • public void cursorForward()
    • Moves the cursor to select the next TripStopNode in this list. If cursor == tail, throw an exception.
    • Throws:
      • EndOfItineraryException - Thrown if cursor is at the tail of the list.
  • public void cursorBackward()
    • Moves the cursor to select the previous TripStopNode in this list. If cursor == head, throw an exception (this includes the case where cursor and head are both null).
    • Throws:
      • EndOfItineraryException- Thrown if cursor is at the head of the list.
  • public void insertBeforeCursor(TripStop newStop)
    • Inserts the indicated TripStop before the cursor.
    • Parameters:
      • newStop
        • The TripStop object to be wrapped and inserted into the list before the cursor.
    • Preconditions:
      • newStop is not null.
    • Postconditions:
      • newStop has been wrapped in a new TripStopNode object
      • If cursor was previously not null, the newly created TripStopNode has been inserted into the list before the cursor.
      • If cursor was previously null, the newly created TripStopNode has been set as the new head of the list (as well as the tail).
      • The cursor now references the newly created TripStopNode .
    • Throws:
      • IllegalArgumentException
        • Thrown if newStop is null.
    • Warning
      • You should NOT move data references around between TripStopNodes to accomplish this method. Instead, you should wrap the TripStop object in a new TripStopNode object, and insert this object into the list before the cursor.
  • public void appendToTail(TripStop newStop)
    • Inserts the indicated TripStop after the tail of the list.
    • Parameters:
      • newStop
        • The TripStop object to be wrapped and inserted into the list after the tail of the list.
    • Preconditions:
      • newStop is not null.
    • Postconditions:
      • newStop has been wrapped in a new TripStopNode object
      • If tail was previously not null, the newly created TripStopNode has been inserted into the list after the tail.
      • If tail was previously null, the newly created TripStopNode has been set as the new head of the list (as well as the tail).
      • The tail now references the newly created TripStopNode.
    • Throws:
      • IllegalArgumentException
        • Thrown if newStop is null.
    • Note:
      • This insertion method does not affect the cursor, unless the list was previously empty. In that case, head, tail, and cursor should all reference the new TripStopNode.
  • public TripStop removeCursor()
    • Removes the TripStopNode referenced by cursor and returns the TripStop inside.
    • Preconditions
      • cursor != null
    • Postconditons
      • The TripStopNode referenced by cursor has been removed from the list.
      • All other TripStopNodes in the list exist in the same order as before.
      • The cursor now references the previous TripStopNode
      • Exceptions: If the cursor was originally the head, the cursor will now reference the current head.
    • Returns
      • The TripStop that was removed
    • Throws
      • EndOfListException if cursor is null.

TripStopNode

  • private TripStop data
  • private TripStopNode next
  • private TripStopNode prev
  • public TripStopNode(TripStop initData)
    • Default constructor.
    • Parameters:
      • initData - The data to be wrapped by this TripStopNode. This parameter should not be null, since we should never have a TripStopNode with null data (remember, this class serves only as a wrapper for the TripStop class).
    • Preconditions:
      • initData is not null.
    • Postconditions:
      • This TripStopNode has been initialized to wrap the indicated TripStop, and prev and next have been set to null.
    • Throws:
      • IllegalArgumentException
        • Thrown if initData is null.
  • public TripStop getData()
    • Returns the reference to the data member variable of the list node.
  • public void setData(TripStop newData)
    • Sets the data private field to the one passed as a parameter.
    • Parameters:
      • newData - Reference to a new TripStop object to update the data member variable. This parameter must not be null, since we should never have a TripStopNode with null data (remember, this class serves only as a wrapper for the TripStop class).
    • Preconditions:
      • newData is not null.
    • Throws:
      • IllegalArgumentException - Thrown if newData is null.
  • public TripStopNode getNext()
    • Returns the reference to the next member variable of the list node. Can be null, means there’s no next TripStopNode.
  • public void setNext(TripStopNode newNext)
    • Updates the next member variable with a new TripStopNode reference.
    • Parameters:
      • newNext - Reference to a new TripStopNode object to update the next member variable. This parameter may be null, since it is okay to have no next TripStopNode (this means we’ve reached the tail of the list!).
  • public TripStopNode getPrev()
    • Gets the reference to the prev member variable of the list node.
    • Returns:
      • The reference of the prev member variable. Note that this return value can be null, meaning that there is no previous TripStopNode in the list. (this means we’ve reached the head of the list!).
  • public void setPrev(TripStopNode newPrev)
    • Updates the prev member variable with a new TripStopNode reference.
    • Parameters:
      • newPrev - Reference to a new TripStopNode object to update the prev member variable. This parameter may be null, since it is okay to have no previousTripStopNode (this means we’ve reached the head of the list!).

TripStop

This class contains private data fields for Location (String), Distance (int), and Activity (String). This class is mutable, so there are getters and setters for all private data fields.
If distance is less than 0, throw an IllegalArgumentException
While you must have a default constructor, you are encouraged to make an overloaded constructor taking all 3 fields as parameters (this is not required).

General Recommendations

You might want to implement a toString() method for TripStop, TripStopNode, and Itinerary to make debugging and printing easier. You do not have to do this, but it will help you.
You can feel free to add extra private methods if you need, but if you want to add a public method, check on the discussion board first.

UI Required Functions

The menu should have options similar to these, and NOT be case sensitive.

  • F-Cursor forward
  • B-Cursor backward
  • I-Insert before cursor
    • Secondary menu asking for Location, Distance, Activity (in that order)
  • A-Append to tail
    • Secondary menu asking for Location, Distance, Activity (in that order)
  • D-Delete and move cursor forward
    • H-Cursor to Head
  • T-Cursor to Tail
  • E-Edit cursor
    • Secondary menu asking for edits to Location, Distance, Activity (in that order, see sample)
  • S-Switch itinerary
  • O-Insert cursor from other itinerary before cursor from this itinerary (via cloning)
  • R-Replace this itinerary with a copy of the other itinerary
  • P-Print current itinerary, including summary (see sample)
  • C-Clear current itinerary
  • Q-Quit

Output Format

All lists must be printed in a nice and tabular form as shown in the sample output. You may use Java style formatting as shown in the following example. The example below shows two different ways of displaying the name and address at pre-specified positions 21, 26, 19, and 6 spaces wide. If the ‘-‘ flag is given, then it will be left-justified (padding will be on the right), else the region is right-justified. The ‘s’ identifier is for strings, the ‘d’ identifier is for integers. Giving the additional ‘0’ flag pads an integer with additional zeroes in front. See here for more details.

1
2
3
4
5
6
7
String name = "Doe Jane";
String address = "32 Bayview Dr.";
String city = "Fishers Island, NY";
int zip = 6390;

System.out.println(String.format("%-21s%-26s%19s%06d", name, address, city, zip));
System.out.printf("%-21s%-26s%19s%06d", name, address, city, zip);