8.18 Ch 8, Part 3: Tabular Output
Write this program using Eclipse. Comment and style the code according to CS 200 Style Guide. Submit the source code files (.java) below. Make sure your source files are encoded in UTF-8. Some strange compiler errors are due to the text encoding not being correct.
The program you are going to write will produce a String containing a tabular representation of a 2D String array. For the 2D arrays, assume that the first index is the row and the second index is the column.
Download the templates and put them into your Eclipse project to get started.
You are asked to implement 3 methods in the TablePrint.java file. The suggested order to complete the methods is: consturctLine, columnWidths, and, finally, constructTableString. The details of what these methods should do can be found in the method header comments.
Also, you are given a working test bench, TestTablePrint.java, to help you debug your code. It is not complete and you should add additional tests. In particular, add some tests involving ragged arrays.
/**
* A class that can be used to build a String for 2D String arrays
to display their contents in a
* tabular format. For the 2D arrays, the first index is taken to be
the rows and the second index is
* the columns.
*/
public class TablePrint {
/**
* Build a String of length len made up of only '-'
characters.
*
* @param len The number of '-' characters in the string.
* @return The String with len '-' characters.
*/
public static String constructLine(int len) {
return "";
}
/**
* For each column in table, find the maximum String length.
*
* Step 1: Determine the maximum number of columns in the table.
This code should work for
* ragged arrays.
* Step 2: Create a 1D integer array to store the maximum lengths
for each column.
* Step 3: Iterate through each cell, updating the maximum lengths
as required.
*
* @param table The table of strings, assumed to be organized as row
x column.
* @return A 1D integer array containing the maximum length String
in each column, following the
* same order of the columns. That is, the length of the maximum
length String found in
* column i of table is store in index i of the return array.
*/
public static int[] columnWidths(String[][] table) {
return null;
}
/**
* Build a String containing a tabular representation of the
contents of the 2D array table,
* where the data is assumed to be row x column. That is, the first
index is the row, and the
* second index is the column.
*
* Step 1: Determine the maximum length Strings in each
column.
* Step 2: Determine the length of the horizontal lines.
* Step 3: Build the String based on the data in table.
* - Before printing the array data, print out a horizontal line of
'-'
* - For each row in the array, the columns should be separated by "
| ". The first column will
* be preceded by "| " and the last column terminated by " |".
* - When firstRowHeaders is true, print out a horizontal line of
'-' after the first row.
* - After printing the array data, print out a horizontal line of
'-'
*
* Example:
* String[][] table = {
* {"a", "aa"},
* {"bbbb", "b", "bb"},
* {"c"}
* };
*
* would produce the String when firstRowHeaders is true:
* "------------------\n" +
* "| a | aa | |\n" +
* "------------------\n" +
* "| bbbb | b | bb |\n" +
* "| c | | |\n" +
* "------------------\n"
* and when firstRowHeaders is false:
* "------------------\n" +
* "| a | aa | |\n" +
* "| bbbb | b | bb |\n" +
* "| c | | |\n" +
* "------------------\n"
* .
*
* Note: Do not duplicate code: Be sure to use the constructLine and
columnWidths methods.
*
* @param table The 2D String array used to generate the tabular
representation.
* @param firstRowHeader A flag that indicates if the first row in
table is assumed to
* contain the headers.
* @return A String containing the tabular representation of
table.
*/
public static String constructTableString(String[][] table, boolean
firstRowHeaders) {
return "";
}
}
TestMethod
import java.util.Arrays;
public class TestTablePrint {
public static boolean testConstructLine() {
System.out.println("Running testConstructLine");
{
//Test 1 -- Length 0 line
if(!TablePrint.constructLine(0).equals("")) {
System.out.println("\tFailed Test 1 (length 0)");
return false;
}
}
{
//Test 2 -- Length 4
if(!TablePrint.constructLine(4).equals("----")) {
System.out.println("\tFailed Test 2 (length 4)");
return false;
}
}
System.out.println("Done testConstructLine");
return true;
}
public static boolean testColumnWidths() {
System.out.println("Running testColumnWidths");
{
//Test 1
String[][] testArr = {
{"a", "aa", "aaa"},
{"bbbb", "b", "bb"},
{"c", "ccc" , "c"}
};
int[] result = TablePrint.columnWidths(testArr);
int[] expected = {4, 3, 3};
if(!Arrays.equals(result, expected)) {
System.out.println("\tFailed Test 1: ");
System.out.println("\tExpected: " +
Arrays.toString(expected));
System.out.println("\tActual: " + Arrays.toString(result));
return false;
}
}
//Test 2 -- TO DO ragged array
System.out.println("Done testColumnWidths");
return true;
}
public static boolean testConstructTableString() {
System.out.println("Running testConstructTableString");
{
//Test 1, headers and no headers
String[][] testArr = {
{"a", "aa", "aaa"},
{"bbbb", "b", "bb"},
{"c", "ccc" , "c"}
};
String expOutput =
"--------------------\n" +
"| a | aa | aaa |\n" +
"| bbbb | b | bb |\n" +
"| c | ccc | c |\n" +
"--------------------\n";
String output = TablePrint.constructTableString(testArr,
false);
if(!expOutput.equals(output)) {
System.out.println("\tFailed Test 1, no headers:");
System.out.println("\tExpected: \n" + expOutput);
System.out.println("\tActual: \n" + output);
return false;
}
String expOutputHeaders =
"--------------------\n" +
"| a | aa | aaa |\n" +
"--------------------\n" +
"| bbbb | b | bb |\n" +
"| c | ccc | c |\n" +
"--------------------\n";
output = TablePrint.constructTableString(testArr, true);
if(!expOutputHeaders.equals(output)) {
System.out.println("\tFailed Test 1, headers:");
System.out.println("\tExpected: \n" + expOutputHeaders);
System.out.println("\tActual: \n" + output);
return false;
}
}
//Test 2 -- TO DO ragged array
System.out.println("Done testConstructTableString");
return true;
}
public static void main(String[] args) {
testConstructLine();
testColumnWidths();
testConstructTableString();
}
}
Here is the answer
/**
* A class that can be used to build a String for 2D String arrays
to display their contents in a
* tabular format. For the 2D arrays, the first index is taken to be
the rows and the second index is
* the columns.
*/
public class TablePrint {
/**
* Build a String of length len made up of only '-'
characters.
*
* @param len The number of '-' characters in the string.
* @return The String with len '-' characters.
*/
public static String constructLine(int len) {
String retstr = "";
for (int i = 0; i < len; i++) {
retstr = retstr + "-";
}
return retstr;
}
/**
* For each column in table, find the maximum String length.
*
* Step 1: Determine the maximum number of columns in the table.
This code should work for
* ragged arrays.
* Step 2: Create a 1D integer array to store the maximum lengths
for each column.
* Step 3: Iterate through each cell, updating the maximum lengths
as required.
*
* @param table The table of strings, assumed to be organized as row
x column.
* @return A 1D integer array containing the maximum length String
in each column, following the
* same order of the columns. That is, the length of the maximum
length String found in
* column i of table is store in index i of the return array.
*/
public static int[] columnWidths(String[][] table) {
int[] noOfcol = new int[table.length];
int strlen = 0;
int tmp = 0;
for(int i = 0; i < table.length; i++) {
// travarse a column
for(int j = 0; j < table[i].length; j++) {
// store the length of string of a column cell
tmp = table[j][i].length();
if(tmp > strlen) {
strlen = tmp;
}
}
// at the end of the loop strlen holds the max string length for
column i;
//System.out.println(strlen);
noOfcol[i] = strlen;
strlen = 0; // reset strlen for next column
}
return noOfcol;
}
/**
* Build a String containing a tabular representation of the
contents of the 2D array table,
* where the data is assumed to be row x column. That is, the first
index is the row, and the
* second index is the column.
*
* Step 1: Determine the maximum length Strings in each
column.
* Step 2: Determine the length of the horizontal lines.
* Step 3: Build the String based on the data in table.
* - Before printing the array data, print out a horizontal line of
'-'
* - For each row in the array, the columns should be separated by "
| ". The first column will
* be preceded by "| " and the last column terminated by " |".
* - When firstRowHeaders is true, print out a horizontal line of
'-' after the first row.
* - After printing the array data, print out a horizontal line of
'-'
*
* Example:
* String[][] table = {
* {"a", "aa"},
* {"bbbb", "b", "bb"},
* {"c"}
* };
*
* would produce the String when firstRowHeaders is true:
* "------------------\n" +
* "| a | aa | |\n" +
* "------------------\n" +
* "| bbbb | b | bb |\n" +
* "| c | | |\n" +
* "------------------\n"
* and when firstRowHeaders is false:
* "------------------\n" +
* "| a | aa | |\n" +
* "| bbbb | b | bb |\n" +
* "| c | | |\n" +
* "------------------\n"
* .
*
* Note: Do not duplicate code: Be sure to use the constructLine and
columnWidths methods.
*
* @param table The 2D String array used to generate the tabular
representation.
* @param firstRowHeader A flag that indicates if the first row in
table is assumed to
* contain the headers.
* @return A String containing the tabular representation of
table.
*/
public static String constructTableString(String[][] table, boolean
firstRowHeaders) {
// determine the parameter of constructLine() function needed in
constructing tables
// for this assume a table row, that contains data with maximum
clumn columnWidths
int len = 0;
int[] widths = columnWidths(table);
for (int i=0; i < widths.length; i++) {
len += widths[i]; // add length for all max column width
}
// The first column will be preceded by "| " and the last column
terminated by " |".
len += "| ".length() + " |".length();
//For each row in the array, the columns should be separated by " |
"
len += (widths.length -1 ) * (" | ".length());
//System.out.println("len " + len);
String tablestr = constructLine(len);
tablestr += "\n";
for (int i = 0; i < table.length; i++) {
tablestr = tablestr + "| ";
for (int j = 0; j< table[i].length; j++) {
tablestr += table[i][j];
// add the separater for cells in middle
if((j+1) != table[i].length)
tablestr += " | ";
}
tablestr = tablestr + " |\n";
if(i== 0 && firstRowHeaders) { // if first row is header
print header line
tablestr += constructLine(len) + "\n";
}
}
tablestr += constructLine(len) + "\n";
return tablestr;
}
}
8.18 Ch 8, Part 3: Tabular Output Write this program using Eclipse. Comment and style the...
I am currently using eclipse to write in java.
A snapshot of the output would be greatly appreciated to verify
that the program is indeed working. Thanks in advance for both your
time and effort.
Here is the previous exercise code:
/////////////////////////////////////////////////////Main
/*******************************************
* Week 5 lab - exercise 1 and exercise 2: *
* ArrayList class with search algorithms *
********************************************/
import java.util.*;
/**
* Class to test sequential search, sorted search, and binary search
algorithms
* implemented in...
I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...
/** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /** * Return whether k is in list. * Precondition: the elements of list are not null. * @param list the array to be searched. * @param k the number to search for. * @return true if k is an element of list, and false otherwise. */ public static boolean contains(Object[][] list, Object k) { return false; } /** * Create a String that...
Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public class Matrix3 { private double[][] matrix; /** * Creates a 3x3 matrix from an 2D array * @param v array containing 3 components of the desired vector */ public Matrix3(double[][] array) { this.matrix = array; } /** * Clones an existing matrix * @param old an existing Matrix3 object */ public Matrix3(Matrix3 old) { matrix = new double[old.matrix.length][]; for(int i = 0; i <...
JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet { /** * Searches through the ArrayList arr, from the first index to the last, returning an ArrayList * containing all the indexes of Strings in arr that match String s. For this method, two Strings, * p and q, match when p.equals(q) returns true or if both of the compared references are null. * * @param s The string...
Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...
please use eclipse
the sample output is incorrect
CPS 2231 Chapter 8 Lab 1 Spring 2019 1. Write a class, SumOrRows. The main method will do the following methods: a. Call a method, createAndFillArray which: defines a 2 dim array of 3 rows and 4 columns, prompts the user for values and stores the values in the array. b. Calls a method, printArray, which: prints the Array as shown below c. Cails a method, calcSums, which: returns à 1 dimensional...
Make the Sudoku algorithm for checking for duplicates to be Big -O of N, instead of N-squared. I.E. No nested for loops in rowIsLatin and colIsLatin. Only nested loop allowed in goodSubsquare. public class Sudoku { public String[][] makeSudoku(String s) { int SIZE = 9; int k = 0; String[][] x = new String[SIZE][SIZE]; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++)...
Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. 2. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in...
This is my code for a TicTacToe game. However, I don't know how to write JUnit tests. Please help me with my JUnit Tests. I will post below what I have so far. package cs145TicTacToe; import java.util.Scanner; /** * A multiplayer Tic Tac Toe game */ public class TicTacToe { private char currentPlayer; private char[][] board; private char winner; /** * Default constructor * Initializes the board to be 3 by 3 and...