Question: All work Should be done on Main Class and DLList Class ///////////////////////////////////////////Main//////////////////////////////////////////////////////////// import listinterface.IList; import objects.DLList; import objects.DLine; public class Main { public static void
All work Should be done on Main Class and DLList Class
///////////////////////////////////////////Main////////////////////////////////////////////////////////////
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);
/*-======================= 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);
/*-======================= 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
//myList1.get(index)
/*-======================= 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 [3rd] 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);
/*-======================= 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
}
}
/////////////////////////////////////////////////////////////////DLList///////////////////////////////////////////////////
package objects;
import listinterface.IList;
public class DLList implements IList {
private DLine head;
private int n;
public DLList() {
this.head = null;
this.n = 0;
}
@Override
public DLine get(int index) {
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 (this.n == 0) {
this.head = new DLine(dl);
} else {
DLine lastElement = this.get(this.n - 1);
lastElement.setNext(new DLine(dl));
}
this.n++;
}
@Override
public int add(DLine dl, int index) {
//TODO
return 0;
}
@Override
public void append(IList ls) {
//TODO
if (this.n == 0) {
this.head = (DLine) ls;
}
else {
DLine lastElement = this.get(this.n - 1);
lastElement.setNext(ls.get(0));
}
// increment the size of the list
this.n++;
displayList();
}
@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;
}
}
////////////////////////////////////////////////////////////////////////////////////////////DLine/////////////////////////////////////////////////////////////////////////////
package objects;
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;
}
}
///////////////////////////////////////////////////////////////////////////////IList//////////////////////////////////////////////////////////////////////
package listinterface;
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(" > ");
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
