Skip to content

Test Task 1

Objective

Test Task 1 of creating a linked list that can take in any length number and turn it into a linked list.

Format of Linked List

  • The head node should be -1 for negative numbers and 0 for positive numbers.

  • The next node should contain the last four numbers in the original number.

  • Each consecutive node should contain the next four numbers moving to the left in the original number.

  • The final node should contain between 1 and 4 numbers representing the last 1 to 4 numbers in the original number.

  • There should be a tester function that prints the nodes in order and a function that prints the numbers as it should be.

Example Test Code:

String originalNumber = "-2415763190";

MyBigInteger newInteger = new MyBigInteger(originalNumber);

System.out.println(orginalNumber);
System.out.println(newInteger);
System.out.println(newInteger.getNodes);

Output in Terminal:

-2415763190
-24 1576 3190
-1 3190 1576 24

Tasking

  • Add a getNodes function to MyBigInteger
  • Figure out why an extra number is being printed.
  • Configure the print function to add the proper spacing
  • Test to see if the above code works
  • Create a testing class for the following numbers.
  • Fix the Issue of 0 not being the start of even integers
  • Test number 23
  • Test number -45678
  • Test Number 56789912882731
  • Test Number -23422912812391112341
  • Update Readme on How to Use the Tester Code
  • Approve Section

Issues

Get Nodes Does Not Exist

Added the following code to MyBigString. It should just loop through all the nodes in the linked list and print the numerical values.

/**  
 * Returns a formatted string that returns the nodes in the order that they are stored. * Ex: If the string "-2415763190" is made into a MyBigInteger Object the call to getNodes() will * give you the string "-1 3190 1576 24" * @return a string representation of the linked list in order  
 */public String getNodes() {  
    StringBuilder nodeString = new StringBuilder();  
  
    // Adding in the sign node  
    if (sign.digits == -1) {  
        nodeString.append(sign.digits);  
    } else {  
        nodeString.append(0);  
    }  
  
    // Adding in the first Node  
    var currentNode = this.getFirstNode();  
    nodeString.append(" ").append(currentNode.digits);  
  
    // Adding in the Remaining Nodes  
    currentNode = currentNode.nextNode;  
    while (currentNode != null) {  
        nodeString.append(" ");  
        nodeString.append(currentNode.digits);  
        currentNode = currentNode.nextNode;  
    }  
  
    return nodeString.toString();  
}

Random Number is Printed

Fix - There was an extra print function in the first constructor that printed the length of the number.

Number is Not Printed With Proper Indents

Fix - Changed the following line to have a space before the format reference:

digits = String.format(" %04d", current.digits);

0's are not the start of big integers in the print

I just did a quick fix on this and checked the sign.digits to see if they were equal to -1 if not I added a to the front;

Other Adjustments

  • Changed the name of higher_node to be nextNode to reflect the singly linked list better.

Adding Tester Code

The following code runs five tests on the class MyBigInteger to test the functionality.

public class Test {  
    private int totalTestsRun = 0;  
    private int totalTestsPassed = 0;  
    private int totalTestsFailed = 0;  
    public String testMyBigIntegerCompiler() {  
        String[] testNums = {"-2415763190", "23", "-45678", "56789912882731", "-23422912812391112341"};  
        String[] resultsTest1 = {"-24 1576 3190", "23", "-4 5678", "56 7899 1288 2731", "-2342 2912 8123 9111 2341"};  
        String[] resultsTest2 = {"-1 3190 1576 24",  
                "0 23",  
                "-1 5678 4",  
                "0 2731 1288 7899 56",  
                "-1 2341 9111 8123 2912 2342"};  
  
        StringBuilder testResults = new StringBuilder();  
  
        testResults.append("Test Results:");  
  
        for (int i = 0; i < testNums.length; i++) {  
            testResults.append(runTest(i, testNums[i], resultsTest1[i], resultsTest2[i]));  
        }  
  
        testResults.append("\n\nNumber of Tests Run: ");  
        testResults.append(totalTestsRun);  
        testResults.append("\nNumber of Tests Passed: ");  
        testResults.append(totalTestsPassed);  
        testResults.append("\nNumber of Tests Failed: ");  
        testResults.append(totalTestsFailed);  
  
        return testResults.toString();  
    }  
  
    private String runTest(int num, String testNum, String result1Num, String result2Num) {  
        StringBuilder testResult = new StringBuilder();  
        MyBigInteger testInteger = new MyBigInteger(testNum);  
  
        testResult.append("\n\nTest ");  
        testResult.append(num);  
        testResult.append(" with number: ");  
        testResult.append(testNum);  
  
        // Running Test 1  
        totalTestsRun++;  
        if (result1Num.equals(testInteger.toString())) {  
            testResult.append("\n\tTest 1 passed. Result: ");  
            testResult.append(testInteger);  
            totalTestsPassed++;  
        } else {  
            testResult.append("\n\tTest 1 Failed. Test result: ");  
            testResult.append(testInteger);  
            testResult.append(" | Expected result: ");  
            testResult.append(result1Num);  
            totalTestsFailed++;  
        }  
  
        totalTestsRun++;  
        // Running Test 2  
        if (result2Num.equals(testInteger.getNodes())) {  
            testResult.append("\n\tTest 2 Passed. Result: ");  
            testResult.append(testInteger.getNodes());  
            totalTestsPassed++;  
        } else {  
            testResult.append("\n\tTest 2 Failed. Test result: ");  
            testResult.append(testInteger.getNodes());  
            testResult.append(" | Expected result: ");  
            testResult.append(result2Num);  
            totalTestsFailed++;  
        }  
  
        return testResult.toString();  
    }  
}

To use this code put the following code into main and it will print to terminal.

Test test = new Test();  
System.out.println(test.testMyBigIntegerCompiler());

Adding to Readme

  • Created an Author Part
  • Created a table of contents
  • Created a description of how to test the program

Updating Git Ignore

Needed it to ignore .DS_Store.

Edited by Key Vollers