EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
9th Edition
ISBN: 9781337671385
Author: FARRELL
Publisher: CENGAGE LEARNING - CONSIGNMENT
bartleby

Videos

Expert Solution & Answer
Book Icon
Chapter 8, Problem 5PE

a.

Explanation of Solution

Program:

File name: “CategorizeStrings.java

//Import necessary header files

import java.util.*;

//Define a class named CategorizeStrings

public class CategorizeStrings

{

    //Define a main method

    public static void main (String[] args)

    {

        //Declare the variables and initialize the values

        final int MAX = 20;

        final int CUTOFF = 10;

        final String QUIT = "ZZZ";

        String entry;

        int shortSub = 0;

        int longSub = 0;

        //Create an object for Scanner class

        Scanner input = new Scanner(System.in);

/*Declare an array to store each String in one of two lists -

one list for short Strings that are 10 characters or fewer

and another list for long Strings that are 11 characters or more*/

        String[] longStrings = new String[MAX];

        String[] shortStrings = new String[MAX];

        //Prompt the user to enter a String or QUIT

        System.out.print("Enter a string or " +

         QUIT + " to quit >> ");

        entry = input.nextLine();

        /*While the user does not enter QUIT and enter less than MAX*/

        while(!entry.equals(QUIT) && (shortSub + longSub < MAX))

        {

            //If there are 10 characters or fewer

            if(entry.length() <= CUTOFF)

            {

                //Assign the value

                shortStrings[shortSub] = entry;

                //Increment shortSub

                ++shortSub;

            }

            //Else there are 11 characters or more

            else

            {

                //Assign the value

                longStrings[longSub] = entry;

                //Increment longSub

                ++longSub;

            }

            //If the length of the characters is less than MAX

            if(shortSub + longSub < MAX)

            {

                //Prompt the user to enter a String or QUIT

                System...

b.

Explanation of Solution

Program:

File name: “CategorizeStrings2.java

//Import necessary header files

import java.util.*;

//Define a class named CategorizeStrings2

public class CategorizeStrings2

{

    //Define a main method

    public static void main (String[] args)

    {

        //Declare the variables and initialize the values

        final int MAX = 20;

        final int CUTOFF = 10;

        final String QUIT = "ZZZ";

        String entry;

        int spaces;

        int oneWordSub = 0;

        int twoWordsSub = 0;

        int threeWordsSub = 0;

        //Create an object for Scanner class

        Scanner input = new Scanner(System.in);

        /*Declare an array to store each String in one of three lists -

        that contain no spaces, one space, or more*/

        String[] oneWord = new String[MAX];

        String[] twoWords = new String[MAX];

        String[] threeWords = new String[MAX];

        //Prompt the user to enter a String or QUIT

        System.out.print("Enter a string or " +

         QUIT + " to quit >> ");

        entry = input.nextLine();

        //While the user does not enter QUIT and enter the //String less than than MAX

        while(!entry.equals(QUIT) && (oneWordSub + twoWordsSub + threeWordsSub < MAX))

        {

            //Assign the value 0 to spaces

            spaces = 0;

//For loop to be executed until x exceeds the string //length

            for(int x = 0; x < entry.length(); ++x)

            {

                //If the character contains a space

                if(entry.charAt(x) == ' ')

                    //Increment spaces

                    ++spaces;

            }

            //If the entered Strings contain no spaces

            if(spaces == 0)

            {

                //Assign the value

                oneWord[oneWordSub] = entry;

                //Increment oneWordSub

                ++oneWordSub;

            }

            //Else if the entered Strings contain one space

            else

            if(spaces == 1)

            {

                //Assign the value

                twoWords[twoWordsSub] = entry;

                //Increment twoWordsSub

                ++twoWordsSub;

            }

//Else the entered Strings contain more than one //space

            else

            {

                //Assign the value

                threeWords[threeWordsSub] = entry;

                //Increment threeWordsSub

                ++threeWordsSub;

            }

            //If the length of all the entered Strings is less //than MAX

            if(oneWordSub + twoWordsSub + threeWordsSub < MAX)

            {

                //Prompt the user to enter a String or QUIT

                System...

Blurred answer
Students have asked these similar questions
In python insert_substrings_into_string(s, substrs): This function takes a string s and a string substrs. The substrs string will contain 0 or more comma-separated substrings. The string s will contain text with 0 or more special placeholder character sequences. These placeholder character sequences will always begin with the percent character %, followed by one or more digits. The function will create a new string from the original string s where all the placeholder character sequences in s are replaced by the substrings in the string substrs. The placeholder %0 should be replaced by the first substring in substrs (i.e., everything before the first comma, if such a comma exists). The placeholder %1 should be replaced by the second substring in substrs (i.e., everything between the first and second comma, if such a comma exists), and so on. The new string should then be returned. Note: If a placeholder sequence refers to an invalid index (e.g., %10 when there are only five substrings…
This application accepts an arbitrary number of user inputs. Every time this application receives a string, it should add the string to a growing string.Newly added strings should be added to the growing string at the index equal to the newly added string's length.
Python task You must write a function control (p_nr) that calculates and returns the control digit in a 10-digit social security number if you send in the first 9 digits in the form of a string. The rules for calculating the check digit in a social security number are as follows:The numbers are multiplied alternately by 2 and 1 and then the products are summed. For a two-digit product, the sum of its numbers is calculated. The sum is then subtracted from the nearest higher tens to obtain the check digit.To calculate the check digit for the social security number with the first 9 digits 811218987, perform the following setup: 8 1 1 2 1 8 9 8 7 2 1 2 1 2 1 2 1 2 16 1 2 2 2 8 18 8 141 + 6 + 1 + 2 + 2 + 2 + 8 + 1 + 8 + 8 + 1 + 4 = 44The check digit is 50-44 = 6 # test  personal_number = input ()  control_digit = control(personal_number) print(control_digit)
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
    EBK JAVA PROGRAMMING
    Computer Science
    ISBN:9781337671385
    Author:FARRELL
    Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Introduction to Linked List; Author: Neso Academy;https://www.youtube.com/watch?v=R9PTBwOzceo;License: Standard YouTube License, CC-BY
Linked list | Single, Double & Circular | Data Structures | Lec-23 | Bhanu Priya; Author: Education 4u;https://www.youtube.com/watch?v=IiL_wwFIuaA;License: Standard Youtube License