Question

Specification Copy the program ValleyPeakPlateau.java to your computer and implement the method named valley_peak_plateau(int[]). The static...

Specification

Copy the program ValleyPeakPlateau.java to your computer and implement the method named valley_peak_plateau(int[]).

The static void valley_peak_plateau(int[] a) method prints to the standard output stream the valleys, peaks, and plateaus found in the int[] a parameter. Only arrays having two or more elements are processed.

The following are definitions for valley, peak, and plateau. Note: A plateau is when a number occurs three or more consecutive times in an array.

   note:
      int[] a ... an array of ints of named a
      int n ... the length of array a, where n ≥ 2 
      int i ... index into array a

   when i is 0
      a[i] is valley if a[i] less than a[i+1]
      a[i] is peak if a[i] greater than a[i+1]
      a[i] is plateau if a[i] equals a[i+1] and a[i+2]

   when 0 < i < n - 1
      a[i] is valley if a[i] is less than both a[i-1] and a[i+1]
      a[i] is peak if a[i] is greater than both a[i-1] and a[i+1]
      a[i] is plateau if a[i] equals both a[i-1] and a[i+1]
           note:  a[i-1] is the start of the plateau and the
                  plateau ends when the number changes (or end 
                  of array is reached) -- see the output if 
                  clarification is needed

   when i is n - 1
      a[i] is valley if a[i] less than a[i-1]
      a[i] is peak if a[i] greater than a[i-1]
      a[i] is plateau if a[i] equals a[i-1] and a[i-2]
/*
 * This program identifies the valleys, peaks, and plateaus
 * that are found in an array of ints.  
 *
 * @creator gdt
 * @created 02017.01.28
 * @updated 02019.01.04  morphed the assignment
 * @see http://azfoo.net/gdt/csc205/assignments/02019/vpp.shtml
 */

public class ValleyPeakPlateau {
   public static void main(String[] argv) {

      int[][] arrays = { 
         { 3, 5, 4, 4, 4, 4, 2, 6, 5, 5, 5, 5, 4, 4, 4, 4, 7, 2, 4, 6, 5 },
         { 9, 9, 9, 9, 9, 4, 7, 9, 9, 9, 9, 2, 9, 9, 9, 9, 9 },
         { 0, 5, 7, 7, 7, 4, 8, 7, 7, 7, 10, 2, 6 },
         { 2, 8, 8, 8, 7, 9, 9, 9, 9, 9, 3, },
         { 1, 9, 3, 5, 2, 7, 3, 5, 1, },
         { 2, 5, 3, 2, 1, 9, 7, 8, },
         { 4, 4, 7, 4, 4, 4, 2, },
         { 2, 5, 3, 3, 7, 2, },
         { 1, 1, 1, 2, 0, 9, },
         { 9, 0, 2, 1, 1, 1, },
         { 1, 2, 3, 3, 2, 1, },
         { 1, 2, 5, 9, 10, },
         { 4, 3, 2, 1, 0, },
         { 7, 7, 7, 7, },
         { 6, 5, 5, 5 },
         { 3, 3, 3, 9 },
         { 1, 3, 2, },
         { 3, 2, 1, },
         { 1, 2, },
         { 7, 5, },
         { 6, 6, },
         { 1, },
         { },
      };
      for (int i = 0; i < arrays.length; i++) 
         valley_peak_plateau(arrays[i]);
   }

   /*
    * TBI (To Be Implemented)...
    *
    * @see http://azfoo.net/gdt/csc205/assignments/02019/vpp.shtml
    */
   static void valley_peak_plateau(int[] a) {

   }

}

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

The Program's Output

The output of the program must match the following. {output file}

array: { 3, 5, 4, 4, 4, 4, 2, 6, 5, 5, 5, 5, 4, 4, 4, 4, 7, 2, 4, 6, 5, }
[0]=3 is a valley
[1]=5 is a peak
[2]=4 is a plateau
[3]=4 is a plateau
[4]=4 is a plateau
[5]=4 is a plateau
[6]=2 is a valley
[7]=6 is a peak
[8]=5 is a plateau
[9]=5 is a plateau
[10]=5 is a plateau
[11]=5 is a plateau
[12]=4 is a plateau
[13]=4 is a plateau
[14]=4 is a plateau
[15]=4 is a plateau
[16]=7 is a peak
[17]=2 is a valley
[19]=6 is a peak
[20]=5 is a valley

array: { 9, 9, 9, 9, 9, 4, 7, 9, 9, 9, 9, 2, 9, 9, 9, 9, 9, }
[0]=9 is a plateau
[1]=9 is a plateau
[2]=9 is a plateau
[3]=9 is a plateau
[4]=9 is a plateau
[5]=4 is a valley
[7]=9 is a plateau
[8]=9 is a plateau
[9]=9 is a plateau
[10]=9 is a plateau
[11]=2 is a valley
[12]=9 is a plateau
[13]=9 is a plateau
[14]=9 is a plateau
[15]=9 is a plateau
[16]=9 is a plateau

array: { 0, 5, 7, 7, 7, 4, 8, 7, 7, 7, 10, 2, 6, }
[0]=0 is a valley
[2]=7 is a plateau
[3]=7 is a plateau
[4]=7 is a plateau
[5]=4 is a valley
[6]=8 is a peak
[7]=7 is a plateau
[8]=7 is a plateau
[9]=7 is a plateau
[10]=10 is a peak
[11]=2 is a valley
[12]=6 is a peak

array: { 2, 8, 8, 8, 7, 9, 9, 9, 9, 9, 3, }
[0]=2 is a valley
[1]=8 is a plateau
[2]=8 is a plateau
[3]=8 is a plateau
[4]=7 is a valley
[5]=9 is a plateau
[6]=9 is a plateau
[7]=9 is a plateau
[8]=9 is a plateau
[9]=9 is a plateau
[10]=3 is a valley

array: { 1, 9, 3, 5, 2, 7, 3, 5, 1, }
[0]=1 is a valley
[1]=9 is a peak
[2]=3 is a valley
[3]=5 is a peak
[4]=2 is a valley
[5]=7 is a peak
[6]=3 is a valley
[7]=5 is a peak
[8]=1 is a valley

array: { 2, 5, 3, 2, 1, 9, 7, 8, }
[0]=2 is a valley
[1]=5 is a peak
[4]=1 is a valley
[5]=9 is a peak
[6]=7 is a valley
[7]=8 is a peak

array: { 4, 4, 7, 4, 4, 4, 2, }
[2]=7 is a peak
[3]=4 is a plateau
[4]=4 is a plateau
[5]=4 is a plateau
[6]=2 is a valley

array: { 2, 5, 3, 3, 7, 2, }
[0]=2 is a valley
[1]=5 is a peak
[4]=7 is a peak
[5]=2 is a valley

array: { 1, 1, 1, 2, 0, 9, }
[0]=1 is a plateau
[1]=1 is a plateau
[2]=1 is a plateau
[3]=2 is a peak
[4]=0 is a valley
[5]=9 is a peak

array: { 9, 0, 2, 1, 1, 1, }
[0]=9 is a peak
[1]=0 is a valley
[2]=2 is a peak
[3]=1 is a plateau
[4]=1 is a plateau
[5]=1 is a plateau

array: { 1, 2, 3, 3, 2, 1, }
[0]=1 is a valley
[5]=1 is a valley

array: { 1, 2, 5, 9, 10, }
[0]=1 is a valley
[4]=10 is a peak

array: { 4, 3, 2, 1, 0, }
[0]=4 is a peak
[4]=0 is a valley

array: { 7, 7, 7, 7, }
[0]=7 is a plateau
[1]=7 is a plateau
[2]=7 is a plateau
[3]=7 is a plateau

array: { 6, 5, 5, 5, }
[0]=6 is a peak
[1]=5 is a plateau
[2]=5 is a plateau
[3]=5 is a plateau

array: { 3, 3, 3, 9, }
[0]=3 is a plateau
[1]=3 is a plateau
[2]=3 is a plateau
[3]=9 is a peak

array: { 1, 3, 2, }
[0]=1 is a valley
[1]=3 is a peak
[2]=2 is a valley

array: { 3, 2, 1, }
[0]=3 is a peak
[2]=1 is a valley

array: { 1, 2, }
[0]=1 is a valley
[1]=2 is a peak

array: { 7, 5, }
[0]=7 is a peak
[1]=5 is a valley

array: { 6, 6, }

array: { 1, } (skipped)

array: { } (skipped)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// ValleyPeakPlateau.java

import java.util.Arrays;

/*

* This program identifies the valleys, peaks, and plateaus

* that are found in an array of ints.

*

* @creator gdt

* @created 02017.01.28

* @updated 02019.01.04 morphed the assignment

* @see http://azfoo.net/gdt/csc205/assignments/02019/vpp.shtml

*/

public class ValleyPeakPlateau {

       public static void main(String[] args) {

            

             int[][] arrays = {

                             { 3, 5, 4, 4, 4, 4, 2, 6, 5, 5, 5, 5, 4, 4, 4, 4, 7, 2, 4, 6, 5 },

                             { 9, 9, 9, 9, 9, 4, 7, 9, 9, 9, 9, 2, 9, 9, 9, 9, 9 },

                             { 0, 5, 7, 7, 7, 4, 8, 7, 7, 7, 10, 2, 6 },

                             { 2, 8, 8, 8, 7, 9, 9, 9, 9, 9, 3, },

                             { 1, 9, 3, 5, 2, 7, 3, 5, 1, },

                             { 2, 5, 3, 2, 1, 9, 7, 8, },

                             { 4, 4, 7, 4, 4, 4, 2, },

                             { 2, 5, 3, 3, 7, 2, },

                             { 1, 1, 1, 2, 0, 9, },

                             { 9, 0, 2, 1, 1, 1, },

                             { 1, 2, 3, 3, 2, 1, },

                             { 1, 2, 5, 9, 10, },

                             { 4, 3, 2, 1, 0, },

                             { 7, 7, 7, 7, },

                             { 6, 5, 5, 5 },

                             { 3, 3, 3, 9 },

                             { 1, 3, 2, },

                             { 3, 2, 1, },

                             { 1, 2, },

                             { 7, 5, },

                             { 6, 6, },

                             { 1, },

                             { },

                          };

            

             for (int i = 0; i < arrays.length; i++)

             {

                    System.out.println(" array : "+Arrays.toString(arrays[i]));

                    valley_peak_plateau(arrays[i]);

                    System.out.println();

             }

       }

             /*

             * TBI (To Be Implemented)...

             *

             * @see http://azfoo.net/gdt/csc205/assignments/02019/vpp.shtml

             */

             static void valley_peak_plateau(int[] a) {

                    if(a.length < 2 )

                           System.out.println("skipped");

                    else

                    {

                           int i,j;

                           for(i=0;i<a.length;)

                           {

                                 if(i == 0)

                                 {

                                        if(a[i] < a[i+1])

                                        {

                                               System.out.println("["+i+"]="+a[i]+" is a valley");

                                               i++;

                                        }else if(a[i] > a[i+1])

                                        {

                                               System.out.println("["+i+"]="+a[i]+" is a peak");

                                               i++;

                                        }     

                                        else

                                        {

                                               for(j=i+1;j<a.length;)

                                               {

                                                     if(a[i] != a[j])

                                                            break;

                                                     else

                                                            j++;

                                               }

                                               if(j-i >= 3)

                                               {

                                                     for(int k=i;k<j;k++)

                                                            System.out.println("["+k+"]="+a[k]+" is a plateau");

                                               }

                                               i=j;

                                        }

                                 }else if(i == a.length-1)

                                 {

                                        if(a[i] < a[i-1])

                                        {

                                               System.out.println("["+i+"]="+a[i]+" is a valley");

                                               i++;

                                        }else if(a[i] > a[i-1])

                                        {

                                               System.out.println("["+i+"]="+a[i]+" is a peak");

                                               i++;

                                        }else

                                               i++;

                                 }else {

                                        if((a[i] < a[i-1]) && (a[i] < a[i+1]))

                                        {

                                               System.out.println("["+i+"]="+a[i]+" is a valley");

                                               i++;

                                        }else if((a[i] > a[i-1]) && (a[i] > a[i+1] ))

                                        {     

                                               System.out.println("["+i+"]="+a[i]+" is a peak");

                                               i++;

                                        }else {

                                               for(j=i+1;j<a.length;)

                                               {

                                                     if(a[i] != a[j])

                                                            break;

                                                     else

                                                            j++;

                                               }

                                               if(j-i >= 3)

                                               {

                                                     for(int k=i;k<j;k++)

                                                            System.out.println("["+k+"]="+a[k]+" is a plateau");

                                               }

                                               i=j;

                                              

                                        }

                                 }

                           }

             }

       }

}

//end of program

Output:

Add a comment
Know the answer?
Add Answer to:
Specification Copy the program ValleyPeakPlateau.java to your computer and implement the method named valley_peak_plateau(int[]). The static...
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
  • Consider a non-empty int array ints. A contiguous subarray ints[ start .. start + len -1...

    Consider a non-empty int array ints. A contiguous subarray ints[ start .. start + len -1 ] (with starting index start and length len) is called a flat if all elements of that subarray are equal. Furthermore, such a subarray is called a plateau if it is flat and each of the elements ints[start -1] and ints[start + len] that immediately proceed/succeed the subarray are either nonexistent (i.e., out of array’s index range) or are strictly smaller than the elements...

  • You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement...

    You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement this method return new int[] {}; }    There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array. public static int[] convertIntegers(List<Integer> integers) Provided code import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class MatrixSearch { // This method converts a list of integers to an array...

  • public static int[] interleaveArray(int[] a, int[] b) Given two arrays, return the array which interleaves the...

    public static int[] interleaveArray(int[] a, int[] b) Given two arrays, return the array which interleaves the elements of the two arrays. The two arrays do not have to be the same length. For example, given a = [1, 2, 3] and b = [4, 5, 6, 7, 8], the method returns c = [1, 4, 2, 5, 3, 6, 7, 8] Parameters: a - given array b - given array Returns: the array which interleaves the elements of the two...

  • input: Task3: Given two int arrays, output true if the two arrays are identical and false...

    input: Task3: Given two int arrays, output true if the two arrays are identical and false if they are not. Input: task3 input Output: Array 1: 2 3 1 6 7 8 1 3 9 5 Array 2: 5 4 7 9 3 2 4 1 6 2 False 10 ܕ ܃ ܃ ܃ ܃ ܃ ܃ ܃ ܃ ܃ ܃ ܃ ܃ ܃ ܃ : : 7 s a

  • 1. What is the output of the following code segment? int array[] = { 8, 6,...

    1. What is the output of the following code segment? int array[] = { 8, 6, 9, 7, 6, 4, 4, 5, 8, 10 }; System.out.println( "Index Value" ); for ( int i = 0; i < array.length; i++ ) System.out.printf( "%d %d\n", i, array[ i ] ); 2. What is the output of the following code segment? char sentence[] = {'H', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u' }; String output = "The sentence...

  • For the following C# program: Let’s add one more user defined method to the program. This...

    For the following C# program: Let’s add one more user defined method to the program. This method, called ReverseDump is to output the values in the array in revere order (please note that you are NOT to use the Array.Reverse method from the Array class). The approach is quite simple: your ReverseDump method will look very similar to the Dump method with the exception that the for loop will count backwards from num 1 to 0 . The method header...

  • With a Scanner object created as follows, what method do you use to read an int...

    With a Scanner object created as follows, what method do you use to read an int value? Scanner input = new Scanner(System.in); A. input.int(); B. input.nextInt(); C. input.integer(); D. input.nextInteger(); What is the output of the following code? x = 0; if (x > 0) System.out.println("x is greater than 0"); else if (x < 0) System.out.println("x is less than 0"); else System.out.println("x equals 0"); A. x is greater than 0 B. x is less than 0 C. x equals 0...

  • Please do in Java as simply as possible :) Exercise #3: Design and implement a program...

    Please do in Java as simply as possible :) Exercise #3: Design and implement a program (name it ArrayMethods), that defines 4 methods as follows: int arrayMax(int[] arr) returns the maximum value in the an array int arrayMin(int[] arr) returns the minimum value in an array void arraySquared(int[] arr) changes every value in the array to its square (value²) void arrayReverse(int[] arr) reverses the array (for example: array storing 7   8   9 becomes 9   8   7 ) The program main...

  • I have a table that looks like this. 1234 5678 9101112 I need to use this...

    I have a table that looks like this. 1234 5678 9101112 I need to use this code public class Lab8 { public static void main(String argv[]) { int ar[][] = new int[3][4]; int n = 1; for (int i=0; i < ar.length; i++) { for (int j=0; j< ar[i].length; j++) { ar[i][j] = n; n++; } } // print out the elements of the array // left to right, top to bottom for (int i=0; i < ar.length; i++) {...

  • Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...

    Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...

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