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 rapid miner how to creat decison trea for all attribute and another one with delete one or more of them also how i know the weight of each attribute and what that mean in impact the result
Q.1. Architecture performance [10 marks] Answer A certain microprocessor requires either 2, 4, or 6 machine cycles to perform various operations. ⚫ (40+g+f)% require 2 machine cycles, ⚫ (30-g) % require 4 machine cycles, and ⚫ (30-f)% require 6 machine cycles. (a) What is the average number of machine cycles per instruction for this microprocessor? Answer (b) What is the clock rate (machine cycles per second) required for this microprocessor to be a "1000 MIPS" processor? Answer (c) Suppose that 35% of the instructions require retrieving an operand from memory which needs an extra 8 machine cycles. What is the average number of machine cycles per instruction, including the instructions that fetch operands from memory?
Q.2. Architecture performance [25 marks] Consider two different implementations, M1 and M2, of the same instruction set. M1 has a clock rate of 2 GHz and M2 has a clock rate of 3.3 GHz. There are two classes of instructions with the following CPIs: Class A CPI for M1 CPI for M2 2.f 1.g B 5 3 C 6 4 Note that the dots in 2 fand 1.g indicate decimal points and not multiplication. a) What are the peak MIPS performances for both machines? b) Which implementation is faster, if half the instructions executed in a certain program are from class A, while the rest are divided equally among classes B and C. c) What speedup factor for the execution of class-A instructions would lead to 20% overall speedup? d) What is the maximum possible speedup that can be achieved by only improving the execution of class-A instructions? Explain why. e) What is the clock rate required for microprocessor M1 to be a "1000 MIPS" (not peak MIPS) processor?

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
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning
Text book image
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:9781337508841
Author:Carey
Publisher:Cengage