![Java: An Introduction to Problem Solving and Programming (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780134462035/9780134462035_largeCoverImage.gif)
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.
- Define main function.
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...
![Check Mark](/static/check-mark.png)
Want to see the full answer?
Check out a sample textbook solution![Blurred answer](/static/blurred-answer.jpg)
Chapter 12 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
- Male comedians were typically the main/dominant star of television sitcoms made during the FCC licensing freeze. Question 19 options: True False In the episode of The Honeymooners that you watched this week, why did Alice decide to get a job outside of the home? Question 1 options: to earn enough money to buy a mink coat to have something to do while the kids were at school to pay the bills after her husband got laid offarrow_forwardAfter the FCC licensing freeze was lifted, sitcoms featuring urban settings and working class characters became far less common. Question 14 options: True Falsearrow_forwardsolve this questions for me .arrow_forward
- a) first player is the minimizing player. What move should be chosen?b) What nodes would not need to be examined using the alpha-beta pruning procedure?arrow_forwardConsider the problem of finding a path in the grid shown below from the position S to theposition G. The agent can move on the grid horizontally and vertically, one square at atime (each step has a cost of one). No step may be made into a forbidden crossed area. Inthe case of ties, break it using up, left, right, and down.(a) Draw the search tree in a greedy search. Manhattan distance should be used as theheuristic function. That is, h(n) for any node n is the Manhattan distance from nto G. The Manhattan distance between two points is the distance in the x-directionplus the distance in the y-direction. It corresponds to the distance traveled along citystreets arranged in a grid. For example, the Manhattan distance between G and S is4. What is the path that is found by the greedy search?(b) Draw the search tree in an A∗search. Manhattan distance should be used as thearrow_forwardwhats for dinner? pleasearrow_forward
- Consider the follow program that prints a page number on the left or right side of a page. Define and use a new function, isEven, that returns a Boolean to make the condition in the if statement easier to understand. ef main() : page = int(input("Enter page number: ")) if page % 2 == 0 : print(page) else : print("%60d" % page) main()arrow_forwardWhat is the correct python code for the function def countWords(string) that will return a count of all the words in the string string of workds that are separated by spaces.arrow_forwardConsider the following program that counts the number of spaces in a user-supplied string. Modify the program to define and use a function, countSpaces, instead. def main() : userInput = input("Enter a string: ") spaces = 0 for char in userInput : if char == " " : spaces = spaces + 1 print(spaces) main()arrow_forward
- What is the python code for the function def readFloat(prompt) that displays the prompt string, followed by a space, reads a floating-point number in, and returns it. Here is a typical usage: salary = readFloat("Please enter your salary:") percentageRaise = readFloat("What percentage raise would you like?")arrow_forwardassume python does not define count method that can be applied to a string to determine the number of occurances of a character within a string. Implement the function numChars that takes a string and a character as arguments and determined and returns how many occurances of the given character occur withing the given stringarrow_forwardConsider the ER diagram of online sales system above. Based on the diagram answer the questions below, a) Based on the ER Diagram, determine the Foreign Key in the Product Table. Just mention the name of the attribute that could be the Foreign Key. b) Mention the relationship between the Order and Customer Entities. You can use the following: 1:1, 1:M, M:1, 0:1, 1:0, M:0, 0:M c) Is there a direct relationship that exists between Store and Customer entities? Answer Yes/No? d) Which of the 4 Entities mention in the diagram can have a recursive relationship? e) If a new entity Order_Details is introduced, will it be a strong entity or weak entity? If it is a weak entity, then mention its type?arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,New Perspectives on HTML5, CSS3, and JavaScriptComputer ScienceISBN:9781305503922Author:Patrick M. CareyPublisher:Cengage LearningNp Ms Office 365/Excel 2016 I NtermedComputer ScienceISBN:9781337508841Author:CareyPublisher:Cengage
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337671385/9781337671385_smallCoverImage.jpg)
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337102100/9781337102100_smallCoverImage.gif)
![Text book image](https://www.bartleby.com/isbn_cover_images/9781305503922/9781305503922_smallCoverImage.gif)