Java Program 1. Define and create an array of 5 integers. The array elements sho
ID: 3594359 • Letter: J
Question
Java Program
1. Define and create an array of 5 integers. The array elements should be initialized to zero.
2. Define and create an array of integers with the values 5, 7, 8, 9 10, 12 using an initializer list. Print the array.
3. Given the Array below, replace the element at position 3 with the value, 99.
int [] list = {88, 0, 11, 22, 55, 77};
What is the value of list.length?
4. Consider the array below:
int [] list = {88, 0, 11, 22, 55, 77};
a. Print all the elements of list from first to last.
b. Print all the elements of list from last to first.
c. Print the element at position 2.
d. What will happen when we try to print list[6]?
5. How would you describe the algorithm for finding the min or max value in an array?
6. How does the loop change the array?
int [][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
for (int row=0; row < matrix.length; row++) {
for (int col = 0; col< matrix[0].length; col++) {
if (row == col)
matrix[row][col] = matrix[row][col] * 10;
} }
7. Consider the code below using the array given. What value is returned?
int [][] matrix = { {1, 2, 3},
{4, 5, 6} };
public static int doSomething (int [][] matrix)
{
int x = 0;
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
x = x + matrix[row][col]; }
}
return x;
}
8. Consider the code below using the array given. What prints?
public class Test {
public static void main(String[] args) {
System.out.println(doIt (3, 4)); ____________________
System.out.println(doIt (4, 3)); ____________________
}
public static int doIt (int a, int b) {
return 5 * a + b;
}
}
9.
A BaseballPlayer has a name, a number of hits, and a number of at-bats. A BaseballPlayer can be created with a name, or with all three data. Provide a getter and setter for any one instance variable (your choice of which). Also, provide a getBattingAverage() method that calculates and returns the BaseballPlayer's batting average which is the number of hits divided by the number of at-bats.
NOTE: The BaseballPlayer class is an object class (a class designed to make objects from), not a driver class. So, there will be no main method. Please feel free to write a driver class to test your BaseballPlayer class in BlueJ if you wish!
Explanation / Answer
1. Define and create an array of 5 integers. The array elements should be initialized to zero.
Answer: int arr[] = new int[5];
2. Define and create an array of integers with the values 5, 7, 8, 9 10, 12 using an initializer list. Print the array.
Answer: int values[] = new int[]{5, 7, 8, 9, 10, 12};
System.out.println(Arrays.toString(values))
3. Given the Array below, replace the element at position 3 with the value, 99.
-> list[3] = 99
int [] list = {88, 0, 11, 22, 55, 77};
What is the value of list.length?
List length will remain same :list.length = 6
4. Consider the array below:
int [] list = {88, 0, 11, 22, 55, 77};
a. Print all the elements of list from first to last.
for (int i = 0; i<list.length ; ++i)
System.out.println(list[i] + " " );
b. Print all the elements of list from last to first.
for (int i = list.length-1; i>=0 ; --i)
System.out.println(list[i] + " " );
c. Print the element at position 2.
System.out.println(list[1] );
d. What will happen when we try to print list[6]?
We will get Array index out of Bound
5. How would you describe the algorithm for finding the min or max value in an array?
Algorithm findMax (A)
START
max = Integer.MIN_VALUE
for i in A.length
if A[i] > max
max = A[i]
return max
END
6. How does the loop change the array?
int [][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
for (int row=0; row < matrix.length; row++) {
for (int col = 0; col< matrix[0].length; col++) {
if (row == col)
matrix[row][col] = matrix[row][col] * 10;
} }
Answer : If row and columns are equal then values get multiplied by 10 ,
int [][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
=> Final value of matrix is : { {10, 2, 3}, {4, 50, 6}, {7, 8, 90} };
7. Consider the code below using the array given. What value is returned?
int [][] matrix = { {1, 2, 3},
{4, 5, 6} };
public static int doSomething (int [][] matrix)
{
int x = 0;
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
x = x + matrix[row][col]; }
}
return x;
}
Answer: returns the sum of all the values in matrix , x = 6 + 10 + 5 = 21
8. Consider the code below using the array given. What prints?
public class Test {
public static void main(String[] args) {
System.out.println(doIt (3, 4)); _____15 + 4 = 19___________
System.out.println(doIt (4, 3)); _______20 + 3 = 23_____________
}
public static int doIt (int a, int b) {
return 5 * a + b;
}
}
Thanks, let me know if there is any concern.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.