Skip to content

Commit 175adfe

Browse files
committed
moar
1 parent 4d1536d commit 175adfe

30 files changed

Lines changed: 756 additions & 0 deletions

‎v3/css/pytutor.css‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,3 +785,65 @@ div.ExecutionVisualizer .pycrazy-highlight-prev-and-cur {
785785
#optTabularView td.stepTableTd {
786786
padding: 3px 10px;
787787
}
788+
789+
790+
/* BEGIN Java frontend by David Pritchard and Will Gwozdz */
791+
792+
/* stack and queue css by Will Gwozdz */
793+
div.ExecutionVisualizer table.queueTbl,
794+
div.ExecutionVisualizer table.stackTbl {
795+
background-color: #ffffc6;
796+
}
797+
798+
div.ExecutionVisualizer table.queueTbl,
799+
div.ExecutionVisualizer table.stackTbl {
800+
border: 0px solid black;
801+
border-spacing: 0px;
802+
}
803+
804+
div.ExecutionVisualizer table.stackTbl td.stackElt,
805+
div.ExecutionVisualizer table.queueTbl td.queueElt {
806+
padding-left: 8px;
807+
padding-right: 8px;
808+
padding-top: 2px;
809+
padding-bottom: 3px;
810+
border-top: 1px solid #555555;
811+
border-bottom: 1px solid #555555;
812+
border-left: 1px dashed #555555;
813+
}
814+
815+
div.ExecutionVisualizer table.stackTbl td.stackFElt,
816+
div.ExecutionVisualizer table.queueTbl td.queueFElt {
817+
background-color: white;
818+
border-top: 1px solid #555555;
819+
border-bottom: 1px solid #555555;
820+
}
821+
822+
div.ExecutionVisualizer table.stackTbl td.stackLElt {
823+
background-color: white;
824+
border-left: 1px solid #555555;
825+
}
826+
827+
div.ExecutionVisualizer table.queueTbl td.queueLElt {
828+
background-color: white;
829+
border-top: 1px solid#555555;
830+
border-bottom: 1px solid #555555;
831+
border-left: 1px dashed #555555;
832+
}
833+
834+
/* This ensures a border is drawn around a dict
835+
if its nested in another object. */
836+
div.ExecutionVisualizer td.stackElt table.dictTbl,
837+
div.ExecutionVisualizer td.stackLElt table.dictTbl,
838+
div.ExecutionVisualizer td.stackFElt table.dictTbl,
839+
div.ExecutionVisualizer td.queueElt table.dictTbl,
840+
div.ExecutionVisualizer td.queueLElt table.dictTbl,
841+
div.ExecutionVisualizer td.queueFElt table.dictTbl {
842+
border: 1px #888 solid;
843+
}
844+
845+
.symbolic {
846+
font-size: 18pt;
847+
}
848+
849+
/* END Java frontend by David Pritchard and Will Gwozdz */

‎v3/java-example-code/Casting.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class Casting {
2+
public static void main(String[] args) {
3+
// casting doesn't change the object
4+
Object obj;
5+
{
6+
Stopwatch w = new Stopwatch();
7+
obj = w;
8+
}
9+
System.out.println(obj); // still a Stopwatch
10+
}
11+
}
12+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class CmdLineArgs {
2+
public static void main(String[] args) {
3+
// you can change "args" in the boxes below this window
4+
int a = Integer.parseInt(args[0]);
5+
int b = Integer.parseInt(args[1]);
6+
int sum = a + b;
7+
int prod = a * b;
8+
int quot = a / b;
9+
int rem = a % b;
10+
System.out.println(a + " + " + b + " = " + sum);
11+
System.out.println(a + " * " + b + " = " + prod);
12+
System.out.println(a + " / " + b + " = " + quot);
13+
System.out.println(a + " % " + b + " = " + rem);
14+
}
15+
}
16+
/*viz_options {"args":["1234","99"]}*/

‎v3/java-example-code/Complex.java‎

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*************************************************************************
2+
* From: http://introcs.cs.princeton.edu/java/home/ (Section 9.7)
3+
*
4+
* Compilation: javac Complex.java
5+
* Execution: java Complex
6+
*
7+
* Data type for complex numbers.
8+
*
9+
* The data type is "immutable" so once you create and initialize
10+
* a Complex object, you cannot change it. The "final" keyword
11+
* when declaring re and im enforces this rule, making it a
12+
* compile-time error to change the .re or .im fields after
13+
* they've been initialized.
14+
*
15+
* % java Complex
16+
* a = 5.0 + 6.0i
17+
* b = -3.0 + 4.0i
18+
* Re(a) = 5.0
19+
* Im(a) = 6.0
20+
* b + a = 2.0 + 10.0i
21+
* a - b = 8.0 + 2.0i
22+
* a * b = -39.0 + 2.0i
23+
* b * a = -39.0 + 2.0i
24+
* a / b = 0.36 - 1.52i
25+
* (a / b) * b = 5.0 + 6.0i
26+
* conj(a) = 5.0 - 6.0i
27+
* |a| = 7.810249675906654
28+
* tan(a) = -6.685231390246571E-6 + 1.0000103108981198i
29+
*
30+
*************************************************************************/
31+
32+
public class Complex {
33+
private final double re; // the real part
34+
private final double im; // the imaginary part
35+
36+
// create a new object with the given real and imaginary parts
37+
public Complex(double real, double imag) {
38+
re = real;
39+
im = imag;
40+
}
41+
42+
// return a string representation of the invoking Complex object
43+
public String toString() {
44+
if (im == 0) return re + "";
45+
if (re == 0) return im + "i";
46+
if (im < 0) return re + " - " + (-im) + "i";
47+
return re + " + " + im + "i";
48+
}
49+
50+
// return abs/modulus/magnitude and angle/phase/argument
51+
public double abs() { return Math.hypot(re, im); } // Math.sqrt(re*re + im*im)
52+
public double phase() { return Math.atan2(im, re); } // between -pi and pi
53+
54+
// return a new Complex object whose value is (this + b)
55+
public Complex plus(Complex b) {
56+
Complex a = this; // invoking object
57+
double real = a.re + b.re;
58+
double imag = a.im + b.im;
59+
return new Complex(real, imag);
60+
}
61+
62+
// return a new Complex object whose value is (this - b)
63+
public Complex minus(Complex b) {
64+
Complex a = this;
65+
double real = a.re - b.re;
66+
double imag = a.im - b.im;
67+
return new Complex(real, imag);
68+
}
69+
70+
// return a new Complex object whose value is (this * b)
71+
public Complex times(Complex b) {
72+
Complex a = this;
73+
double real = a.re * b.re - a.im * b.im;
74+
double imag = a.re * b.im + a.im * b.re;
75+
return new Complex(real, imag);
76+
}
77+
78+
// scalar multiplication
79+
// return a new object whose value is (this * alpha)
80+
public Complex times(double alpha) {
81+
return new Complex(alpha * re, alpha * im);
82+
}
83+
84+
// return a new Complex object whose value is the conjugate of this
85+
public Complex conjugate() { return new Complex(re, -im); }
86+
87+
// return a new Complex object whose value is the reciprocal of this
88+
public Complex reciprocal() {
89+
double scale = re*re + im*im;
90+
return new Complex(re / scale, -im / scale);
91+
}
92+
93+
// return the real or imaginary part
94+
public double re() { return re; }
95+
public double im() { return im; }
96+
97+
// return a / b
98+
public Complex divides(Complex b) {
99+
Complex a = this;
100+
return a.times(b.reciprocal());
101+
}
102+
103+
// return a new Complex object whose value is the complex exponential of this
104+
public Complex exp() {
105+
return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
106+
}
107+
108+
// return a new Complex object whose value is the complex sine of this
109+
public Complex sin() {
110+
return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im));
111+
}
112+
113+
// return a new Complex object whose value is the complex cosine of this
114+
public Complex cos() {
115+
return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im));
116+
}
117+
118+
// return a new Complex object whose value is the complex tangent of this
119+
public Complex tan() {
120+
return sin().divides(cos());
121+
}
122+
123+
124+
125+
// a static version of plus
126+
public static Complex plus(Complex a, Complex b) {
127+
double real = a.re + b.re;
128+
double imag = a.im + b.im;
129+
Complex sum = new Complex(real, imag);
130+
return sum;
131+
}
132+
133+
134+
135+
// sample client for testing
136+
public static void main(String[] args) {
137+
Complex a = new Complex(5.0, 6.0);
138+
Complex b = new Complex(-3.0, 4.0);
139+
140+
System.out.println("a = " + a);
141+
System.out.println("b = " + b);
142+
System.out.println("Re(a) = " + a.re());
143+
System.out.println("Im(a) = " + a.im());
144+
System.out.println("b + a = " + b.plus(a));
145+
System.out.println("a - b = " + a.minus(b));
146+
System.out.println("a * b = " + a.times(b));
147+
System.out.println("b * a = " + b.times(a));
148+
System.out.println("a / b = " + a.divides(b));
149+
System.out.println("(a / b) * b = " + a.divides(b).times(b));
150+
System.out.println("conj(a) = " + a.conjugate());
151+
System.out.println("|a| = " + a.abs());
152+
}
153+
154+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class ControlFlow {
2+
public static void main(String[] args) {
3+
while (true) {
4+
double roll = 3*Math.random();
5+
if (roll > 2.8)
6+
break;
7+
if ((int)roll == 2)
8+
continue;
9+
else if ((int)roll == 0)
10+
System.out.println("low");
11+
else
12+
System.out.println("hi");
13+
}
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class Exception {
2+
public static void main(String[] args) {
3+
// this one will be caught
4+
int x = 0;
5+
try {
6+
x = 1/x;
7+
}
8+
catch (RuntimeException e) {
9+
x = -1;
10+
System.out.println("Caught!");
11+
}
12+
System.out.println(x);
13+
14+
System.out.println(1/0);
15+
System.out.println("this won't run");
16+
}
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class ExceptionFlow {
2+
static String trace = "";
3+
4+
static void f() {
5+
trace += "call-f ";
6+
g();
7+
trace += "return-f ";
8+
}
9+
10+
static void g() {
11+
trace += "call-g ";
12+
int z = 1/0;
13+
trace += "return-g ";
14+
}
15+
16+
public static void main(String[] args) {
17+
try {
18+
f();
19+
}
20+
catch (RuntimeException e) {
21+
trace += "caught!";
22+
}
23+
System.out.println(trace);
24+
}
25+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class ExecLimit {
2+
public static void main(String[] args) {
3+
int sum = 0;
4+
for (int i=0; i<300; i++)
5+
sum += i;
6+
}
7+
}

‎v3/java-example-code/Forest.java‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Princeton COS 126 Written Exam 2, Fall 2010, Question 3
2+
// note: this is a proof-of-concept, but to do this on
3+
// paper you would instead skip as many internal steps of
4+
// root, merge, and merged as possible
5+
6+
public class Node {
7+
private Node next;
8+
private int index;
9+
private Node root() {
10+
if (next == null) return this;
11+
return next.root();
12+
}
13+
public static void merge(Node a, Node b) {
14+
a.root().next = b.root();
15+
}
16+
public static boolean merged(Node a, Node b) {
17+
return a.root() == b.root();
18+
}
19+
public static void main(String[] args) {
20+
Node[] f = new Node[8];
21+
for (int i=0; i<8; i++) {f[i] = new Node(); f[i].index=i;}
22+
merge(f[0], f[3]);
23+
merge(f[1], f[2]);
24+
merge(f[1], f[4]);
25+
merge(f[5], f[6]);
26+
merge(f[3], f[4]);
27+
merge(f[7], f[5]);
28+
29+
System.out.println(merged(f[0], f[3]));
30+
System.out.println(merged(f[0], f[7]));
31+
System.out.println(merged(f[1], f[3]));
32+
System.out.println(merged(f[4], f[5]));
33+
}
34+
}

‎v3/java-example-code/Knapsack.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class Knapsack {
2+
public static void main(String[] args) {
3+
int[] itemSizes = {3, 5, 6, 8};
4+
int capacity = 15;
5+
boolean[] canMakeSum = new boolean[capacity+1];
6+
canMakeSum[0] = true;
7+
for (int size : itemSizes) {
8+
for (int i=capacity; i>=size; i--)
9+
canMakeSum[i] |= canMakeSum[i-size];
10+
}
11+
System.out.println(java.util.Arrays.toString(canMakeSum));
12+
}
13+
}

0 commit comments

Comments
 (0)