Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

File CountLetters.java contains a program that reads a word from the user and prints the number of occurrences of each letter in the word. Save it to your directory and study it, then compile and run it to see how it works. In reading the code, note that the word is converted to all upper case first, then each letter is translated to a number in the range 0..25 (by subtracting 'A') for use as an index. No test is done to ensure that the characters are in fact letters.

1. Run CountLetters and enter a phrase, that is, more than one word with spaces or other punctuation in between. It should throw an ArrayIndexOutOfBoundsException, because a non-letter will generate an index that is not between 0 and 25. It might be desirable to allow non-letter characters, but not count them. Of course, you could explicitly test the value of the character to see if it is between 'A' and 'Z'. However, an alternative is to go ahead and use the translated character as an index, and catch an ArrayIndexOutOfBoundsException if it occurs. Since you want don't want to go further when a non-letter occurs, the handler will be empty. Modify this method as follows:

§ Put the body of the first for loop in a try.

§ Add a catch that catches the exception, but don't go further with it.

Compile and run your program.

2. Now modify the body of the catch so that it prints a useful message (e.g., "Not a letter") followed by the exception. Compile and run the program. Although it's useful to print the exception for debugging, when you're trying to smoothly handle a condition that you don't consider erroneous you often don't want to. In your print statement, replace the exception with the character that created the out of bounds index. Run the program again; much nicer!

// ****************************************************************

// CountLetters.java

//

// Reads a words from the standard input and prints the number of

// occurrences of each letter in that word.

//

// ****************************************************************

import java.util.Scanner;

public class CountLetters

{

public static void main(String[] args)

{

int[] counts = new int[26];

Scanner scan = new Scanner(System.in);

//get word from user

System.out.print("Enter a single word (letters only, please): ");

String word = scan.nextLine();

//convert to all upper case

word = word.toUpperCase();

//count frequency of each letter in string

for (int i=0; i < word.length(); i++)

counts[word.charAt(i)-'A']++;

//print frequencies

System.out.println();

for (int i=0; i < counts.length; i++)

if (counts [i] != 0)

System.out.println((char)(i +'A') + ": " + counts[i]);

}

}

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M92799185
  • Price:- $10

Priced at Now at $10, Verified Solution

Have any Question?


Related Questions in Computer Engineering

Is there any difference in terms of the information that

Is there any difference in terms of the information that could be provided to the decision maker by an IS that was created using HSM and an IS that was created using SSM?

Question project assignment is to have you do more research

Question: Project assignment is to have you do more research on the physical security topic. In theory, everything may look straight forward, however, in real life it might not be like that. In this project you will need ...

What is a survey and pros and cons of using this method to

What is a Survey and pros and cons of using this method to collect data are? Have you done a survey? How successful were your results?

Prove the sieve of eratosthenes algorithm has runtime

Prove the Sieve of Eratosthenes algorithm has runtime complexity ≤ O(n log log n). Sieve Input: an integer n > 1. Let A be an array of Boolean values, indexed by integers 2 to n, initially all set to true. for i = 2,3,4, ...

Arectangular box without a top has surface area 2500 cm2

A rectangular box without a top has surface area 2500 cm 2 . The length x of its base is 18 cm longer that its width. • (a) Use MATLAB's polynomial functions to express the volume V of the box in terms of x. • (b) Plot V ...

Setting youre a junior accountant for a steel manufacturing

Setting: You're a junior accountant for a steel manufacturing firm in West Virginia, with $3 billion in revenues in 2016. You're sitting in a budget meeting as the assistant to the CFO. The meeting is with the executive ...

What is the lithium by mass in a sample of pure lithium

What is the % lithium by mass in a sample of pure lithium carbonate that has a sample size of 10.0g?

Question suppose you are given two sets of 100 points that

Question : Suppose you are given two sets of 100 points that fall within the unit square. One set of points (a) is arranged so that the points are uniformly spaced. The other set of points (b) is randomly generated from ...

Not many applications use this type of direct connection ex

Not many applications use this type of direct connection (ex: ftp, ssh, tenet, smtp, httpd, pop) anymore unless it is within the corporate firewall. Why do you think this is? Pick 1 or 2 as example.

Question synchronization barriers are a common paradigm in

Question : Synchronization barriers are a common paradigm in many parallel applications. A barrier is supposed to block a calling thread until all N threads have reached the barrier. (Parallel applications often divide u ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As