Answered step by step
Verified Expert Solution
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 nu 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 twodimensional 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 of arrayD
is nonzero.
Precondition: is a valid row index in arrayD
Postcondition: arrayD is unchanged.
public static boolean isNonzeroRowint arrayD int
t to be implemented in part a
Returns the number of rows in array that contain all
nonzero values.
Postcondition: arrayD is unchanged.
public static int numNonzeroRows int arrayD
implementation not shown
Returns a new, possibly smaller, twodimensional array that
contains only rows from arrayD with no zeros, as described
in part b
Precondition: arrayD contains at least one column
and at least one row with no zeros.
Postcondition: arrayD is unchanged.
public static int resizeint arrayD
to be implemented in part b
a Write the method isNonzeroRow, which returns true if and only if all elements in row of a two
dimensional array array are not equal to zero.
For example, consider the following statement, which initializes a twodimensional array.
int arr
Sample calls to isNonzeroRow are shown below.
Complete the isNonzeroRow method.
b Write the method resize, which returns a new twodimensional array containing only rows from
array D with all nonzero 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 twodimensional array and calls the resize method.
int arr
;
int smaller ArrayResizer.resize;
When the code segment completes, the following will be the contents of smaller.
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, twodimensional array that
contains only rows from arrayD with no zeros, as described
in part b
Precondition: arrayD contains at least one column
and at least one row with no zeros.
Postcondition: arrayD is unchanged.
public static int resizeint arrayD
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