Tester File TwoDUtilTesterjava Write this program using JAVA
Tester File:
TwoDUtilTester.java
Write this program using JAVA for an intro java class
Solution
TwoDUtilTester.java
/**
* TwoDUtilTester has 3 methods public String shortest(), String lastColumn(), int howMany(char) which returns us shortest String in the 2d array, all strings in last column seperated by * and how many times a character appears in the array. These method works on the the 2D array provided.
*
* @Utkarsh Kumar
* @1/11/2016
*/
import java.io.*;
public class TwoDUtilTester
{
public static void main(String[] args)throws Exception
{
String[][] words = { {\"The\", \"big\", \"bad\",\"wolf\"},
{\"is\", \"at\", \"the\", \"door.\"},
{\"What\", \"should\", \"we\", \"do?\"}
};
String[][] words2 = { {\"To\", \"code\"},
{\"or\", \"not\"},
{\"to\", \"code\"},
{\"That\", \"is\"},
{\"the\", \"question\"}
};
TwoDUtil processor = new TwoDUtil(words);
System.out.println(processor.shortest());
System.out.println(\"Expected: is\");
System.out.println(processor.lastColumn());
System.out.println(\"Expected: wolf*door.*do?\");
System.out.println(processor.howMany(\'w\'));
System.out.println(\"Expected: 3\");
System.out.println(processor.howMany(\'x\'));
System.out.println(\"Expected: 0\");
TwoDUtil processor2 = new TwoDUtil(words2);
System.out.println(processor2.shortest());
System.out.println(\"Expected: To\");
System.out.println(processor2.lastColumn());
System.out.println(\"Expected: code*not*code*is*question\");
System.out.println(processor2.howMany(\'o\'));
System.out.println(\"Expected: 7\");
}
}
class TwoDUtil
{
String s[][];
int a;
int b;
TwoDUtil(String [][] word)
{
a=word.length;
b=word[0].length;
int i,j;
try
{
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
s[i][j]=word[i][j];
System.out.println(word[i][j]);
}}
}
catch(Exception e)
{}
}
public String shortest()
{
int i,j,l=0,m=0;
String n=\"\";
try
{
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
if(s[i][j].length()<s[l][m].length())
{
l=i;
m=j;
}
}}
n=s[l][m];
}
catch(Exception e)
{}
return n;
}
public String lastColumn()
{
String k=\"\";
int i;
try
{
for(i=0;i<a;i++)
{
k=k+s[i][b-1];
if(i!=(a-1))
k=k+\"*\";
}}
catch(Exception e)
{}
return k;
}
public int howMany(char c)
{
int i,j,k,l=0,count=0,z;
z=(int)c;
char x=\' \';
String d=\"\";
try
{
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
d=s[i][j];
for(k=0;k<d.length();k++)
{
x=d.charAt(k);
l=(int)x;
if((l==z)||(l==(z-26))||(l==(z+26)))
count++;
}
}
}
}
catch(Exception e)
{}
return count;
}
}



