Step 1: Edit StringExplode.java
Download the StringExplode.java file, and open it in jGrasp (or a text editor of your choice). This program will “explode” a String into an array of characters (char[]), and then print out the array. The String comes from the first command-line argument. Example output with the command-line argument foo is below:
f o o
---------------------------------------------------------------------
public class StringExplode { // TODO - write your code below this comment. // You will need to write a method that takes a string and // returns an array of the characters in that String. // // You may (and will need) to use the charAt and length // methods of String. You may NOT use any other methods // of String. // // DO NOT MODIFY printArray! public static void printArray(char[] array) { for (int index = 0; index < array.length; index++) { System.out.println(array[index]); } } // DO NOT MODIFY main! public static void main(String[] args) { char[] exploded = explode(args[0]); printArray(exploded); } }
-------------------------------------------------------------------------------------
Step 2: Open StringExplodeTest.java as a Test File, and Edit It
Download the StringExplodeTest.java file, being sure to put it in the same folder/directory as your StringExplode.java file. Open this file in jGrasp as a test file, using the same instructions you've used in previous labs. You need to write a number of tests in this file, and all of them must pass. The comments in the file provide more details.
-----------------------------------------------------------------------------------------
import static org.junit.Assert.assertArrayEquals; import org.junit.Test; public class StringExplodeTest { // TODO - write tests for the method you wrote in StringExplode.java. // You should test with strings of length 0, 1, 2, and 3. }
StringExplode.java
public class StringExplode {
// TODO - write your code below this comment.
// You will need to write a method that takes a string and
// returns an array of the characters in that String.
//
// You may (and will need) to use the charAt and length
// methods of String. You may NOT use any other methods
// of String.
//
public static char[] explode(String msg){
char array[]=null;
if(msg==null)
{
System.out.println("Null String\n");
}
else if(msg="")
{
System.out.println("Empty String\n");
}
else
{
array=new char[msg.length()];
for (int index = 0; index <msg.length(); index++) {
array[index]=msg.charAt(index);
}
}
return array;
}
// DO NOT MODIFY printArray!
public static void printArray(char[] array) {
if(array==null)
{
System.out.println("Empty Array\n");
}
else
{
for (int index = 0; index < array.length; index++) {
System.out.println(array[index]);
}
}
}
// DO NOT MODIFY main!
public static void main(String[] args) {
char[] exploded = explode(args[0]);
printArray(exploded);
}
}
Test Class:
import static org.junit.Assert.assertArrayEquals;
import org.junit.Test;
public class StringExplodeTest {
StringExplode test;
@Test
public void testExplode1(){
test = new StringExplode();
String teststring=null;
char testarray[]=test.explode(teststring);
test.printArray(testarray);
}
@Test
public void testExplode2(){
test = new StringExplode();
String teststring="hello";
char testarray[]=test.explode(teststring);
test.printArray(testarray);
}
@Test
public void testExplode3(){
test = new StringExplode();
String teststring="";
char testarray[]=test.explode(teststring);
test.printArray(testarray);
}
}
Get Answers For Free
Most questions answered within 1 hours.