Created
December 14, 2016 17:24
-
-
Save begetan/f26c566f2acfc87f75e0d9d44e38dd91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: interfaces/AdaptedRandomDoubles.java | |
// Creating an adapter with inheritance. | |
import java.nio.*; | |
import java.util.*; | |
public class AdaptedRandomDoubles extends RandomDoubles implements Readable { | |
private int count; | |
public AdaptedRandomDoubles(int count) { | |
this.count = count; | |
} | |
public int read(CharBuffer cb) { | |
if(count-- == 0) | |
return -1; | |
String result = Double.toString(next()) + " "; | |
cb.append(result); | |
return result.length(); | |
} | |
public static void main(String[] args) { | |
Scanner s = new Scanner(new AdaptedRandomDoubles(7)); | |
while(s.hasNextDouble()) | |
System.out.print(s.nextDouble() + " "); | |
} | |
} /* Output: | |
0.7271157860730044 0.5309454508634242 0.16020656493302599 0.18847866977771732 0.5166020801268457 0.2678662084200585 0.2613610344283964 | |
*///:~ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment