Java: An Introduction to Problem Solving and Programming (8th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 12, Problem 3E

Explanation of Solution

Creating “CharacterFrequency.java”:

  • Import required package.
  • Define “CharacterFrequency” class.
    • Define main function.
      • Create array for digit count using “ArrayList”.
      • Initialize the digit counts.
      • Create object for scanner class.
      • Prompt statement for telephone number.
      • Read the telephone number from user.
      • Compute the count of each character in the telephone number using “for” loop.
        • Define each character in telephone number.
        • Performs “switch” case.
          • If the digit character is “0”, then increment the count of character “0” using “set” and “get” method
          • If the digit character is “1”, then increment the count of character “1”.
          • If the digit character is “2”, then increment the count of character “2”.
          • If the digit character is “3”, then increment the count of character “3”.
          • If the digit character is “4”, then increment the count of character “4”.
          • If the digit character is “5”, then increment the count of character “5”.
          • If the digit character is “6”, then increment the count of character “6”.
          • If the digit character is “7”, then increment the count of character “7”.
          • If the digit character is “8”, then increment the count of character “8”.
          • If the digit character is “9”, then increment the count of character “9”.
        • Prompt statement for count of each digit.
        • Display the count of each digit using “for” loop.
          • Display the count by using “get” method.
        • Display count of each digit.

Program:

The below java program is used to compute the count of each digit in a telephone number using “ArrayList”.

“CharacterFrequency.java”

//Import required package

import java.util.*;

//Define "CharacterFrequency" class

public class CharacterFrequency

{

    //Define main function

    public static void main(String[] args)

    {

/* Create array for digit count using "ArrayList" */

ArrayList<Integer> digit_count = new ArrayList<Integer>();

        //Initialize the digit counts

        for(int i = 0; i < 10; i++)

        {

            digit_count.add(0);

        }

        //Create object for scanner class

        Scanner reader = new Scanner(System.in);

        //Prompt statement for user

        System.out.print("Enter a telephone number: ");

        //Read the telephone number from user

        String telephoneNumber = reader.next();

/* Compute the count of each character in the telephone number */

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

        {

/* Define each character in telephone number */

Character digit = telephoneNumber.charAt(i);

            //Performs "switch" case

            switch(digit)

            {

/* If the digit character is "0", then */

                case '0':

/* Increment the count of character '0' by using "set" and "get" method */

digit_count.set(0, digit_count.get(0)+1);

                    break;

/* If the digit character is "1", then */

                case '1':

/* Increment the count of character '1' */

digit_count...

Blurred answer
Students have asked these similar questions
Using examples of your own, write short notes to explain how ArrayList and LinkedList work in Java. please give me a code example how to do that?
What is the difference in the result of returning the words in a HashSet compared with returning them in an ArrayList?
Write a program that reads words into two ArrayLists list1 and list2 and then creates a third ArrayList that contains words which are common to both list1 and list2.

Chapter 12 Solutions

Java: An Introduction to Problem Solving and Programming (8th Edition)

Ch. 12.1 - Prob. 12STQCh. 12.2 - Prob. 13STQCh. 12.2 - Prob. 14STQCh. 12.2 - Prob. 15STQCh. 12.2 - Prob. 16STQCh. 12.3 - Prob. 17STQCh. 12.3 - Prob. 18STQCh. 12.3 - Prob. 19STQCh. 12.3 - Write a definition of a method isEmpty for the...Ch. 12.3 - Prob. 21STQCh. 12.3 - Prob. 22STQCh. 12.3 - Prob. 23STQCh. 12.3 - Prob. 24STQCh. 12.3 - Redefine the method getDataAtCurrent in...Ch. 12.3 - Repeat Question 25 for the method...Ch. 12.3 - Repeat Question 25 for the method...Ch. 12.3 - Repeat Question 25 for the method...Ch. 12.4 - Revise the definition of the class ListNode in...Ch. 12.4 - Prob. 30STQCh. 12.5 - What is the purpose of the FXML file?Ch. 12.5 - Prob. 32STQCh. 12 - Repeat Exercise 2 in Chapter 7, but use an...Ch. 12 - Prob. 2ECh. 12 - Prob. 3ECh. 12 - Repeat Exercises 6 and 7 in Chapter 7, but use an...Ch. 12 - Write a static method removeDuplicates...Ch. 12 - Write a static method...Ch. 12 - Write a program that will read sentences from a...Ch. 12 - Repeat Exercise 12 in Chapter 7, but use an...Ch. 12 - Write a program that will read a text file that...Ch. 12 - Revise the class StringLinkedList in Listing 12.5...Ch. 12 - Prob. 12ECh. 12 - Write some code that will use an iterator to...Ch. 12 - Prob. 14ECh. 12 - Write some code that will use an iterator to...Ch. 12 - Prob. 17ECh. 12 - Revise the method selectionSort within the class...Ch. 12 - Repeat the previous practice program, but instead...Ch. 12 - Repeat Practice Program 1, but instead write a...Ch. 12 - Write a program that allows the user to enter an...Ch. 12 - Write a program that uses a HashMap to compute a...Ch. 12 - Write a program that creates Pet objects from data...Ch. 12 - Repeat the previous programming project, but sort...Ch. 12 - Repeat the previous programming project, but read...Ch. 12 - Prob. 9PPCh. 12 - Prob. 10PPCh. 12 - Prob. 11PPCh. 12 - Prob. 12PPCh. 12 - Prob. 13PPCh. 12 - Prob. 14PPCh. 12 - Prob. 15PP
Knowledge Booster
Background pattern image
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
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning