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...
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;...
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 _____________________________{ _____________________________________ _____________________________________...
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 =...
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)...
1. you will just write your name in bytes - once in UTF8 bytes, once in...
1. you will just write your name in bytes - once in UTF8 bytes, once in UTF16 bytes. Name :Andrew Submit: 1. Your .java file 2. A screenshot showing your code ran and gave the right output. ---- /** * In this program you will write your first name in bytes in 2 different encodings. * Then convert the byte array to a String and print it out. * * TODO in lecture: show students how I google to find...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1...
Write a for loop that prints: 1 2 ... userNum Ex: userNum = 4 prints: 1 2 3 4 import java.util.Scanner; public class ForLoops { public static void main (String [] args) { int userNum; int i; Scanner input = new Scanner(System.in); userNum = input.nextInt(); for (/* Your code goes here */) { System.out.print(i + " "); } } } Need to fill out the "for" solved in JAVA
import java.util.Scanner; import java.util.Random; public class ScoreDice { private Random r; public ScoreDice(long seed) { r...
import java.util.Scanner; import java.util.Random; public class ScoreDice { private Random r; public ScoreDice(long seed) { r = new Random(seed); } public int rollD6() { return r.nextInt(6) + 1; } public int score() { int roll1 = rollD6(); int roll2 = rollD6(); return scoreWithNumbers(roll1, roll2); }    // You will need to write a static method named scoreWithNumbers. // scoreWithNumbers returns a score based on the values // of its inputs, as such: // // - If both inputs are 1...
Download the attached .java file. Run it, become familiar with its processes. Your task is to...
Download the attached .java file. Run it, become familiar with its processes. Your task is to turn TemperatureConversion into GUI based program. it should, at the least, perform similar functions as their text output versions. The key factor to remember is that the workings should remain the same (some tweaks may be necessary) between text and GUI programs, while the means pf visual presentation and user interaction changes. You must properly document, comment, indent, space, and structure both programs. import...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT