0

This code is for learning generic class and stack operation. but it has some error about declare variable type

package lab11_2_590510535;
import java.util.Scanner;

class Queue <TYPE>{
    private int count;
    private int front;
    private int rear;
    private int n;
    private Object [] item;
    private TYPE queueFront;
    static void pl(Object a){System.out.println(a);}
    static void p(Object a){System.out.print(a);}

Queue(int x){
    n = x;
    item = new Object[n];
    front = 0;
    rear = -1;
    count = 0;
}

public boolean isEmpty(){
    for(int i = 0 ; i<item.length ; i++){
        if(item[i] != null){return false;}
    }
    return true;
}

public boolean isFull(){
    if(item[item.length] != null){return true;}
    else{return false;}
}

public void enqueue(TYPE v){
    if(!isFull()){
        if(rear < n-1){
            rear++;
            item[rear] = v;
            count++;
        }
    }else{pl("Queue is Full.");}
}
public TYPE dequeue(){
    if(!isEmpty()){
        queueFront = item[front];
        front++;
        count++;
    }else{p("Queue is empty.");}
    return queueFront;
}
public void show(){
 for(int i = 0 ; i <item.length ; i++){
     if(item[i] !=  null){p(item[i] + ", ");}
 }
}


public class Lab11_2_590510535 {
    static void pl(Object a){System.out.println(a);}
    static void p(Object a){System.out.print(a);}
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    Scanner keyboard = new Scanner(System.in);
    char choice;
    int N;
    p("Enter N: ");
    N = keyboard.nextInt();
    p("Choice input type; int(1) , char(2): ");
    choice = keyboard.next().charAt(0);
    if(choice == '1'){Queue <Integer> q = new Queue(N);}
    else if(choice == '2'){Queue <Character> q = new Queue(N);}
    do{
        pl("1) enqueue"); pl("2) dequeue"); pl("3) show"); pl("4) exit");
        p("Enter function number : ");
        choice = keyboard.next().charAt(0);
        if(choice == '1'){
            p("Enter data: ");
            Object s = keyboard.next();
            q.enqueue(s);
        }
        else if(choice == '2'){Object s =  q.dequeue();
            pl(s);
        }
        else if(choice == '3'){q.show();}
    }while(choice != '4');

    }
}

1.After user input choice and I create generic object with type casting in do...while loop it can't find "q".

2.In public TYPE dequeue() method line "queueFront = item[front];" Object can't be convert to TYPE ,how can I fix it.

J. Sumittanun
  • 49
  • 1
  • 7
  • Everything is object and using Object is worst way to learn generic or even writing program at this stage.. – Luai Ghunim Nov 20 '17 at 00:32
  • The structure of your program is incorrect and Java does not work the way you appear to think it does. It is time to go back to some basic tutorials to understand variable scope. Unfortunately, StackOverflow is not a tutorial site and correcting your program would amount to a basic tutorial, which is off-topic here. – Jim Garrison Nov 20 '17 at 00:44

1 Answers1

0

Try to change private Object [] item to private TYPE [] item. Also you will need to know how to create a generic array for your example: How to create a generic array? How to create a generic array in Java?

You will need to change item = new Object[n]; to item = (TYPE[]) new Object[n];

However you should avoid creating generic types and instead you should inject them or use a factory to create them.

CodesInTheDark
  • 111
  • 2
  • 6