Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25
  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

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

    Java Code:
          String queueTailAddElement = "";//String Resp1 = "purple";  //??????? why is this an empty String???
            queue = x.queueTailAdd(queueTailAddElement);
    The code should be passing a value in the String, not an empty String
    Java Code:
            String queueTailAddElement = "purple";
            queue = x.queueTailAdd(queueTailAddElement);
    Why doesn't the output say something like, "[blue, pink, green, yellow, Resp1]" instead?
    Where in the posted output do you want to see that String?

    For example:
    Inputting preloaded String response black for dequeTailAdd element.
    [Resp4, Resp3, Resp5] <<<< This should be [blue, pink, green, yellow, Resp1]
    Last edited by Norm; 07-18-2019 at 02:06 PM.
    If you don't understand my response, don't ignore it, ask a question.

  2. #22
    SamJava_the_Hut is offline Senior Member
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    100
    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?
    Mark your comments so they are easy see. For example:

    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:
    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;
        }
    Quote Originally Posted by Norm View Post
    Java Code:
          String queueTailAddElement = "";//String Resp1 = "purple";  //??????? why is this an empty String???
            queue = x.queueTailAdd(queueTailAddElement);
    The code should be passing a value in the String, not an empty String
    Java Code:
            String queueTailAddElement = "purple";
            queue = x.queueTailAdd(queueTailAddElement);
    Norm, if we were just going to hard code "purple" right before the queueTailAdd function call, then what purpose did this serve in the GenericsSuite class:
    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");
    Why did you have me do that?

  3. #23
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

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

    Why did you have me do that?
    I gave an example of how the String could be coded with the Scanner class's constructor.
    Scanner input = new Scanner("Resp1\nResp2\nResp3\nResp4\nResp5");
    I did not give code that code be copied into your program without requiring some thought about what it means.
    It was up to you to replace the contents of the String with the values you want to see. for example:
    Scanner input = new Scanner("purple\nwhite\n...");
    However the calls to the Scanner class methods should NOT be in the methods being tested. The calls to the Scanner methods should be in the method doing the testing. In this code that is the main method.
    Last edited by Norm; 07-22-2019 at 02:14 PM.
    If you don't understand my response, don't ignore it, ask a question.

  4. #24
    SamJava_the_Hut is offline Senior Member
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    100
    Rep Power
    0

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

    There's still a major problem with the output. As you know, a Queue or Deque looks like this:

    HEAD [elements] TAIL

    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;
    	}
    	
    	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) {//"purple"
    		System.out.println("Are we adding to the same queue list? It should have a size of 4.");
    		System.out.println("The actual size of the queue list that the stupid program thinks it's " +
    						   "writing to is:");
    		System.out.println(queue.size());
    		queue.add(element);//((LinkedList<T>) queue).add(element);
    		return queue;
    	}	
    	
    	@Override
    	public void enqueue(T element) {//"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();
    	}	
    			
    }//end of GenericsSuite class
    As you can see, the queue list is declared on line 31 outside of all of the other methods, so it should have scope over the other methods.

    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.*;
    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){		
    		
    		/*
    		BuiltInExceptions z = new BuiltInExceptions();
    		//z.dinoEggs();
    		//z.parseNumType();
    		*/
    		
    		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 = "purple";//String Resp1 = "purple";
    		queue = x.queueTailAdd(queueTailAddElement);
    		System.out.println(queue);//we should see something like: [blue,pink,green,yellow,purple]
    		
    		String queueEnqueueElement = "white";//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();
    				
    	}//end of main method
    	
    }//end of class
    The output is this:
    Queue testing phase:
    Starting queue list: [blue, pink, green, yellow]
    Are we adding to the same queue list? It should have 4 elements.
    The actual size of the queue list that the stupid program thinks it's writing to is:
    0
    [purple]

    [purple, white]
    The value of the current queue head is: purple
    Now removing purple
    [white]
    The queue is not empty.
    queue's final size is: 1


    That said, my question is this:
    Why the hell, then, does it keep creating a new queue list after the fillQueueList method executes?

    This:
    0
    [purple]


    Should be:
    4
    [blue, pink, green, yellow, purple]


    Because the string purple was added to the tail to the queue list by the queueTailAdd method. "queue", has all of the other methods within its scope. What am I missing?

  5. #25
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

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

    There are several variables named: queue
    Java Code:
        private Queue<T> queue = new LinkedList<>();       //<<<<< When is anything added to this one
         
    //    @Override
        public Queue<T> fillQueueList(T[] startingArr){
            Queue<T> queue = new LinkedList<>();    //  <<< this one has items added to it
    If you don't understand my response, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

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
  •