Solved–Write a method that takes a two-dimensional integer array — Lab 10 –SOlution

$30.00 $19.00

This lab will help you to practice working with two-dimensional arrays. All parts of this lab must be part of a single java program – do not turn in more than one .zip file. Write a method that takes a two-dimensional integer array as input and returns a copy of the array with any negative…

You’ll get a: . zip file solution

 

 
Categorys:
Tags:

Description

5/5 – (2 votes)

This lab will help you to practice working with two-dimensional arrays. All parts of this lab must be part of a single java program – do not turn in more than one

.zip file.

  1. Write a method that takes a two-dimensional integer array as input and returns a copy of the array with any negative values replaced by zero. For instance, when called like this from main:

int[][] array1 = {{1, -4, -3, 6, 7}, {-14, 3, 5, 9}}; int[][] array2 = negativesToZero(array1);

array2 should have these elements:

1

0

0

6

7

0

3

5

9

0

array1 should be the same as it was before the method was called.

  1. Write a method that takes a square two dimensional array of characters and returns true if the characters along the upper left to lower right diagonal are all the same, and false otherwise. For instance, when called like this from main:

char[][] array3 = {

‘d’},

{‘a’, ‘b’, ‘c’,

{‘e’, ‘a’, ‘g’,

‘h’},

{‘i’,

‘j’,

‘a’,

‘l’},

{‘m’,

‘n’,

‘o’,

‘a’}};

System.out.println(“Diagonal check: ” + checkDiagonal1(array3));

the program should display:

Diagonal check: true

And when called like this:

char[][] array4 = {

‘d’},

{‘a’, ‘b’, ‘c’,

{‘e’, ‘a’, ‘g’,

‘h’},

{‘i’,

‘j’,

‘a’,

‘l’},

{‘m’,

‘n’,

‘o’,

‘p’}};

System.out.println(“Diagonal check: ” + checkDiagonal1(array4));

the program should display:

Diagonal check: false

  1. Do the same thing as in Problem 2, but for the lower left to upper right diagonal. For example, when called like this from main:

char[][] array5 = {

‘d’},

{‘a’, ‘b’, ‘c’,

{‘e’, ‘f’, ‘d’,

‘h’},

{‘i’,

‘d’,

‘k’,

‘l’},

{‘d’,

‘n’,

‘o’,

‘p’}};

System.out.println(“Other diagonal check: ” + checkDiagonal2(array5));

the program should produce the output:

Other diagonal check: true

And when called with this input:

char[][] array6 = {

‘d’},

{‘a’, ‘b’, ‘c’,

{‘e’, ‘f’, ‘x’,

‘h’},

{‘i’,

‘d’,

‘k’,

‘l’},

{‘d’,

‘n’,

‘o’,

‘p’}};

System.out.println(“Other diagonal check: ” + checkDiagonal2(array6));

the program should produce:

Other diagonal check: false

  1. Write a method that finds the median value within a two dimensional array of integers. Hint: you might want to copy the values into a single dimensional array and use Arrays.sort. Assuming your method is called like this from main:

int[][] array7 = {{1, -4, -3, 6, 7}, {-14, 3, 5, 9}}; System.out.println(“Median: ” + findMedian(array7));

it should produce the output

Median: 3.0

And if called with this input:

int[][] array8 = {{1, -4, -3, 6, 7}, {-14, 3, 5, 9, 2}}; System.out.println(“Median: ” + findMedian(array8));

it should produce the output

Median: 2.5

  1. Write a method that takes a square two-dimensional character array and reflects it across the upper left to lower right diagonal. For instance, if called like this from main:

char[][] array9 = {

{‘a’, ‘b’, ‘c’},

{‘d’, ‘e’, ‘f’},

{‘g’, ‘h’, ‘i’}

};

reflect(array9);

array9 would then contain the values

a

d

g

b

e

h

c

f

i

If called with this input:

char[][] array10 = {

{‘a’, ‘b’, ‘c’, ‘d’},

{‘e’, ‘f’, ‘g’, ‘h’},

{‘i’, ‘j’, ‘k’, ‘l’},

{‘m’, ‘n’, ‘o’, ‘p’}

};

reflect(array10);

array10 would then contain the values

a

e

i

m

b

f

j

n

c

g

k

o

d

h

l

p

Rubric

For each question (20 points total):

  • Computes the correct answer: 2 point

  • Complies with all directions: 1 point

  • Follows style guidelines and commented as appropriate: 1 point