Create a class named Module 4 containing the following data members and methods
ID: 3736354 • Letter: C
Question
Create a class named Module4 containing the following data members and methods (note that all data members and methods are for objects unless specified as being for the entire class)
When submitted, this class should not have a main method. If you wish to test your methods, you might write a simple class that creates a Module4 object and creates & receives arrays and displays their contents.
To avoid the tedium of entering array values by hand, you might also find is useful to create arrays in your code using contents in braces, which builds an array with the exact number of elements in the sequence you provide, such as:
int x = {0, 1, 2, 3} or int[] y = {-1, -2, 0, -3, 5, 7, 13};
Additional Constraints
The program should be implemented as a single file holding the public class Module4. Comments should be provided at the start of the class file (i.e., above the class definition) giving the class author’s name and the date the program was finished.
Explanation / Answer
public class Module4{
public void displayContents(int[] arr){
for (int i = 0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
}
public int[] removeNegatives(int[] arr){
int count = 0;
for (int i = 0; i < arr.length; i++){
if (arr[i] < 0)
count++;
}
int[] arr1 = new int[count];
for (int i = 0; i < arr.length; i++){
if (arr[i] < 0)
arr1[i] = arr[i];
}
return arr1;
}
public int[][] arrayProduct(int[] a, int[] b){
int arr[][] = new int[a.length][b.length];
for (int i = 0; i<a.length; i++){
for (int j = 0; j < b.length; j++){
arr[i][j] = a[i]*b[j];
}
}
return arr;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.