|
print("=== Objects Demo ===");
|
|
print("");
|
|
|
|
print("--- Object Instantiation ---");
|
|
class Point {
|
|
x = 0;
|
|
y = 0;
|
|
|
|
Point(this, x, y) {
|
|
this.x = x;
|
|
this.y = y;
|
|
}
|
|
|
|
toString(this) {
|
|
return "(" + str(this.x) + ", " + str(this.y) + ")";
|
|
}
|
|
|
|
distanceFromOrigin(this) {
|
|
return this.x * this.x + this.y * this.y;
|
|
}
|
|
}
|
|
|
|
p1 = new Point(3, 4);
|
|
p2 = new Point(10, 20);
|
|
|
|
print("Point 1:", p1.toString());
|
|
print("Point 2:", p2.toString());
|
|
|
|
print("");
|
|
print("--- Property Access ---");
|
|
print("p1.x =", p1.x);
|
|
print("p1.y =", p1.y);
|
|
print("p2.x =", p2.x);
|
|
print("p2.y =", p2.y);
|
|
|
|
print("");
|
|
print("--- Property Modification ---");
|
|
p1.x = 100;
|
|
p1.y = 200;
|
|
print("After modification, p1:", p1.toString());
|
|
|
|
print("");
|
|
print("--- Method Calls ---");
|
|
print("p2.distanceFromOrigin():", p2.distanceFromOrigin());
|
|
|
|
print("");
|
|
print("--- Multiple Objects ---");
|
|
class Rectangle {
|
|
width = 0;
|
|
height = 0;
|
|
|
|
Rectangle(this, w, h) {
|
|
this.width = w;
|
|
this.height = h;
|
|
}
|
|
|
|
area(this) {
|
|
return this.width * this.height;
|
|
}
|
|
|
|
perimeter(this) {
|
|
return 2 * (this.width + this.height);
|
|
}
|
|
}
|
|
|
|
rect1 = new Rectangle(5, 10);
|
|
rect2 = new Rectangle(3, 7);
|
|
rect3 = new Rectangle(8, 8);
|
|
|
|
print("Rectangle 1: " + str(rect1.width) + "x" + str(rect1.height));
|
|
print(" Area:", rect1.area());
|
|
print(" Perimeter:", rect1.perimeter());
|
|
|
|
print("Rectangle 2: " + str(rect2.width) + "x" + str(rect2.height));
|
|
print(" Area:", rect2.area());
|
|
print(" Perimeter:", rect2.perimeter());
|
|
|
|
print("Rectangle 3: " + str(rect3.width) + "x" + str(rect3.height));
|
|
print(" Area:", rect3.area());
|
|
print(" Perimeter:", rect3.perimeter());
|
|
|
|
print("");
|
|
print("--- Object Composition ---");
|
|
class Circle {
|
|
center = null;
|
|
radius = 0;
|
|
|
|
Circle(this, centerX, centerY, radius) {
|
|
this.center = new Point(centerX, centerY);
|
|
this.radius = radius;
|
|
}
|
|
|
|
describe(this) {
|
|
print("Circle at", this.center.toString(), "with radius", this.radius);
|
|
}
|
|
}
|
|
|
|
circle = new Circle(5, 5, 10);
|
|
circle.describe();
|
|
print("Center x:", circle.center.x);
|
|
print("Center y:", circle.center.y);
|
|
|
|
print("");
|
|
print("--- Object Arrays ---");
|
|
class Student {
|
|
name = "";
|
|
grade = 0;
|
|
|
|
Student(this, name, grade) {
|
|
this.name = name;
|
|
this.grade = grade;
|
|
}
|
|
}
|
|
|
|
students = {};
|
|
students.push(new Student("Alice", 95));
|
|
students.push(new Student("Bob", 87));
|
|
students.push(new Student("Charlie", 92));
|
|
students.push(new Student("Diana", 88));
|
|
|
|
print("Student Grades:");
|
|
totalGrade = 0;
|
|
for (i = 0; i < len(students); i++) {
|
|
print(" " + students[i].name + ":", students[i].grade);
|
|
totalGrade += students[i].grade;
|
|
}
|
|
average = totalGrade / len(students);
|
|
print("Average grade:", average);
|
|
|
|
print("");
|
|
print("--- Object State ---");
|
|
class BankAccount {
|
|
owner = "";
|
|
balance = 0;
|
|
transactions = null;
|
|
|
|
BankAccount(this, owner, initial) {
|
|
this.owner = owner;
|
|
this.balance = initial;
|
|
this.transactions = {};
|
|
this.transactions.push("Initial deposit: " + str(initial));
|
|
}
|
|
|
|
deposit(this, amount) {
|
|
this.balance += amount;
|
|
this.transactions.push("Deposit: " + str(amount));
|
|
}
|
|
|
|
withdraw(this, amount) {
|
|
if (amount > this.balance) {
|
|
print("Insufficient funds!");
|
|
return 0;
|
|
}
|
|
this.balance -= amount;
|
|
this.transactions.push("Withdrawal: " + str(amount));
|
|
return 1;
|
|
}
|
|
|
|
getStatement(this) {
|
|
print("=== Account Statement ===");
|
|
print("Owner:", this.owner);
|
|
print("Transactions:");
|
|
for (i = 0; i < len(this.transactions); i++) {
|
|
print(" ", this.transactions[i]);
|
|
}
|
|
print("Current Balance:", this.balance);
|
|
}
|
|
}
|
|
|
|
account = new BankAccount("John Doe", 1000);
|
|
account.deposit(500);
|
|
account.deposit(250);
|
|
account.withdraw(300);
|
|
account.withdraw(100);
|
|
account.getStatement();
|
|
|
|
print("");
|
|
print("--- Object Equality ---");
|
|
class Token {
|
|
type = "";
|
|
value = "";
|
|
|
|
Token(this, type, value) {
|
|
this.type = type;
|
|
this.value = value;
|
|
}
|
|
|
|
equals(this, other) {
|
|
return this.type == other.type && this.value == other.value;
|
|
}
|
|
}
|
|
|
|
t1 = new Token("NUMBER", "42");
|
|
t2 = new Token("NUMBER", "42");
|
|
t3 = new Token("STRING", "hello");
|
|
|
|
print("t1 equals t2:", t1.equals(t2));
|
|
print("t1 equals t3:", t1.equals(t3));
|
|
|
|
print("");
|
|
print("--- Factory Pattern ---");
|
|
class ShapeFactory {
|
|
ShapeFactory(this) {
|
|
}
|
|
|
|
createRectangle(this, w, h) {
|
|
return new Rectangle(w, h);
|
|
}
|
|
|
|
createSquare(this, size) {
|
|
return new Rectangle(size, size);
|
|
}
|
|
}
|
|
|
|
factory = new ShapeFactory();
|
|
rect = factory.createRectangle(4, 6);
|
|
square = factory.createSquare(5);
|
|
|
|
print("Rectangle area:", rect.area());
|
|
print("Square area:", square.area());
|