So I really need help with this Java lab... I missed a couple of lectures and no
ID: 3566398 • Letter: S
Question
So I really need help with this Java lab... I missed a couple of lectures and now I'm completely lost :-( If you do try this question, please comment the steps as much as possible! Please and thank you :)
The code (2 .java files) provided to us is at the bottom
Activity 1: Count Letters (2 marks)
q1 contains a program which reads a word from the user and prints the number of each letter in the word. Compile your project and run it to check the output. 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. Now test this method by inputting a word consisted of only letter (A~z), an expected output is shown below:
Now lets try to input a string containing some other characters, such as space or digital. Unfortunately, this time we encounter an exception called ArrayIndexOutOfBoundsException shown below:
1) Now, lets check the source code, there are two for loops, select one of them and surround it with trycatch block. This time, dont do anything in catch block. Run it again and check the output. Your code should be able to deal with illegal input.
2) Now modify the body of catch block so that it prints a useful message (e.g., Not a letter) followed by the exception. Compile and run the program. An expected output is shown below:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
lab5.java (MAIN)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
public class lab5 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Please enter a number (1~2) to select a question:");
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
questions q = new questions();
int quest = scan.nextInt();
switch(quest)
{
case 1: q.q1(); break;
case 2: q.q2(); break;
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
questions.java
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class questions {
void q1()
{
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]);
}
}
}
void q2()
{
int creditHrs; // number of semester hours earned
double qualityPts; // number of quality points earned
double gpa; // grade point (quality point) average
String line, name, inputName = "students.txt";
String outputName = "warning.txt";
/* Please uncomment the this code block
try
{
//Set up scanner to input file
//Set up output file stream
//Print a header to the output file
outfile.println();
outfile.println("Student on Academic Warning");
outfile.println();
//Process the input file, one token at a time
while()
{
// Get the credit hours and quality points and
// determine if the student is on warning. If so,
// write the student data to the output file.
}
}
*/
}
}
Explanation / Answer
void q1()
{
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++)
{
try {
counts[word.charAt(i) - 'A']++;
}
catch(Exception e){
System.out.println("Not a Letter");
}
}
//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]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.