Introduction of Computer System Design
�Object Oriented Programming and Jack
Chester Rebeiro, IIT Madras
slides inspired / borrowed from https://www.nand2tetris.org/
class abstraction
class Point {
field int x, y;
constructor Point new(int ax, int ay) {
let x = ax;
let y = ay;
return this;
}
}
var Point p;
let p = Point.new(3, 4);
usage of class
class variables
Object of Point
methods are public
class Point {
field int x, y;
constructor Point new(int ax, int ay) {� . . .
}� method int getX() {� return x;� }� method int getY() {� return y;� }� method void move(int dx, int dy) {� let x = x + dx;� let y = y + dy;� return;� }�}
var Point p;
let p = Point.new(3, 4);�do p.move(4, 1); // Point is at (7, 5)��do.Output.printInt(p.x) // error!
do.Output.printInt(p.getX())� // prints 7
Using class point in class line
class Point {
field int x, y;
constructor Point new(int ax, int ay) {
let x = ax;
let y = ay;
return this;
}
method int getX() {
return x;
}
method int getY() {
return y;
}
}
class Line {
field Point start, end;
constructor Line new(Point a, Point b) {
let start = a;
let end = b;
return this;
}
method void draw() {
do Screen.drawLine(
start.getX(), start.getY(),
end.getX(), end.getY()
);
return;
}
}
A complete program
class Point {
field int x, y;
constructor Point new(int ax, int ay) {
let x = ax;
let y = ay;
return this;
}
method int getX() {
return x;
}
method int getY() {
return y;
}
}
class Line {
field Point start, end;
constructor Line new(Point a, Point b) {
let start = a;
let end = b;
return this;
}
method void draw() {
do Screen.drawLine(
start.getX(), start.getY(),
end.getX(), end.getY()
);
return;
}
}
class Main {
function void main() {
var Point start, end;
var Line line;
let start = Point.new(100, 50);
let end = Point.new(50, 150);
let line = Line.new(start, end);
do line.draw();
return;
}
}
Every program should have a class Main, and a function main within it. ��This marks the program's entry point
A complete program to draw a triangle
class Polygon {
field Line side1, side2, side3;
constructor Polygon new(Point a, Point b, Point c) {
let side1 = Line.new(a, b);
let side2 = Line.new(b, c);
let side3 = Line.new(c, a);
return this;
}
method void draw() {
do side1.draw();
do side2.draw();
do side3.draw();
return;
}
}
class Main {
function void main() {
var Point a, b, c;
var Polygon triangle;
let a = Point.new(100, 50);
let b = Point.new(50, 150);
let c = Point.new(150, 150);
let triangle = Polygon.new(a, b, c);
do triangle.draw();
return;
}
}
Find the number of points defined
class Point {
field int x, y;
static int count;
constructor Point new(int ax, int ay) {
let x = ax;
let y = ay;
let count = count + 1;
return this;
}
method int getX() {
}
method int getY() {
}
function int getCount() {
return count;
}
}
class Main {
function void main() {
var Point a, b, c;
var Polygon triangle;
let a = Point.new(100, 50);
let b = Point.new(50, 150);
let c = Point.new(150, 150);
let triangle = Polygon.new(a, b, c);
do triangle.draw();
do Output.printString("Number of points: ");
do Output.printInt(Point.getCount());
do Output.println();
return;
}
}
Prints 3 points
Class Point and Memory Allocations
class Point {
field int x, y;
static int count;
constructor Point new(int ax, int ay) {
let x = ax;
let y = ay;
let count = count + 1;
return this;
}
method int getX() {
}
method int getY() {
}
function int getCount() {
return count;
}
}
Class Point and Memory Allocations
class Point {
field int x, y;
static int count;
constructor Point new(int ax, int ay) {
let x = ax;
let y = ay;
let count = count + 1;
return this;
}
method int getX() {
}
method int getY() {
}
function int getCount() {
return count;
}
}
Class Point and Memory Allocations
class Point {
field int x, y;
static int count;
constructor Point new(int ax, int ay) {
let x = ax;
let y = ay;
let count = count + 1;
return this;
}
method int getX() {
}
method int getY() {
}
function int getCount() {
return count;
}
}
Object Distructors
class Point {
field int x, y;
static int count;
constructor Point new(int ax, int ay) {
}
method int getX() {
}
method int getY() {
}
function int getCount() {
}
method void dispose() {
let count = count - 1;
do Memory.deAlloc(this);
return;
}
}
class Main {
function void main() {
var Point a, b, c;
. . .
. . .
do a.dispose();
return;
}
}
Free memory when it no longer used
A node in a Linked List
class Node {
field int value;
field Node next;�
constructor Node new(int data) {
let value = data;
let next = null;
return this;
}
method int getValue() {
return value;
}
method Node getNext() {
return next;
}
method void setNext(Node node) {
let next = node;
return;
}
method void dispose() {
do Memory.deAlloc(this);
return;
}
}
Linked List Example
class LinkedList {
field Node head;
constructor LinkedList new() {
let head = null;
return this;
}
method void add(int value)
method void traverse()
method boolean delete(int value)
method void dispose()
}
class Main {
function void main() {
var LinkedList list;
let list = LinkedList.new();
do list.add(10);
do list.add(20);
do list.add(30);
do list.traverse(); // 10 -> 20 -> 30 -> null
let deleted = list.delete(20);
do list.traverse(); // 10 -> 30 -> null
let deleted = list.delete(10);
do list.traverse(); // 30 -> null
let deleted = list.delete(30);
do list.traverse(); // null
do list.dispose();
return;
}
}
class Main {
function void main() {
var LinkedList list;
let list = LinkedList.new();
}
}
Linked List : Add element to list
class LinkedList {
field Node head;
constructor LinkedList new()
method void add(int value) {� var Node node, current;� let node = Node.new(value);� if (head = null) {� let head = node;� } else {� let current = head;� while (~(current.getNext() = null)) {� let current = current.getNext();� }� do current.setNext(node);� }� return;� }
}
class Main {
function void main() {
var LinkedList list;
let list = LinkedList.new();
do list.add(10);
do list.add(20);
do list.add(30);
}
}
Linked List : Traverse list
class LinkedList {
field Node head;
constructor LinkedList new()
method void add(int value);
method void traverse() {
var Node current;
let current = head;
while (~(current = null)) {
do Output.printInt(current.getValue());
do Output.printString(" -> ");
let current = current.getNext();
}
do Output.printString("null");
do Output.println();
return;
}�}
class Main {
function void main() {
var LinkedList list;
let list = LinkedList.new();
do list.add(10);
do list.add(20);
do list.add(30);
do list.traverse(); // 10 -> 20 -> 30 -> null
}
}
Linked List : Delete Element from List
class LinkedList {
field Node head;
constructor LinkedList new()
method void add(int value);
method void traverse();
method delete(int value){
// Todo
}
}
class Main {
function void main() {
var LinkedList list;
let list = LinkedList.new();
do list.add(10);
do list.add(20);
do list.add(30);
do list.traverse(); // 10 -> 20 -> 30 -> null� do list.delete(20); // 10 -> 30 -> null
}
}