Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in Java that determines if a string is a series of up to 10 cons

ID: 3763767 • Letter: W

Question

Write a program in Java that determines if a string is a series of up to 10 consecutive sequence numbers or the number sequence contains the same number. The program must call for user input in which the numbers are separated by the dash character, e.g., “16-17-18-19”, “102-101-100-99-98-97”, “56-57-57”. The program should display “Correct consecutive sequence”, “Incorrect consecutive sequence”, “Pair of numbers found”, “Pair of numbers not found” and/or “Invalid Input”. The program must iteratively ask for user input until a terminating character is entered. The program must have at least 2 functions excluding the main() function. Be sure to comment each line of code.

Explanation / Answer

For consecutive

public static boolean contains(int[] set1, int[] set2) {

     OUTER:

     for (int i = 0; i < set1.length - set2.length; i++) {

         for (int j = 0; j < set2.length; j++) {

             if (set1[i + j] != set2[j])

                  continue OUTER;

         }

         return true;

     }

     return false;

}

To avoid a label you can use a method which might be clearer

public static boolean contains(int[] set1, int[] set2) {

     for (int i = 0; i < set1.length - set2.length; i++)

         if (!matches(set1, i, set2))

             return false;

     return true;

}

public static boolean matches(int[] set1, int off, int[] set2) {

     for (int j = 0; j < set2.length; j++)

         if (set1[off + j] != set2[j])

               return false;

     return true;

}

If it only needs to be in order

public static boolean contains(int[] set1, int[] set2) {

     for (int i = 0, j = 0; i < set1.length; i++)

         if (set1[i] == set2[j])

             if (++j >= set2.length)

                 return true;

     return false;

}

…………………………..

when your recursive logic completes, you have an IEnumerable<int> to iterate through. Right? Maybe not. string has methods, such as Join() that can make your "digit separator" string very explicit - if you're using .net 4+ you can use the Join<T>(string, IEnumerable<T>) overload:

A iterative, recursive and a straightforward way:

Here is a more functional approach using recursion and returns back the formatted string rather than writing to Console.

Here is another refactor.

For consecutive

public static boolean contains(int[] set1, int[] set2) {

     OUTER:

     for (int i = 0; i < set1.length - set2.length; i++) {

         for (int j = 0; j < set2.length; j++) {

             if (set1[i + j] != set2[j])

                  continue OUTER;

         }

         return true;

     }

     return false;

}

To avoid a label you can use a method which might be clearer

public static boolean contains(int[] set1, int[] set2) {

     for (int i = 0; i < set1.length - set2.length; i++)

         if (!matches(set1, i, set2))

             return false;

     return true;

}

public static boolean matches(int[] set1, int off, int[] set2) {

     for (int j = 0; j < set2.length; j++)

         if (set1[off + j] != set2[j])

               return false;

     return true;

}

If it only needs to be in order

public static boolean contains(int[] set1, int[] set2) {

     for (int i = 0, j = 0; i < set1.length; i++)

         if (set1[i] == set2[j])

             if (++j >= set2.length)

                 return true;

     return false;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote