Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Business Management 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]);

}

}

Business Management, Management Studies

  • Category:- Business Management
  • Reference No.:- M92796162
  • Price:- $10

Priced at Now at $10, Verified Solution

Have any Question?


Related Questions in Business Management

One of the most important contributions of hofstedes

One of the most important contributions of Hofstede's studies regarding working in diverse teams is the idea of "diversity distance". How would this idea be useful in creating trust in a virtual global team?

How do you apply the five components of the information

How do you apply the five components of the information systems to an information systems application like "online bill pay" system offered by many banks.

There are 5 categories of strategy formulation available to

There are 5 categories of strategy formulation available to utilize: directional strategies, adaptive, market entry, competitive, and implementation strategies. Is it important to formulate strategies in this specific or ...

Freddie needs to set goals for his team of real estate

Freddie needs to set goals for his team of real estate agents. Freddie wants the goals to improve task performance for his team. Use Freddie's situation to identify and explain the factors that alter the effects of goal ...

What are some of the reasons that email communications seem

What are some of the reasons that email communications seem to be so overwhelming and time consuming? What can be done to eliminate the "reply all" approach to business communications?

Can you please direct me on how i should go about

Can you please direct me on how I should go about describing an interview by providing 3 structured behavioral questions? Choose a job that you are interested in. You may relate it to your term project by choosing a movi ...

In the basic break-even equation the term contribution is

In the basic break-even equation, the term contribution is used. What does the term contribution mean here? What does this number tell the manager?

Do you think it is more important to end wealth inequality

Do you think it is more important to end wealth inequality or income inequality? What methods and policies, public or private, do you think would decrease inequality without resulting in people being stuck in poverty and ...

You are to prepare and submit assessment 3 as an individual

You are to prepare and submit Assessment 3 as an individual, based on the Virtucon/Globex scenario, and your peer to peer learning from Assessment 2. This is NOT a team assessment. You are required to develop a charter f ...

Discussion topic 1there are several functions of

Discussion Topic 1 There are several functions of advertising that must be kept in mind when putting together MARCOM for a company. These include informing, influencing, reminding and increasing salience, adding value, a ...

  • 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