Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Can someone explain this java program to me in detail step-by-step. I want a

ID: 3556269 • Letter: 1

Question

1. Can someone explain this java program to me in detail step-by-step. I want a detailed explanation step by step on how this program was written and which technique was used

public class Project2 {

/**
Return the index in the letter array
*/
public static int letterIndex(char ch)
{
ch = Character.toUpperCase(ch);
return (int)ch - (int)'A';
}
  
/**
Return the index in the digit array
*/
public static int digitIndex(char ch)
{
return (int)ch - (int)'0';
}
  
/**
Process the line of text
*/
public static void processLine(String line)
{
int[] letters = new int[26]; // A- Z
int[] digits = new int[10]; // 0- 9
int special = 0;
  
for (int i = 0; i < line.length(); i++)
{
char ch = line.charAt(i);
int index = letterIndex(ch);
// ch is a letter
if (index >= 0 && index < letters.length)
{
letters[index]++;
}
else
{
// ch is digit or special char
index = digitIndex(ch);
if (index >= 0 && index < digits.length)
digits[index]++;
else if (!Character.isWhitespace(ch))
special++;   
}   
  
  
if (ch == '.') // end of line
break;
  
}
  
System.out.println("Character Number of Times");
for (int i = 0; i < letters.length; i++)
{
char ch = (char)((int)'A' + i);
if (letters[i] > 0)
{
System.out.print(" " + ch);
System.out.println(" " + letters[i]);
}
}
for (int i = 0; i < digits.length; i++)
{
char ch = (char)((int)'0' + i);
if (digits[i] > 0)
{
System.out.print(" " + ch);
System.out.println(" " + digits[i]);
}
}
System.out.println("Number of special characters: " + special);
  
}
  
/**
Read lines of text and output the letters
*/
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
  
System.out.print("Enter a line: ");
String line = keyboard.nextLine().trim();
  
while (!line.isEmpty())
{
processLine(line);
  
  
System.out.print("Enter a line: ");
line = keyboard.nextLine().trim();
}
  
}
  
}

Explanation / Answer

Lets start from the main....

1) In main method Scanner keyboard = new Scanner(System.in); is defined to take the input from user.

Here we are taking a line as a input ( as long as line is not empty) and then processing that line using method processLine(line);

2) Each line will be argument of the method processLine(line);

3) In processLine() method we are creating array of letters [0-26] and digit [0-10] to count the occurence of each letter(alphabet or digit) in that line.

for (int i = 0; i < line.length(); i++)
{
char ch = line.charAt(i);
int index = letterIndex(ch);
// ch is a letter
if (index >= 0 && index < letters.length)
{
letters[index]++;
}
else
{
// ch is digit or special char
index = digitIndex(ch);
if (index >= 0 && index < digits.length)
digits[index]++;
else if (!Character.isWhitespace(ch))
special++;   
}   

in the above for loop we are traversing the line character by character and increasing the occurence in digit[]

if it is a space we are incrementing the special character

public static int letterIndex(char ch)
{
ch = Character.toUpperCase(ch);
return (int)ch - (int)'A';
}

this method is used to find the which letter it is in the line ( we are considering 1-26 for A-Z)

You can better understand by the output below

Enter a line: You are dude
Character Number of Times
A 1
D 2
E 2
O 1
R 1
U 2
Y 1
Number of special characters: 0

If you have any doubts feel free to ask