This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Point; | |
public class Vehicle{ | |
private Point location = new Point(); | |
private String name; | |
public Vehicle(Point location, String name){ | |
this.location = new Point(location); //Since Point object is mutable, it should be copied. | |
vehicleName = name; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//A custom toString method that returns a meaningful string representation of an ArrayList object | |
public String toString() { | |
String strOutput = "MyArrayListObject ["; //Prints constructor name + left bracket | |
for(int i = 0; i < myArr.size(); i++){ //myArr = ArrayList instance variable | |
strOutput += myArr.get(i) + ", "; | |
} | |
strOutput = strOutput + "]"; | |
return strOutput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//converting a Java ArrayList to an int[] array | |
int[] arr = new int[myArrList.size()]; | |
for (int i=0; i < arr.length; i++){ | |
arr[i] = myArrList.get(i).intValue(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Person(firstName, lastName){ | |
this.firstName = firstName; | |
this.lastName = lastName; | |
}; | |
Person.prototype.getFullName = function(){ | |
return this.firstName + " " + this.lastName; | |
}; | |
function Employee(firstName, lastName, position){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The Factory Pattern | |
var createPerson = function(firstName, lastName) { | |
return { | |
firstName : firstName, | |
lastName : lastName, | |
greet : function(){ | |
return "Hey there " + firstName + " " + lastName; | |
} | |
}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The constructor pattern | |
function Robot(name, model, job){ | |
this.name = name; | |
this.model = model; | |
this.job = job; | |
this.sayMyName = function() { | |
alert(this.name); | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Example 1 | |
var createPerson = function(firstName, lastName){ | |
var person = {}; | |
//Descriptor object - define single property | |
//2 types - Data & Accessor descriptors | |
Object.defineProperty(person, "firstName", { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Selecting all the external links of a page | |
$('a[href^="http://"]') | |
//Selecting all the internal links of a page | |
$('a:not(a[href^="http://"])') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class QuickFindUnion | |
def initialize(n) | |
@id = Array.new(n) {|i| i} | |
end | |
def connected?(p, q) | |
puts @id[p] == @id[q] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class QuickUnion | |
def initialize(n) | |
@id = Array.new(n) {|i| i} | |
end | |
def root(i) | |
while i != @id[i] do | |
i = @id[i] | |
end |
OlderNewer