Java: An Introduction to Problem Solving and Programming (7th Edition)
Java: An Introduction to Problem Solving and Programming (7th Edition)
7th Edition
ISBN: 9780133766264
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
Compare and contrast the array and arrayList. Give at least one example that describe when to use arrayList compared to an array.
5 and 6
java  Write a program to do the following: Create an ArrayList of Student.                                                Add 7 elements to the ArrayList.  Use for each loop to print all the elements of an ArrayList.                            Remove an element from an ArrayList using the index number.  Remove the element from an ArrayList using the content (element name).

Chapter 12 Solutions

Java: An Introduction to Problem Solving and Programming (7th 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 - 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