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);
}
}
Deque
Comments (21)
- Popular
- New
- Old
You must be signed in to leave a comment
Anonymous #11023086
21 June 2022, 13:37
thats brilient
0
Lisa L
7 June 2022, 10:13
I won't think about deque or queues at this time. An ArrayLIst or an array are more than enough for now. Learning specialities is for later I'd say.
The easiest way to create a random number is to use Math.random()
That'll return a double in the range of 0 (inclusive) to 1 (exclusive) like 0.37627383.
This you can convert to the number you desire like for doubles between 0 - 9: Math.random() * 10
If you want ints between 0 - 9 then (int) (Math.random() * 10)
ints from 5 - 9: (int) (Math.random() * 5 + 5)
If you want to randomly creaty a lowercase letter... look at an ascii table like https://www.asciitable.com/
You'll see that each char has an index. Means there's a mapping from the index to the character. So eg. the index (decimal) 97 refers to 'a', 97 + 26 then refers to 'z'
so you easily can create random chars with that knowledge like
;
or in one line
You also could make it a method
now the rest should be easy for you ;)
0
Karol Grzeszczak
7 June 2022, 20:14
thank you so much i'll try two ways but i still have to do it Deque
0
Karol Grzeszczak
7 June 2022, 20:34
I'm also a bit scared of the pace of the course but I have to make it, the stakes are high for me
0
Lisa L
7 June 2022, 21:05
Yes, the course here can be very demanding at times. And some of the tasks are not doable with just the knowledge you got solely at Codegym. You should read additional books, too. That helps a lot.
0
Karol Grzeszczak
7 June 2022, 21:17
I mean, I'm taking another course plus codegym plus normal job
so I get up 5-6am and go to bed around 11-12pm
I buy a lot of books and sometimes normal talk are better for understanding because there always a lot variables
they cheated because the course I'm taking is not only for 20 hours a week like they said
I still need a lot of tips but from rookie in a month I think I've made a lot of progress anyway
I often come across walls, but I don't have much time to stick to them
for example now
You gave me a generator for letters from a to z and thank you very for this much but for example I have a problem to modify it only for the letter a
I have to generate a string of random length max (50) only one letter, not letters from a to z
like aaaaaaaaaaa
aaa
aaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaa
a
aaaaaaaaaaa
aa
aaaaaaa
etc...
0
Lisa L
8 June 2022, 05:17
If you need just the letter a, then you do not need a generator. It's just
char c = 'a';
(or you write code that has 97 as result and from that you initialize the char).
to repeat it x times you use a loop
Here you can modify the count variable to use random values (1 - 50)
int count = (char) (Math.random * 50 + 1);
you also could use the Random class if you like that one more. The usage is the same
random.nextInt(50) + 1;
(nextInt creates random values from 0 (incl) to 50 (excl) -> from 0 to 49, so you just add 1, like with Math.random()).
If you want to create 50 random strings, then you use an outer loop that runs 50 times. Count changes so this variable needs to be moved inside the outer loop.
Similar result, it needs to be reset after the inner loop ran and you saved the result.
And yes. This is not easy. It's a lot of things that you learn at the same time and it is confusing. I needed several attempts at several stages and I also gave up several times 🤣. But I also have seen that some paradigmas that weren't understandable at all at the beginning for me became common at some point. I didn't even think about it anymore. But you always will see/ learn new things when coding. The feeling that you make stupid mistakes will never go away.
0
Karol Grzeszczak
8 June 2022, 05:35
and so for a whole life 😄
0
Karol Grzeszczak
8 June 2022, 05:56
I'm getting old, but yeah it's fun 😃😃
0
Karol Grzeszczak
8 June 2022, 06:09
I don't know how to add it to pass the results to the array
0
Lisa L
8 June 2022, 06:19
You better use a list instead of an array.
0
Karol Grzeszczak
8 June 2022, 20:14
I changed the example to my version of the assignment
I was instructed to do it this way
1. In main, you create a queue with generated Strings
word merging should be done in main i mean generate random 'a'
2. You pass it to executeTasks, they are split into the appropriate lists
but as mentioned i have a problem with referencing things in code
it is still confusing for me☹️
☹️☹️☹️
0
Lisa L
9 June 2022, 05:47
This way?
0
Karol Grzeszczak
9 June 2022, 05:54
😲 wow
yes like this
0
Karol Grzeszczak
9 June 2022, 08:34
Ok You haven't added me to your friends, so i have reached message limit for today 🤣
next question
I need make Book ( class) with title and author
I wanted to change your generator so that many letters popped out like jhggusyafuc etc..
but it doesn't fit
i would like to generate random letters for titles and authors and insert them into linkedlist
random name generation fits because the list is going to be long hery long
0
Lisa L
9 June 2022, 13:58
I have explained how to create random characters above, eg.
this creates a random lowercase char. Now instead of appending the same char random times to a string, you create a random amount of random chars. Packed into your main:
works ;)
now you could replace concatenation with a StringBuilder (as before)
0
Lisa L
9 June 2022, 14:02
later, when you're a pro coder you probably create a Supplier and use a stream to generate the strings...
+1
Karol Grzeszczak
9 June 2022, 14:07
I'll study it when I get home thanks a lot 😊
0
Karol Grzeszczak
10 June 2022, 04:02
and now I have a problem how to pass the random result to produce a new book (object) with these random generated string in linked list
0
Lisa L
10 June 2022, 12:11
If you want a random string, then you just modify the above loop. You do not need NUM_TASKS strings but you start with one and you do not add it to a queue. So it should look like (using StringBuilder)...
cause you need two random strings you can think about creating a method that does the job for you and instead of passing the string as argument to the constructor you call the just written method.
In therory I have written nothing new, just reused the above said ;) 0
Karol Grzeszczak
11 June 2022, 08:21
yeah like this he put's the same title and author into every created book
0