Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Simple 2D Iterator Solution in java #347

Merged
merged 1 commit into from
Oct 4, 2022

Conversation

codingis4noobs2
Copy link
Contributor

Language

Java

Challenge

Simple 2D Iterator

Closes #181

@codingis4noobs2 codingis4noobs2 changed the title Create Codingis4noobs2.java Added Simple 2D Iterator Solution in java Oct 4, 2022
}

public boolean hasNext() {
return row < array.length && col < array[row].length;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if you actually need to check the column here. Your next() function seems like it would prevent col from ever being >= the number of columns in your input array.

}

public class Codingis4noobs2 {
public static void main(String[] args) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing "wrong" with demonstrating your solution this way, but I'm going to add a comment to the PR demonstrating how to incorporate and test your code with JUnit. It'll probably look a bit wonky since it'll be using JUnit directly (instead of through an IDE or using a package manager), but it's worth taking a look at. I'm not going to ask that you change your PR to use JUnit, just take a look.

@ericwburden
Copy link
Collaborator

This solution works! If you wanted to use JUnit tests, we support that in the repo by leveraging the JUnit jar directly. It's included in ./resources/java along with a bash script that makes running the test a bit easier. You could incorporate the JUnit tests into your code like so:

// 2D Array Itreator

// Testing Imports
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Iterator;
import java.util.NoSuchElementException;

class NestedIterator<T> implements Iterator<T> {
   // clipped for brevity
}

public class Codingis4noobs2 {
    public static void main(String[] args) {
        Integer[][] array = new Integer[][] {
                { 1, 2, 3 },
                { 4, 5, 6 },
                { 7, 8, 9 }
        };
        NestedIterator<Integer> iterator = new NestedIterator<Integer>(array);
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    // Test case one
    @Test
    @DisplayName("Should return 1-9, in order")
    public void testCaseOne() {
        Integer[][] array = new Integer[][] {
                { 1, 2, 3 },
                { 4, 5, 6 },
                { 7, 8, 9 }
        };
        NestedIterator<Integer> iterator = new NestedIterator<Integer>(array);
        assertEquals(iterator.next(), 1);
        assertEquals(iterator.next(), 2);
        assertEquals(iterator.next(), 3);
        assertEquals(iterator.hasNext(), true);
        assertEquals(iterator.next(), 4);
        assertEquals(iterator.next(), 5);
        assertEquals(iterator.next(), 6);
        assertEquals(iterator.hasNext(), true);
        assertEquals(iterator.next(), 7);
        assertEquals(iterator.next(), 8);
        assertEquals(iterator.next(), 9);
        assertEquals(iterator.hasNext(), false);
    }
}

You can run the tests by copying .resources/java/java-test-runner.sh to your solutions directory, updating the path to the JUnit jar in that file to account for the extra nesting, and running the bash script. I'd update the test runner script like so:

#!/bin/bash

# Note: This script will execute as if in the path from which it is called
junit_jar="../../../../../resources/java/junit5-1.8.0-M1.jar"

# Runs the JUnit5 tests
run_test() {
    # Get a list of all files in the current directory that end in `.java`
    java_files=$(ls | grep java)
    
    # Compile the source and test files for this challenge, put the *.class files
    # in the 'out/' folder. Include the JUnit standalone .jar in the classpath.
    javac -d out -cp $junit_jar $java_files

    # Run the standalone JUnit testrunner with desired options, on the *.class files
    # in the 'out/' folder.
    java -jar $junit_jar \
         --classpath out \
         --scan-class-path \
         --details tree \
         --include-classname '.*'
}

# clipped for brevity

If you're wondering, this is exactly how I went about testing your solution to make sure it worked. 😁

@ericwburden ericwburden added the hacktoberfest-accepted Accepted for Hacktoberfest label Oct 4, 2022
Copy link
Collaborator

@drkennetz drkennetz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I second what @ericwburden said. Take a look at the JUnit implementation he added to the PR.

@drkennetz drkennetz merged commit e399b0e into codeconnector:main Oct 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest-accepted Accepted for Hacktoberfest
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Hacktoberfest] Java | Simple 2D Iterator
3 participants