COMP SCI 200 University of Wisconsin NestedPrinting Class Java Lab Report
Question Description
I need help fixing the bugs in this program I made. All the methods in the NestedPrinting class are done I just need help fixing the methods I made that return the wrong output the grader was not expecting and fix them to return the output that it does expect. The errors are in the pdf file attached and my code with the methods and to test them is in this message below. Thank you.
/**
* A class that can be used to build String representations of 1D, 2D, and 3D String arrays
* to display their contents.
*/
//NestedPrinting.java//
public class NestedPrinting {
/**
* Takes a one-line String of input and makes a new version of
* it which ends with a suffix and is of the given length.
* To make it the correct length, the original input
* should either be truncated (losing some characters)
* or it should be padded at the end with spaces (‘ ‘)
* before adding the suffix.
*
* @param input A single line String to be fixed
* @param suffix The String that the output should end with
* @param length The total length of the output, including the suffix
* @return A String of the given length which ends with the suffix
**/
public static String endWith(String input, String suffix, int length) {
String endWidth = “”;
//to give the end of input with one space until a new length is achieved.
for(int i = input.length(); i < length; i++) {
input = input + ” “;
}
//to get the required length of the input string
int requiredInputLength = length – suffix.length();
//to make the new string by adding the substring of input from 0
//to requiredInputLength and suffix
endWidth = input.substring(0, requiredInputLength) + suffix;
//return endWidth
return endWidth;
}
/**
* Returns a line of characters which consists of
* lineChar repeated length times.
* @param lineChar The character to create a line out of
* @param length The length of the returned String
* @return A String of the given length consisting only of lineChar
*/
public static String lineOf(char lineChar, int length) {
String lineOf = “”;
//to add lineChar to lineOf from 0 to length
for(int i = 0; i < length; i++) {
lineOf = lineOf + lineChar;
}
return lineOf;
}
/**
* Returns a single String as a representation of an array of Strings.
* The return should start with ‘{‘ and end with ‘}’. The elements
* of the array should be included without quotes and with commas
* after all but the last element of the array.
* There should be no newline at the end of the returned String.
*
* Example output:
* arrToString(new String[] { “hello”, “there”, “everyone” })
* returns: “{hello, there, everyone}”
*
* @param arr An array of Strings to be printed
* @return A String which has the array on a single line
*/
public static String arrToString(String[] arr) {
if(arr.length <= 0) {
return “”;
}
String arrToString1D = “{“;
//to append each array element besides the last with newString with a space
int i;
for( i = 0; i < arr.length – 1; i++) {
arrToString1D = arrToString1D + arr[i] + “, “;
}
//to append the last element
arrToString1D = arrToString1D + arr[i] + “}”;
return arrToString1D;
}
/**
* Returns a single String as a representation of an 2D array of Strings.
* The returned String should have arr.length+2 lines and it
* should end with a newline.
* Every line should start with the given prefix and
* end with the given suffix, but each line will have different contents.
*
* If adding the prefix + suffix would make the line longer
* than length, then the contents of the line should be truncated
* to allow for them to be added. You can assume that the
* length of prefix + suffix is always less than or equal
* to the length.
*
* The first line’s contents is just “{“.
* The final line’s contents is just “}”.
* The inner line’s contents are two spaces (” “) plus the
* String representation of a 1D array from arrToString(String[])
* plus a comma after all but the last element of the 2D array.
*
* Example Output (prefix is “_”, suffix is “!”, length is 15):
* _{ !
* _ {Hi, Guy}, !
* _ {2D} !
* _} !
*
* hint: use the arrToString(String[] arr) method you created
*
* @param arr The 2D array we’re trying to represent as a String
* @param prefix A String which should be at the start of
* every line of the output
* @param suffix A String which should be at the end of
* every line of the output
* @param length The length of every line of the output
*/
public static String arrToString(String[][] arr, String prefix, String suffix, int length) {
if(arr.length <= 0) {
return “”;
}
//to create the first line
String arrToString2D = endWith(prefix + “{“, suffix, length) + “n”;
//append all the elements from array except the last
int i;
for(i = 0; i < arr.length -1; i++) {
arrToString2D += endWith(prefix + arrToString(arr[i]), “, ” + suffix, length) + “n”;
}
//to append the last element
arrToString2D += endWith(prefix + arrToString(arr[i]), suffix, length) + “n”;
//to append the last line
arrToString2D += endWith(prefix + “}”, suffix, length) + “n”;
//to return arrToString
return arrToString2D;
}
/**
* Returns a single String as a representation of an 3D array of Strings.
* EVERY LINE should start with the given prefix and
* end with the given suffix, but each line will have different contents.
*
* The first line’s contents is just “{“.
* The final line’s contents is just “}”.
* Between them will be a blocks of lines for each 2D array which
* should be created using arrToString(String[][]). Every line in each block
* should have two additional spaces (” “) after the prefix.
* Between every block, there should be a divider which is a single
* line which is just the divider character repeated to fit the length.
*
* Example Output (prefix is “_”, suffix is “!?”,
* divider is ‘-“, length is 15):
* _{ !?
* _ { !?
* _ {Hi, Guy!?
* _ {3D} !?
* _ } !?
* _————!?
* _ { !?
* _ {Nope} !?
* _ } !?
* _} !?
*
*
* hint: use the arrToString(String[][] arr, String prefix, String suffix, int length) method you created
* @param arr The 3D array we’re trying to represent as a String
* @param prefix A String which should be at the start of
* every line of the output
* @param suffix A String which should be at the end of
* every line of the output
* @param divider A character to be repeated to divide blocks
* of 2D arrays
* @param length The length of every line of the output
* @return A String representing the 3D array
*/
public static String arrToString(String[][][] arr, String prefix, String suffix, char divider, int length) {
String arrToString3D = “”;
int i;
// to append all the elements from array
for (i = 0; i < arr.length; i++) {
arrToString3D += arrToString(arr[i], prefix, suffix, length);
arrToString3D += lineOf(divider, length) + “n”;
}
// return newString
return arrToString3D;
}
/**
* Represents 3D array inside a box.
* The left and right variables represent the sides of the box
* and the divider is both the top and bottom (including corners).
*
* Example Output (left is “_”, right is “!?”,
* divider is ‘-“, length is 15):
* —————
* _{ !?
* _ { !?
* _ {Hi, Guy!?
* _ {3D} !?
* _ } !?
* _————!?
* _ { !?
* _ {Nope} !?
* _ } !?
* _} !?
* —————
*
* @param arr The 3D array we’re trying to represent as a String
* @param left A String which should be at the start of
* every line of the 3D array representation
* @param right A String which should be at the end of
* every line of the 3D array representation
* @param divider A character to be repeated to divide blocks
* of 2D arrays and fill the top and bottom of the box
* @param length The length of every line of the output
* @return A String representing the boxed 3D array
*/
public static String arrInBox(String[][][] arr, String left, String right, char divider, int length) {
// create first line
String arrInBox = lineOf(divider, length) + “n”;
arrInBox += endWith(left, right, length) + “n”;
arrInBox += arrToString(arr, left, right, divider, length) + “n”;
// return newString
return arrInBox;
}
}
//TestNestedPrinting.java//
import java.util.Arrays;
public class TestNestedPrinting {
public static boolean testEndWith() {
System.out.println(“Running testEndWith”);
// Test 1 — Length 0 line
if(!NestedPrinting.endWith(“”, “!”, 2).equals(” !”)) {
System.out.println(“tFailed Test 1 (padding)”);
return false;
}
// Test 2 — Length 4
if(!NestedPrinting.endWith(“XY”, “Z”, 2).equals(“XZ”)) {
System.out.println(“tFailed Test 2 (truncating)”);
return false;
}
System.out.println(“Done testEndWith”);
return true;
}
public static boolean testLineOf() {
System.out.println(“Running testLineOf”);
// Test 1 — Length 0 line
if(!NestedPrinting.lineOf(‘X’, 0).equals(“”)) {
System.out.println(“tFailed Test 1 (length 0)”);
return false;
}
// Test 2 — Length 4
if(!NestedPrinting.lineOf(‘-‘, 4).equals(“—-“)) {
System.out.println(“tFailed Test 2 (length 4)”);
return false;
}
System.out.println(“Done testLineOf”);
return true;
}
public static boolean testArrToString1D() {
System.out.println(“Running testArrToString1D”);
// Test 1
String[] arr1 = new String[] { “hello”, “there”, “everyone” };
String exp = “{hello, there, everyone}”;
String actual = NestedPrinting.arrToString(arr1);
if (!actual.equals(exp)) {
System.out.println(“tFailed Test 1”);
return false;
}
// Test 2 – TODO: Add additional test(s)
String[] arr2 = new String[] { “Welcome”, “to”, “Java”, “World”, “Good”, “Day!” };
String exp2 = “{Welcome, to, Java, World, Good, Day!}”;
String actual2 = NestedPrinting.arrToString(arr2);
if (!actual2.equals(exp2))
{
System.out.println(“tFailed Test 2”);
return false;
}
System.out.println(“Done testArrToString1D”);
return true;
}
public static boolean testArrToString2D() {
System.out.println(“Running testArrToString2D”);
// Test 1
String[][] arr1 = new String[][] {
{ “Hi”, “Guy” },
{ “2D” }
};
String exp =
“START{ ENDn”
+ “START {Hi, Guy},ENDn”
+ “START {2D} ENDn”
+ “START} ENDn”;
String actual = NestedPrinting.arrToString(arr1, “START”, “END”, 20);
if (!actual.equals(exp)) {
System.out.println(“t Failed Test 1”);
return false;
}
// Test 2 – TODO: Add additional test(s)
String[][] arr2 = new String[][] { { “Working”, “with”, “2D” }, { “array”, “in” }, {“Java”} };
String exp2 = “START{ ENDn” + “START {Working, with, 2D},ENDn”
+ “START {array, in},ENDn” + “START {Java} ENDn” +”START} ENDn”;
String actual2 = NestedPrinting.arrToString(arr2, “START”, “END”, 20);
if (!actual2.equals(exp2))
{
System.out.println(“t Failed Test 2”);
return false;
}
System.out.println(“Done testArrToString2D”);
return true;
}
public static boolean testArrToString3D() {
System.out.println(“Running testArrToString3D”);
// Test 1
String[][][] arr1 = new String[][][] {
{
{ “Hi”, “Guy” },
{ “3D” }
},
{
{ “Nope” }
}
};
String exp =
“_{ !?n”
+ “_ { !?n”
+ “_ {Hi, Guy!?n”
+ “_ {3D} !?n”
+ “_ } !?n”
+ “_————!?n”
+ “_ { !?n”
+ “_ {Nope} !?n”
+ “_ } !?n”
+ “_} !?n”;
String actual = NestedPrinting.arrToString(arr1, “_”, “!?”, ‘-‘, 15);
if (!actual.equals(exp)) {
System.out.println(“t Failed Test 1”);
return false;
}
System.out.println(“Done testArrToString3D”);
return true;
}
public static boolean testArrInBox() {
System.out.println(“Running testArrInBox”);
// Test 1
String[][][] arr1 = new String[][][] {
{
{ “Hi”, “Guy” },
{ “3D” }
},
{
{ “Nope” }
}
};
String exp =
“—————n”
+ “_{ !?n”
+ “_ { !?n”
+ “_ {Hi, Guy!?n”
+ “_ {3D} !?n”
+ “_ } !?n”
+ “_————!?n”
+ “_ { !?n”
+ “_ {Nope} !?n”
+ “_ } !?n”
+ “_} !?n”
+ “—————n”;
String actual = NestedPrinting.arrInBox(arr1, “_”, “!?”, ‘-‘, 15);
if (!actual.equals(exp)) {
System.out.println(“t Failed Test 1”);
return false;
}
System.out.println(“Done testArrInBox”);
return true;
}
public static void main(String[] args) {
testEndWith();
testLineOf();
testArrToString1D();
testArrToString2D();
testArrToString3D();
testArrInBox();
}
}
"Place your order now for a similar assignment and have exceptional work written by our team of experts, guaranteeing you "A" results."