INTRODUCTION

This document describes Assignment for Programming Fundamentals. The assignment will require you to write a class to represent a mutable list of characters. It will contain an array of chars and a variety of methods for accessing, modifying and resizing the array.

You will find instructions for completing the assignment in the doc folder in JavaDoc format. Start with /doc/progfund/CharList.html.

Run the provided JUnitTestDriver to see if your methods are working and to get a good estimation of your final grade. The JUnitTestDriver will give you a mark. Marks may be added or deducted for a variety of reasons.

There may be problems the JUnitTestDriver does not help you with. To debug your code, you will need to create your own main method in the MyTestDriver class. In this class you should call each method you have completed to check that it is working. You should add test cases to MyTestDriver whenever you have completed a method. Test early and test often.

CharList.java

package progfund;

/**
* < h3>Overview< /h3>
* < p>
* This assignment will require you to write a class to represent a resizable
* array of characters. It should act much like a String. Unlike a String
* however, the characters can be changed or deleted directly.< /p>
*
* < p>
* To make your life easier you will be provided with an API specification (just
* like the JavaDocs you use for your programming). Your job is to follow this
* API to fill out the method stubs with appropriate code. You should also
* ensure that this code is uncrashable. If your code detects faulty/incorrect
* input it should handle it as instructed in the API.< /p>
*
* < h3>Data Structure< /h3>
* < p>
* We will be storing chars into an array within the CharList class. Something
* to note is that arrays are immutable (Their length cannot be changed once
* they are created.) A large part of this assignment is to add methods to the
* CharList class to simulate an array that is mutable. These methods should
* allow adding and deleting chars from the array and changing the array's size.
* Your array must always be exactly the size necessary to fit all the
* characters. There must be no null chars on the end. To ensure your code is
* uncrashable you will have to handle incorrect arguments with if statements.
* < /p>
*
* < h3>Testing< /h3>
* < p>
* MyTestDriver contains a main method. You will need to modify the main method
* to test your classes. This is worth 20 marks so we expect to see at least 20
* test cases. Test each of your methods. Test the expected input, test the
* boundary cases, test outside the boundary cases and test the ridiculous
* cases. < /p>
*
* < p>
* JUnitTestDriver has been provided to help you test your classes as well.
* Running JUnitTestDriver as a JUnit Test should give you a good estimation of
* how many marks your CharList is worth. Note that CharList is worth 80 marks
* and MyTestDriver is worth 20 marks. You will have to complete both.< /p>
*
* < h3>How To Do The Assignment< /h3>
* < p>
* We suggest you write your code in the order that the methods appear. The most
* independent methods are listed first. More dependent methods are written
* last. You can call your earlier methods to complete the later methods. For
* example, deleteChar() might call charAt().< /p>
* < p>
* Consider using print statements to print the contents of the array to make
* sure it contains the correct characters and length.< /p>
*
* < h3>Additional Requirements< /h3>
* < p>
* You must provide an academic integrity statement at the top of each class you
* write. This should include your name, id number, date and the statement,
* "This is my own work as defined by the SAIBT / EIBT Academic Misconduct
* policy." You will need to comment your code thoroughly while you are
* programming.< /p>
*
* < p>
* In general, you may not use methods from other classes. The only exceptions
* are string.length() and string.charAt() may be used in your constructor. The
* use of any other methods may result in a loss of marks.< /p>
*/
public class CharList {

/**
* Array to store chars
*/
private char[] characters = null;

/**
* The instance variable characters will be initialized to length 0.
*/
public CharList() {
}

/**
* The instance variable characters will be initialized to the same length
* and contain the same characters as the argument String. If the argument
* is null, the characters array will be length 0.
*
* @param other The String to copy.
*/
public CharList(String other) {
}

/**
* The instance variable characters will be initialized to the same length
* and contain the same characters as the argument CharList. If the argument
* is null, the characters array will be length 0.
*
* @param other the CharList to copy
*/
public CharList(CharList other) {
}

/**
* Returns the number of non-null characters in the instance variable. Do
* not count null chars ('\0').
*
* @return The length of the String
*/
public int length() {
}

/**
* Compares to CharLists returning true if they contain the same chars and
* are the same length.
*
* @param other presumed to be a CharList. Must type-cast to CharList if it
* is an instance of this class.
* @return true if both arrays contain the same chars and are the same
* length.
*/
@Override
public boolean equals(Object other) {
}

/**
* Return the char at the argument index from the characters array.
*
* @param index the position of the char to return.
* @return The char at the given index or '' if no char is available.
*/
public char charAt(int index) {
}

/**
* Returns a new, deep-copy of the instance variable. You must use a loop to
* copy the characters into a new array.
*
* @return A char array copy of the instance variable, characters.
*/
public char[] toCharArray() {
}

/**
* Returns a copy of the instance variable as a String. You must use a loop
* to copy the characters into a String.
*
* @return String representation of the instance variable, characters.
*/
@Override
public String toString() {
}

/**
* Adds the argument character to the end of this CharList's characters. The
* resulting array will replace this CharList's instance variable.
*
* @param letter The char to append.
*/
public void append(char letter) {
}

/**
* Adds each character from the argument CharList to the end of this
* CharList's characters. The resulting array will replace this CharList's
* instance variable.
*
* @param other The CharList to append.
*/
public void append(CharList other) {
}

/**
* Adds each character from the argument CharList to the start of this
* CharList's characters. The resulting array will replace this CharList's
* instance variable.
*
* @param other The CharList to prepend.
*/
public void prepend(CharList other) {
}

/**
* Adds each character from the argument CharList into this CharList's at
* the specified index. All characters at that index or beyond will be
* pushed to the end.
*
* If the specified index is equal to the length, it will add the characters
* to the end of the array. If the specified index is less than 0 or greater
* than the length, this method does nothing.
*
* @param other The CharList to insert.
* @param index The position to insert at.
*/
public void insert(CharList other, int index) {
}

/**
* Returns the substring in the range from start up to and excluding stop,
* stepping by step each letter.
* < p>
* Examples:< /p>
* < ul>
* < li>new CharList("crown").getSlice(0, 2, 1) returns "cr"< /li>
* < li>new CharList("crown").getSlice(0, 5, 1) returns "crown"< /li>
* < li>new CharList("crown").getSlice(0, 5, 2) returns "con"< /li>
* < li>new CharList("crown").getSlice(0, 6, 1) returns null< /li>
* < li>new CharList("crown").getSlice(-1, -5, -1) returns null< /li>
* < /ul>
*
* @param start Index of first character.
* @param stop Index of the character to stop before.
* @param step The amount to add to the index after each letter.
* @return A CharList representing the chars in this range. If start is less
* than zero, or end is greater than the length or start is greater than
* end, or step is negative, return null.
*/
public CharList getSlice(int start, int stop, int step) {
}

/**
* Searches through the characters in this CharList for the sequence
* matching the argument CharList. If found, returns the index of the first
* character in the sequence where it was found. If it was not found,
* returns -1.
*
* @param substring Substring to find.
* @return The index of the first character of the substring or -1 if not in
* the CharList.
*/
public int indexOf(CharList substring) {
}

/**
* Deletes the char at the argument index. Characters will be shifted back
* to fill in the empty space. The char array will be resized to fit only
* the non-null characters. Any blank space must be removed such that
* "racecar".deleteChar(3) results in the CharList "raccar". If the index is
* out of bounds, the array will not be changed and the method returns
* false.
*
* @param index position of char to delete
* @return true if index is within the bounds of the array
*/
public boolean deleteChar(int index) {
}

/**
* Deletes the substring from start (inclusive) up to end (exclusive),
* stepping by step each letter. The empty space is filled by shifting the
* leftover characters back and resizing the array to fit only the
* characters that are left.
* < p>
* Examples:< /p>
* < ul>
* < li>new CharList("crown").deleteSlice(1, 2, 1) becomes "cown"< /li>
* < li>new CharList("crown").deleteSlice(0, 2, 1) becomes "own"< /li>
* < li>new CharList("crown").deleteSlice(0, 6, 1) stays "crown"< /li>
* < li>new CharList("crown").deleteSlice(0, 5, 2) stays "rw"< /li>
* < li>new CharList("crown").deleteSlice(-1, -6, -1) stays "crown"< /li>
*
* < /ul>
*
* @param start index of the char to start at.
* @param stop index of the char to stop before.
* @param step the amount to add to the index after deleting a letter.
*/
public void deleteSlice(int start, int stop, int step) {
}

/**
* Deletes the first instance of the argument substring if it is found in
* this CharList. Any blank space must be removed such that
* "racecar".deleteSubstring("cec") results in the CharList "raar"
*
* @param substring The string to find.
* @return the index of the first character in the substring that was
* deleted or -1 if the substring was not found.
*/
public int deleteSubstring(CharList substring) {
}

/**
* Returns the number of instances of substring that are found while
* iterating through the calling object. If the argument CharList is null or
* an empty string, it will return -1.
*
* @param substring the sequence of characters to find.
* @return The number of times the argument substring was found in this
* CharList or -1 to indicate an error.
*/
public int count(CharList substring) {
}

/**
* Creates and returns an array of CharLists where each item is a token from
* this CharList that ends with either the argument separator or the end of
* the CharList.
* < p>
* Examples:< /p>
* < ul>
* < li>"you and me".split(' ') returns ["you", "and", "me"]< /li>
* < li>"28/08/1989".split('/') returns ["28", "08", "1989"]< /li>
* < li>"aabaa".split('a') returns ["", "", "b", "", ""]< /li>
* < li>"".split('.') returns [""]< /li>
* < /ul>
*
* @param separator The char to delimit each token.
* @return An array of CharList substrings where each substring is delimited
* by instances of the argument separator.
*/
public CharList[] split(char separator) {
}
}

JUnitTestDriver

package testing;

import progfund.CharList;

import java.lang.reflect.Field;

import log.DetailCollector;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import static org.junit.Assert.*;


/**
* < p>Run this class as a JUnit Test to get a good indication of how
* many marks you should receive. This is not a guarantee of your
* actual final marks. Marks may be deducted for no / poor
* commenting, poor variable names, poor style, use of the keywords
* break / continue, plagiarism, incorrect submission, etc. Your
* teacher will be checking your code thoroughly in addition to
* running this TestDriver.< /p>
*
* < p>There may be problems that this JUnitTestDriver
* does not help you with. Make sure you are testing your own work
* in MyTestDriver. You will be marked on the tests you write to a
* total of 20 marks.< /p>
*
* < p>Each test case has two dependencies - the CharList char array is
* named 'characters' and all the constructor tests pass. Each test
* case contains a number of sub-tests. Each sub-test is dependent on
* the previous sub-test. If a sub-test fails, JUnit will flag the
* test case as failed and it will not check the next sub-test.< /p>
*
* @author Michael
*
*/

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class JUnitTestDriver{

private static int subMarks = 0;
private static int methodMarks = 0;
private static int totalMarks = 0;
private static int maxMarks = 80;

@BeforeClass
public static void setUpBeforeClass() {
System.out.println("Testing CharList Class");

}

@Before
public void setUp() {
subMarks = 0;
System.out.println();
}

@After
public void tearDown() {
totalMarks += subMarks;
System.out.println("\tSubtotal: " + subMarks + " / " +methodMarks);
}

@AfterClass
public static void tearDownAfterClass(){
System.out.println("\nFinished Testing CharList Class");
System.out.println("Total: " + totalMarks + " / " +maxMarks+"\n");
DetailCollector.collect(totalMarks + "/" + 80);

if(totalMarks < 10){
System.out.println("Looks like you have just started.");
System.out.println("You can use this Test Driver to check your progress.");
System.out.println("Start by writing the constructors, then your own main method to see if they work.");
System.out.println("There may be problems that this Test Driver cannot help you with.");
System.out.println("Good luck programmer.");
}
else if(totalMarks < 30){
System.out.println("Looks like you are making progress. Keep going.");
System.out.println("You may want to focus on the toString, length, equals, and charAt methods.");
}
else if(totalMarks < 50){
System.out.println("Great work. Keep it up!");
System.out.println("Make sure you're checking for nulls.");
System.out.println("Check your arguments are valid and handle them appropriately.");
}
else if(totalMarks < 70){
System.out.println("Almost there!");
System.out.println("It's the little things that cause the biggest problems isn't it?");
System.out.println("Sometimes its best to take a step back and think about the algorithm.");
System.out.println("Maybe writing pseudocode or drawing a diagram would help.");
}
else if(totalMarks < 80){
System.out.println("So close!");
System.out.println("I know you can do this.");
System.out.println("Sometimes the problem isn't where you expect it to be.");
System.out.println("Make sure you have your own main method to test specific bugs.");
System.out.println("Print things out, do some debugging!");
}
else if(totalMarks == 80){
System.out.println("Great job!");
System.out.println("Your code has passed all the tests executed by this test driver.");
System.out.println("Remember, this is not a guarantee of your final marks.");
System.out.println("Your supervisor may test for more cases than are outlined here.");
System.out.println("You may also be marked on code style.");
System.out.println("Make sure you have meaningful variable names and that you have left\n"
+ "a comment to describe any complicated code.");
System.out.println("Once you've done that, take a break.");
System.out.println("You've earned it! :D");
}
else{
System.out.println("Looks like you got more that 100%");
System.out.println("That's incredible!");
System.out.println("Maybe you should be teaching programming.");
System.out.println("...instead of cheating.");
}

System.out.println("\n**IMPORTANT**\nMake sure you write MyTestDriver. It is worth 20 marks.");
System.out.println("You will not receive full marks unless you have written MyTestDriver, have ");
System.out.println("meaningful variable names, left comments and avoided break and continue.");

}


@Test
public void test01ConstructorNoArguments(){
try{
System.out.println("Testing Default Constructor");
methodMarks = 1;

System.out.println("\tTesting CharList()");
CharList charList = new CharList();
assertArrayEquals(new char[]{}, getCharArray(charList));
passTest(1);
}
catch(AssertionError e){
failTest(e.toString());
throw e;
}
catch(Exception e){
failTest(e.toString());
throw e;
}
}

@Test
public void test02ConstructorString(){
try{
System.out.println("Testing CharList(String arg)");
methodMarks = 3;

System.out.println("\tTesting CharList('apple') ");
CharList charList = new CharList("apple");
assertArrayEquals("apple".toCharArray(), getCharArray(charList));
passTest(2);

System.out.println("\tTesting CharList((String)null) ");
charList = new CharList((String)null);
assertArrayEquals("".toCharArray(), getCharArray(charList));
passTest(1);
}
catch(AssertionError e){
failTest(e.toString());
throw e;
}
catch(Exception e){
failTest(e.toString());
throw e;
}
}

@Test
public void test03ConstructorCharList(){
try{
System.out.println("Testing CharList(CharList other)");
methodMarks = 4;

System.out.println("\tTesting CharList(new CharList('hello'))");
CharList charList = new CharList(new CharList("hello"));
assertArrayEquals("hello".toCharArray(), getCharArray(charList));
passTest(2);

System.out.println("\tTesting CharList(new CharList((CharList)null))");
charList = new CharList((CharList)null);
assertArrayEquals("".toCharArray(), getCharArray(charList));
passTest(1);

System.out.println("\tTesting CharList(new CharList('hello')) - Deep Copied");
CharList other = new CharList("hello");
charList = new CharList(other);

System.out.println("\t\tCopied array changed to 'fello'");
System.out.println("\t\tNew array should not change...");
char[] charArray = getCharArray(other);
charArray[0] = 'f';
assertArrayEquals("hello".toCharArray(), getCharArray(charList));
passTest(1);
}
catch(AssertionError e){
failTest(e.toString());
throw e;
}
catch(Exception e){
failTest(e.toString());
throw e;
}
}

@Test
public void test04Length(){
try{
System.out.println("Testing length()");
methodMarks = 2;

System.out.println("\tTesting 'hello'.length()");
CharList charList = new CharList("hello");
assertEquals(5, charList.length());
passTest(1);

System.out.println("\tTesting ''.length()");
charList = new CharList();
assertEquals(0, charList.length());
passTest(1);

}
catch(AssertionError e){
failTest(e.toString());
throw e;
}
catch(Exception e){
failTest(e.toString());
throw e;
}
}

@Test
public void test05Equals(){
try{
System.out.println("Testing equals()");
methodMarks = 6;

System.out.println("\tTesting 'hello'.equals('hello')");
CharList charList = new CharList("hello");
CharList charList2 = new CharList("hello");
assertTrue(charList.equals(charList2));
passTest(2);

System.out.println("\tTesting !('hello'.equals('goodbye'))");
charList = new CharList("hello");
charList2 = new CharList("goodbye");
assertFalse(charList.equals(charList2));
passTest(2);

System.out.println("\tTesting !('hello'.equals(15))");
System.out.println("\t\tThe equals method *must* have a parameter of type Object.");
System.out.println("\t\tThis will allow us to enter an argument of type int");
charList = new CharList("hello");
assertFalse(charList.equals(15));
passTest(1);

System.out.println("\tTesting !('hello'.equals(null))");
charList = new CharList("hello");
assertFalse(charList.equals(null));
passTest(1);
}
catch(AssertionError e){
failTest(e.toString());
throw e;
}
catch(Exception e){
failTest(e.toString());
throw e;
}
}

@Test
public void test06CharAt(){
try{
System.out.println("Testing charAt()");
methodMarks = 4;

System.out.println("\tTesting 'Hamburger'.charAt(0)");
CharList charList = new CharList("Hamburger");
assertEquals('H', charList.charAt(0));
passTest(1);


System.out.println("\tTesting 'Hamburger'.charAt(8)");
charList = new CharList("Hamburger");
assertEquals('r', charList.charAt(8));
passTest(1);


System.out.println("\tTesting 'Hamburger'.charAt(-1)");
charList = new CharList("Hamburger");
assertEquals('\0', charList.charAt(-1));
passTest(1);

System.out.println("\tTesting 'Hamburger'.charAt(10)");
charList = new CharList("Hamburger");
assertEquals('\0', charList.charAt(10));
passTest(1);
}catch(AssertionError e){
failTest(e.toString());
throw e;
}
catch(Exception e){
failTest(e.toString());
throw e;
}
}

@Test
public void test07ToCharArray() {
try{
System.out.println("Testing toCharArray()");
methodMarks = 2;

System.out.println("\tTesting 'Apple'.toCharArray()");
CharList charList = new CharList("Apple");
assertArrayEquals("Apple".toCharArray(), charList.toCharArray());
passTest(1);

System.out.println("\tTesting ''.toCharArray()");
charList = new CharList();
assertArrayEquals("".toCharArray(), charList.toCharArray());
passTest(1);
}
catch(AssertionError e){
failTest(e.toString());
throw e;
}
catch(Exception e){
failTest(e.toString());
throw e;
}
}

@Test
public void test08ToString(){
try{
System.out.println("Testing toString()");
methodMarks = 2;

System.out.println("\tTesting 'Banana'.toString()");
CharList charList = new CharList("Banana");
assertEquals("Banana", charList.toString());
passTest(1);

System.out.println("\tTesting ''.toCharArray()");
charList = new CharList();
assertEquals("", charList.toString());
passTest(1);



}
catch(AssertionError e){
failTest(e.toString());
throw e;
}
catch(Exception e){
failTest(e.toString());
throw e;
}


}

@Test
public void test09Append(){
try{
System.out.println("Testing append(char)");
methodMarks = 2;

System.out.println("\tTesting ''.append('H')'");
CharList charList = new CharList();
charList.append('H');
assertArrayEquals("H".toCharArray(), getCharArray(charList));
passTest(1);

System.out.println("\tTesting 'H'.append('i')");
charList = new CharList("H");
charList.append('i');
assertArrayEquals("Hi".toCharArray(), getCharArray(charList));
passTest(1);

}catch(AssertionError e){
failTest(e.toString());
throw e;
}
catch(Exception e){
failTest(e.toString());
throw e;
}
}

@Test
public void test10Append(){
try{
System.out.println("Testing append(CharList)");
methodMarks = 5;

System.out.println("\tTesting ''.append('Hello')'");
CharList charList = new CharList();
charList.append(new CharList("Hello"));
assertArrayEquals("Hello".toCharArray(), getCharArray(charList));
passTest(2);

System.out.println("\tTesting 'Hello'.append('Bob')");
charList = new CharList("Hello");
charList.append(new CharList("Bob"));
assertArrayEquals("HelloBob".toCharArray(), getCharArray(charList));
passTest(1);

System.out.println("\tTesting 'HelloBob'.append('')");
charList = new CharList("HelloBob");
charList.append(new CharList(""));
assertArrayEquals("HelloBob".toCharArray(), getCharArray(charList));
passTest(1);

System.out.println("\tTesting 'HelloBob'.append(null)");
charList = new CharList("HelloBob");
charList.append(null);
assertArrayEquals("HelloBob".toCharArray(), getCharArray(charList));
passTest(1);

}catch(AssertionError e){
failTest(e.toString());
throw e;
}
catch(Exception e){
failTest(e.toString());
throw e;
}
}

@Test
public void test11Prepend(){
try{
System.out.println("Testing prepend()");
methodMarks = 5;

System.out.println("\tTesting ''.prepend('Hello')'");
CharList charList = new CharList();
charList.prepend(new CharList("Hello"));
assertArrayEquals("Hello".toCharArray(), getCharArray(charList));
passTest(2);

System.out.println("\tTesting 'Hello'.prepend('Bob')");
charList = new CharList("Hello");
charList.prepend(new CharList("Bob"));
assertArrayEquals("BobHello".toCharArray(), getCharArray(charList));
passTest(1);

System.out.println("\tTesting 'HelloBob'.prepend('')");
charList = new CharList("BobHello");
charList.prepend(new CharList(""));
assertArrayEquals("BobHello".toCharArray(), getCharArray(charList));
passTest(1);

System.out.println("\tTesting 'HelloBob'.prepend(null)");
charList = new CharList("BobHello");
charList.prepend(null);
assertArrayEquals("BobHello".toCharArray(), getCharArray(charList));
passTest(1);
}
catch(AssertionError e){
failTest(e.toString());
throw e;
}
catch(Exception e){
failTest(e.toString());
throw e;
}
}

@Test
public void test12Insert(){
try{
System.out.println("Test insert()");
methodMarks = 6;

System.out.println("\tTesting ''.insert('Skate', 0)'");
CharList charList = new CharList();
charList.insert(new CharList("Skate"), 0);
assertArrayEquals("Skate".toCharArray(), getCharArray(charList));
passTest(1);

System.out.println("\tTesting 'Skate'.insert('board', 5)'");
charList = new CharList("Skate");
charList.insert(new CharList("board"), 5);
assertArrayEquals("Skateboard".toCharArray(), getCharArray(charList));
passTest(1);

System.out.println("\tTesting 'Skateboard'.insert('at', 4)'");
charList = new CharList("Skateboard");
charList.insert(new CharList("at"), 4);
assertArrayEquals("Skatateboard".toCharArray(), getCharArray(charList));
passTest(1);


System.out.println("\tTesting 'Skateboard'.insert('at', 42)'");
charList = new CharList("Skateboard");
charList.insert(new CharList("at"), 42);
assertArrayEquals("Skateboard".toCharArray(), getCharArray(charList));
passTest(1);

System.out.println("\tTesting 'Skateboard'.insert('at', -1)'");
charList = new CharList("Skateboard");
charList.insert(new CharList("at"), -1);
assertArrayEquals("Skateboard".toCharArray(), getCharArray(charList));
passTest(1);


System.out.println("\tTesting 'Skate'.insert(null, 3)'");
charList = new CharList("Skate");
charList.insert(null, 3);
assertArrayEquals("Skate".toCharArray(), getCharArray(charList));
passTest(1);

}
catch(AssertionError e){
failTest(e.toString());
throw e;
}
catch(Exception e){
failTest(e.toString());
throw e;
}
}

@Test
public void test13getSlice(){
try{
System.out.println("Testing getSlice()");
methodMarks = 7;

System.out.println("\tTesting 'Stalwart'.getRange(0, 8, 1)");
CharList charList = new CharList("Stalwart");
CharList result = charList.getSlice(0, 8, 1);
assertArrayEquals("Stalwart".toCharArray(), getCharArray(result));
passTest(1);

System.out.println("\tTesting 'Stalwart'.getRange(2, 7, 2) = 'awr'");
charList = new CharList("Stalwart");
result = charList.getSlice(2, 7, 2);
assertArrayEquals("awr".toCharArray(), getCharArray(result));
passTest(1);

System.out.println("\tTesting 'Stalwart'.getRange(-1, 2, 1)");
charList = new CharList("Stalwart");
result = charList.getSlice(-1, 2, 1);
assertNull(result);
passTest(1);

System.out.println("\tTesting 'Stalwart'.getRange(1, 20, 1)");
charList = new CharList("Stalwart");
result = charList.getSlice(1, 20, 1);
assertNull(result);
passTest(1);

System.out.println("\tTesting 'Stalwart'.getRange(6, 1, 1)");
charList = new CharList("Stalwart");
result = charList.getSlice(6, 1, 1);
assertNull(result);
passTest(1);

System.out.println("\tTesting 'Stalwart'.getRange(6, 1, -1)");
charList = new CharList("Stalwart");
result = charList.getSlice(6, 1, -1);
assertNull(result);
passTest(1);

System.out.println("\tTesting 'Stalwart'.getRange(2, 6, 0)");
charList = new CharList("Stalwart");
result = charList.getSlice(2, 6, 0);
assertNull(result);
passTest(1);
}
catch(AssertionError e){
failTest(e.toString());
throw e;
}
catch(Exception e){
failTest(e.toString());
throw e;
}
}

@Test
public void test14IndexOf(){
try{
System.out.println("Testing indexOf()");
methodMarks = 6;

System.out.println("\tTesting 'Slaughter'.indexOf('laugh')");
CharList charList = new CharList("Slaughter");
int index = charList.indexOf(new CharList("laugh"));
assertEquals(1, index);
passTest(1);

System.out.println("\tTesting 'Slaughter'.indexOf('salutations')");
charList = new CharList("Slaughter");
index = charList.indexOf(new CharList("salutations"));
assertEquals(-1, index);
passTest(1);

System.out.println("\tTesting 'Slaughter'.indexOf('Sla')");
charList = new CharList("Slaughter");
index = charList.indexOf(new CharList("Sla"));
assertEquals(0, index);
passTest(1);

System.out.println("\tTesting 'Slaughter'.indexOf('ter')");
charList = new CharList("Slaughter");
index = charList.indexOf(new CharList("ter"));
assertEquals(6, index);
passTest(1);

System.out.println("\tTesting 'Slaughter'.indexOf(null)");
charList = new CharList("Slaughter");
index = charList.indexOf(null);
assertEquals(-1, index);
passTest(1);

System.out.println("\tTesting 'Slaughter'.indexOf('')");
charList = new CharList("Slaughter");
index = charList.indexOf(new CharList(""));
assertEquals(-1, index);
passTest(1);
}
catch(AssertionError e){
failTest(e.toString());
throw e;
}
catch(Exception e){
failTest(e.toString());
throw e;
}
}

@Test
public void test15DeleteChar(){
try{
System.out.println("Testing deleteChar()");
methodMarks = 5;

System.out.println("\tTesting 'Polar Bear'.deleteChar(5)");
CharList charList = new CharList("Polar Bear");
boolean result = charList.deleteChar(5);
assertArrayEquals("PolarBear".toCharArray(), getCharArray(charList));
assertTrue(result);
passTest(1);

System.out.println("\tTesting 'Polar Bear'.deleteChar(0)");
charList = new CharList("Polar Bear");
result = charList.deleteChar(0);
assertArrayEquals("olar Bear".toCharArray(), getCharArray(charList));
assertTrue(result);
passTest(1);

System.out.println("\tTesting 'Polar Bear'.deleteChar(9)");
charList = new CharList("Polar Bear");
result = charList.deleteChar(9);
assertArrayEquals("Polar Bea".toCharArray(), getCharArray(charList));
assertTrue(result);
passTest(1);

System.out.println("\tTesting 'Polar Bear'.deleteChar(-9)");
charList = new CharList("Polar Bear");
result = charList.deleteChar(-9);
assertArrayEquals("Polar Bear".toCharArray(), getCharArray(charList));
assertFalse(result);
passTest(1);

System.out.println("\tTesting 'Polar Bear'.deleteChar(19)");
charList = new CharList("Polar Bear");
result = charList.deleteChar(19);
assertArrayEquals("Polar Bear".toCharArray(), getCharArray(charList));
assertFalse(result);
passTest(1);
}
catch(AssertionError e){
failTest(e.toString());
throw e;
}
catch(Exception e){
failTest(e.toString());
throw e;
}
}

@Test
public void test16DeleteSlice(){
try{
System.out.println("Testing deleteSlice()");
methodMarks = 8;

System.out.println("\tTesting 'Emporer Penguin'.deleteSlice(5, 11, 1)");
CharList charList = new CharList("Emporer Penguin");
charList.deleteSlice(5, 11, 1);
assertArrayEquals("Emporguin".toCharArray(), getCharArray(charList));
passTest(1);

System.out.println("\tTesting 'Emporer Penguin'.deleteSlice(0, 5, 1)");
charList = new CharList("Emporer Penguin");
charList.deleteSlice(0, 5, 1);
assertArrayEquals("er Penguin".toCharArray(), getCharArray(charList));
passTest(1);

System.out.println("\tTesting 'Emporer Penguin'.deleteSlice(11, 15, 1)");
charList = new CharList("Emporer Penguin");
charList.deleteSlice(11, 15, 1);
assertArrayEquals("Emporer Pen".toCharArray(), getCharArray(charList));
passTest(1);

System.out.println("\tTesting 'Emporer Penguin'.deleteSlice(10, 5, 1)");
charList = new CharList("Emporer Penguin");
charList.deleteSlice(10, 5, 1);
assertArrayEquals("Emporer Penguin".toCharArray(), getCharArray(charList));
passTest(1);

System.out.println("\tTesting 'Emporer Penguin'.deleteSlice(-1, 5, 1)");
charList = new CharList("Emporer Penguin");
charList.deleteSlice(-1, 5, 1);
assertArrayEquals("Emporer Penguin".toCharArray(), getCharArray(charList));
passTest(1);

System.out.println("\tTesting 'Emporer Penguin'.deleteSlice(5, 16, 1)");
charList = new CharList("Emporer Penguin");
charList.deleteSlice(5, 16, 1);
assertArrayEquals("Emporer Penguin".toCharArray(), getCharArray(charList));
passTest(1);

System.out.println("\tTesting 'Emporer Penguin'.deleteSlice(0, 15, 3) = 'mpre Pngin'");
charList = new CharList("Emporer Penguin");
charList.deleteSlice(0, 15, 3);
assertArrayEquals("mpre Pngin".toCharArray(), getCharArray(charList));
passTest(1);

System.out.println("\tTesting 'Emporer Penguin'.deleteSlice(5, 10, 0)");
charList = new CharList("Emporer Penguin");
charList.deleteSlice(5, 10, 0);
assertArrayEquals("Emporer Penguin".toCharArray(), getCharArray(charList));
passTest(1);
}
catch(AssertionError e){
failTest(e.toString());
throw e;
}
catch(Exception e){
failTest(e.toString());
throw e;
}
}

@Test
public void test17DeleteSubstring(){
try{
System.out.println("Testing deleteSubstring()");
methodMarks = 6;

System.out.println("\tTesting 'Toucan doing the cancan'.deleteSubstring('can')");
CharList charList = new CharList("Toucan doing the cancan");
int result = charList.deleteSubstring(new CharList("can"));
assertArrayEquals("Tou doing the cancan".toCharArray(), getCharArray(charList));
assertEquals(3, result);
passTest(2);

System.out.println("\tTesting 'Toucan doing the cancan'.deleteSubstring('polka')");
charList = new CharList("Toucan doing the cancan");
result = charList.deleteSubstring(new CharList("polka"));
assertArrayEquals("Toucan doing the cancan".toCharArray(), getCharArray(charList));
assertEquals(-1, result);
passTest(2);

System.out.println("\tTesting 'Toucan doing the cancan'.deleteSubstring('')");
charList = new CharList("Toucan doing the cancan");
result = charList.deleteSubstring(new CharList(""));
assertArrayEquals("Toucan doing the cancan".toCharArray(), getCharArray(charList));
assertEquals(-1, result);
passTest(1);

System.out.println("\tTesting 'Toucan doing the cancan'.deleteSubstring(null)");
charList = new CharList("Toucan doing the cancan");
result = charList.deleteSubstring(null);
assertArrayEquals("Toucan doing the cancan".toCharArray(), getCharArray(charList));
assertEquals(-1, result);
passTest(1);
}
catch(AssertionError e){
failTest(e.toString());
throw e;
}
catch(Exception e){
failTest(e.toString());
throw e;
}
}


@Test
public void test19Count(){
try{
System.out.println("Testing count()");
methodMarks = 2;

System.out.println("\tTesting 'toucan doing the can can.'.count(\"can\")");
CharList charList = new CharList("toucan doing the can can.");
int count = charList.count(new CharList("can"));
assertEquals(3, count);
passTest(1);

System.out.println("\tTesting 'toucan doing the can can.'.count(\"\")");
charList = new CharList("toucan doing the can can.");
count = charList.count(new CharList(""));
assertEquals(-1, count);
passTest(1);
}
catch(AssertionError e){
failTest(e.toString());
throw e;
}
catch(Exception e){
failTest(e.toString());
throw e;
}
}


@Test
public void test20Split(){
try{
System.out.println("Testing split()");
methodMarks = 4;

System.out.println("\tTesting \"you and me\".split(' ')");
CharList charList = new CharList("you and me");
CharList[] tokens = charList.split(' ');
assertArrayEquals(new CharList[] {new CharList("you"),
new CharList("and"), new CharList("me")}, tokens);
passTest(1);

System.out.println("\tTesting \"28/08/1989\".split('/')");
charList = new CharList("28/08/1989");
tokens = charList.split('/');
assertArrayEquals(new CharList[] {new CharList("28"),
new CharList("08"), new CharList("1989")}, tokens);
passTest(1);

System.out.println("\tTesting \"aabaa\".split('a')");
charList = new CharList("aabaa");
tokens = charList.split('a');
assertArrayEquals(new CharList[] {new CharList(""),
new CharList(""),new CharList("b"),
new CharList(""), new CharList("")}, tokens);
passTest(1);

System.out.println("\tTesting \"\".split('.')");
charList = new CharList("");
tokens = charList.split('.');
assertArrayEquals(new CharList[] {new CharList("")}, tokens);
passTest(1);

}
catch(AssertionError e){
failTest(e.toString());
throw e;
}
catch(Exception e){
failTest(e.toString());
throw e;
}
}




/**
* Utility method that allows me to access the CharList private instance variable, characters.
* @param charListArg The CharList object to reflect into.
* @return the private characters array from the argument.
*/
public static char[] getCharArray(CharList charListArg){
if(charListArg != null) {
try{

Class< ? extends CharList> charListReflect = charListArg.getClass();
Field charsReflect = charListReflect.getDeclaredField("characters");
charsReflect.setAccessible(true);
char[] charsGot = (char[]) charsReflect.get(charListArg);
return charsGot;
}
catch(NoSuchFieldException e){
System.err.println("ERROR: Could not access private characters. Make sure the variable name is \"characters\" and it is private");
e.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}
}
return null;
}

/**
* Called to notify the user of a passed test.
* @param marks the number of marks the test is worth.
*/
public static void passTest(int marks){
subMarks += marks;
System.out.println("\t\tPASSED (+"+ marks +")");
}

/**
* Called to notify the user of a failed test.
* @param message the message to display on failure.
*/
public static void failTest(String message){
System.out.println("\t\tFAILED: " + message);
}
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.