Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with converting ASCII to Char for the code below. It is a JAVA char que array. I'm able to add a char

I need help with converting ASCII to Char for the code below. It is a JAVA char que array. I'm able to add a char but when I try to get it (read from an array) instead of a char I get an ASCII for that char. I believe that changes must be done to either "public void display()" or "case 2". So far I tried this "System.out.println("Result : "+ char(ad.get(scan.nextInt();" but that didn't work for me...

import java.util.Scanner; class CharacterQueue { private int[] a; private int j, n; public CharacterQueue() { j = 0; n = 0; resize(); }

public boolean isEmpty() { return n == 0; }

public void clear() { j = 0; n = 0; resize(); }

public int getSize() { return n; } private void resize() { int[] temp = new int[Math.max(2 * n, 1)]; for (int k = 0; k < n; k++) temp[k] = a[(j + k) % a.length]; a = temp; j = 0; } public int get(int i) { return a[(j + i) % a.length]; }

void add(int i, int x) { if (n + 1 > a.length) resize(); if (i < n/2) { j = (j == 0) ? a.length - 1 : j - 1; for (int k = 0; k <= i - 1; k++) a[(j + k) % a.length] = a[(j + k + 1)%a.length]; } else { for (int k = n; k > i; k--) a[(j + k) % a.length] = a[(j + k - 1)%a.length]; } a[(j + i) % a.length] = x; n++; }

public int remove(int i) { int x = a[(j + i) % a.length]; if (i < n/2) { for (int k = i; k > 0; k--) a[(j + k) % a.length] = a[(j + k - 1) % a.length]; j = (j + 1) % a.length; } else { for (int k = i; k < n - 1; k++) a[(j + k) % a.length] = a[(j + k + 1) % a.length]; } n--; if (3 * n < a.length) resize(); return x; }

public void display() { System.out.print( " Array Deque: " ); int p = j; for (int i = 0; i < n; i++) { char letter = (char) a[p % a.length]; System.out.print(letter); System.out.printf(" "); p++; } System.out.println(); } }

public class CharacterQueueTest { public static void main(String[] args) { Scanner scan = new Scanner(System.in);

CharacterQueue ad = new CharacterQueue();

char ch; do { System.out.println( "======================" ); System.out.println( "Array Deque Operations" ); System.out.println( "======================" ); System.out.println(); System.out.println( "1. Add" ); System.out.println( "2. Get" ); System.out.println( "3. Remove" ); System.out.println( "4. Check If Empty" ); System.out.println( "5. Clear All" ); System.out.println( "6. Check Size" ); System.out.println(); System.out.printf( "Please enter your choice: " );

int choice = scan.nextInt();

switch (choice) { case 1 : System.out.printf( "Enter index and element: " ); ad.add(scan.nextInt(), scan.next().charAt(0) ); break;

case 2 : System.out.printf( "Enter index: " ); System.out.println( "ASCII Result: " + ad.get(scan.nextInt() )); break;

case 3 : System.out.println( " Enter index" ); ad.remove(scan.nextInt() ); break;

case 4 : System.out.println( " Empty Status: " + ad.isEmpty() ); break;

case 5 : System.out.println( " Array Deque Cleared!" ); ad.clear(); break;

case 6 : System.out.println( " Size = "+ ad.getSize() ); break;

default : System.out.println( "Wrong Entry. "); break; } ad.display();

System.out.printf( "Would you like to continue (Type: y/n)? " ); ch = scan.next().charAt(0); System.out.println(); } while (ch == 'Y'|| ch == 'y');

} }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started