Cs Problem Set Answers
Essay by addictatypically • December 19, 2015 • Course Note • 8,081 Words (33 Pages) • 1,157 Views
public class Problem3
/*
*Write a method (an instance method)
public boolean rowBingo(int c)
*that returns true if board has at least one row where no element in the row is the
*character given by the parameter c. Otherwise it returns false. (The first index of
*the array is the row index and the dimensions of the array are size by size.)
*/
{
public static void main(String[] asd)
{
Bingo bingo = new Bingo(4);//4 is the dimension of the square array
boolean found = bingo.rowBingo(4);
System.out.println(found);
}
}
class Bingo
{ // methods go here.....
int [ ][ ] board = {{4,8,3,9}, {1,2,4,4}, {4,8,3,4}, {3,3,4,5}};
int n;
public Bingo(int size)
{
n = size;
}
public boolean rowBingo(int x)
{
boolean noElements;
for(int r = 0; r < n; r++)
{
noElements = true;
for(int c = 0; c < n; c++)
{
if(board[r][c] == x)
{
noElements = false;
break;
}
}
if(noElements == true)
{
return true;
}
}
return false;
}
}
public class Problem4
/*
*
*Write a method (an instance method)
public boolean theSame()
*that returns true if at least one row in the array board is the same as the first row.
* Otherwise it returns false. For instance in
4 8 3 4
3 5 5 1
4 8 3 4
9 2 4 9
*The 3rd is the same as the first row, so the method returns true.
*/
{
public static void main(String[] asd)
{
Bingo bingo = new Bingo(4);//4 is the dimension of the square array
boolean found = bingo.theSame();
System.out.println(found);
}
}
class Bingo
{ // methods go here.....
int [ ][ ] board = {{4,8,3,4}, {1,2,4,4}, {4,8,3,9}, {3,3,4,5}};
int n;
public Bingo(int size)
{
n = size;
}
public boolean theSame()
{
boolean theSame;
for(int r = 1; r < n; r++)
{
theSame = true;
for(int c = 0; c < n; c++)
{
if(board[r][c] != board[0][c])
{
theSame = false;
break;
}
}
if(theSame == true)
{
return true;
}
}
return false;
}
}
//sorting Characters
public class Snort
{
public static void main(String[] asd)
{
final int DIM = 20;
Character[] x = new Character[DIM];
for(int j = 0; j < DIM; j++)
{
int num = (int)(26*Math.random() );
char letter = (char)('a' + num);
x[j] = letter ; //boxing
}
System.out.println("before sorting");
print(x);
Sort2.sort(x);
System.out.println("after sorting");
print(x);
}
...
...