Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    95
    Rep Power
    0

    Default My function calls to a generic collections class are not doing anything

    Hi. I have a simple queue list, and I'm trying to make queue changes to it using another class, but the methods of that other class don't seem to be doing anything.
    Here's the code:

    Queue.java
    Java Code:
    package samsExperiments;
    import java.util.LinkedList;
    
    public class Queue<T> {//remember, Queue is FIFO
    	
    	private LinkedList<T> myQueueList = new LinkedList<>();//type variable T allows us to store any type object
    															 //in the queue that we want.
    	
    	//add an element to the to the end of the queue
    	public void enqueue(T element) {
    		myQueueList.add(element);
    	}
    	
    	//remove the element at the front of the queue
    	public T dequeue() {
    		return myQueueList.removeFirst();
    	}
    	
    	public int size() {
    		return myQueueList.size();
    	}
    }
    SamsExperimentsMain.java
    Java Code:
    package samsExperiments;
    
    import java.util.Scanner;
    import java.util.Arrays;
    import java.util.Collections;
    import SortingAlgorithms.*;
    import customExceptions.BuiltInExceptions;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Vector;
    import java.util.Hashtable;
    import java.util.Map;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.ListIterator;
    
    public class SamsExperimentsMain {
    	
    	public static void main(String[] args){		
    
    		Queue q = new Queue();
    		
    		LinkedList<String> qList = new LinkedList<>();
    		qList.add("mat");
    		qList.add("cat");
    		qList.add("sat");
    		qList.add("fat");
    		qList.add("rat");
    		Collections.sort(qList);
    		System.out.println(qList);
    		String element = qList.get(2);
    		q.enqueue(element);
    		System.out.println(qList);
    		q.dequeue();
    		int size;
    		size = q.size();
    		System.out.println("Size = " + size);		
    		
    	}//end of main method	
    	
    }//end of class

    The output is this:

    [cat, fat, mat, rat, sat]
    [cat, fat, mat, rat, sat]
    Size = 0

    The output I was expecting is this:

    [cat, fat, mat, rat, sat]
    [mat, cat, fat, rat, sat]
    Size = 5

    What happened? Why are my function calls not doing anything to the list?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,995
    Rep Power
    32

    Default Re: My function calls to a generic collections class are not doing anything

    Check which collection the code is working with. There are two: Queue and LinkedList

    Note: Queue is used without a generic type being defined. See how LinkedList is declared and used.
    Last edited by Norm; 06-30-2019 at 02:51 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    95
    Rep Power
    0

    Default Re: My function calls to a generic collections class are not doing anything

    I'm having trouble populating my Queue and Deque objects, and I don't know what I'm doing wrong.
    Here's the code:

    GenericsSuite.java
    Java Code:
    package samsExperiments;
    import java.util.Scanner;
    import interfaces.GenericQueue;
    import java.util.Collections;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Stack;
    import java.util.Deque;
    import java.util.ArrayDeque;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.lang.reflect.Array;
    //Queue = FIFO, Stack = LIFO, LinkedList = FILO; Deque can be both a stack and a queue.
    
    public class GenericsSuite<T>{
    	
    	//Queue Stuff:
    	private Queue<T> queue = new LinkedList<>();	
    	
    	public Queue<T> fillQueueList(T[] startingArr){
    		Queue<T> queue = new LinkedList<>();
    		for(T element : startingArr) {
    			queue.add(element);
    		}
    		System.out.println("Expected list: " + queue);
    		return queue;
    	}
    	
    	public void queueTailAdd(T element) {
    		System.out.println("Enter an element of the correct datatype to add for the queue list tail:");
    		Scanner input = new Scanner(System.in);
    		element = (T) input.next();//we had to add a cast to T by putting "(T)" for the sake of the generic datatype.
    		queue.add(element);
    	}
    	
    	public void enqueue(T element) {
    		queue.offer(element);//LinkedList equivalent of queue.addLast(element)
    	}
    	
    	public T dequeue() {
    		return queue.remove();//The queue interface only allows you to remove elements from the head of the queue.
    	}
    	
    	public int size() {
    		return queue.size();
    	}
    	
    	
    	//Deque stuff:
    	private Deque<T> deque = new LinkedList<>();
    	
    	public Deque<T> fillDequeList(T[] startingArr){
    		Deque<T> deque = new LinkedList<>();
    		//Deque<T> deque = new ArrayDeque<>();//experiment with later...
    		for(T element : startingArr) {
    			queue.add(element);
    		}
    		System.out.println("Expected list: " + deque);
    		return deque;
    	}
    	
    	public void dequeTailAdd(T element) {
    		System.out.println("Enter an element of the correct datatype to add for the deque list tail:");
    		Scanner input = new Scanner(System.in);
    		element = (T) input.next();//we had to add a cast to T by putting "(T)" for the sake of the generic datatype.
    		((LinkedList<T>) queue).addLast(element);
    	}
    	
    	public void dequeHeadAdd(T element) {
    		System.out.println("Enter an element of the correct datatype to add for the deque list head:");
    		Scanner input = new Scanner(System.in);
    		element = (T) input.next();//we had to add a cast to T by putting "(T)" for the sake of the generic datatype.
    		((LinkedList<T>) queue).addFirst(element);
    	}
    	
    	public T dequeHeadDelete() {
    		System.out.println("Removing " + deque.peekFirst() + " at [0]");
    		return deque.removeFirst();
    	}
    	
    	public T dequeTailDelete() {
    		System.out.println("Removing " + deque.peekLast() + " at [" + (queue.size()-1) + "]");
    		return deque.removeLast();
    	}
    	
    	public void dequeRegularAdd(T element) {
    		System.out.println("Enter an element of the correct datatype to add for the regular add method:");
    		Scanner input = new Scanner(System.in);
    		element = (T) input.next();//we had to add a cast to T by putting "(T)" for the sake of the generic datatype.
    		((LinkedList<T>) queue).addFirst(element);
    	}
    	
    	public void dequeHeadPeek() {
    		System.out.println("The first element in the deque list currently is: " + deque.peekFirst());
    	}
    	
    	public void dequeTailPeek() {
    		System.out.println("The last element in the deque list currently is: " + deque.peekLast());
    	}
    }
    SamsExperimentsMain.java
    Java Code:
    package samsExperiments;
    
    import java.util.Scanner;
    import java.util.Arrays;
    import java.util.Calendar;
    import java.util.Collections;
    import java.util.Deque;
    
    import SortingAlgorithms.*;
    import customExceptions.BuiltInExceptions;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Vector;
    import java.util.Hashtable;
    import java.util.Map;
    import java.util.Queue;
    import java.util.TreeMap;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.ListIterator;
    import java.util.Locale;
    import java.time.DayOfWeek;
    import java.time.LocalDate;//yyyy-mm-dd
    import java.time.LocalTime;//hh-mm-ss-nn
    import java.time.Month;
    import java.time.LocalDateTime;//yyyy-mm-dd-hh-ss-nn
    import java.time.*;
    
    public class SamsExperimentsMain {
    	
    	public static void main(String[] args){
    		
    		String[] startingArr = {"blue","pink","green","yellow"};
    		
    		GenericsSuite x = new GenericsSuite();
    		
    		//Queue stuff:
    		System.out.println("Queue testing phase:");
    		Queue<String> queue = new LinkedList<>();
    		x.fillQueueList(startingArr);
    		System.out.println("Actual returned queue list: " + queue);
    		
    		Object element = null;
    		x.queueTailAdd(element);		
    		System.out.println(queue);//we should see something like: [purple, blue, pink, green, yellow]
    		
    		x.dequeue();
    		System.out.println(queue);//we should see something like: [blue, pink, green, yellow]
    		int qSize = x.size();
    		System.out.println("queue's final size is: " + qSize);
    		System.out.println();
    		
    		//Deque stuff:
    		System.out.println("Deque testing phase:");
    		Deque<String> deque = new LinkedList<>();
    		x.fillDequeList(startingArr);
    		System.out.println("Actual returned deque list: " + deque);
    		
    		Object element2 = null;
    		x.dequeRegularAdd(element2);
    		System.out.println(deque);//we should see something like: [purple, blue, pink, green, yellow]
    		
    		x.dequeHeadAdd(element2);
    		System.out.println(deque);//we should see something like: [red, purple, blue, pink, green, yellow]
    		
    		x.dequeTailAdd(element2);
    		System.out.println(deque);//we should see something like: [red, purple, blue, pink, green, yellow, black]
    		
    		x.dequeHeadDelete();
    		System.out.println(deque);//we should see something like: [purple, blue, pink, green, yellow, black]
    		
    		x.dequeTailDelete();
    		System.out.println(deque);//we should see something like: [purple, blue, pink, green, yellow]		
    		
    	}//end of main method	
    	
    }//end of class
    The output looks like this:
    Queue testing phase:
    Expected list: [blue, pink, green, yellow]
    Actual returned queue list: []
    Enter an element of the correct datatype to add for the queue list tail:
    purple
    []
    []
    queue's final size is: 0

    Deque testing phase:
    Expected list: []
    Actual returned deque list: []
    Enter an element of the correct datatype to add for the regular add method:
    purple
    []
    Enter an element of the correct datatype to add for the deque list head:
    red
    []
    Enter an element of the correct datatype to add for the deque list tail:
    black
    []
    Removing null at [0]
    Exception in thread "main" java.util.NoSuchElementException
    at java.util.LinkedList.removeFirst(Unknown Source)
    at samsExperiments.GenericsSuite.dequeHeadDelete(Gene ricsSuite.java:78)
    at samsExperiments.SamsExperimentsMain.main(SamsExper imentsMain.java:69)

    It's painfully obvious that the program crashes right when it's trying to remove elements that don't even exist. Why are the add methods in the functions of my GenericsSuite class not doing anything?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,995
    Rep Power
    32

    Default Re: My function calls to a generic collections class are not doing anything

    Can you fix the warning so the code gets a clean compile?
    For example:
    warning: [rawtypes] found raw type: GenericsSuite
    warning: [unchecked] unchecked cast
    warning: [unchecked] unchecked call to dequeHeadAdd(T) as a member of the raw type GenericsSuite

    You need to properly use generics

    Also the code has too many different Scanner objects that make it impossible to execute for testing without continually having to make responses. There should be one Scanner that can be preloaded in its constructor with all the needed input for the program and not require user input.


    Java Code:
            x.queueTailAdd(element);        //<<<<<< adds element to one queue
            System.out.println(queue);     //  <<<<<< displays contents of another queue
    Why use the two queues? Adding to one and showing the other does not make sense.
    Last edited by Norm; 07-01-2019 at 01:56 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    95
    Rep Power
    0

    Default Re: My function calls to a generic collections class are not doing anything

    Quote Originally Posted by Norm View Post
    Can you fix the warning so the code gets a clean compile?
    For example:
    warning: [rawtypes] found raw type: GenericsSuite
    warning: [unchecked] unchecked cast
    warning: [unchecked] unchecked call to dequeHeadAdd(T) as a member of the raw type GenericsSuite

    You need to properly use generics
    It seems I have gravely mistaken the actual application of generics. You know that startingArr array of color strings? Well, I thought that by using a raw type of T for the functions in my GenericsSuite class, it would make my program more dynamic, because it would magically enable my queue and deque to accept values of any datatype from the startingArr array.

    But to my understanding, that poses a danger of the user accidentally entering mixed datatypes into the queue or deque, right?

    Quote Originally Posted by Norm View Post
    Also the code has too many different Scanner objects that make it impossible to execute for testing without continually having to make responses. There should be one Scanner that can be preloaded in its constructor with all the needed input for the program and not require user input.
    Can you give me some pseudo code please?

    Quote Originally Posted by Norm View Post
    Java Code:
            x.queueTailAdd(element);        //<<<<<< adds element to one queue
            System.out.println(queue);     //  <<<<<< displays contents of another queue
    Why use the two queues? Adding to one and showing the other does not make sense.
    That's definitely not what I intended. Though I'm also still confused about how to distinguish between raw types and type parameter names, especially when it comes to calling the functions from my main method:

    SamsExperimentsMain.java
    Java Code:
    package samsExperiments;
    
    import java.util.Scanner;
    import java.util.Arrays;
    import java.util.Calendar;
    import java.util.Collections;
    import java.util.Deque;
    
    import SortingAlgorithms.*;
    import customExceptions.BuiltInExceptions;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Vector;
    import java.util.Hashtable;
    import java.util.Map;
    import java.util.Queue;
    import java.util.TreeMap;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.ListIterator;
    import java.util.Locale;
    import java.time.DayOfWeek;
    import java.time.LocalDate;//yyyy-mm-dd
    import java.time.LocalTime;//hh-mm-ss-nn
    import java.time.Month;
    import java.time.LocalDateTime;//yyyy-mm-dd-hh-ss-nn
    import java.time.*;
    
    public class SamsExperimentsMain {
    	
    	public static void main(String[] args){
    		
    		String[] startingArr = {"blue","pink","green","yellow"};
    		
    		GenericsSuite x = new GenericsSuite();
    		
    		//Queue stuff:
    		System.out.println("Queue testing phase:");
    		Queue<String> queue = new LinkedList<>();
    		queue = x.fillQueueList(startingArr);
    		System.out.println("Actual returned queue list: " + queue);
    		
    		Object element = null;
    		queue = x.queueTailAdd(element);		
    		System.out.println(queue);//we should see something like: [purple, blue, pink, green, yellow]
    		
    		queue = x.dequeue();
    		System.out.println(queue);//we should see something like: [blue, pink, green, yellow]
    		int qSize = x.size();
    		System.out.println("queue's final size is: " + qSize);
    		System.out.println();
    		
    	}//end of main method	
    	
    }//end of class
    Line 44 error: "Type mismatch: cannot convert from void to Queue<String>"

    Line 47 error: "Type mismatch: cannot convert from Object to Queue<String>"

    Does this mean I have to make 4 copies of the same function with different generic datatypes like this:
    Java Code:
    //public Queue<String> queueTailAdd(T element)
    	//public Queue<Integer> queueTailAdd(T element)
    	//public Queue<Character> queueTailAdd(T element)
    	//public Queue<Double> queueTailAdd(T element)
    	public void queueTailAdd(T element) {
    		System.out.println("Enter an element of the correct datatype to add for the queue list tail:");
    		Scanner input = new Scanner(System.in);
    		element = (T) input.next();//we had to add a cast to T for the sake of the raw datatype.		
    		queue.add(element);
    	}
    just so I can pass values from any type of startingArr array? And if that's true, what good are these then:

    The most commonly used type parameter names are:

    E - Element (used extensively by the Java Collections Framework)
    K - Key
    N - Number
    T - Type
    V - Value
    S,U,V etc. - 2nd, 3rd, 4th types

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,995
    Rep Power
    32

    Default Re: My function calls to a generic collections class are not doing anything

    some pseudo code
    Java Code:
      Scanner scnr = new Scanner("resp1\nresp2\nresp3\n"); // Load constructor with the 3 needed responses
    Using a Scanner with preloaded responses for testing ensures that the same response is given each time the code is executed and makes the testing easier because the tester does not have to type in anything.


    Here is an example of declaring a generic collection that takes Strings:
    Java Code:
          Queue<String> queue = new LinkedList<>();
    Here is an example of NOT using a generic collection correctly:
    Java Code:
        GenericsSuite x = new GenericsSuite();   //<<<<< Missing <TYPE> see above

    Line 44 error: "Type mismatch: cannot convert from void to Queue<String>"
    The method does not return a Queue object. It is defined as void.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    95
    Rep Power
    0

    Default Re: My function calls to a generic collections class are not doing anything

    Quote Originally Posted by Norm View Post
    Java Code:
      Scanner scnr = new Scanner("resp1\nresp2\nresp3\n"); // Load constructor with the 3 needed responses
    Using a Scanner with preloaded responses for testing ensures that the same response is given each time the code is executed and makes the testing easier because the tester does not have to type in anything.
    Dude, I have never even imagined a scanner constructor looking like that in my life
    Forgive me for requesting spoon feeding, but can you tell me what java-file or class this constructor goes in?
    How do I make sure that they automatically get entered in, with a set up like this:
    Java Code:
    String[] startingArr = {"blue","pink","green","yellow"};
    		
    		GenericsSuite<String> x = new GenericsSuite();
    		
    		Scanner queueInput = new Scanner("queueTailAdd\n");
    		
    		queueTailAdd = "purple";//[purple, blue, pink, green, yellow]
    		
    		Scanner dequeInput = new Scanner("dequeRegularAdd\n dequeHeadAdd\n dequeTailAdd\n ");
    		
    		dequeRegularAdd = "purple";//[purple, blue, pink, green, yellow]
    		dequeHeadAdd = "red";//[red, purple, blue, pink, green, yellow]
    		dequeTailAdd = "black";//[red, purple, blue, pink, green, yellow, black]
    Quote Originally Posted by Norm View Post
    Here is an example of declaring a generic collection that takes Strings:
    Java Code:
          Queue<String> queue = new LinkedList<>();
    Here is an example of NOT using a generic collection correctly:
    Java Code:
        GenericsSuite x = new GenericsSuite();   //<<<<< Missing <TYPE> see above
    Well, my attempt to do what you said looks like this:
    Java Code:
    GenericsSuite<String> x = new GenericsSuite();
    Which left me with another warning, "Type safety: The expression of type GenericsSuite needs unchecked conversion to conform to GenericsSuite<String>".

    Quote Originally Posted by Norm View Post
    The method does not return a Queue object. It is defined as void.
    My fix attempt looks like this:
    Java Code:
    public Queue<T> queueTailAdd(T element) {
    		//System.out.println("Enter an element of the correct datatype to add for the queue list tail:");
    		//Scanner input = new Scanner(System.in);
    		//element = (T) input.next();//we had to add a cast to T for the sake of the raw datatype.		
    		return queue.add(element);
    	}
    The return statement error now says, "Type mismatch: cannot convert from boolean to Queue<T>".
    How in the world does the program confuse a Queue<T> for boolean? What's true or false got to do with adding an element to a list here?
    I never even specified any boolean conditions or variables within the scope of the queueTailAdd method definition.

    One last thing. Can you please just give me the syntax that will fix the warning complaints about these statements:
    Java Code:
    element = (T) input.next();
    I don't know anything about manually casting, and I'm still struggling to understand how raw datatypes integrate with Java 8 generics.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,995
    Rep Power
    32

    Default Re: My function calls to a generic collections class are not doing anything

    Type mismatch: cannot convert from boolean to Queue<T>".
    What value does the add method return? The error message says it returns a boolean, not a Queue<T> object.
    The type returned by the add method must match what the queueTailAdd method is defined to return.

    "Type safety: The expression of type GenericsSuite needs unchecked conversion to conform to GenericsSuite<String>".
    Compare your statements with the one that worked. Do you see the missing <>?


    what java-file or class this constructor goes in?
    It would go in the GenericsSuite class and all the Scanner objects defined inside the methods would be removed.
    Something like this:
    Java Code:
    public class GenericsSuite<T>{
    
        Scanner input = new Scanner("resp1\nresp2\netc..."); // <<<<<<  preload all responses  
         
        //Queue Stuff:
        private Queue<T> queue = new LinkedList<>();    
         
        public Queue<T> fillQueueList(T[] startingArr){
            Queue<T> queue = new LinkedList<>();
            for(T element : startingArr) {
                queue.add(element);
            }
            System.out.println("Expected list: " + queue);
            return queue;
        }
         
        public void queueTailAdd(T element) {
            System.out.println("Enter an element of the correct datatype to add for the queue list tail:");
    //        Scanner input = new Scanner(System.in);
            element = (T) input.next();//we had to add a cast to T by putting "(T)" for the sake of the generic datatype.
            queue.add(element);
        }
         
        public void enqueue(T element) {
            queue.offer(element);//LinkedList equivalent of queue.addLast(element)
        }
         
        public T dequeue() {
            return queue.remove();//The queue interface only allows you to remove elements from the head of the queue.
        }
         
        public int size() {
            return queue.size();
        }
         
         
        //Deque stuff:
        private Deque<T> deque = new LinkedList<>();
         
        public Deque<T> fillDequeList(T[] startingArr){
            Deque<T> deque = new LinkedList<>();
            //Deque<T> deque = new ArrayDeque<>();//experiment with later...
            for(T element : startingArr) {
                queue.add(element);
            }
            System.out.println("Expected list: " + deque);
            return deque;
        }
         
        public void dequeTailAdd(T element) {
            System.out.println("Enter an element of the correct datatype to add for the deque list tail:");
    //        Scanner input = new Scanner(System.in);
            element = (T) input.next();//we had to add a cast to T by putting "(T)" for the sake of the generic datatype.
            ((LinkedList<T>) queue).addLast(element);
        }
         
        public void dequeHeadAdd(T element) {
            System.out.println("Enter an element of the correct datatype to add for the deque list head:");
    //        Scanner input = new Scanner(System.in);
            element = (T) input.next();//we had to add a cast to T by putting "(T)" for the sake of the generic datatype.
            ((LinkedList<T>) queue).addFirst(element);
        }
         
        public T dequeHeadDelete() {
            System.out.println("Removing " + deque.peekFirst() + " at [0]");
            return deque.removeFirst();
        }
         
        public T dequeTailDelete() {
            System.out.println("Removing " + deque.peekLast() + " at [" + (queue.size()-1) + "]");
            return deque.removeLast();
        }
         
        public void dequeRegularAdd(T element) {
            System.out.println("Enter an element of the correct datatype to add for the regular add method:");
    //        Scanner input = new Scanner(System.in);
            element = (T) input.next();//we had to add a cast to T by putting "(T)" for the sake of the generic datatype.
            ((LinkedList<T>) queue).addFirst(element);
        }
         
        public void dequeHeadPeek() {
            System.out.println("The first element in the deque list currently is: " + deque.peekFirst());
        }
         
        public void dequeTailPeek() {
            System.out.println("The last element in the deque list currently is: " + deque.peekLast());
        }
    }
    Requesting a value inside of a worker method is wrong. The value should be obtained in the testing code and passed to the worker method.
    Last edited by Norm; 07-02-2019 at 12:57 PM.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    95
    Rep Power
    0

    Default Re: My function calls to a generic collections class are not doing anything

    Quote Originally Posted by Norm View Post
    Requesting a value inside of a worker method is wrong. The value should be obtained in the testing code and passed to the worker method.
    Agreed. Unfortunately, a lot of google search resources say to just use the @SuppressWarnings("unchecked") thing, and I'm sick of it. I don't want to sweep it under the rug.

    I understand this:
    Java Code:
    int a = (int) 3.678;
    All it does is shave off the 678. Makes perfect sense.

    This not so much:
    Java Code:
    element = (T) input.next();//Type safety: Unchecked cast from String to T
    Seriously, what does that mean, and what's the syntax to fix that statement? I'm stumped.
    How do you tell it to use a scanner with preloaded values:
    Java Code:
    String Resp1 = "purple";//queueTailAdd
    	String Resp2 = "orange";//dequeRegularAdd
    	String Resp3 = "red";//dequeHeadAdd
    	String Resp4 = "black";//dequeTailAdd
    	Scanner input = new Scanner("Resp1\nResp2\nResp3\nResp4");
    At my current level of understanding, I also find it very odd that we're using predefined values from a Scanner that's normally used for getting user input from a keyboard. Should that feel weird, or am I missing something?

    All of the code snippets above are from this GenericsSuite.java:
    Java Code:
    package samsExperiments;
    import interfaces.Pair;
    import java.util.Scanner;
    import interfaces.GenericsInt;
    import java.util.Collections;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Stack;
    import java.util.Deque;
    import java.util.ArrayDeque;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Arrays;
    import java.lang.reflect.Array;
    import java.util.HashMap;//HashMap<K,V>()
    import java.util.Map;
    import java.util.TreeMap;//TreeMap<K,V>()
    //Queue = FIFO, Stack = LIFO, LinkedList = FILO; Deque can be both a stack and a queue.
    
    public class GenericsSuite<T> implements GenericsInt<T>{
    	
    	private T t;
    	
    	public void set(T t) {
    		this.t = t;
    	}
    	public T get() {
    		return t;
    	}
    	String Resp1 = "purple";//queueTailAdd
    	String Resp2 = "orange";//dequeRegularAdd
    	String Resp3 = "red";//dequeHeadAdd
    	String Resp4 = "black";//dequeTailAdd
    	Scanner input = new Scanner("Resp1\nResp2\nResp3\nResp4");	
    	
    	//Queue Stuff:
    	private Queue<T> queue = new LinkedList<>();	
    	
    	@Override
    	public Queue<T> fillQueueList(T[] startingArr){
    		Queue<T> queue = new LinkedList<>();
    		for(T element : startingArr) {
    			queue.add(element);
    		}
    		System.out.println("Expected list: " + queue);
    		return queue;
    	}
    	
    	@Override
    	public Queue<T> queueTailAdd(T element) {
    		//System.out.println("Enter an element of the correct datatype to add for the queue list tail:");
    		//Scanner input = new Scanner(System.in);
    		element = (T) input.next();//Type safety: Unchecked cast from String to T		
    		queue.add(element);
    		return queue;
    	}
    	
    	@Override
    	public void enqueue(T element) {
    		queue.offer(element);//LinkedList equivalent of queue.addLast(element)
    	}
    	
    	@Override
    	public T dequeue() {
    		return queue.remove();//The queue interface only allows you to remove elements from the head of the queue.
    	}
    	
    	@Override
    	public int size() {
    		return queue.size();
    	}
    	
    	
    	//Deque stuff:
    	private Deque<T> deque = new LinkedList<>();
    	
    	@Override
    	public Deque<T> fillDequeList(T[] startingArr){
    		Deque<T> deque = new LinkedList<>();
    		//Deque<T> deque = new ArrayDeque<>();//experiment with later...
    		for(T element : startingArr) {
    			queue.add(element);
    		}
    		System.out.println("Expected list: " + deque);
    		return deque;
    	}
    	
    	@Override
    	public void dequeRegularAdd(T element) {
    		System.out.println("Enter an element of the correct datatype to add for the regular add method:");
    		//Scanner input = new Scanner(System.in);
    		element = (T) input.next();
    		((LinkedList<T>) queue).addFirst(element);
    	}
    	
    	@Override
    	public void dequeHeadAdd(T element) {
    		System.out.println("Enter an element of the correct datatype to add for the deque list head:");
    		Scanner input = new Scanner(System.in);
    		element = (T) input.next();//we had to add a cast to T for the sake of the raw datatype.
    		((LinkedList<T>) queue).addFirst(element);
    	}
    	
    	@Override
    	public void dequeTailAdd(T element) {
    		System.out.println("Enter an element of the correct datatype to add for the deque list tail:");
    		Scanner input = new Scanner(System.in);
    		element = (T) input.next();//we had to add a cast to T for the sake of the raw datatype.
    		((LinkedList<T>) queue).addLast(element);
    	}
    	
    	
    	
    	@Override
    	public T dequeHeadDelete() {
    		System.out.println("Removing " + deque.peekFirst() + " at [0]");
    		return deque.removeFirst();
    	}
    	
    	@Override
    	public T dequeTailDelete() {
    		System.out.println("Removing " + deque.peekLast() + " at [" + (queue.size()-1) + "]");
    		return deque.removeLast();
    	}
    	
    	
    	@Override
    	public void dequeHeadPeek() {
    		System.out.println("The first element in the deque list currently is: " + deque.peekFirst());
    	}
    	
    	@Override
    	public void dequeTailPeek() {
    		System.out.println("The last element in the deque list currently is: " + deque.peekLast());
    	}
    	
    	private Stack<T> stack = new Stack<>();
    	
    	
    	//Use ArrayList elements to populate a hashmap:
    	List<String> list = new ArrayList<>();
    	Map<Integer,List<String>> myMap = new HashMap<>();
    
    	@Override
    	public boolean isEmpty() {
    		// TODO Auto-generated method stub
    		return false;
    	}
    	@Override
    	public boolean isFull() {
    		// TODO Auto-generated method stub
    		return false;
    	}	
    }
    The errors are gone in GenericsSuite.java, but now I'm having type collisions in my SamsExperimentsMain.java class too:
    Java Code:
    package samsExperiments;
    
    import java.util.Scanner;
    import java.util.Arrays;
    import java.util.Calendar;
    import java.util.Collections;
    import java.util.Deque;
    
    import SortingAlgorithms.*;
    import customExceptions.BuiltInExceptions;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Vector;
    import java.util.Hashtable;
    import java.util.Map;
    import java.util.Queue;
    import java.util.TreeMap;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.ListIterator;
    import java.util.Locale;
    import java.time.DayOfWeek;
    import java.time.LocalDate;//yyyy-mm-dd
    import java.time.LocalTime;//hh-mm-ss-nn
    import java.time.Month;
    import java.time.temporal.ChronoUnit;
    import java.time.LocalDateTime;//yyyy-mm-dd-hh-ss-nn
    import java.io.IOException;
    import java.time.*;
    
    public class SamsExperimentsMain {
    	
    	public static void main(String[] args){
    		
    		String[] startingArr = {"blue","pink","green","yellow"};
    		
    		GenericsSuite<String> x = new GenericsSuite<>();	
    		
    		//Queue stuff:
    		System.out.println("Queue testing phase:");
    		Queue<String> queue = new LinkedList<>();
    		queue = x.fillQueueList(startingArr);
    		System.out.println("Actual returned queue list: " + queue);
    		
    		Object element = null;
    		queue = x.queueTailAdd(element);		
    		System.out.println(queue);//we should see something like: [purple, blue, pink, green, yellow]
    		
    		queue = x.dequeue();
    		System.out.println(queue);//we should see something like: [blue, pink, green, yellow]
    		int qSize = x.size();
    		System.out.println("queue's final size is: " + qSize);
    		System.out.println();
    		
    		//Deque stuff:
    		System.out.println("Deque testing phase:");
    		Deque<String> deque = new LinkedList<>();
    		x.fillDequeList(startingArr);
    		System.out.println("Actual returned deque list: " + deque);
    		
    		Object element2 = null;
    		x.dequeRegularAdd(element2);
    		System.out.println(deque);//we should see something like: [purple, blue, pink, green, yellow]
    		
    		x.dequeHeadAdd(element2);
    		System.out.println(deque);//we should see something like: [red, purple, blue, pink, green, yellow]
    		
    		x.dequeTailAdd(element2);
    		System.out.println(deque);//we should see something like: [red, purple, blue, pink, green, yellow, black]
    		
    		x.dequeHeadDelete();
    		System.out.println(deque);//we should see something like: [purple, blue, pink, green, yellow, black]
    		
    		x.dequeTailDelete();
    		System.out.println(deque);//we should see something like: [purple, blue, pink, green, yellow]		
    		
    	}//end of main method	
    	
    }//end of class
    line 46: "The method queueTailAdd(String) in the type GenericsSuite<String> is not applicable for the arguments (Object)"

    line 49: "Type mismatch: cannot convert from String to Queue<String>"


    Is <T> not a dynamic object that can accept the <String> generic type?

    Isn't the statement that invokes a method supposed to be what chooses the Wrapper Class type that replaces the <T>?
    And if so, why isn't line 37 in SamsExperimentsMain.java not satisfying that requirement:
    Java Code:
    GenericsSuite<String> x = new GenericsSuite<>();
    and why isn't line 46 cooperating:
    Java Code:
    queue = x.queueTailAdd(element);
    Seriously, what the hell? Can't a string be an object? How is it that primitive types and wrapper classes are so different?

    If this is true:
    --Primitive Type and Wrapper Class equivalents:
    -int = Integer
    -char = Character
    -String = String
    -double = Double

    Then why am I having so much trouble?
    For example, if this code snippet:
    Java Code:
    @Override
    	public Queue<T> queueTailAdd(T element) {
    		//System.out.println("Enter an element of the correct datatype to add for the queue list tail:");
    		//Scanner input = new Scanner(System.in);
    		element = (T) input.next();//Type safety: Unchecked cast from String to T		
    		queue.add(element);
    		return queue;
    	}
    looked like this instead:
    Java Code:
    @Override
    	public Queue<String> queueTailAdd(T element) {
    		//System.out.println("Enter an element of the correct datatype to add for the queue list tail:");
    		//Scanner input = new Scanner(System.in);
    		element = (T) input.next();//Type safety: Unchecked cast from String to T		
    		queue.add(element);
    		return queue;
    	}
    Doesn't that defeat the whole purpose of Generics being dynamic when it comes to <T> allowing us to choose a wrapper class replacement?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,995
    Rep Power
    32

    Default Re: My function calls to a generic collections class are not doing anything

    The code does not use the value of element that is passed to it. It immediately changes the value it has received.
    It should use the value that is passed to it, not change it.
    Java Code:
        public Queue<String> queueTailAdd(T element) {
            //System.out.println("Enter an element of the correct datatype to add for the queue list tail:");
            //Scanner input = new Scanner(System.in);
            element = (T) input.next();//        <<<<< Do  not do that
            queue.add(element);
            return queue;
        }
    The code should look like this:
    Java Code:
        public Queue<T> queueTailAdd(T element) {
            queue.add(element);   // Use the passed value
            return queue;
        }
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    95
    Rep Power
    0

    Default Re: My function calls to a generic collections class are not doing anything

    Quote Originally Posted by Norm View Post
    The code should look like this:
    Java Code:
        public Queue<T> queueTailAdd(T element) {
            queue.add(element);   // Use the passed value
            return queue;
        }
    OK, but you forgot how to tell me how to pass the predefined values to the worker methods:
    Java Code:
    String Resp1 = "purple";//queueTailAdd
        String Resp2 = "orange";//dequeRegularAdd
        String Resp3 = "red";//dequeHeadAdd
        String Resp4 = "black";//dequeTailAdd
        Scanner input = new Scanner("Resp1\nResp2\nResp3\nResp4");

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,995
    Rep Power
    32

    Default Re: My function calls to a generic collections class are not doing anything

    how to pass the predefined values to the worker methods:
    The same way to pass any value to a method:
    Java Code:
       theMethod(theValue);  //  pass theValue to theMethod
    Another example:
    Java Code:
          Scanner scnr = new Scanner("put the desired responses here");  // declare with user's responses
    
          ...   later in the code
          System.out.println("Enter data");        // ask user
          String resp = scnr.nextLine();           // get preloaded response
          theMethod(resp);                         // call method and pass it the response
    
          ... somewhere else in the code
          void theMethod(String val) {
             // process val here
          }
    Last edited by Norm; 07-09-2019 at 01:53 PM.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    95
    Rep Power
    0

    Default Re: My function calls to a generic collections class are not doing anything

    The way my main method currently is, the String and the generic <String> types are not happy with each other.

    SamsExperimentsMain.java:
    Java Code:
    package samsExperiments;
    
    import java.util.Scanner;
    import java.util.Arrays;
    import java.util.Calendar;
    import java.util.Collections;
    import java.util.Deque;
    
    import SortingAlgorithms.*;
    import customExceptions.BuiltInExceptions;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Vector;
    import java.util.Hashtable;
    import java.util.Map;
    import java.util.Queue;
    import java.util.TreeMap;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.ListIterator;
    import java.util.Locale;
    import java.time.DayOfWeek;
    import java.time.LocalDate;//yyyy-mm-dd
    import java.time.LocalTime;//hh-mm-ss-nn
    import java.time.Month;
    import java.time.temporal.ChronoUnit;
    import java.time.LocalDateTime;//yyyy-mm-dd-hh-ss-nn
    import java.io.IOException;
    import java.time.*;
    
    public class SamsExperimentsMain {
    	
    	public static void main(String[] args){
    		
    		String[] startingArr = {"blue","pink","green","yellow"};
    		
    		GenericsSuite<String> x = new GenericsSuite<>();	
    		
    		//Queue stuff:
    		System.out.println("Queue testing phase:");
    		Queue<String> queue = new LinkedList<>();
    		queue = x.fillQueueList(startingArr);
    		System.out.println("Actual returned queue list: " + queue);
    		
    		Queue<String> element = null;
    		queue = x.queueTailAdd(element);		
    		System.out.println(queue);//we should see something like: [purple, blue, pink, green, yellow]
    		
    		queue = x.dequeue();
    		System.out.println(queue);//we should see something like: [blue, pink, green, yellow]
    		int qSize = x.size();
    		System.out.println("queue's final size is: " + qSize);
    		System.out.println();
    		
    		//Deque stuff:
    		System.out.println("Deque testing phase:");
    		Deque<String> deque = new LinkedList<>();
    		x.fillDequeList(startingArr);
    		System.out.println("Actual returned deque list: " + deque);
    		
    		Object element2 = null;
    		x.dequeRegularAdd(element2);
    		System.out.println(deque);//we should see something like: [purple, blue, pink, green, yellow]
    		
    		x.dequeHeadAdd(element2);
    		System.out.println(deque);//we should see something like: [red, purple, blue, pink, green, yellow]
    		
    		x.dequeTailAdd(element2);
    		System.out.println(deque);//we should see something like: [red, purple, blue, pink, green, yellow, black]
    		
    		x.dequeHeadDelete();
    		System.out.println(deque);//we should see something like: [purple, blue, pink, green, yellow, black]
    		
    		x.dequeTailDelete();
    		System.out.println(deque);//we should see something like: [purple, blue, pink, green, yellow]			
    		
    	}//end of main method
    	
    }//end of class
    Line 46: "The method queueTailAdd(String) in the type GenericsSuite<String> is not applicable for the arguments (Queue<String>)".

    Lines 62, 65, and 68: "The method dequeTailAdd(String) in the type GenericsSuite<String> is not applicable for the arguments (Object)".

    The errors on lines 46, 62, 65, and 68 make sense, but I don't know how to fix them.

    It's making me wonder if I should just make all of my methods in the GenericsSuite class type void, since the deleting methods are all void and work just fine.
    Either way, have a look at my now compile-error-free GenericsSuite class and GenericsInt interface:

    GenericsSuite.java:
    Java Code:
    package samsExperiments;
    import interfaces.Pair;
    import java.util.Scanner;
    import interfaces.GenericsInt;
    import java.util.Collections;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Stack;
    import java.util.Deque;
    import java.util.ArrayDeque;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Arrays;
    import java.lang.reflect.Array;
    import java.util.HashMap;//HashMap<K,V>()
    import java.util.Map;
    import java.util.TreeMap;//TreeMap<K,V>()
    //Queue = FIFO, Stack = LIFO, LinkedList = FILO; Deque can be both a stack and a queue.
    
    public class GenericsSuite<T> implements GenericsInt<T>{
    	
    	private T t;
    	
    	public void set(T t) {
    		this.t = t;
    	}
    	public T get() {
    		return t;
    	}
    	String Resp1 = "purple";//queueTailAdd
    	String Resp2 = "orange";//dequeRegularAdd
    	String Resp3 = "red";//dequeHeadAdd
    	String Resp4 = "black";//dequeTailAdd
    	Scanner input = new Scanner("Resp1\nResp2\nResp3\nResp4");	
    	
    	//Queue Stuff:
    	private Queue<T> queue = new LinkedList<>();	
    	
    	@Override
    	public Queue<T> fillQueueList(T[] startingArr){
    		Queue<T> queue = new LinkedList<>();
    		for(T element : startingArr) {
    			queue.add(element);
    		}
    		System.out.println("Expected list: " + queue);
    		return queue;
    	}
    	
    	@Override
    	public Queue<T> queueTailAdd(T element) {
    		System.out.println("Inputting preloaded String response " + Resp1 + " for queueTailAdd element.");
    		Resp1 = input.next();
    		element = (T) Resp1;//Type safety: Unchecked cast from String to T		
    		((LinkedList<T>) queue).add(element);
    		return queue;
    	}	
    	
    	@Override
    	public void enqueue(T element) {
    		queue.offer(element);//LinkedList equivalent of queue.addLast(element)
    	}
    	
    	@Override
    	public void dequeue() {
    		//The queue interface only allows you to remove elements from the head of the queue.
    		//Maybe this is why it's sensible to use the void return type?
    		queue.remove();
    		//return queue.remove();
    	}
    	/*
    	@Override
    	public T dequeue() {
    		return queue.remove();//The queue interface only allows you to remove elements from the head of the queue.
    	}
    	*/
    	@Override
    	public int size() {
    		return queue.size();
    	}
    	
    	
    	//Deque stuff:
    	private Deque<T> deque = new LinkedList<>();
    	
    	@Override
    	public Deque<T> fillDequeList(T[] startingArr){
    		Deque<T> deque = new LinkedList<>();
    		//Deque<T> deque = new ArrayDeque<>();//experiment with later...
    		for(T element : startingArr) {
    			queue.add(element);
    		}
    		System.out.println("Expected list: " + deque);
    		return deque;
    	}
    	
    	@Override
    	public void dequeRegularAdd(T element) {
    		System.out.println("Inputting preloaded String response " + Resp2 + " for dequeRegularAdd element.");
    		Resp2 = input.next();
    		element = (T) Resp2;
    		((LinkedList<T>) deque).addFirst(element);
    	}
    	
    	@Override
    	public void dequeHeadAdd(T element) {
    		System.out.println("Inputting preloaded String response " + Resp3 + " for dequeHeadAdd element.");
    		Resp3 = input.next();
    		element = (T) Resp3;
    		((LinkedList<T>) deque).addFirst(element);
    	}
    	
    	@Override
    	public void dequeTailAdd(T element) {
    		System.out.println("Inputting preloaded String response " + Resp4 + " for dequeTailAdd element.");
    		Resp4 = input.next();
    		element = (T) Resp4;
    		((LinkedList<T>) deque).addLast(element);
    	}
    	
    	
    	
    	@Override
    	public T dequeHeadDelete() {
    		System.out.println("Removing " + deque.peekFirst() + " at [0]");
    		return deque.removeFirst();
    	}
    	
    	@Override
    	public T dequeTailDelete() {
    		System.out.println("Removing " + deque.peekLast() + " at [" + (queue.size()-1) + "]");
    		return deque.removeLast();
    	}
    	
    	
    	@Override
    	public void dequeHeadPeek() {
    		System.out.println("The first element in the deque list currently is: " + deque.peekFirst());
    	}
    	
    	@Override
    	public void dequeTailPeek() {
    		System.out.println("The last element in the deque list currently is: " + deque.peekLast());
    	}	
    }
    GenericsInt.java:
    Java Code:
    package interfaces;
    import interfaces.Pair;
    import java.util.Scanner;
    import interfaces.GenericsInt;
    import java.util.Collections;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Stack;
    import java.util.Deque;
    import java.util.ArrayDeque;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Arrays;
    import java.lang.reflect.Array;
    import java.util.HashMap;//HashMap<K,V>()
    import java.util.Map;
    import java.util.TreeMap;//TreeMap<K,V>()
    
    public interface GenericsInt<T> {
    	
    	boolean isEmpty();
    	boolean isFull();	
    	int size();
    	
    	//Queue Stuff
    	Queue<T> fillQueueList(T[] t);
    	Queue<T> queueTailAdd(T t);
    	void enqueue(T t);
    	void dequeue();	
    
    	//Deque Stuff
    	Deque<T> fillDequeList(T[] t);
    	void dequeRegularAdd(T t);
    	void dequeHeadAdd(T t);
    	void dequeTailAdd(T t);
    	T dequeHeadDelete();
    	T dequeTailDelete();
    	void dequeHeadPeek();
    	void dequeTailPeek();
    }
    I never imagined that it would be this difficult to make Primitive and Wrapper Class data types get along with each other.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,995
    Rep Power
    32

    Default Re: My function calls to a generic collections class are not doing anything

    "The method queueTailAdd(String) in the type GenericsSuite<String> is not applicable for the arguments (Queue<String>)".
    The method takes a String not a queue object as its argument.
    how to fix them.
    Use a String as argument to the method.

    Java Code:
    GenericsSuite<String> x = new GenericsSuite<>();
    That defines an instance of the class that takes String. That means the T in the class's definition is a String.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,995
    Rep Power
    32

    Default Re: My function calls to a generic collections class are not doing anything

    Your testing classes are way, way, way too big. Start with a small program of 10-20 lines, get it to work and then add another 5-10 lines, get it to work and continue.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    95
    Rep Power
    0

    Default Re: My function calls to a generic collections class are not doing anything

    Quote Originally Posted by Norm View Post
    The method takes a String not a queue object as its argument.
    Use a String as argument to the method.

    Java Code:
    GenericsSuite<String> x = new GenericsSuite<>();
    That defines an instance of the class that takes String. That means the T in the class's definition is a String.
    Surely you're not telling me to do this,
    Java Code:
    @Override
    	public Queue<String> queueTailAdd(String element) {//The method queueTailAdd(String) of type GenericsSuite<T> 
    												  //must override or implement a supertype method
    		System.out.println("Inputting preloaded String response " + Resp1 + " for queueTailAdd element.");
    		Resp1 = input.next();
    		element = (T) Resp1;//Type mismatch: cannot convert from T to String		
    		((LinkedList<T>) queue).add(element);//The method add(T) in the type LinkedList<T> is not 
    											 //applicable for the arguments (String)
    		return queue;
    	}
    Or this:
    Java Code:
    public class GenericsSuite<String> implements GenericsInt<String>{
    Or this:
    Java Code:
    queue = x.queueTailAdd(String element);//String cannot be resolved to a variable
    		                                       //Syntax error on token "element", delete this token
    Please forgive me, but I suck at argument passing, especially when it's generic.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,995
    Rep Power
    32

    Default Re: My function calls to a generic collections class are not doing anything

    The code has correct method definitions:
    Java Code:
       public Queue<T> queueTailAdd(T element) {
    Define an instance of the class to be used with Strings:
    Java Code:
      GenericsSuite<String> x = new GenericsSuite<>();
    and call its methods with Strings:
    Java Code:
      
       String element = "some value";
       queue = x.queueTailAdd(element);
    Last edited by Norm; 07-12-2019 at 01:49 AM.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    95
    Rep Power
    0

    Default Re: My function calls to a generic collections class are not doing anything

    I'm having some non-syntax-error output problems.
    Here's my latest code:

    GenericsInt.java:
    Java Code:
    package interfaces;
    import interfaces.Pair;
    import java.util.Scanner;
    import interfaces.GenericsInt;
    import java.util.Collections;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Stack;
    import java.util.Deque;
    import java.util.ArrayDeque;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Arrays;
    import java.lang.reflect.Array;
    import java.util.HashMap;//HashMap<K,V>()
    import java.util.Map;
    import java.util.TreeMap;//TreeMap<K,V>()
    
    public interface GenericsInt<T> {
    	
    	//Queue Stuff
    	Queue<T> fillQueueList(T[] t);
    	Queue<T> queueTailAdd(T t);	
    	void enqueue(T t);//add an element at the tail
    	void dequeue();//remove the element at the head
    	void peek(T t);//see what value is in the head element
    	boolean isQueueEmpty();
    	int queueSize();
    
    	//Deque Stuff
    	Deque<T> fillDequeList(T[] t);
    	Deque<T> dequeRegularAdd(T t);
    	Deque<T> dequeHeadAdd(T t);
    	Deque<T> dequeTailAdd(T t);
    	T dequeHeadDelete();
    	T dequeTailDelete();
    	void dequeHeadPeek();
    	void dequeTailPeek();
    }
    GenericsSuite.java:
    Java Code:
    package samsExperiments;
    import interfaces.Pair;
    import java.util.Scanner;
    import interfaces.GenericsInt;
    import java.util.Collections;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Stack;
    import java.util.Deque;
    import java.util.ArrayDeque;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Arrays;
    import java.lang.reflect.Array;
    import java.util.HashMap;//HashMap<K,V>()
    import java.util.Map;
    import java.util.TreeMap;//TreeMap<K,V>()
    //Queue = FIFO, Stack = LIFO, LinkedList = FILO; Deque can be both a stack and a queue.
    
    public class GenericsSuite<T> implements GenericsInt<T>{
    	
    	private T t;
    	
    	public void set(T t) {
    		this.t = t;
    	}
    	public T get() {
    		return t;
    	}
    	//startingArr = "blue","pink","green","yellow"
    	String Resp1 = "purple";//queueTailAdd
    	String Resp2 = "white";//enqueue
    	String Resp3 = "orange";//dequeRegularAdd
    	String Resp4 = "red";//dequeHeadAdd
    	String Resp5 = "black";//dequeTailAdd
    	Scanner input = new Scanner("Resp1\nResp2\nResp3\nResp4\nResp5");
    	
    	//Queue Stuff - HEAD [elements] TAIL
    	private Queue<T> queue = new LinkedList<>();	
    	
    	@Override
    	public Queue<T> fillQueueList(T[] startingArr){
    		Queue<T> queue = new LinkedList<>();
    		for(T element : startingArr) {
    			queue.add(element);
    		}
    		System.out.println("Starting queue list: " + queue);
    		return queue;
    	}
    	
    	@Override
    	public Queue<T> queueTailAdd(T element) {
    		System.out.println("Inputting preloaded String response " + Resp1 + " for queueTailAdd element.");
    		Resp1 = input.next();
    		element = (T) Resp1;//"purple"		
    		queue.add(element);//((LinkedList<T>) queue).add(element);
    		return queue;
    	}	
    	
    	@Override
    	public void enqueue(T element) {
    		System.out.println("Inputting preloaded String response " + Resp2 + " for enqueue element.");
    		Resp2 = input.next();
    		element = (T) Resp2;//"white"
    		queue.offer(element);//LinkedList equivalent of queue.addLast(element); adds element to the tail
    	}
    	
    	@Override
    	public void peek(T element) {
    		element = queue.peek();
    		System.out.println("The value of the current queue head is: " + element);
    	}
    	
    	@Override
    	public void dequeue() {
    		//The queue interface only allows you to remove elements from the head of the queue.
    		System.out.println("Now removing " + queue.peek());
    		queue.remove();
    	}
    	
    	@Override
    	public boolean isQueueEmpty() {
    		if(queue.isEmpty()) {
    			System.out.println("The queue is empty.");
    			return true;
    		}
    		else {
    			System.out.println("The queue is not empty.");
    			return false;
    		}
    	}
    	
    	@Override
    	public int queueSize() {
    		return queue.size();
    	}	
    	
    	//Deque stuff:
    	private Deque<T> deque = new LinkedList<>();
    	
    	@Override
    	public Deque<T> fillDequeList(T[] startingArr){
    		Deque<T> deque = new LinkedList<>();
    		//Deque<T> deque = new ArrayDeque<>();//experiment with later...
    		for(T element : startingArr) {
    			deque.add(element);
    		}
    		System.out.println("Starting deque list: " + deque);
    		return deque;
    	}
    	
    	@Override
    	public Deque<T> dequeRegularAdd(T element) {
    		System.out.println("Inputting preloaded String response " + Resp3 + " for dequeRegularAdd element.");
    		Resp3 = input.next();
    		element = (T) Resp3;//"orange"
    		deque.addFirst(element);//((LinkedList<T>) deque).addFirst(element);
    		return deque;
    	}
    	
    	@Override
    	public Deque<T> dequeHeadAdd(T element) {
    		System.out.println("Inputting preloaded String response " + Resp4 + " for dequeHeadAdd element.");
    		Resp4 = input.next();
    		element = (T) Resp4;//"red"
    		deque.addFirst(element);//((LinkedList<T>) deque).addFirst(element);
    		return deque;
    		
    	}
    	
    	@Override
    	public Deque<T> dequeTailAdd(T element) {
    		System.out.println("Inputting preloaded String response " + Resp5 + " for dequeTailAdd element.");
    		Resp5 = input.next();
    		element = (T) Resp5;//"black"
    		deque.addLast(element);//((LinkedList<T>) deque).addLast(element);
    		return deque;
    	}	
    	
    	@Override
    	public T dequeHeadDelete() {
    		System.out.println("Removing " + deque.peekFirst() + " at [0]");
    		return deque.removeFirst();
    	}
    	
    	@Override
    	public T dequeTailDelete() {
    		System.out.println("Removing " + deque.peekLast() + " at [" + (queue.size()-1) + "]");
    		return deque.removeLast();
    	}	
    	
    	@Override
    	public void dequeHeadPeek() {
    		System.out.println("The first [head] element in the deque list currently is: " + deque.peekFirst());
    	}
    	
    	@Override
    	public void dequeTailPeek() {
    		System.out.println("The last [tail] element in the deque list currently is: " + deque.peekLast());
    	}
    	
    }//end of GenericsSuite class
    SamsExperimentsMain.java:
    Java Code:
    package samsExperiments;
    
    import java.util.Scanner;
    import java.util.Arrays;
    import java.util.Calendar;
    import java.util.Collections;
    import java.util.Deque;
    
    import SortingAlgorithms.*;
    import customExceptions.BuiltInExceptions;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Vector;
    import java.util.Hashtable;
    import java.util.Map;
    import java.util.Queue;
    import java.util.TreeMap;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.ListIterator;
    import java.util.Locale;
    import java.time.DayOfWeek;
    import java.time.LocalDate;//yyyy-mm-dd
    import java.time.LocalTime;//hh-mm-ss-nn
    import java.time.Month;
    import java.time.temporal.ChronoUnit;
    import java.time.LocalDateTime;//yyyy-mm-dd-hh-ss-nn
    import java.io.IOException;
    import java.time.*;
    
    public class SamsExperimentsMain {
    	
    	public static void main(String[] args){		
    		
    		String[] startingArr = {"blue","pink","green","yellow"};
    		
    		GenericsSuite<String> x = new GenericsSuite<>();	
    		
    		//Queue stuff:
    		System.out.println("Queue testing phase:");
    		Queue<String> queue = new LinkedList<>();
    		queue = x.fillQueueList(startingArr);		
    		
    		String queueTailAddElement = "";//String Resp1 = "purple";
    		queue = x.queueTailAdd(queueTailAddElement);
    		System.out.println(queue);//we should see something like: [blue,pink,green,yellow,purple]
    		
    		String queueEnqueueElement = "";//String Resp2 = "white";
    		x.enqueue(queueEnqueueElement);
    		System.out.println(queue);//we should see something like: [blue,pink,green,yellow,purple,white]
    		
    		String peekElement = "";
    		x.peek(peekElement);
    		
    		x.dequeue();
    		System.out.println(queue);//we should see something like: [pink,green,yellow,purple,white]
    		boolean isQueueEmpty = x.isQueueEmpty();
    		int qSize = x.queueSize();
    		if(isQueueEmpty == false) {
    			System.out.println("queue's final size is: " + qSize);
    		}		
    		System.out.println();
    		
    		//Deque stuff:
    		System.out.println("Deque testing phase:");
    		Deque<String> deque = new LinkedList<>();
    		deque = x.fillDequeList(startingArr);
    		
    		String dequeRegularAddElement = "";//String Resp3 = "orange";
    		deque = x.dequeRegularAdd(dequeRegularAddElement);
    		System.out.println(deque);//we should see something like: [orange,blue,pink,green,yellow]
    		
    		String dequeHeadAddElement = "";//String Resp4 = "red";
    		deque = x.dequeHeadAdd(dequeHeadAddElement);
    		System.out.println(deque);//we should see something like: [red,orange,blue,pink,green,yellow]
    		
    		String dequeTailAddElement = "";//String Resp5 = "black";
    		deque = x.dequeTailAdd(dequeTailAddElement);
    		System.out.println(deque);//we should see something like: [red,orange,blue,pink,green,yellow,black]
    		
    		x.dequeHeadDelete();
    		System.out.println(deque);//we should see something like: [orange,blue,pink,green,yellow,black]
    		
    		x.dequeTailDelete();
    		System.out.println(deque);//we should see something like: [orange,blue,pink,green,yellow]
    		
    		x.dequeHeadPeek();
    		
    		x.dequeTailPeek();
    		
    	}//end of main method
    	
    }//end of class
    The output is this:
    Queue testing phase:
    Starting queue list: [blue, pink, green, yellow]
    Inputting preloaded String response purple for queueTailAdd element.
    [Resp1]
    Inputting preloaded String response white for enqueue element.
    [Resp1, Resp2]
    The value of the current queue head is: Resp1
    Now removing Resp1
    [Resp2]
    The queue is not empty.
    queue's final size is: 1

    Deque testing phase:
    Starting deque list: [blue, pink, green, yellow]
    Inputting preloaded String response orange for dequeRegularAdd element.
    [Resp3]
    Inputting preloaded String response red for dequeHeadAdd element.
    [Resp4, Resp3]
    Inputting preloaded String response black for dequeTailAdd element.
    [Resp4, Resp3, Resp5]
    Removing Resp4 at [0]
    [Resp3, Resp5]
    Removing Resp5 at [0]
    [Resp3]
    The first [head] element in the deque list currently is: Resp3
    The last [tail] element in the deque list currently is: Resp3


    Why are the element add and remove element functions not using the linked lists from the fillQueueList and fillDequeList functions?

    And why is not using the correct color name strings from here:
    Java Code:
    String Resp1 = "purple";//queueTailAdd
    	String Resp2 = "white";//enqueue
    	String Resp3 = "orange";//dequeRegularAdd
    	String Resp4 = "red";//dequeHeadAdd
    	String Resp5 = "black";//dequeTailAdd
    	Scanner input = new Scanner("Resp1\nResp2\nResp3\nResp4\nResp5");

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,995
    Rep Power
    32

    Default Re: My function calls to a generic collections class are not doing anything

    Why are the element add and remove element functions not using ...
    Can you add some comments to the output where you think the output should be different?
    Mark your comments so they are easy see. For example:
    Now removing Resp1
    [Resp2]
    The queue is not empty.
    queue's final size is: 1 <<<<<<<<<<< Wrong Here the size should be 2
    The add methods should NOT be calling Scanner methods to read in the data to be added. The data to be added should be passed to the method by the calling code:
    get data from user
    callMethod(usersData)
    Java Code:
       @Override
        public Queue<T> queueTailAdd(T element) {
            System.out.println("Inputting preloaded String response " + Resp1 + " for queueTailAdd element.");  //<<<<<<<< NO
            Resp1 = input.next();                  // <<<<<<<<<< NO
            element = (T) Resp1;//"purple"      //<<<<<<<<<<<< NO this changes the value of the passed variable
            queue.add(element);//((LinkedList<T>) queue).add(element);
            return queue;
        }
    The method should look like this:
    Java Code:
       @Override
        public Queue<T> queueTailAdd(T element) {
              queue.add(element);  //  add the value passed to the method
            return queue;
        }
    Last edited by Norm; 07-18-2019 at 01:05 PM.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    95
    Rep Power
    0

    Default Re: My function calls to a generic collections class are not doing anything

    Quote Originally Posted by Norm View Post
    Can you add some comments to the output where you think the output should be different?
    Norm, unfortunately, there isn't much more I comments I can add that will fulfill that request any further.
    As you may be aware, queues and deques look like this:

    HEAD [elements] TAIL

    That is why I put all of the "we should see something like:..." comments in SamsExperimentsMain.java.
    For example:
    Java Code:
    String queueTailAddElement = "";//String Resp1 = "purple";
    		queue = x.queueTailAdd(queueTailAddElement);
    		System.out.println(queue);//we should see something like: [blue,pink,green,yellow,purple]
    If you looked at the output towards the end of my last post, not only did my program completely ignore the linked list built from iterating through the startingArr array, but the "purple" populated the linked list as "Resp1" instead of "purple". It shouldn't be:
    [Resp1].
    It should be:
    [blue,pink,green,yellow,purple]

    Did you not read the existing code comments in my last post? I'm sorry, but I don't know what else you're looking for when you ask me, "Can you add some comments to the output where you think the output should be different?"

    In any case, the greatest problem that needs to be addressed first is the fact that all methods after the "fillQueueList" and "fillDequeList" ignore those.
    For example, what happened with the output here:
    Queue testing phase:
    Starting queue list: [blue, pink, green, yellow]
    Inputting preloaded String response purple for queueTailAdd element.
    [Resp1]

    Why doesn't the output say something like, "[blue, pink, green, yellow, Resp1]" instead?

Page 1 of 2 12 LastLast

Similar Threads

  1. function does not calls in actionListener
    By Adithya in forum Advanced Java
    Replies: 5
    Last Post: 06-21-2014, 02:37 PM
  2. Collections sorting a list made of generic class objects
    By andreiutz10 in forum New To Java
    Replies: 5
    Last Post: 02-07-2012, 05:56 PM
  3. Generic objects and collections
    By robbie.26 in forum Advanced Java
    Replies: 2
    Last Post: 12-24-2011, 11:29 AM
  4. Generic Collections---Dysfuntional Java
    By beezerbutt in forum New To Java
    Replies: 6
    Last Post: 06-28-2009, 07:37 PM
  5. Replies: 2
    Last Post: 08-03-2007, 04:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •