Write a program, including comments for each block of code, to create two arrays
ID: 3543634 • Letter: W
Question
Write a program, including comments for each block of code, to create two arrays namely asciiArray and decimalArray o 52 elements each. In the asciiArray, store letters of the English alphabet; both lower and upper case letters. In the decimalArray, store the corresponding decimal values of each of the letters in the specific position in the asciiArray.
For example,
if asciiArray[0] holds 'A' then ----> decimalArray[0] will hold the value 65.
Pass these arrays to a method displayDevValue. Inside the method, prompt the user to enter any of the letters of the English alphabet and display the corresponding decimal value. The program will need to include the following method outside of the main() method below:
Method: Signature:
displayDecValue public static void displayDecValue(char [ ] ascii, int [ ] dec)
Sample Run:
Enter a letter (a-z or A-Z): C
Decimal value of 'C' is: 67
Thank You!
Explanation / Answer
import java.util.Scanner;
public class ParallelArrays {
public static void main(String[] args) {
char[] asciiArray = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z' }; //declaration of character array to store alphabets.
int[] decimalArray = new int[asciiArray.length]; //decimal value array gets value from ascii array
// Now run it for length of Ascii Array
for (int i = 0; i < asciiArray.length; i++) {
decimalArray[i] = (int)asciiArray[i];
}
displayDecValue(asciiArray, decimalArray); //pass ascii array and decimal array for the function displayDecValue
}
public static void displayDecValue(char[] ascii, int[] dec) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a letter (a-z or A-Z): ");
char inputChar = input.next().charAt(0);
for (int i = 0; i < ascii.length; i++) {
if (inputChar == ascii[i]) {
System.out.printf("Decimal value of '" + ascii[i] + "'
is: "
+ dec[i]);
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.