CSC 102 Lecture Notes Week 1
Introduction to the Course
Introduction to Java
int i; double x,y,z; char c1, c2;
/**** * * A simple Java program that defines a rectangle data structure and two methods * that operate on rectangles. * */ public class Rectangle { int x; int y; int width; int height; Rectangle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } void move(int x_increment, int y_increment) { x = x + x_increment; y = y + y_increment; } boolean equals(Rectangle r) { return x == r.x && y == r.y && width == r.width && height == r.height; } public static void main(String[] args) { Rectangle r1 = new Rectangle(10, 20, 100, 200); Rectangle r2 = new Rectangle(20, 30, 100, 200); boolean eq; eq = r1.equals(r2); if (eq == false) { System.out.println("r1 not = r2"); } else { System.out.println("r1 = r2"); } r1.move(10, 10); eq = r1.equals(r2); if (eq == false) { System.out.println("r1 not = r2"); } else { System.out.println("r1 = r2"); } } }rectangle.c:
/**** * * A simple C program that defines a rectangle data structure and two functions * that operate on rectangles. * */ #include <stdio.h> struct Rectangle { int x; int y; int width; int height; }; struct Rectangle move(struct Rectangle r, int x_increment, int y_increment) { r.x = r.x + x_increment; r.y = r.y + y_increment; return r; } unsigned char equals(struct Rectangle r1, struct Rectangle r2) { return r1.x == r2.x && r1.y == r2.y && r1.width == r2.width && r1.height == r2.height; } int main() { struct Rectangle r1 = {10, 20, 100, 200}; struct Rectangle r2 = {20, 30, 100, 200}; unsigned char eq; eq = equals(r1, r2); if (eq == 0) { printf("r1 not = r20); } else { printf("r1 = r20); } r1 = move(r1, 10, 10); eq = equals(r1, r2); if (eq == 0) { printf("r1 not = r20); } else { printf("r1 = r20); } }
withRectangle r1 = new Rectangle(10, 20, 100, 200);
struct Rectangle r1 = {10, 20, 100, 200};
withvoid move(int x_increment, int y_increment) { x = x + x_increment; y = y + y_increment; }
and comparestruct Rectangle move(struct Rectangle r, int x_increment, int y_increment) { r.x = r.x + x_increment; r.y = r.y + y_increment; return r; }
withboolean equals(Rectangle r) { return x == r.x && y == r.y && width == r.width && height == r.height; }
unsigned char equals(struct Rectangle r1, struct Rectangle r2) { return r1.x == r2.x && r1.y == r2.y && r1.width == r2.width && r1.height == r2.height; }
c.m(...)where the comparable invocation in C is a function f called with a struct parameter s, like this
f(s, ...)
void move(int x_increment, int y_increment) { this.x = this.x + x_increment; this.y = this.y + y_increment; }
where this is necessary because the names of the constructor parameters are the same as the names of the class data fields.Rectangle(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; }
Rectangle(int x_val, int y_val, int width_val, int height_val) { x = x_val; y = y_val; width = width_val; height = height_val; }