Write a class called StringCode with the following functions. (Do not change the
ID: 3890612 • Letter: W
Question
Write a class called StringCode with the following functions. (Do not change these function names or return types, else the tests will fail). public static String blowup(String str) Returns a version of the original string as follows: each digit 0-9 that appears in the original string is replaced by that many occurrences of the character to the right of the digit. So the string "a3tx2z yields "attttxzzz", and "12x" yields "2xxx". A digit not followed by a character (i.e. at the end of the string) is replaced by nothing (Keep the digit as is). public static int maxRun(String str) Given a string, returns the length of the largest run in the string. A "run" is a series of zero or more adjacent characters that are the same. So the max run of "xxyyyz" is 3, and the max run of "xyz" is 1. A junit test for the Matrix class has been provided under Files rightarrow Homework rightarrow HW01. Your class must pass all the tests in that file. Import static org.junit.Assert.*: import org.junit.Test: import java.util.*: /* EmailsTest - unit tests for the Emails class. */ public class MatrixTest { // Matrix Create @Test public void testMatrixCreate() { Matrix m = new Matrix(10, 4): assertEquals(10,m.getrow()): } @Test public void testMatrixString() { Matrix m = new Matrix(10, 4): m.setValueAt (0, 0, 10): m.setValueAt (0, 0, -10): System.out.println(m): } @Test public void testValueSet() { Matrix m = new Matrix(10, 4): m.setValueAt (0, 0, 10): m.setValueAt (0, 0, -10): assertEquals (10, m.getValueAt(0, 0)): } public void testAdd() { Matrix m1 = new Matrix (10, 4): m1.setValueAt(0, 0, 10): matrix m2 = new Matrix(10, 4): m2.setValueAt (0, 0, 10): Matrix m = Matrix.add(m1, m2): assertEquals (20, m.getValueAt(0, 0)): } @Test public void testMultiply() { Matrix m = new Matrix(10, 4): m.setValueAt(0, 0, 10): Matrix m1 = m.multiply(2): assertEquals(20, m1.getValueAt(0, 0)): } @Test public void testGetDiagonalSum() { Matrix m = new Matrix (10, 10): m.setValueAt (0, 0, 10): m.setValueAt(1, 1, -10): assertEquals (0, m.getDiagonalSum()): } }Explanation / Answer
import java.io.*;
class StringCode {
public static String blownup(String str){
String ans = "";
for (int i = 0; i<str.length(); i++){
if (str.charAt(i) >= '0' && str.charAt(i) <= '9'){
if ((str.charAt(i+1) < '0' || str.charAt(i+1) > '9') && (str.charAt(i-1) >= '0' && str.charAt(i-1) <= '9'))
ans = ans + str.charAt(i);
for (int j=0; j<Character.getNumericValue(str.charAt(i)); j++){
if (str.charAt(i+1) < '0' || str.charAt(i+1) > '9')
ans = ans + str.charAt(i+1);
}
if (i == str.length()-1)
ans = ans + str.charAt(i);
}
else {
ans = ans + str.charAt(i);
}
}
return ans;
}
public static int maxRun(String str){
int count = 0;
for (int i = 0; i<str.length(); i++){
int count1 = 1;
for (int j = i+1; j<str.length(); j++){
if (str.charAt(i) == str.charAt(j))
count1++;
else
break;
}
if (count1 > count)
count = count1;
}
return count;
}
}
public class DemoStringCode {
public static void main(String[] args){
System.out.println(StringCode.blownup("a3tx2z"));
System.out.println(StringCode.blownup("12x"));
System.out.println(StringCode.maxRun("xxyyyyz"));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.