Question
Question 1 Problems 1 3 refer to the following class meant to represent the spinner in a board game: class Spinner { private int numOptions;
Question 1
Problems 1 3 refer to the following class meant to represent the spinner in a board game:
class Spinner {
private int numOptions;
private int chosen;
public Spinner (int n) {
if (n >= 2 && n <= 20) {
numOptions = n;
}
spin ();
}
public void spin () {
chosen = (int)(Math.random () * numOptions) + 1;
}
}
Consider the following code:
Spinner s = new Spinner(17);
System.out.println(s);
The intention is for this code to print the value stored in chosen. For this to work correctly which of the following must be true?
The variable chosen must be declared as private.
A toString method must be added.
An equals method must be added.
A mutator for the variable method for chosen must be added.
The variable chosen must be declared as public.
---------------------------------------------------------------------------------------
Question 2 1 pts
Which of the following would be correct declarations for a default constructor?
I.
public Spinner() {
this(2);
}
II.
public Spinner() {
numOptions = n;
}
III.
public Spinner() {
numOptions = 2;
spin ();
}
III only
I only
II only
I, II and III
I and III only
------------------------------------------------------------------------------------------
Question 3.15
Consider the following code.
int[][] matrix = new int[4][5];
Suppose we want to initialize matrix to the following rows and columns.
0 1 2 3 4
4 3 2 1 0
0 1 2 3 4
4 3 2 1 0
Which of the options below correctly initializes matrix?
I.
for (int i = 0; i < matrix.length; i += 2) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = j;
matrix[i + 1][j] = j;
}
}
II.
for (int i = 0; i < matrix.length; i += 2) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = j;
matrix[i + 1][matrix[i].length - j - 1] = j;
}
}
III.
for (int i = 0; i < matrix.length; i += 2) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = j;
matrix[i + 1][matrix[i].length - j - 1] = i;
}
}
I only
II and III only
I, II and III
II only
III only
-------------------------------------------------------------------------------
Question 4.20
You need a method to find the maximum value in every row of an array. Which of the following methods works as intended?
I.
public static int[] findMaxList (int a[][]) {
int temp [] = new int [a.length];
for(int i = 0; i < a.length; i++)
{
int max = a[i][0];
for(int j = 0; j < a[0].length; j++) {
if (a[i][j] > max) {
max = a[0][j];
}
}
temp [i] = max;
}
return temp;
}
II.
public static int[] findMaxList (int a[][]) {
int temp [] = new int [a.length];
for(int i = 0; i < a.length; i++) {
int max = a[i][0];
for(int j =0; j < a[0].length; j++) {
if (a[i][j] > max) {
max = a[i][j];
}
}
temp [i] = max;
}
return temp;
}
III.
public static int[] findMaxList (int a[][]) {
int temp [] = new int [a.length];
for(int i = 0; i < a.length; i++) {
int max = a[i][0];
for(int j = 0; j < a[0].length; j++) {
if(a[i][j] > a[0][j]) {
max = a[i][j];
}
}
temp [i] = max;
}
return temp;
}
II only
I, II and III
III only
I only
II and III only
-----------------------------------------------------------------------------------
Question 5.13
Consider the following method intended to swap the first and last rows in a two-dimensional array:
public static void swapRow (int[][] a) {
/* missing code */
}
Which of the following correctly replaces /* missing code */?
None of the answers listed.
for (int k = 0; k < a[0].length; k++) {
int last = a.length;
int temp = a [0][k];
a[0][k] = a[last][k];
a[last][k] = temp;
}
for (int k = 0; k < a[0].length; k++) {
int last = a.length - 1;
int temp = a [0][k];
a[0][k] = a[last][k];
a[last][k] = temp;
}
for (int k = 0; k < a[0].length; k++) {
int last = a.length;
a[0][k] = a[last][k];
a[last][k] = a [0][k];
}
for (int k = 0; k < a[0].length; k++) {
int last = a.length - 1;
a[0][k] = a[last][k];
a[last][k] = a [0][k];
}
---------------------------------------------------------------------
3rd time posting a question trying to add some --- lines to help improve reading the questions: Taking suggestions on how to improve my question posting skills. To help furture people answer my questions.
And a Giant Thank you to the person(or group or something) who helps answer my question! :)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started