Trying to use applab to transition from APCSP to APCSA, so I’m trying to implement a class in applab. I’m using known javascript code to create the class and constructor, and initially applab doesn’t flag any errors. When I try to run the code, however, I’m getting a “Unexpected token” error for the line Class Draph{ which is the line declaring the class. Applab recognizes the Class, constructor, and this keywords as they are immediately color coded. Has anyone ever coded a class successfully in applab? Am I mssing something specific to this compiler?
Entire code below:
class Dgraph{
constructor Dgraph(vTotal){
this.vTotal = vTotal;
this.adjList = new Map();
}
addVertex(v) {
this.adjList.set(v, );
}
addEdge(v,map){
this.adjList.get(v).push(map);
}
printGraph(){
var getKeys = this.adjList.keys();
for (var i of getKeys){
var getValues = this.adjList.get(i);
var adder = “”;
for (var j of getValues){
adder += j + " ";
}
console.log(i + " -> " + adder);
}
}
}
var vertexTotal = prompt(“How many vertices?”);
var vertices = ;
for (var k=0; k<vertexTotal; k++){
var tempV = prompt(“What is the name of vertex “+ (k+1)+”?”);
appendItem(vertices,tempV);
}
var graph = new Dgraph(vertexTotal);
for (var l=0;l<vertexTotal;l++){
graph.addVertex(vertices[l]);
}
var tempE1;
var tempE2;
var flag = true;
while (flag){
var cont = prompt(“Do you want to add an edge? Y/N”);
if (cont == “N”)
flag = false;
else if (cont == “Y”){
tempE1 = prompt(“What is the starting vertex?”);
tempE2 = prompt(“What is the ending vertex?”);
graph.addEdge(tempE1,tempE2);
}
}
graph.printGraph();
Hi @kjstacey,
Welcome to the code.org forums.
I won’t be much help since I haven’t tried doing stuff with classes in App Lab, but I’ll try to get someone else with more knowledge about that to take a look at this. Do keep in mind some features may not be supported, since our priorities are to support the use of App Lab in context of the CS Principles course, and OOP is outside of that as far as I know. (But we’ll do our best to help)
Meanwhile, it really helps us troubleshoot if you can use the “Share” feature in App Lab and post the link here so someone can directly dig around in the code and run it.
Frank
Thank you for your reply, like I said I’m hoping to transition from csp to CSA which uses java, so if I could show them classes in app lab it would make an easier transition. (Which is something to think about if you ever want to expand your curriculum options
upwards ). I did publish the code. It’s under graph class example oop.
The Javascript in App Lab is version ES5. That means class isn’t implemented yet. However, that doesn’t matter because Javascript uses an entirely different approach to objects.
In ES5 you create objects as functions like this:
function Bike(model,color) {
this.model = model;
this.color = color;
}
It is a function that is a constructor. You can then add superclass methods like this:
Bike.prototype.getDetails = function () {
return this.model+’ bike is '+this.color;
};
By changing the prototype you change all objects created by the Bike function. Create new Bike objects like this:
var bike1 = new Bike(‘BMW’,‘Black’);
var bike2 = new Bike(‘classic’,‘red’);
console.log(bike1.getDetails());
console.log(bike2.getDetails());
It is a different twist on objects. Learning objects in Java often causes people to confuse polymorphism with inheritance. There is no danger of that here.