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
Please use java. Show the steps with pictures. In this assignment, you will implement a class called​ArrayAndArrayList.​ This class includes some interesting methods for working with Arrays and ArrayLists. For example, the ​ArrayAndArrayList​ class has a “findMax” method which finds and returns the max number in a given array. For a defined array: int[] array = {1, 3, 5, 7, 9}, calling findMax(array) will return 9. There are 4 methods that need to be implemented in the A​ rrayAndArrayList​ class: ● howMany(int[] array, int element) - Counts the number of occurrences of the given element in the given array. ● findMax(int[] array) - Finds the max number in the given array. ● maxArray(int[] array) - Keeps track of every occurrence of the max number in the given array. ● swapZero(int[] array) - Puts all of the zeros in the given array, at the end of the given array. Each method has been defined for you, but without the code. See the javadoc for each method for instructions on what the…
Please use java only and public int method. Show the steps with the pictures also. In this assignment, you will implement a class called​ArrayAndArrayList.​ This class includes some interesting methods for working with Arrays and ArrayLists. For example, the ​ArrayAndArrayList​ class has a “findMax” method which finds and returns the max number in a given array. For a defined array: int[] array = {1, 3, 5, 7, 9}, calling findMax(array) will return 9. There are 4 methods that need to be implemented in the A​ rrayAndArrayList​ class: ● howMany(int[] array, int element) - Counts the number of occurrences of the given element in the given array. ● findMax(int[] array) - Finds the max number in the given array. ● maxArray(int[] array) - Keeps track of every occurrence of the max number in the given array. ● swapZero(int[] array) - Puts all of the zeros in the given array, at the end of the given array. Each method has been defined for you, but without the code. See the javadoc for each…
Explain the fundamental difference between an array and an arraylist.

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