Question

Step 1: Edit StringExplode.java Download the StringExplode.java file, and open it in jGrasp (or a text...

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.
}

Homework Answers

Answer #1

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);
}
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Download the ProductUpTo3.java file, and open it in jGrasp (or a text editor of your choice)....
Download the ProductUpTo3.java file, and open it in jGrasp (or a text editor of your choice). This program will read in three integers with Scanner, put the values in an array of int, and then print the product of the three values. Example output of the program is shown below, with user input shown in bold: Enter first integer: 3 Enter second integer: 4 Enter third integer: 5 Product: 60 More details about the method you need to write are...
Step 1: Get OperationsBetween.java Compiling Download the OperationsBetween.java file, and open it in jGrasp (or a...
Step 1: Get OperationsBetween.java Compiling Download the OperationsBetween.java file, and open it in jGrasp (or a text editor of your choice). This program takes two command-line arguments representing the minimum and maximum number in a numeric range. Currently, the code does not compile. As a first step, you need to get this code to compile. To this end, you'll need to implement the following: Instance variables Constructor A “stub” for the sum method, which will allow the call to sum...
i am trying to wrire a word and change it to capital letters using client-server. im...
i am trying to wrire a word and change it to capital letters using client-server. im not able to write any words when i run the file in netbeans or command promot. is it something with my code? /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package assignment3retake; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter;...
PP 8.2 Modify the program from PP 8.1 so that it works for numbers in the...
PP 8.2 Modify the program from PP 8.1 so that it works for numbers in the range between −25 and 25 THIS IS MY CODE! please edit this...Thank you import java.util.Scanner; public class Array { public static void main (String[] args) { Scanner scan = new Scanner (System.in); int[] integs = new int[51]; System.out.println("Enter integers 0-50 [enter -1 to quit]: "); int nums = scan.nextInt(); while (nums != -1 && nums>=0 && nums<=50) { integs[nums]++; nums = scan.nextInt(); } for...
I need to come up with the pseudocode for a min and max value finder for...
I need to come up with the pseudocode for a min and max value finder for an array, uses recursion. first I started writing a java program to get a better understanding but it seams not to work properly. also Im having trouble calculating the time efficiency for the code. public class Max_min {     /**      * @param args the command line arguments      */     static int Max;     static int Min;     static int index;     static...
All the code below is in the file CatFish.java. Complete the classes below. Keep it very...
All the code below is in the file CatFish.java. Complete the classes below. Keep it very simple. The code you write must produce the output shown at the bottom of the page. Read the whole problem first - how you follow later instructions may affect early choices. Assume you have any import atatements you need. You may not need all blank lines. interface Movers{ public void move(); //this method will print (see output below) } class Cat _____________________________{ _____________________________________ _____________________________________...
This file that has the outline of an iterator which will be used to return each...
This file that has the outline of an iterator which will be used to return each char within a String. Complete the hasNext() and next() methods to enable this iteration. In case it is of use, String has two methods which may provide useful -- size() (returning the number of chars it contains) and charAt() (returning the character at the location passed as an argument). import java.util.Iterator; import java.util.NoSuchElementException; public class CharacterIterator implements Iterator<Character> { /** The String whose characters...
Using JAVA For this assignment, you will analyze code that uses a file input stream and...
Using JAVA For this assignment, you will analyze code that uses a file input stream and a file output stream. Read through the linked Java™ code. In a Microsoft® Word document, answer the following questions: Could this program be run as is? If not, what is it lacking? Does this program modify the contents of an input stream? In what way? What are the results of running this code? ********************************************** CODE TO ANALYZE  ******************************************************** /********************************************************************** *   Program:   Datasort *   Purpose:   ...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...