Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Questions are in the Main Java file. I bolded the questions Main.java file: import listinterface.IList; import objects.DLList; import objects.DLine; public class Main { public static

Questions are in the Main Java file. I bolded the questions

Main.java file:

import listinterface.IList; import objects.DLList; import objects.DLine;

public class Main {

public static void main(String[] args) {

/*-======================= S-1 ======================== * * Test: size() * * - Create a list ('myList1') and check its size. * * * * (!) You should see 0 as the size. * * ======================= S-1 ======================== */startSection(1); IList myList1 = new DLList(); System.out.println("The size of an empty list: " + myList1.size());

/*-======================= S-2 ======================== * * Test: add(int), size() * * - Add 5 elements to 'myList1' using your add method. * * (elements: DLines of size {0,1,2,3,4}. * * - Check the size of the list. Is it 5? * * - Print your list using displayList and verify it. * * * * (!) You should see exactly this: * * < [ 0]|[ 1]|[ 2]|[ 3]|[ 4] > * * ======================= S-2 ======================== */startSection(2); myList1.add(new DLine(0)); myList1.add(new DLine(1)); myList1.add(new DLine(2)); myList1.add(new DLine(3)); myList1.add(new DLine(4)); myList1.displayList();

/*-======================= S-3 ======================== * * Test: get(int) * * - Get the [first], [3rd] and [last] elements. * * - Print the length of them one by one. * * - Get the 100th element! What do you get? * * * * (!) You should see 0, 3, and 4, respectively. * * ======================= S-3 ======================== */startSection(3); //TODO /*-======================= S-4 ======================== * * Test: add(DLine, int) * * - Add a new DLine (size 11) as the [first] element. * * - Display your list and verify it. * * - Add a new DLine (size 22) as the [2nd] element. * * - Display your list and verify it. * * - Add a new DLine (size 33) as the [last] element. * * - Display your list and verify it. * * * * (!) You should see exactly this, at the end: * * < [11]|[ 0]|[22]|[ 1]|[ 2]|[ 3]|[33]|[ 4] > * * * * - Add a new DLine (size 44) as the [100th] element. * * - Print out what the add method returns. * * * * (!) You should see -1. * * ======================= S-4 ======================== */startSection(4); //TODO

/*-======================= S-5 ======================== * * Test: remove(int) * * - Remove the DLine at index 0. * * - Display your list and verify it. * * - Remove the DLine at index now 1. * * - Display your list and verify it. * * - Remove the DLine at index now 4. * * - Display your list and verify it. * * - Remove the last DLine. * * - Display your list and verify it. * * * * (!) You should see exactly this, at the end: * * < [ 0]|[ 1]|[ 2]|[ 3]|[ 4] > * * * * - Remove the DLine at index 100! * * - Print out what the add method returns. * * * * (!) You should see -1. * * * * - Display your list and make sure it is not affected * * by the last operation. * * ======================= S-5 ======================== */startSection(5); //TODO

/*-======================= S-6 ======================== * * Test: append(DLList) * * - Create a new DLList called 'myList2'. * * - Add 3 elements to it using your add method. * * (elements: DLines of size {100, 101, 102}. * * - Print both lists and make sure they are: * * < [ 0]|[ 1]|[ 2]|[ 3]|[ 4] > * * < [100]|[101]|[102] > * * - Append 'myList2' to the end of 'myList1' * * - Print out 'myList2' again. * * * * (!) You should see exactly this: * * < [ 0]|[ 1]|[ 2]|[ 3]|[ 4]|[100]|[101]|[102] > * * ======================= S-6 ======================== */startSection(6); //TODO

/*-======================= S-7 ======================== * * Test: appendAt(DLList, int) * * - Create a new DLList called 'myList3'. * * - Add 2 elements to it using your add method. * * (elements: DLines of size {50, 60}. * * - Print both 'myList1' and 'myList3'. Are they: * * < [ 0]|[ 1]|[ 2]|[ 3]|[ 4]|[100]|[101]|[102] > * * < [50]|[60] > * * - Append 'myList3' at position 20 of 'myList1' * * - Print out what this method returns. * * * * (!) You should see -1. * * * * - Append 'myList3' at position 4 of 'myList1' * * - Print out 'myList2' again. * * * * (!) You should see exactly this: * * < [ 0]|[ 1]|[ 2]|[ 3]|[ 4]|[50]|[60]|[100]|[101]|[102] > * * ======================= S-7 ======================== */startSection(7); //TODO

/*-======================= S-8 ======================== * * Test: empty() * * - Empty your entire 'myList1' using this method. * * - Display the list, and print out the size. Correct? * * ======================= S-8 ======================== */startSection(8); //TODO }

/** IGNORE THIS METHOD **/ private static void startSection(int i) { System.out.print(" :::::::::::::::::::::::"); System.out.print(" START [" + i + "] "); System.out.print("::::::::::::::::::::::: "); }

/** IGNORE THIS METHOD **/ private static void endSection(int i) { System.out.print(" ________________________"); System.out.print(" END [" + i + "] "); System.out.print("________________________ "); }

}

DLList.java file:

package objects;

import listinterface.IList;

/** * This class should implement 'IList' * */ public class DLList implements IList {

/** The head (i.e., first element) of the list */ private DLine head; /** The number of elements in the list */ private int n;

public DLList() { this.head = null; this.n = 0; }

@Override public DLine get(int index) {

// if index is not invalid. if (index >= this.n || index < 0) return null;

DLine currentDLine = this.head;

for (int i = 0; i < index; i++) { currentDLine = currentDLine.getNext(); }

return currentDLine; }

@Override public void add(DLine dl) {

// If the list is empty, add this as the first element (head) if (this.n == 0) { this.head = new DLine(dl); } // otherwise, add it to the end of the list else { DLine lastElement = this.get(this.n - 1); lastElement.setNext(new DLine(dl)); } // increment the size of the list this.n++; }

@Override public int add(DLine dl, int index) { //TODO return 0; }

@Override public void append(IList ls) { //TODO }

@Override public int appendAt(IList ls, int index) { //TODO return -1; }

@Override public int remove(int index) { //TODO return 0;

}

@Override public void empty() { //TOOD }

@Override public int size() { return this.n; }

}

IList.java :

package listinterface;

/** * This interface declares the important methods needed for implementation of a * custom LinkedList. * * * @param */ public interface IList {

/** * Return the element of the list at position index. * * @param index index of the element to be returned. * @return the queried element if index is valid, and * null otherwise. */ public DLine get(int index);

/** * appends the object dl to the end of this list. * * @param dl the object to be added to the list. */ public void add(DLine dl);

/** * inserts the object dl at position index and shifts * all the following elements one place to the right. * * @param dl the object to be added to the list. * @param index the index where the object should be added at. * @return 0 if the object is successfully added, and -1 otherwise. */ public int add(DLine dl, int index);

/** * appends the list ls to the end of this list. * * @param ls the list to be appended. */ public void append(IList ls);

/** * appends the list ls to this list at position index. * In other words, after it is appended, the first element of ls * will be at position index + 1 in the existing list, and its last * element will be at position index + ls.size() - 1. After that, * the remaining elements of the existing list follow as before. * * @param ls * @param index * @return 0 if the new list is successfully added, and -1 otherwise. */ public int appendAt(IList ls, int index);

/** * removes the element at the position index of the list and shift * all the following elements back. * * @param index the position of the element which should be removed. * @return 0 if the element is successfully removed, and -1 otherwise. */ public int remove(int index);

/** * Empty the list by making the head point to null. */ public void empty();

/** * @return the size of the list. */ public int size();

/** * YOU DON'T NEED TO MODIFY THIS! * This method makes it easy to print the entire list at once. */ public default void displayList() {

int i = 0;

System.out.print("\t< "); if (this.size() != 0) { for (; i < this.size() - 1; i++) { System.out.print(this.get(i).toString() + "|"); } System.out.print(this.get(i).toString()); } System.out.print(" > "); } }

DLine.java file:

package objects;

/** * DO NOT MODIFY THIS CLASS! We made all the necessary modifications before. * * DLine is a line with a certain length. This class creates nodes with two * pieces of information: the length of the DLine, and the address of another * DLine to which this DLine is connected. * * This class is created to be used for the purpose of understanding Singly Linked-Lists * and also sorting methods. * * */ public class DLine implements Comparable {

/** Length of the line */ public int length;

/** A link to the next element; */ private DLine next;

/** * The default constructor. Using this constructor, an instance of DLine will be * created with the default length of 2. */ public DLine() { this.length = 2; this.next = null; }

/** * The constructor. Using this constructor, an instance of DLine will be created * with the length of len. * * @param len * length of the line. */ public DLine(int len) { this.length = len; this.next = null; }

/** * Copy constructor. This should be used to avoid unwanted effects due to some * objects being linked together. * * @param dl * the object based on which a copy should be created. */ public DLine(DLine dl) { this.length = dl.length; this.next = null; }

/** * Getter for the class field next * * @return the DLine next to this DLine. If this node is not connected to any * other node, then it returns null. */ public DLine getNext() { return (this.next); }

/** * Setter for the class field next * * @param dl * the DLine to which this DLine should be connected. */ public void setNext(DLine dl) { this.next = dl; }

/** * This method overrides 'toString' for DLine objects. This is to make it easy * to print the lines. */ @Override public String toString() { String len = this.length < 10 ? "[ " + this.length + "]" : "[" + this.length + "]"; return len; }

/** * This method allows instances of this class to be comparable with one another. * * Example: * if(line1.compareTo(line2) > 0) {//line1 is longer than line2.} */ @Override public int compareTo(DLine line) { return this.length - line.length; }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases And Python Programming MySQL MongoDB OOP And Tkinter

Authors: R. PANNEERSELVAM

1st Edition

9357011331, 978-9357011334

More Books

Students also viewed these Databases questions

Question

Provide examples of Dimensional Tables.

Answered: 1 week ago