Question

JAVA HELP (ARRAYS)

Assignment 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) 1) Data members: none 2) Methods a. A method named displayContents that accepts an array of type int and prints their values to the b. A method named cemoveNegatives that accepts an array of type int, copies all its non-negative the new array should be the exact size required to hold all the non-negatives values of the c. A method named arrav2roduct that accepts two arrays of type intnamed x and y and returns a corresponding x and y elements, i.e., 2dArray[wndexLwndex] = XHo index] * ywndex]; console, with each array element on a separate line values into a new int array (and maintains their sequence), and returns the new array. Note that parameter array two-dimensional array. Each element in the two-dimensional array should hold the product of the 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 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: intx = {0, 1, 2, 3} or int® y = {-1,-2, 0,-3, 5, 7, 13);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class Module4 {
void displayContents(int arr[])//diplay content
{
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);
}
int[] removeNegatives(int arr[])//remove -ves
{int count_negatives=0;
for(int i=0;i<arr.length;i++)//count -ves
if(arr[i]<0)
count_negatives++;
int new_arr[]=new int[arr.length-count_negatives];//create array
int k=0;
for(int i=0;i<arr.length;i++)
if(arr[i]>=0)//if +ve then keep in new array
new_arr[k++]=arr[i];
return new_arr;   
}
int[][] arrayProduct(int x[] ,int y[])
{
int Array_2d[][]=new int[x.length][y.length];
for(int i=0;i<x.length;i++)//compute product
for(int j=0;j<y.length;j++)
Array_2d[i][j]=x[i]*y[j];
return Array_2d;
  
}
}

//test

public class test {//test Module4
public static void main(String args[])
{ int x[]={0,1,2,3},y[]={-1,-2,0,-3,5,7,13},z[][];
Module4 m=new Module4();
m.displayContents(x);
m.displayContents(y);
System.out.println("After removing -ves in array y");
m.displayContents(m.removeNegatives(y));
z=m.arrayProduct(x, y);
System.out.println("Array Product is:");
for(int i=0;i<x.length;i++)
{for(int j=0;j<y.length;j++)
System.out.print(z[i][j]+"\t");
System.out.println();}
}
}

Add a comment
Know the answer?
Add Answer to:
JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 3. Write Java methods to accomplish each of the following Write a method named simpleAry which...

    3. Write Java methods to accomplish each of the following Write a method named simpleAry which accepts and return nothing. It creates an array that can hold ten integers. Fill up each slot of the array with the number 113. Then, display the contents of the array on the screen. You must use a loop to put the values in the array and also to display them. Write a method named sury which accepts an array of type double with...

  • You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must...

    You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must include all the following described methods. Each of these methods should be public and static.Write a method named getFirst, that takes an Array of int as an argument and returns the value of the first element of the array. NO array lists. Write a method named getLast, that takes an Array of int as an argument and returns the value of the last element...

  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

  • Java Programming Assignment Write a class named 2DArrayOperations with the following static methods: getTotal . This...

    Java Programming Assignment Write a class named 2DArrayOperations with the following static methods: getTotal . This method should accept a two-dimensional array as its argument and return the total of all the values in the array. Write overloaded versions of this method that work with int , double , and long arrays. (A) getAverage . This method should accept a two-dimensional array as its argument and return the average of all the values in the array. Write overloaded versions of...

  • Hello! This is C++. Q3. Write a program Define a Super class named Point containing: An...

    Hello! This is C++. Q3. Write a program Define a Super class named Point containing: An instance variable named x of type int. An instance variable named y of type int. Declare a method named toString() Returns a string representation of the point. Constructor that accepts values of all data members as arguments. Define a Sub class named Circle. A Circle object stores a radius (double) and inherit the (x, y) coordinates of its center from its super class Point....

  • Write a full class definition for a class named Player , and containing the following members: A data member name of t...

    Write a full class definition for a class named Player , and containing the following members: A data member name of type string . A data member score of type int . A member function called setName that accepts a parameter and assigns it to name . The function returns no value. A member function called setScore that accepts a parameter and assigns it to score . The function returns no value. A member function called getName that accepts no...

  • Given the following construct a UML diagram for Java: Design a class named location for locating...

    Given the following construct a UML diagram for Java: Design a class named location for locating a maximal value and its location in a two-dimensional array. The class contains public data fields row, column, and maxValue that store the maximum value and its indices in a two-dimensional array with row and column as int types and maxValue as double type. Write the following method that returns the location of the largest element in two-dimensional array: Public static Location locateLargest(double[][] a)...

  • QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members:...

    QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members: • A private String data field named name for the name of the ship. • A private String data field named yearBuilt for the year that the ship was built. • A constructor that creates a ship with the specified name and the specified year that the ship was built. • Appropriate getter and setter methods. A toString method that overrides the toString method...

  • Java arrays Create method named repeatedN that takes two integer parameters named range and n and...

    Java arrays Create method named repeatedN that takes two integer parameters named range and n and returns an integer array Method should return an integer array that has numbers 0 - range repeated in order n times If range is less than or equal to 0; return an array that has 0 repeated n times Create a second method named printArray that takes an integer array as a parameter. The method should print out every element on the same line...

  • I need assistance writing the following Java class. Design a class named Location for locating a...

    I need assistance writing the following Java class. Design a class named Location for locating a maximal value and its location in a two-dimensional array. The class contains public data fields row, column, and maxValue that store the maximal value and its indices in a two-dimensional array with row and column as int types and maxValue as a double type. Write the following method that returns the location of the largest element in a two-dimensional array: public static Location locateLargest(double[][]...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT