Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************************************
- * Dependencies: DynamicArray<String>.java StdDraw.java StdOut.java
- *
- * This program takes the maximum array capacity N as a command-line argument.
- * Then, the user repeatedly clicks cells in the array to fill with "X".
- * Cells that are in between the size and the capacity of the array are
- * marked with a full black square.
- *
- ****************************************************************************/
- package prove;
- import edu.princeton.cs.introcs.StdDraw;
- import edu.princeton.cs.introcs.StdOut;
- public class InteractiveArrayVisualizer {
- public static void main(String[] args) throws Exception {
- // Array of maximum capacity N (wouldn't fit the line otherwise...)
- int N = 20;
- if (args.length == 1) N = Integer.parseInt(args[0]);
- // repeatedly open site specified my mouse click and draw resulting system
- StdOut.println(N);
- StdDraw.show(0);
- DynamicArray<String> v = new DynamicArray<String>();
- v.add("X");
- ArrayVisualizer.draw(v);
- StdDraw.show(0);
- while (true) {
- // detected mouse click
- if (StdDraw.mousePressed()) {
- // screen x coordinates (don't care about y)
- double x = StdDraw.mouseX();
- // convert to position i
- int i = (int) (Math.floor(x+0.5) );
- // open site i provided it's in bounds
- if (i >= 0 && i <= N ) {
- StdOut.println(i);
- if (i < v.size() - 1) {
- v.set(i, "X");
- }
- else if (i == v.size() - 1) {
- v.pop();
- }
- else {
- v.add("X"); // Random value
- }
- StdOut.println("Size: " + v.size());
- StdOut.println("Capacity: " + v.capacity());
- }
- // draw the array
- StdDraw.show(0);
- ArrayVisualizer.draw(v);
- }
- StdDraw.show(100);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement