Commit ba020de9 authored by Adam Blank's avatar Adam Blank
Browse files

Code after lecture

parent 9ecf4e10
Showing with 21 additions and 6 deletions
+21 -6
......@@ -4,7 +4,7 @@ public class DoggieAnimation {
StdDraw.setCanvasSize(1024, 768);
StdDraw.setScale(-1024, 1024);
IntBag bag = null;
IntBag bag = new IntBag(1, 10);
// Add each picture once
for (int i = 1; i <= 10; i++) {
......@@ -22,7 +22,7 @@ public class DoggieAnimation {
// draw it
StdDraw.picture(0, 0, pic);
// pause
StdDraw.pause(2000);
StdDraw.pause(20);
System.out.println(pic);
}
}
......
import java.util.ArrayList;
import java.util.Random;
/**
* An IntBag represents a multiset of integers between min and max.
* That is, each number between min and max can occur some non-negative
* number of times in a particular IntBag.
*/
public class IntBag {
// arrays
// lists
// dictionaries
// sets
// tuples
private final ArrayList<Integer> list;
private final int min;
private final int max;
// constructors in java are like __init__ in python
public IntBag(int min, int max) {
this.list = new ArrayList<>();
this.min = min;
this.max = max;
}
/**
......@@ -15,7 +29,7 @@ public class IntBag {
* @param value the value to be added
*/
public void add(int value) {
this.list.add(value);
}
/**
......@@ -24,7 +38,7 @@ public class IntBag {
* @return true iff the bag is empty
*/
public boolean isEmpty() {
return false;
return this.list.isEmpty();
}
/**
......@@ -33,6 +47,7 @@ public class IntBag {
* @return the element picked from the bag
*/
public int pick() {
return -1;
Random r = new Random();
return this.list.get(r.nextInt(min - 1, max));
}
}
pictures/hopper1.jpg

135 KB

pictures/hopper10.jpg

83.6 KB

pictures/hopper2.jpg

161 KB

pictures/hopper3.jpg

83.9 KB

pictures/hopper4.jpg

104 KB

pictures/hopper5.jpg

57.2 KB

pictures/hopper6.jpg

39.9 KB

pictures/hopper7.jpg

64.9 KB

pictures/hopper8.jpg

116 KB

pictures/hopper9.jpg

87.5 KB

Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment