Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
bartleby

Videos

Textbook Question
Book Icon
Chapter 22, Problem 22.1PE

Program to display maximum consecutive increasingly ordered substring

Program Plan:

  • Create the class “Exercise22_01”.
  • In the main() function,
    • Read the input object to read the input from user.
    • Call the function maxConsecutivesSortedSubstring()to print the maximum ordered substring.
  • In the maxConsecutivesSortedSubstring(),
    • Initially assign the maximum length of the substring.
    • Use for() loop to execute the whole length of the string.
      • Check the position of character at “i” and “i-1” and compare the values.
        • If it is lesser, assign the “i” to “current”.
      • If the condition not satisfies, then increment the length of maximum consecutive length.
      • Use for() loop to iterate the string values.
      • Check whether the current length of string is less than the largest string.
      • If yes, then assign the current element length to largest subsequence element.
      • Construct the character sequence using while() loop at the end of the string.
      • Return the string.
  • In the maxConsecutivesSortedSubstring1(),
    • Use the for loop to iterate the whole length of string.
      • Check the position of character at “i” and “i-1” and compare the values.
      • Return the sorted substring.

This program reads the input from user and displays the maximum consecutive increasingly ordered substring.

Program:

//Declare the class Exercise22_01

public class Exercise22_01 {

       //Declare the main function

       public static void main(String[] args) {

          //Create the input object

           java.util.Scanner input = new

           java.util.Scanner(System.in);

          //Read the string

          System.out.print("Enter a string: ");

          String s = input.nextLine();

          /*Call the function maxConsecutiveSortedSubstring() to print the maximum consecutive sorted substring*/

          System.out.println("Maximum consecutive substring

            is " + maxConsecutiveSortedSubstring(s));

        }

        /*Define the function maxConsecutiveSortedSubstring()*/

        public static String

            maxConsecutiveSortedSubstring(String s) {

          //Declare the array and assign the length of array

          int[] maxConsecutiveLength = new int[s.length()];

          //Assign the variable as 0

          int current = 0;

          //Execute the for loop until the condition fails

          for (int i = 1; i < s.length(); i++) {

             /*Check whether the character is smaller than 

             the current character stored in string*/

            if (s.charAt(i) <= s.charAt(i - 1)) {

             //Assign the “i” value to current variable

             current = i;

           } else {

             /*Execute the for loop until the condition fails*/

             for (int j = i - 1; j >= current; j--)

             //Increment the sequence of length

               maxConsecutiveLength[j]++;

           }

         }

         //Assign the length of sequence at index “i”

         int currentMaxLength = maxConsecutiveLength[0];

         int index = 0;

         //Execute the for loop until the length of string

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

             //Check whether the condition is true

             if (maxConsecutiveLength[i] > currentMaxLength)

             {

                /*Assign the maximum sequence length to current length */

                currentMaxLength = maxConsecutiveLength[i];

                //Assign the index position

                index = i;

           }

         }

         //Return the substring

         return s.substring(index, index + currentMaxLength + 1);

       }

     //Define the function for the sorted substring

     public static String

     maxConsecutiveSortedSubstring1(String s) {

          //Assign the current length

          int currentMaxLength = 1;

          //Assign the last index of sorted substring

          int lastIndexOfMaxConsecutiveSortedSubstring = 0;

          //Assign the possible length

          int possibleMaxLength = 1;

          //Execute the for loop until it fails

          for (int i = 1; i < s.length(); i++) {

               //Check the position of the string

               if (s.charAt(i) > s.charAt(i - 1)) {

                    //Check the condition

                    if

                    (lastIndexOfMaxConsecutiveSortedSubstri

                    ng == i - 1) {

                         /* Add the max consecutive substring*/

                         currentMaxLength++;

                         /*Add the index of max consecutive substring*/

                         lastIndexOfMaxConsecutiveSortedSub

                         string++;

                    //If condition not satisfies

                    } else {

                         //Increment the length

                         possibleMaxLength++;

                         //Check the condition

                         if (possibleMaxLength >

                         currentMaxLength) {

                              /*Assign the possible length to current maximum length*/

                              currentMaxLength =

                              possibleMaxLength;

                              /*Assign the index of the string*/

                              lastIndexOfMaxConsecutiveSort

                              edSubstring = i;

                              possibleMaxLength = 1;

                         }

                    }

               }

          }

          //Return the sorted substring

          return

          s.substring(lastIndexOfMaxConsecutiveSortedSubstr

          ing - currentMaxLength + 1,lastIndexOfMaxConsecutiveSortedSubstring + 1);

     }

}

Running time complexity:

The above program executes in Ο ( n 2 ) because the loop gets iterated two times consecutively to determine the length of the string. Therefore, the running time is Ο ( n 2 ) .

Sample Output:

Enter a string: abcabcdgabmnsxy

Maximum consecutive substring is abmnsxy

Expert Solution & Answer
Check Mark
Program Plan Intro

Program to display maximum consecutive increasingly ordered substring

Program Plan:

  • Create the class “Exercise22_01”.
  • In the main() function,
    • Read the input object to read the input from user.
    • Call the function maxConsecutivesSortedSubstring()to print the maximum ordered substring.
  • In the maxConsecutivesSortedSubstring(),
    • Initially assign the maximum length of the substring.
    • Use for() loop to execute the whole length of the string.
      • Check the position of character at “i” and “i-1” and compare the values.
        • If it is lesser, assign the “i” to “current”.
      • If the condition not satisfies, then increment the length of maximum consecutive length.
      • Use for() loop to iterate the string values.
      • Check whether the current length of string is less than the largest string.
      • If yes, then assign the current element length to largest subsequence element.
      • Construct the character sequence using while() loop at the end of the string.
      • Return the string.
  • In the maxConsecutivesSortedSubstring1(),
    • Use the for loop to iterate the whole length of string.
      • Check the position of character at “i” and “i-1” and compare the values.
      • Return the sorted substring.
Program Description Answer

This program reads the input from user and displays the maximum consecutive increasingly ordered substring.

Explanation of Solution

Program:

//Declare the class Exercise22_01

public class Exercise22_01 {

  //Declare the main function

  public static void main(String[] args) {

     //Create the input object

java.util.Scanner input = new

java.util.Scanner(System.in);

    //Read the string

    System.out.print("Enter a string: ");

    String s = input.nextLine();

/*Call the function maxConsecutiveSortedSubstring() to print the maximum consecutive sorted substring*/

    System.out.println("Maximum consecutive substring

is " + maxConsecutiveSortedSubstring(s));

  }

/*Define the function maxConsecutiveSortedSubstring()*/

  public static String

maxConsecutiveSortedSubstring(String s) {

    //Declare the array and assign the length of array

    int[] maxConsecutiveLength = new int[s.length()];

    //Assign the variable as 0

    int current = 0;

    //Execute the for loop until the condition fails

    for (int i = 1; i < s.length(); i++) {

/*Check whether the character is smaller than 

the current character stored in string*/

      if (s.charAt(i) <= s.charAt(i - 1)) {

   //Assign the “i” value to current variable

        current = i;

      } else {

/*Execute the for loop until the condition fails*/

        for (int j = i - 1; j >= current; j--)

//Increment the sequence of length

          maxConsecutiveLength[j]++;

      }

    }

    //Assign the length of sequence at index “i”

    int currentMaxLength = maxConsecutiveLength[0];

    int index = 0;

    //Execute the for loop until the length of string

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

 //Check whether the condition is true

      if (maxConsecutiveLength[i] > currentMaxLength)

 {

/*Assign the maximum sequence length to current length */

        currentMaxLength = maxConsecutiveLength[i];

   //Assign the index position

        index = i;

      }

    }

    //Return the substring

return s.substring(index, index + currentMaxLength + 1);

  }

//Define the function for the sorted substring

public static String

maxConsecutiveSortedSubstring1(String s) {

//Assign the current length

int currentMaxLength = 1;

//Assign the last index of sorted substring

int lastIndexOfMaxConsecutiveSortedSubstring = 0;

//Assign the possible length

int possibleMaxLength = 1;

//Execute the for loop until it fails

for (int i = 1; i < s.length(); i++) {

//Check the position of the string

if (s.charAt(i) > s.charAt(i - 1)) {

//Check the condition

if

(lastIndexOfMaxConsecutiveSortedSubstri

ng == i - 1) {

/* Add the max consecutive substring*/

currentMaxLength++;

/*Add the index of max consecutive substring*/

lastIndexOfMaxConsecutiveSortedSub

string++;

//If condition not satisfies

} else {

//Increment the length

possibleMaxLength++;

//Check the condition

if (possibleMaxLength >

currentMaxLength) {

/*Assign the possible length to current maximum length*/

currentMaxLength =

possibleMaxLength;

/*Assign the index of the string*/

lastIndexOfMaxConsecutiveSort

edSubstring = i;

possibleMaxLength = 1;

}

}

}

}

//Return the sorted substring

return

s.substring(lastIndexOfMaxConsecutiveSortedSubstr

ing - currentMaxLength + 1,lastIndexOfMaxConsecutiveSortedSubstring + 1);

}

}

Running time complexity:

The above program executes in Ο(n2) because the loop gets iterated two times consecutively to determine the length of the string. Therefore, the running time

is Ο(n2) .

Sample Output

Enter a string: abcabcdgabmnsxy

Maximum consecutive substring is abmnsxy

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Add a function to get the CPI values from the user and validate that they are greater than 0. 1. Declare and implement a void function called getCPIValues that takes two float reference parameters for the old_cpi and new_cpi. 2. Move the code that reads in the old_cpi and new_cpi into this function. 3. Add a do-while loop that validates the input, making sure that the old_cpi and new_cpi are valid values. + if there is an input error, print "Error: CPI values must be greater than 0." and try to get data again. 4. Replace the code that was moved with a call to this new function. - Add an array to accumulate the computed inflation rates 1. Declare a constant called MAX_RATES and set it to 20. 2. Declare an array of double values having size MAX_RATES that will be used to accumulate the computed inflation rates. 3. Add code to main that inserts the computed inflation rate into the next position in the array. 4. Be careful to make sure the program does not overflow the array. - Add a…
Computer Science C# Programming   Object Orientation   Please use Arrays or ArrayLists where appropriate   Implement a menu driven program that has some operations for a food truck. A food truck has multiple food items as well as a menu which contains a selection of the food items which will be sold for that day. A maximum of 10 food items can appear on the menu for any day. For each food item keep a code, description, category, price and quantity in stock (code, description and category are not usually changed).   Create a comma delimited text file called "items.txt" for 15 or more food items, indicating the item's description, category, price and quantity in stock.   Read all the food items from the text file “Items.txt”   Display all the food items available for this food truck.   Continuously prompt the user for a food item’s code to be placed on the menu until the menu is full or the user enters -1. Note that only food items which have a positive quantity in stock may be added to…
Purpose of this assignment: To allow student to be able to implement a Java-based application by implementing specific algorithms. Instruction: This is an individual assignment. You have to use Java to load data from text file. The text file contains data of customer. You program will be able to perform the following: • Two sorting algorithms are implemented. The user is allow to choose the desire sorting algorithm. Then, it will show the sequence of sorting in step-by-step. Finally, it will display the sort result. Search function is simplement. It will allow the program to search specific customer data based on the usre input. The program will display the searching steps until the intended data is found.

Chapter 22 Solutions

Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
  • Text book image
    Microsoft Visual C#
    Computer Science
    ISBN:9781337102100
    Author:Joyce, Farrell.
    Publisher:Cengage Learning,
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
1.1 Arrays in Data Structure | Declaration, Initialization, Memory representation; Author: Jenny's lectures CS/IT NET&JRF;https://www.youtube.com/watch?v=AT14lCXuMKI;License: Standard YouTube License, CC-BY
Definition of Array; Author: Neso Academy;https://www.youtube.com/watch?v=55l-aZ7_F24;License: Standard Youtube License