Question

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 separated by spaces and place the cursor on the next line after printing all the elements

example:

int[] a = repeatedN(4, 2);     {0, 1, 2, 3, 4, 0, 1, 2, 3, 4}

int[] a2 = repeatedN(0, 3);   {0, 0, 0}

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

check out the solution and let me know if any queries through COMMENTS.

--------------------------------------------------------------------------

public class MyClass {
// main method
public static void main(String args[]) {
// method call and save the returned array
int[] a = repeatedN(4, 2);
// method call with returned array
printArray(a);
  
// remove comments for the below 2 lines for another method call
//int[] a2 = repeatedN(0, 3);
//printArray(a2);
  
} // main method ends
  
// method which returns integer array
public static int[] repeatedN(int range, int n) {
// finding size of array
int size = (range+1)*n;
// declare an array with required size
int[] arr = new int[size];
// initialize the index of newly created array
int k=0;
  
// if range <= 0 then array contains 0 in n times
if(range <= 0) {
for(int i=0; i<n; i++) {
arr[i] = 0;
}
}
// if range > 0 then array contains from 0-range in n times
else {
for(int i=0; i<n; i++) {
for(int j=0; j<=range; j++) {
arr[k] = j;
k++;
}
}
}
// return array
return arr;
  
} // method ends
  
// method which prints returned integer array
public static void printArray(int[] repeatedArray) {
// print the array in same line with spaces in between
for(int i=0; i<repeatedArray.length; i++) {
System.out.print(repeatedArray[i] + "\t");
}
// place the cursor in next line
System.out.println();
} // method ends
  
} // class ends
---------------------------------------------------------------

--------------

OUTPUT ::

Add a comment
Know the answer?
Add Answer to:
Java arrays Create method named repeatedN that takes two integer parameters named range and n and...
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
  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • 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 HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods...

    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...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • Write a method named factorial that accepts an integer n as a parameter and returns the...

    Write a method named factorial that accepts an integer n as a parameter and returns the factorial of n, or n!. A factorial of an integer is defined as the product of all integers from 1 through that integer inclusive. For example, the call of factorial(4) should return 1 2 3 4, or 24. The factorial of 0 and 1 are defined to be 1. You may assume that the value passed is non-negative and that its factorial can fit...

  • Java Array The method rotateLeft() takes in a reference a to an integer array and a...

    Java Array The method rotateLeft() takes in a reference a to an integer array and a positive integer n and it returns a new array whose contents is the contents of the input array rotated to the left n places. So each element a[i] of the input array should be placed at location b[i-n] of the returned array. If the index i-n is negative, then that index should "wrap" around to the end of the output array. For example, if...

  • Create a class named HW that has the main method. Leave the main method empty for...

    Create a class named HW that has the main method. Leave the main method empty for now. • Create a method named allPairs that takes an integer array parameter named a that contains only positive integers and does not return anything. The method should print out all pairs of numbers that have a sum that is prime. If there are no pairs that have a sum that is prime, print out "No pairs". Note that in the first example, (2,...

  • In this lab, you will create one class named ArrayFun.java which will contain a main method...

    In this lab, you will create one class named ArrayFun.java which will contain a main method and 4 static methods that will be called from the main method. The process for your main method will be as follows: Declare and create a Scanner object to read from the keyboard Declare and create an array that will hold up to 100 integers Declare a variable to keep track of the number of integers currently in the array Call the fillArray method...

  • 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...

  • JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named...

    JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named problem1. 2. Create a Java class named StringParser with the following: ApublicstaticmethodnamedfindIntegerthattakesaStringandtwocharvariables as parameters (in that order) and does not return anything. The method should find and print the integer value that is located in between the two characters. You can assume that the second char parameter will always follow the firstchar parameter. However, you cannot assume that the parameters will be different from...

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