Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA. Assume that the classes listed in the Java Quick Reference have

SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not nu11 and that
methods are called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed in
classes defined in that question. Writing significant amounts of code that can be replaced by a call to one
of these methods will not receive full credit.
This question involves manipulating a two-dimensional array of integers. You will write two static methods
of the ArrayResizer class, which is shown below.
public class ArrayResizer
/* Returns true if and only if every value in row r of array2D
is non-zero.
Precondition: r is a valid row index in array2D.
Postcondition: array2D is unchanged.
public static boolean isNonzeroRow(int[][] array2D, int r)
(/t to be implemented in part (a)*/)
/* Returns the number of rows in array 2D that contain all
non-zero values.
Postcondition: array2D is unchanged.
public static int numNonzeroRows (int[][] array2D)
(/* implementation not shown */}
/* Returns a new, possibly smaller, two-dimensional array that
contains only rows from array2D with no zeros, as described
in part (b).
Precondition: array2D contains at least one column
and at least one row with no zeros.
Postcondition: array2D is unchanged.
public static int[][] resize(int[][] array2D)
(/* to be implemented in part (b)*/}
(a) Write the method isNonzeroRow, which returns true if and only if all elements in row r of a two-
dimensional array array 2D are not equal to zero.
For example, consider the following statement, which initializes a two-dimensional array.
int[][] arr ={{2,1,0},
{1,3,2},
0,0,0},
{4,5,6}
Sample calls to isNonzeroRow are shown below.
Complete the isNonzeroRow method.
(b) Write the method resize, which returns a new two-dimensional array containing only rows from
array 2 D with all non-zero values. The elements in the new array should appear in the same order as the order in
which they appeared in the original array.
The following code segment initializes a two-dimensional array and calls the resize method.
int[][] arr ={{2,1,0},
{1,3,2},
{0,0,0},
{4,5,6}};
int[][] smaller = ArrayResizer.resize(arr);
When the code segment completes, the following will be the contents of smaller.
{{1,3,2},{4,5,6}}
A helper method, numNonzeroRows, has been provided for you. The method returns the number of rows in its two-
dimensional array parameter that contain no zero values.
Complete the resize method. Assume that isNonzeroRow works as specified, regardless of what you wrote in
part (a). You must use numNonZeroRows and isNonZeroRow appropriately to receive full credit.
/* Returns a new, possibly smaller, two-dimensional array that
contains only rows from array2D with no zeros, as described
in part (b).
Precondition: array2D contains at least one column
and at least one row with no zeros.
Postcondition: array2D is unchanged.
public static int[][] resize(int[][] array2D)
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

More Books

Students also viewed these Databases questions

Question

LO10.2 List the conditions required for purely competitive markets.

Answered: 1 week ago