Guest User

Untitled

a guest
Sep 9th, 2015
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. //AdaptRandDoubles.java
  2. package my.eckel;
  3.  
  4. import java.nio.*;
  5. import java.util.*;
  6.  
  7. public class AdaptRandDoubles extends RandDoubles implements Readable {
  8.     private int count;
  9.     public AdaptRandDoubles(int count) {
  10.         this.count = count;
  11.     }
  12.     @Override
  13.     public int read(CharBuffer cb) {
  14.         if (count-- == 0)
  15.             return -1;
  16.         String result = Double.toString(next()) + " ";
  17.         cb.append(result);
  18.         return result.length();
  19.     }
  20.     public static void main(String[] args) {
  21.         Scanner s = new Scanner(new AdaptRandDoubles(2));
  22.         while (s.hasNextDouble())
  23.             System.out.println(s.nextDouble() + " ");
  24.     }
  25. }
  26. //------------------------------------------------------------
  27. //RandDoubles.java
  28. package my.eckel;
  29.  
  30. import java.util.*;
  31.  
  32. public class RandDoubles {
  33.     private static Random rand = new Random(47);
  34.     public double next() { return rand.nextDouble(); }
  35.     public static void main(String[] args) {
  36.         RandDoubles rd = new RandDoubles();
  37.         for (int i = 0; i < 7; i++)
  38.             System.out.println(rd.next() + " ");
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment