-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
401 lines (356 loc) · 17.3 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<!DOCTYPE html>
<html>
<link href="style.css" rel="stylesheet" type="text/css">
<div class="boxed">
<font size="30" align="center">Java Visualization</font>
</div>
<body>
<div class= "TextBox">
<h1>
<center id = "Title"> </center>
</h1>
<hr style="border-top: dotted 2.5px;" />
<div id = "Output"></div>
</div>
<div class = "Help">
<h1>
<center>So What is Happening?</center>
</h1>
<hr style="border-top: dotted 2.5px;" />
<div id= "help"></div>
</div>
<div class = "Output">
<h1>
<center>Output</center>
</h1>
<hr style="border-top: dotted 2.5px;" />
<div id= "output1"></div>
</div>
<div class = "previous" onclick="currentVisual.Previous()">
<button class="Pbutton">Previous</button>
</div>
<div class = "next" onclick="currentVisual.Next()">
<button class="Nbutton">Next</button>
</div>
<div class="dropdown">
<button class="dropbtn">Java Visual Type:</button>
<div class="dropdown-content">
<button href="#" onclick="JavaVisualizationFactory('Loops And Conditionals')" class = "dButton">LoopsAndConditional</button>
<button href="#" onclick="JavaVisualizationFactory('Conditionals')" class = "dButton">Conditionals</button>
<button href="#" onclick="JavaVisualizationFactory('While Loops')" class = "dButton">While Loops</button>
<button href="#" onclick="JavaVisualizationFactory('Do-While Loops')" class = "dButton">Do-While Loops</button>
<button href="#" onclick="JavaVisualizationFactory('For-Each Loop')" class = "dButton">For-Each Loops</button>
</div>
</div>
<script src="https://d3js.org/d3.v5.js"></script>
<script>
//class which all visualizations will inherit from
class JavaVisualization
{
constructor(words, repeat, repeatStart,repeatEnd,currentNum,numItems, type, answers, help){}
//displays the code to the screen
display()
{
this.words.forEach(this.show);
document.getElementById("help").innerHTML = this.help[0];
}
//gets items for displaying the code
show(item, index){
var div = document.createElement('div');
div.innerHTML = index + "- " + item;
if(index == 0)
{
div.setAttribute("class", "Code selected");
}
else
{
div.setAttribute("class", "Code");
}
div.setAttribute("id", index)
document.getElementById("Output").appendChild(div)
}
//goes to the next line and prints output depending on the visualization
Next(){
document.getElementById(this.currentNum.toString()).setAttribute("class", "Code");
this.currentNum++;
document.getElementById("help").innerHTML = this.help[this.currentNum];
if(this.answers.length != 0 && this.currentNum == this.answers[this.answers.length -1].split (",", 1))
{
if(this.type == "Loops And Conditionals")
{
if(this.repeats % 2 == 1){
var ans = this.answers.pop();
var data = ans.split(",", 2);
var div = document.createElement('div');
div.innerHTML = data[1];
document.getElementById("output1").appendChild(div);
}
}
if(this.type == "Conditionals")
{
var ans = this.answers.pop();
var data = ans.split(",", 2);
var div = document.createElement('div');
div.innerHTML = data[1];
document.getElementById("output1").appendChild(div);
}
if(this.type == "While Loops")
{
var ans = this.answers.pop();
var data = ans.split(",", 2);
var div = document.createElement('div');
div.innerHTML = data[1];
document.getElementById("output1").appendChild(div);
}
if(this.type == "Do-While Loops")
{
var ans = this.answers.pop();
var data = ans.split(",", 2);
var div = document.createElement('div');
div.innerHTML = data[1];
document.getElementById("output1").appendChild(div);
}
if(this.type == "For-Each Loop")
{
var ans = this.answers.pop();
var data = ans.split(",", 2);
var div = document.createElement('div');
div.innerHTML = data[1];
document.getElementById("output1").appendChild(div);
}
}
if(this.currentNum >= this.numItems)
{
JavaVisualizationFactory(this.type);
}
if(this.repeats-1 != 0)
{
if(this.currentNum > this.repeatEnd)
{
this.currentNum = this.repeatStart;
this.repeats--;
}
}
document.getElementById(this.currentNum.toString()).setAttribute("class", "Code selected");
}
// goes back a line if not on the first line
Previous()
{
document.getElementById(this.currentNum.toString()).setAttribute("class", "Code");
if(this.currentNum != 0){
this.currentNum--;
document.getElementById("help").innerHTML = this.help[this.currentNum];
}
document.getElementById(this.currentNum.toString()).setAttribute("class", "Code selected");
}
}
// Loops and conditionals visualization
class LoopsAndConditional extends JavaVisualization
{
constructor(){
super();
this.words = ["public static void main(String[] args) {"," for(int i = 0; i < 5; i++){", "  if(i%2 != 0){", "   System.out.println(i);", "  }", " }", "}"];
this.help = ["This is a the Main method, Usually when a program starts it begins in the main method if you are using Java!",
"We start with we a what we call a 'For Loop', it basically just loops the same block of code until a certain condition is met in this case when 'i' is equal to 5.",
"This is a condition, If the condition is met then what ever is inside the statement is executed, else it is ignored. In this case it will print the value of 'i'.",
"This line of code outputs a value to the console as seen below. So the current value of 'i' will be printed.",
"This is a curly bracket it signifies the end of a code block. In this case it will be the if statement.",
"This is a curly bracket it signifies the end of a code block. In this case it will be the For Loop.",
"This is a curly bracket it signifies the end of a code block. In this case it will be the Main method"];
this.answers = ["3,5","3,3","3,1"];
this.repeats = 5;
this.repeatStart = 1;
this.repeatEnd = 4;
this.type = "Loops And Conditionals";
this.currentNum = 0;
this.numItems = this.words.length;
}
}
//Conditionals visualization
class Conditionals extends JavaVisualization
{
constructor(){
super();
this.words = ["public static void main(String[] args) {"," int num = 10;", " If(num == 5){" ,"  System.out.println('True!');", " }else{" ,"  System.out.println('It's not five!');", " }", "}"];
this.help = ["This is a the Main method, Usually when a program starts it begins in the main method if you are using Java!",
"Here we declaring a integer variable called 'num' and setting it equal to 10. It is important that we end it with a semicolon!",
"This is a condition, If the condition is met then what ever is inside the statement is executed, else it is ignored. With this example we are checking if it is equal to 5.",
"Since the conditional was not satisfied this line will be ignored!",
"A else statement excutes only if the previous conditional was not met. So now that we now our variable isn't 5 we can be sure that whatever is executed next is true!",
"This line prints whatever we want to be outputed; in this case we want to print that our variable isn't 5.",
"This is a curly bracket it signifies the end of a code block. In this case it will be the else statement.",
"This is a curly bracket it signifies the end of a code block. In this case it will be the Main method"];
this.answers = ["5, It's not five!"];
this.repeats = 1;
this.repeatStart = 0;
this.repeatEnd = 0;
this.currentNum = 0;
this.numItems = this.words.length;
this.type = "Conditionals";
}
}
//While Loop visualization
class WhileLoops extends JavaVisualization
{
constructor(){
super();
this.words = ["public static void main(String[] args) {", " String word = 'Hello';", " while(true){",
"  System.out.println(word);","  word = word + 'o';","  if(word.equals('Helloooo')){",
"   break;", "  }", " }", "}"];
this.help = ["This is a the Main method, Usually when a program starts it begins in the main method if you are using Java!",
"Here we are declaring a string variable which is essentially any text you want to store in a variable.",
"This is the While-Loop, It keeps looping a block of code until the condition is no longer met or in this case when you use a break",
"Here we are printing the string we stored in 'word' variable",
"In this we are adding a 'o' to the text in the word variable",
"When this condition is met then the following code will be execute else it will be ignored",
"This ends the While-Loop. The while loop can also be stopped if the condition is not met",
"This is a curly bracket it signifies the end of a code block. In this case it will be the If-Statement",
"This is a curly bracket it signifies the end of a code block. In this case it will be the While-Loop",
"This is a curly bracket it signifies the end of a code block. In this case it will be the Main method"];
this.answers = ["3, Hellooo","3, Helloo", "3, Hello"];
this.repeats = 3;
this.repeatStart = 2;
this.repeatEnd = 7;
this.currentNum = 0;
this.numItems = this.words.length;
this.type = "While Loops";
}
}
//do-While java visualization
class DoWhileLoops extends JavaVisualization
{
constructor(){
super();
this.words = ["public static void main(String[] args) {", " int num = 0;"," boolean keepRunning = true;"," do{", "  System.out.println(num);", "  num++;"
,"  if(num == 7){", "   keepRunning = false;"
,"  }"," }while(keepRunning);","}"];
this.help = ["This is a the Main method, Usually when a program starts it begins in the main method if you are using Java!",
"Here we are declaring a integer value. At first we set it to 0.",
"This is called a boolean which is has either a true/false. In this case it is set to true.",
"This is a Do-While loop unlike other loops this one runs once and then checks the condition in the end of the loop!",
"Here we simply print out the 'num' variable value.",
"We add 1 to our 'num' variable.",
"When this condition is met then the following code will be execute else it will be ignored.",
"When the condition is met this will execute, the boolean is set to false so the do-while loop condition is no longer met.",
"This is a curly bracket it signifies the end of a code block. In this case it will be the If-Statement.",
"This is the end of the Do-While Loop. In this the condition is checked if it is not met the loop stops.",
"This is a curly bracket it signifies the end of a code block. In this case it will be the Main method."];
this.answers = ["4, 3","4, 2","4, 1","4, 0"];
this.repeats = 4;
this.repeatStart = 3;
this.repeatEnd = 9;
this.currentNum = 0;
this.numItems = this.words.length;
this.type = "Do-While Loops";
}
}
//The For-Each loop visualization
class ForEachLoop extends JavaVisualization
{
constructor(){
super();
this.words = ["public static void main(String[] args) {", " int[] numbers = {5,9,10};"," for(num: numbers){", "  System.out.println(num);"," }","}"];
this.help = ["This is a the Main method, Usually when a program starts it begins in the main method if you are using Java!",
"Here we are declaring a array of numbers that include 5,9,10.",
"This is a For-Each loop whichs loops through a block of code until there are no more items to loop through in the array. In this case the array 'number'.",
"Here we print out the value of 'num' which is a item in the number array we created.",
"This is a curly bracket it signifies the end of a code block. In this case it will be the For-Each Loop",
"This is a curly bracket it signifies the end of a code block. In this case it will be the Main method."];
this.answers = ["3, 10","3, 9","3, 5"];
this.repeats = 3;
this.repeatStart = 2;
this.repeatEnd = 4;
this.currentNum = 0;
this.numItems = this.words.length;
this.type = "For-Each Loop";
}
}
var currentVisual = new JavaVisualization();
//The factory that creates new java visualizations depending on the input of the parameters
function JavaVisualizationFactory(type)
{
document.getElementById("Output").innerHTML = "";
document.getElementById("output1").innerHTML = "";
if(type == "Conditionals")
{
currentVisual = new Conditionals();
}
if(type == "Loops And Conditionals")
{
currentVisual = new LoopsAndConditional();
}
if(type == "While Loops")
{
currentVisual = new WhileLoops();
}
if(type == "Do-While Loops")
{
currentVisual = new DoWhileLoops();
}
if(type == "For-Each Loop")
{
currentVisual = new ForEachLoop();
}
document.getElementById("Title").innerHTML = "Java Visualization: " + currentVisual.type;
currentVisual.display();
}
//This creates a visualization at the start of the website
JavaVisualizationFactory("While Loops");
//Just so we can use the left and right keys on the website
document.onkeydown = function(e)
{
switch (e.keyCode)
{
case 37:
currentVisual.Previous();
break;
case 39:
currentVisual.Next();
break;
}
}
</script>
</body>
<meta charset="utf-8">
<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>
<script>
// set the dimensions and margins of the graph
var width = 450
height = 450
margin = 40
// The radius of the pieplot is half the width or half the height (smallest one). I substract a bit of margin.
var radius = Math.min(width, height) / 2 - margin
// append the svg object to the div called 'my_dataviz'
var svg = d3.select("#my_dataviz")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
// Create dummy data
var data = {a: 10, b: 10, c:10, d:10, e:10}
// set the color scale
var color = d3.scaleOrdinal()
.domain(data)
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56"])
// Compute the position of each group on the pie:
var pie = d3.pie()
.value(function(d) {return d.value; })
var data_ready = pie(d3.entries(data))
// Build the pie chart: Basically, each part of the pie is a path that we build using the arc function.
svg
.selectAll('whatever')
.data(data_ready)
.enter()
.append('path')
.attr('d', d3.arc()
.innerRadius(0)
.outerRadius(radius)
)
.attr('fill', function(d){ return(color(d.data.key)) })
.attr("stroke", "black")
.style("stroke-width", "2px")
.style("opacity", 0.7)
</script>
</html>