Guest User

Untitled

a guest
Jan 4th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Queue;
  5. import java.util.Random;
  6.  
  7. class WhileLoop {
  8.  
  9.     public static void main(String[] args) {
  10.         Task34 task1 = new Task34();
  11.         task1.run();
  12.  
  13.     }
  14.  
  15. }
  16.  
  17. class Task34 {
  18.  
  19.     public void run() {
  20.         final Queue<String> queue = new ArrayDeque<>();
  21.         final RandomLengthStringGenerator randomLengthStringGenerator = new RandomLengthStringGenerator();
  22.  
  23.         for (int i = 0; i < 50; i++) {
  24.             final String randomString = randomLengthStringGenerator.generateRandomString();
  25.             queue.offer(randomString);
  26.         }
  27.  
  28.         final QueueSplitter queueSplitter = new QueueSplitter(queue);
  29.  
  30.         final List<String> onlyOdd = queueSplitter.getOnlyOdd();
  31.         final List<String> onlyEven = queueSplitter.getOnlyEven();
  32.  
  33.         System.out.println(onlyOdd);
  34.         System.out.println();
  35.         System.out.println(onlyEven);
  36.     }
  37.  
  38. }
  39.  
  40. class QueueSplitter {
  41.  
  42.     private Queue<String> queue;
  43.  
  44.     public QueueSplitter(Queue<String> queue) {
  45.         this.queue = queue;
  46.     }
  47.  
  48.     public List<String> getOnlyEven() {
  49.         final List<String> result = new ArrayList<>();
  50.  
  51.         for (String element : queue) {
  52.             if (element.length() % 2 == 0) {
  53.                 result.add(element);
  54.             }
  55.         }
  56.         return result;
  57.  
  58.     }
  59.  
  60.     public List<String> getOnlyOdd() {
  61.         final List<String> result = new ArrayList<>();
  62.  
  63.         for (String element : queue) {
  64.             if (element.length() % 2 != 0) {
  65.                 result.add(element);
  66.             }
  67.         }
  68.  
  69.         return result;
  70.  
  71.     }
  72.  
  73. }
  74.  
  75. class RandomLengthStringGenerator {
  76.  
  77.     private Random random = new Random();
  78.  
  79.     public String generateRandomString() {
  80.         int randomLength = random.nextInt(50) + 1;
  81.  
  82.         String result = "";
  83.  
  84.         for (int i = 0; i < randomLength; i++) {
  85.             result += "a";
  86.         }
  87.  
  88.         return result;
  89.     }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment