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...
Want to see the full answer?
Check out a sample textbook solutionChapter 12 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
- 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).arrow_forwardUse the ArrayList class Add and remove objects from an ArrayList Protect from index errors when removing Practice with input loop Details: This homework is for you to get practice adding and removing objects from an ArrayList. The Voter class was used to create instances of Voters which held their name and a voter identification number as instance variables, and the number of instances created as a static variable. This was the class diagram: The constructor takes a string, passed to the parameter n, which is the name of the voter and which should be assigned to the name instance variable. Every time a new voter is created, the static variable nVoters should be incremented. Also, every time a new voter is created, a new voterID should be constructed by concatenating the string “HI” with the value of nVoters and the length of the name. For example, if the second voter is named “Clark Kent”, then the voterID should be “HI210” because 2 is the value of nVoters and 10 is the number…arrow_forwardPlease use java. Show the steps with pictures. In this assignment, you will implement a class calledArrayAndArrayList. 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…arrow_forward
- Write Java code for the following: Create an Arraylist that can hold Integer objects. Check and display whether the Arraylist is empty or not. Add an element 50 into the Arraylist. Add another element 60 in the position of 0 in the Arraylist. Replace the element to 70 in index 1.arrow_forward/** * The constructor has been partially implemented for you. cards is the * ArrayList where you'll be adding all the cards you're given. In addition, * there are two arrays. You don't necessarily need to use them, but using them * will be extremely helpful. * * The rankCounts array is of the same length as the number of Ranks. At * position i of the array, keep a count of the number of cards whose * rank.ordinal() equals i. Repeat the same with Suits for suitCounts. For * example, if your Cards are (Clubs 4, Clubs 10, Spades 2), your suitCounts * array would be {2, 0, 0, 1}. * * @param cards * the list of cards to be added */ public PokerAnalysis(List<Card> cards) { this.cards = new ArrayList<Card>(); this.rankCounts = new int[Rank.values().length]; this.suitCounts = new int[Suit.values().length]; throw new UnsupportedOperationException(); }arrow_forwardWITHOUT USING VECTOR ARRAY. Using c++ . Write different code rather than 2 codes that already existed in this question on this website.arrow_forward
- The implementation of a queue in an array, as given in this chapter, uses the variable count to determine whether the queue is empty or full. You can also use the variable count to return the number of elements in the queue. On the other hand, class linkedQueueType does not use such a variable to keep track of the number of elements in the queue. Redefine the class linkedQueueType by adding the variable count to keep track of the number of elements in the queue. Modify the definitions of the functions addQueue and deleteQueue as necessary. Add the function queueCount to return the number of elements in the queue. Also, write a program to test various operations of the class you defined.arrow_forwardI have an ArrayList and I want to sort it by increasing string length. The ArrayList class has a method sort(Comparator comp). Give a class that implements the Comparator interface that I can use to sort my ArrayList.arrow_forwardWrite Program Java language Create an Animal class and an arraylist from that class you created. Then, add objects to that arraylist and print the elements?arrow_forward
- Define a class called Book. This class should store attributes such as the title, ISBN number, author, edition, publisher, and year of publication. Provide get/set methods in this class to access these attributes. Define a class called Bookshelf, which contains the main method. This class should create a few book objects with distinct names and store them in an ArrayList. This class should then list the names of all books in the ArrayList. Enhance the program by providing a sort function, which will sort the books in ascending order of their year of publication. Create a few more Bookobjects with the same names but with different edition numbers, ISBNs, and years of publication. Add these new Book objects to the ArrayList, and display the book list sorted by book name; for duplicate books of the same name, sort the list by year of publication. (Hint: You will need to define a comparator class that takes two Book objects as parameters of the compareTo This method should do a two-step…arrow_forward1. Write a program that would resemble a power utility billing system. There will be an input window that would enter or input the meter number (5 numeric digits) and the present meter reading in kilowatt hours, the maximum would be 9999 kilowatts. A search from a 2d-arraylist would be made in order to get the previous meter reading of a particular meter number. The 2d- arraylist consists of meter number (5 numeric digits) and the previous meter reading in kilowatt hours (4 numeric digits with 9999 as maximum value). Provide at least 5 sample data or 5 rows with meter number and previous meter reading each row for the 2d-arraylist. When an input of meter number and present meter reading is made, search the 2d-arraylist for the equivalent meter number. If a match is found, get the kilowatt hour used (KWH) by subtracting the previous meter reading from the present meter reading. Note that if the present meter reading is less than previous meter reading, add 10000 first to the present…arrow_forwardOnly number 5arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning