logo Practice-It logo

repeatedSequence

Language/Type: Java arrays
Author: Helene Martin (on 2011/12/14)

Write a method named repeatedSequence that accepts two arrays of integers a1 and a2 as parameters and returns true if a2 is composed entirely of repetitions of a1 and false otherwise. For example, if a1 stores the elements {2, 1, 3} and a2 stores the elements {2, 1, 3, 2, 1, 3, 2, 1, 3}, the method would return true.

If the length of a2 is not a multiple of the length of a1, your method should return false. You may assume that both arrays passed to your method will have a length of at least 1.

The following table shows some calls to your method and their expected results:

Arrays Returned Value
int[] a1 = {2, 1};
int[] a2 = {2, 1, 2, 1, 2, 1};
repeatedSequence(a1, a2) returns true
int[] a3 = {2, 1, 3};
int[] a4 = {2, 1, 3, 2, 1, 3, 2};
repeatedSequence(a3, a4) returns false
int[] a5 = {23};
int[] a6 = {23, 23, 23, 23};
repeatedSequence(a5, a6) returns true
int[] a7 = {5, 6, 7, 8};
int[] a8 = {5, 6, 7, 8};
repeatedSequence(a7, a8) returns true
int[] a9 = {5, 6};
int[] a0 = {5, 6, 7, 5, 6, 5};
repeatedSequence(a9, a0) returns false

For full credit, do not modify the elements of the arrays passed in.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.