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.:- M92797963
  • Price:- $20

Priced at Now at $20, Verified Solution

Have any Question?


Related Questions in Business Management

Compute the cross elasticity of demand and characterize the

Compute the cross elasticity of demand and characterize the goods as complements or substitutes (Please use "Arc Elasticity" to calculate) a. Regular Flu shot offered by pharmacy Boxes of Tamu Flu sold by pharmacy Price: ...

Total quality management involves a continuous improvement

Total quality management involves a continuous improvement approach. 1. How is continuous improvement related to innovation? 2. What is breakthrough innovation? 3. What are the risks and rewards associated with innovatio ...

With an emphasis on some of the security ethical and

With an emphasis on some of the security, ethical, and societal challenges of IT in the business world. Please discuss a real-world example/application of an organization that has dealt with crimes including hacking, cyb ...

Can you please tell me the difference in content between an

Can you please tell me the difference in content between an executive summary, an informative abstract, and an introduction?

What happens if wages and prices adjust very slowly in

What happens if wages and prices adjust very slowly in response to various shocks to the economy? Does this make business cycle, i.e. expansions and contractions in the economy, shorter or longer?

1 define organizational communication2 what interesting

1. Define organizational communication 2. What interesting about the subject of organizational communication

What moral difference if any does it make who is dumping

What moral difference, if any does it make who is dumping, why they are doing it, where they are doing it, or what the product is?

The evolutionary process is one important perspective of

The evolutionary process is one important perspective of strategic management. What is evolutionary process?

Business managementcan you share an example from recent

Business management Can you share an example from recent news (within the last one or two years) that demonstrates either a hard power or soft power negotiating approach, and describe the negotiation outcome. Was it a su ...

According to firestones tire recall case evaluate and

According to Firestone's tire recall case, evaluate and discuss the role of leadership when commercial realities conflict with the public safety concerns and the ethical dilemma that ensue for leaders in such situation. ...

  • 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