How to send from deque and split it into two lists?

205 Views Asked by At

Hello everyone I was given the assignment in which I have

  1. In main, you create a queue with generated Strings word genereting should be done in main
  2. You pass it to executeTasks, they are split into the appropriate lists

I have a problem with referencing things in code it is still confusing for me☹️

it was much easier on the numbers

Write a program that in the ArrayDeque queue will put 50 objects storing strings (strings), consisting of the letter 'a' repeated a random number of times (repeat range: 1-50). Filling the object with the repeats of the letter 'a' can be done with a for loop. Part 2

Extend the program from the first part in such a way that you pass the created queue to the method of your class, which will separate the objects from the queue into two ArrayList collections. One of them will hold objects with an even number of 'a' characters, the other one with an odd number.

in general, I have done the task in a slightly different way, but I need to correct it

import java.util.*;                                                     
import java.lang.*;                                                     


class TaskManager {                                                     

List<String> executedTasks;
List<String> even;
List<String> odd;

// constructor
public TaskManager() {                                               
    executedTasks = new ArrayList<>();
    even = new ArrayList<>();
    odd = new ArrayList<>();

}                                                                    

//method serving the list of tasks
public void executeTasks(Deque<String> theQueue) {                   
    while (theQueue.size() > 0) {                                     
        String theTask = theQueue.poll();                              
        System.out.println("Processing the task: " + theTask);
        char c = 'a';
        for (int n = 0; n < 50; n++) {
            String result = "";
            int count = (char) (Math.random() * 50 + 1);

            for (int i = 0; i < count; i++) {
                result += c;
            }                                  
            System.out.println(result);
            if (result.length() % 2 == 0) {
                even.add(result);
            } else {
                odd.add(result);
            }
        executedTasks.add(theTask);


            System.out.println("\nExecuted total " + executedTasks.size() + " tasks\n");  


        }

    }

 }
}


/* Name of the class has to be "Main" only if the class is public. */
public class QueuesAndLoops {                                                  

    public static void main(String[] args) throws java.lang.Exception { 


        char c = 'a';


        Deque<String> taskQueue1 = new ArrayDeque<>();                    

        for (int n = 0; n < 1; n++) {                                       
            taskQueue1.offer("The first task number " + (n + 1));
            
        }


        TaskManager taskExecutor = new TaskManager();                     
        taskExecutor.executeTasks(taskQueue1);                            

        System.out.println("Parzyste:");
        System.out.println(taskExecutor.even);
        System.out.println("Nieparzyste:");
        System.out.println(taskExecutor.odd);
    }
}

and this code works but I have a problem to change it to the required version

1

There are 1 best solutions below

0
On

no one replied but it was about reworking it in a similar way

import java.util.*;
import java.lang.*;


class TaskManager {

List<String> even;
List<String> odd;

// constructor
public TaskManager() {
    even = new ArrayList<>();
    odd = new ArrayList<>();

}

//method serving the list of tasks
public void executeTasks(Deque<String> theQueue) {

        String task;
        while ((task = theQueue.poll()) != null) { 
            System.out.println(task);

           boolean isAdded = task.length() % 2 == 0 ? even.add(task) : odd.add(task);


        }
    }
}


public class QueuesAndLoops {

    public static void main(String[] args) throws java.lang.Exception {

        final int NUM_TASKS = 50;
        String chr = "a";
        Deque<String> taskQueue1 = new ArrayDeque<>();

        for (int i = 0; i < NUM_TASKS; i++) {
            taskQueue1.offer(chr.repeat((int) (Math.random() * 50 + 1)));
        }

        TaskManager taskExecutor = new TaskManager();
        taskExecutor.executeTasks(taskQueue1);

        System.out.println("Even:");
        System.out.println(taskExecutor.even);
        System.out.println("Odd:");
        System.out.println(taskExecutor.odd);
    }
}