Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Task: For the Ls class: (Parts 1-4 have already been answered and implemented into the Ls class so there is no need to attempt them.

Task:

For the Ls class: (Parts 1-4 have already been answered and implemented into the Ls class so there is no need to attempt them. only 5-8)

(i use java and EclipseIDE, thank you)

1. Write a startsWith method that returns true if the beginning of an Ls list has the same elements as a list passed as an argument, otherwise false. Note all lists startsWith and empty list.

Example:

new Ls(1,2,3,4,5).startsWith(new Ls(1,2,3)) returns true

new Ls(1,2,3,4,5).startsWith(new Ls(1,3,2)) returns false

new Ls(1,2,3).startsWith(new Ls()) returns true

2. Write an indexOf method such that the argument could be an Ls list. It should return the index where the argument is found as a sub-list. Suggest using the startsWith method. It may be helpful.

Example:

new Ls(1,2,3,4,5).indexOf(new Ls(3,4)) returns 2.

Should return -1 if the sublist cannot be found.

3. Write a concat method with returns a new list formed out of append a second Ls to the Ls list.

Example:

new Ls(1,2,3,4).concat(new Ls(5,6,7)) returns a list (1,2,3,4,5,6,7)

4. Write a sublist method with returns a sublist having the similar semantics to Javas substring method that takes argument. Have sublist throw an IndexOutOfBoundsException if the parameters are out of bounds.

Example:

new Ls(1,2,3,4).sublist(2) returns a list (3,4)

5. Write a sublist method with returns a sublist having the similar semantics to Javas substring method that takes two arguments. Have sublist throw an IndexOutOfBoundsException if the parameters are out of bounds.

Example:

new Ls(1,2,3,4).sublist(1,3) returns a list (2,3)

6. Write a flatten method with returns a flattened list out of a list that may contain other lists, as so on.

Example:

new Ls(1,2,new Ls(3,4),5,new Ls(new Ls(7))) returns a list (1,2,3,4,5,6,7)

7. Write a replace method to perform a replace all and anywhere within a hierarchical Ls list (lists within lists).

Example:

new Ls(1,2,new Ls(1,2,3), 2, 1).replaceAll(1,99) returns a list

(99,2,(99,2,3),2,99)

8. Write a compareTo method compare the list against another list. For this task, have the Ls implement Javas Comparable interface. You can assume the car implements Comparable (it is OK if the method throws ClassCastException). Make sure to handle the case where one list is simply longer than the other, but all values are the same up to the shortest length are the same.

Example:

new Ls(1,2,3,4).compareTo(new Ls(1,2,4)) returns -1

new Ls(1,2,3,4).compareTo(new Ls(1,2,3)) returns 1

Requirements:

Your solutions must use recursion.

The list must be immutable. Thus, the methods return new instances of an Ls list when needed.

Write a jUnit test program to thoroughly test each of the above methods (dont just use the examples above).

Ls.java:

import java.util.NoSuchElementException; /** * A class that has similar semantics to Scheme/Lisp lists, except it is object * oriented instead of functional. Base methods are car, cdr, and cons: * * car returns the value of the first element in the list, cdr returns the * sublist past the first element, and cons returns a new list composed of new * car inserted at the beginning of a list * * Ls lists are considered to be non-mutable. That is a given instance must not * change its contained set of elements. Any modifications, must result in * creation of new Ls lists. * * Ls lists can be instantiated yet be considered to be empty. A method isEmpty * returns true for an empty Ls list, otherwise false. * * @author */ public class Ls { private E car; private Ls cdr; /** * Constructor to build a scheme list. * * @param car * the car of the list * @param cdr * the cdr for the list, must not be null * @throws RuntimeException * on attempt to create a Ls list with a null cdr */ private Ls(E car, Ls cdr) { this.car = car; this.cdr = cdr; } /** * Private constructor to recursively construct a scheme list over a * variable list of parameters. * * @param i * the current position when constructing the list * @param parms * the variable list of parameters */ private Ls(int i, E... parms) { if (i < parms.length) { car = parms[i]; cdr = new Ls(i + 1, parms); } else { car = null; cdr = null; // Ls with an empty cdr is the end marker node } } /** * Constructor to create a scheme list from its parameters. * * @param parms * the variable list of parameters */ public Ls(E... parms) { this(0, parms); } /** * Get the car * * @return the car - the first element in the list * @throws NoSuchElementException * on an empty list */ public E car() { if (cdr == null) { throw new NoSuchElementException(); } return car; } /** * Get the cdr * * @return the cdr - Ls list past the first element * @throws NoSuchElementException * on an empty list */ public Ls cdr() { if (cdr == null) { throw new NoSuchElementException(); } return cdr; } /** * Cons a new car onto a list. * * @param car * the new car. * @return a new scheme list with the car at the front of the list */ public Ls cons(E car) { return new Ls(car, this); } /** * Identify if a scheme list is empty * * @return true if the scheme list is empty, otherwise false. */ public boolean isEmpty() { return cdr == null; } /** * Get the string representation of the list. The string representation is a * comma separated list of elements (as strings) enclosed in parentheses. * * @return string representation of a list. */ public String toString() { return "(" + toRemainingString(); } /** * Private method to print the string representation of a list past the * leading square bracket. If empty, prints the closing parenthesis, * otherwise prints the car and then uses recursion to print the remaining * elements. * * @return string representation of a list past the leading parenthesis. */ private String toRemainingString() { return isEmpty() ? ")" : car.toString() + (cdr.isEmpty() ? "" : ",") + cdr.toRemainingString(); } /** * Get the size of the list * * @return number of elements in the list */ public int size() { if (isEmpty()) { return 0; } else { return 1 + cdr.size(); } } /** * Compares the specified object with this Ls for equality. Returns true * only if the specified object is also an Ls, both lists have the same * size, and all corresponding pairs of elements in the two lists are equal. * In other words, two Ls lists are defined to be equal if they contain the * same elements in the same order. * * @param other * the other object * @return true if equals, otherwise false */ public boolean equals(Object other) { if (!(other instanceof Ls)) { return false; } Ls otherLs = (Ls) other; if (isEmpty() && otherLs.isEmpty()) { return true; } if (isEmpty() || otherLs.isEmpty()) { return false; } if (car.equals(otherLs.car())) { return cdr.equals(otherLs.cdr); } else { return false; } } public boolean startsWith(Ls otherList) { boolean status = true; Ls thisList = this; while (!otherList.isEmpty()) { E thisElement = thisList.car(); E otherElement = otherList.car(); if (!thisElement.equals(otherElement)) { return false; } thisList = thisList.cdr(); otherList = otherList.cdr(); if (thisList.isEmpty()) { return false; } } return status; } public int indexOf(Ls otherList) { int i = 0; Ls thisList = this; while (i != size()) { if (thisList.startsWith(otherList)) { return i; } thisList = thisList.cdr(); i++; } return -1; } public Ls concat(Ls otherList) { Ls output; if(size() >= 2) { output = new Ls(this.car(), this.cdr()); } else if(size() == 1) { output = new Ls(this.car()); } else { output = new Ls(); } while(!otherList.isEmpty()) { append(output, otherList.car()); otherList = otherList.cdr(); } return output; } private void append(Ls list, E elem) { while(!list.cdr().isEmpty()) { list = list.cdr(); } list.cdr = new Ls(elem); } public Ls sublist(int index) { if (index < 0 || index > size()) { throw new IndexOutOfBoundsException(); } Ls thisList = this; int i = 0; while (i != index) { thisList = thisList.cdr(); i++; } return new Ls(thisList.car(), thisList.cdr()); } } TestLs.java:  
public class TestLs { public static void main(String[] args) { Ls l1 = new Ls(1,2,3,4,5); System.out.println(l1); Ls l2 = new Ls(1,2,3); System.out.println(l2); Ls l3 = new Ls(1,3,2); System.out.println(l3); Ls l4 = new Ls(); System.out.println(l4); Ls l5 = new Ls(3,4); System.out.println(l5); System.out.println(l1.startsWith(l2)); System.out.println(l1.startsWith(l3)); System.out.println(l1.startsWith(l4)); System.out.println(l1.indexOf(l5)); System.out.println(l1.concat(l3)); System.out.println(l1.sublist(2)); } }

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_2

Step: 3

blur-text-image_3

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

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions

Question

What would you do?

Answered: 1 week ago