Welcome

Welcome to my blog.


I will be updating this blog with lecture notes and miscellaneous information for:

CSE 1030 : Winter Term
Prof. A. Eckford, Section-Z: MWF at 10:30 in R S137



Feel free to leave me a message or send me an e-mail at producap@yorku.ca.

Here is a link for MyMail, Hotmail, G-mail, and PRISM login.

On that note, you must have a PRISM account in order to write the lab tests.

If you have not created a PRISM account yet (e.g., if you received transfer credit for 1020), see the lab monitor in CSEB 1006 as soon as possible.

I will also leave the blog open to comments, etc. much like the CSE forums.

Thanks for visiting.

Phil.

Monday, March 10, 2008

CSE Lecture 3/Mar/2008





Please click on the desired image to view on a larger scale.

CSE Lecture 29/Feb/2008



Please click on the desired image to view on a larger scale.

CSE Lecture 25/ Feb/2008 (Alternative)


Please click on the desired image to view on a larger scale.

Sunday, March 9, 2008

CSE Lectures 22/Feb/2008 to 27/Feb/2008 Part 2




Please click on the desired image to view on a larger scale.

CSE Lectures 22/Feb/2008 to 27/Feb/2008 Part 1







Please click on the desired image to view on a larger scale.

CSE Lecture 8/Feb/2008





Please click on the desired image to view on a larger scale.

CSE Lecture 6/Feb/2008

Mutators and Accessors in Compositions

-avoid privacy leaks
-make defensive copies whenever mutable objects are passed into or out of the composition. No changes are needed for immutable objects.

Mutator
-create a copy of the external object and start that in our object's attribute.

Accessor
-create a copy of the objects attribute and return that copy.

Constructor
-just like the mutator.

Copy Constructors of Compositions and Aggregates
-"deep copy" --> respect the semantics of the internal objects.

-composition: copy constructor also creates new copies of all internal mutable objects.
-aggregate: only copy references, do not create new copies of internal objects.

Arrays

-Array is similar to ArrayList
- a list of objects stores by index
- all objects of the same type

Array Syntax (starting with primitives)
e.g. a list of 5 integers: int[] x = new int[5]; //creates an empty array of 5 integers.
x.length gives you the length of the array (5 in this case)

e.g. I want to store the int 1024 in the first element of the array.
x[0] = 1024; //note that array indices, like ArrayList, start at zero.

Say you want to get the third element in the array and store it in a separate int.
int y;
y = x[2];

Arrays can be initialized at declaration.
int[] x = {1024,345,-2,123,81};

Monday, February 4, 2008

CSE Lecture 4/FEB/08

Note: See the previous lectures notes for the majority of this lectures contents.

Defensive Copying and Privacy Leaks

Composition: external interference with the inner workings of the class are undersirable.

Aggregates: external changes are OK.

The method of Copy Constructors (previous lectures) is called defensive copying.

CSE Lecture 1/FEB/08

Collections

-A collection is an aggregate(or composition) in which more than one of each type is allowed.
-The number required is not known in advance.
e.g. Portfolio --> collection of Investments

-We have already seen HashMap from SingletonPerState

Collection Constructors

-Best way: create an empty collection object in the constructor.
-Also, provide a public add() method for loading objects into the collection. This method generally returns a boolean: false if it fails, true otherwise.

Collection Objects - 3 Types

Lists - objects in order, duplicates are OK
Sets - order does not matter, duplicates are not allowed.
Maps - objects are associated with keys.

See code for Portfolio

Iterators and Iterable

-Iterator and Iterable are interfaces.
-A class implementing an interface must implement all the methods specified in the interface exactly. This means the same return type, same name (case-sensitive), same number, order, and types of paramters.


--> MyClass implements MyInterface

public class MyClass implements MyInterface
{
.
.
.
}

The Iterator interface allows a method to take some action on each element in a collection. It contains a method that returns the collection's Iterator --> repackaging the collection for iterator operations.

See Rest of Portfolio code including getValue() method.


Thursday, January 31, 2008

CSE Lecture 30/JAN/2008

Aggregation

-An aggregate is a class where some of the attributes are themselves objects.
-If all attributes are primitive, the class is not an aggregate.

e.g. public class BankProducts
{
private BankAccount savings;
private BankAccount chequing;
private Loan homeMortgage;
private Loan lineOfCredit;

}

- a composition is an aggregation in which the objects inside the class "live and die" with the object. In effect, it is created when the object is created and destroyed when the object is destroyed.

Aggregate: only holds references to external objects. These objects exist apart from the aggregate.

Composition: locally creates its own objects. Objects are only relevant to the object holding them.

External forces can generally change the value of an object in an aggregate. This is good for an aggregate, bad for a composition.

We don't want external forces messing with the internal structure of a composition.

Aggregate Constructors

-Aggregates keep references to external objects. They don't generally make local copies.

e.g. public class Investment
{

public class Investment(Stock s, int num)
{
this.s = s;
this.num = num;
}

}

Composition Constructor

-Objects in a composition reflect the internal workings of an object.

-Local versions, not accessible from the outside, should be kept.
-Extends the concept of private access type.

public class CompositionWithStock
{

private Stock s;
Public ComparisonWithStock(Stock s, int num)
{ //create a local copy of s and store it
this.s = new Stock(s);
this.num = num
}

CSE Lecture 28/JAN/2008

Singleton-per-state

--> state attribute for a class(int)
---> only one object is allowed for each state. e.g. process information for each process in a UNIX environment.


Aside

HashMap

-A way of storing objects with respect to a key (i.e. an integer)

Objects: a,b,c
HashMap: h

h.put(a, 100) places the object a with the key 100.

-Say I want to store objects of type MyObject with respect to integers.

-HashMap h = new HashMap();

Singleton-per-state design pattern.

-constructor is private
-private static HashMap stores the instances with respect to status.
-getInstance(int state) is a public static method which checks the HashMap for an instance corresponding to state. If it exists, it is returned, if not, store it in HashMap, return it.

You cannot access non-static attributes from a static method.

Sunday, January 27, 2008

CSE Lecture 25/Jan/2008

Singleton Class

Before, we had utility classes, no instances were allowed.

Singleton class -> only one object is allowed to be instantiated.

Good for:

- Things that need to be globally constant, e.g. configuration and system status details.
- The key is the access specifier of the constructor: it must be private.

Object creation will take place within the class ---> created instances will be returned using a method other than the constructor...e.g getInstance()

Attempt 1
- Use static features to count # of instances created -> instanceCount variable.
- getInstance() checks instanceCount.
- If 0, create an instance and return it, set instance to 1
- If 1, return null.

Better way
- get Instance returns the single instance whenever it is called.
- class has to keep track of the object reference - via a private static variable.

Attempt 2a
- getInstance() checks instanceCount
-If 0, creates a new instance and saves it to a private static attribute and increments instanceCount
-returns the reference to the object.

Attempt 2b
-Don't need an instance counter
-Initialize instance when declared.

-This attempt is a design pattern for singleton classes.

Singleton Design Pattern

-private static instance holder --> creates and holds the only instance
-public static getInstance() -> returns instance
-private constructor -> prevents instances from being created by the client.
-no mutator for the instance holder, instance is final.

CSE Lecture 23/Jan/2008

Immutable classes are in effect "constant" objects. There is no reason to have more than one instance of an immutable object.

If I declare an object to be final, does that make the object immutable?

No. Final applies to the value in memory of the object.

An object is immutable if:

1) All the attributes are primitives, and private, and
2) None of the methods are mutators.

Mixed features of classes

-features that combine static and non-static features.
-static --> class
-non static --> features in the class.

Examples:

-Instance counter: how many instances of a class have been created.
-Serial number creator: every object is stamped with a unique serial number.

Monday, January 21, 2008

CSE Lecture 21/Jan/2007

Object Copying

-to copy non-objects, all we do is e.g.

int x =5;
int y;
y = x;

-is this the right way to copy an object?

BankAccount b = new BankAccount(100)
BankAccount c;
c = b;

c is not a copy, it is a reference to the same object.

You must:

1) create a new object
2) initialize it with the same state as the original.
e.g. double x = b.getBalance();
BankAccount c = new BankAccount(x);

Better Solution: The Copy Constructor

-Takes only one parameter -> the object to be copied.
BankAccount b = new BankAccount(100);
BankAccount c = new BankAccount(b); //create a copy of b

public BankAccount(BankAccount original)
{

//assign all attributes of this to be equal to attributes of original
balance = original.balance;

}

-It is the class's responsibility to provide a copy constructor.

-Sometimes there is a method called clone()
-only to be used if no alternative exists
-never write classes containing clone(), always use the copy constructor.

Mutable vs. Immutable classes

Mutable
-attributes can be changed after the object is constructed.

Immutable
-attributes cannot be changed after construction.

Immutable classes have no method to modify attributes, but provide access method to get the attributes.

If you can show that your class is immutable, you don't need a copy constructor.

ImmutableClass i = new ImmutableClass(5);
ImmutableClass j = i; //this is fine

Mutable classes require copy constructors.

CSE Lecture 18/Jan/2007

The this Keyword

-this refers to the current object
-this is always legal to use, sometimes makes the program easier to read
-can be used to resolve ambiguities in variable scope.

public class MyClass
{
private int x;
.
.
.
public void printX()
{
System.out.println(x); <--- can replace x by this.x
}
public void printOtherX(MyClass m)
{
System.out.println(this.x);
System.out.println(m.x);
}
}

Note: methods in classes have access to all private members of any object of that same class type(just not this).

This() as a method refers to the constructor of the current object.

public class MyClass
{
private int x;

public MyClass(int x)
{ this.x = x}

public MyClass() //default constructor
{this(0);
}

Two Special Methods: equals() and toString()

toString()

-creates a string representation of your object.
-method is called whenever your object is cast to a String

e.g. MyClass m = new MyClass();
System.out.println(m);

Expected style of toString output:

Class Name[attribute=value,attribute=value...]

e.g. public class MyClass

{

private int x;
private double y;
x = 0;
y = 3.5;

public String toString()

{

String s;
s = "MyClass[x=" + x + ", y=" + y + "]";
return s;

}

Should return MyClass[x-0,y=3.5]

equals()

-intended to determine whether two objects are equal.
-the == in e.g. m==n is true whenever m and n are objects if and only if m and n point to the same object.
-because of this we need the equals method.

public boolean equals(Object o)
{
MyClass m = (MyClass) o; //this is called a cast.

if ((x==m.x) && (y==m.y))
{return true;}
else {return false;}

}

Use getClass() method to determine whether the classes are equal.

{ if(this.getClass() == o.getClass())
{

MyClass m = (MyClass) o;
.
.
.
}else{return false}

}

Thursday, January 17, 2008

CSE Lecture Jan/16/2008

Separations of Concerns

This course is all about taking specifications and creating APIs. These allow the application programmer enough to access to perform the tasks required in the API, but no more.

Public Interface of an Object

Constructors, methods, and any attributes that are public. This is the way in which the client interacts with the API.

Implementing a public interface e.g. Bank Account class (see Bank Account code).

Calling the constructor within the class: this.(); where any parameters must be inserted into the brackets.

CSE Lecture 14/Jan/2008

Method Overloading

You are allowed to have more than one method with the same name. as long as the methods with the same name take different numbers and/or types of parameters.

All methods with the same function may have the same name. This reduces confusion.

e.g. a method that squares a number, without overloading:

public static int SquareInt(int x)
{ return x * x;}
public static double SquareDouble(double x)
{return x * x;}

With overloading:

public static int square(int x)
{return x * x;}
public static double square(double x)
{return x * x;}

For Java to use one method over another is called "overriding."

Overloaded methods provide for default parameters, and should call each other.

Interfaces as Parameters

Interface: a specification for a class containing method declarations.

A class that is declared to implement the interface must implement those methods exactly as specified.

e.g. Comparable

int compareTo(Object o)

Object a implements Comparable, as well as object b.

int x = a.compareTo(b);

If a > b, x > 0
If a < b, x < 0
If x == b, x == 0

Method "printCompare" takes two arguments and prints to the screen whether one argument is greater than the other. This is typically an easier method to implement.

Tuesday, January 15, 2008

CSE Lecture 11/JAN/2008

Java Packages

Ref: http://java.sun.com/docs/books/tutorial/java/package

You can assemble classes into packages, bundling related classes into one unit (one name space).

Creating a Package

e.g. a package named MyPackage
- create a directory called MyPackage/
- all classes intended for the package go in the directory
- First line of the java file for all classes in package: package MyPackage;

Using a Package

- Within the package, you use the usual simple names e.g. just Class A.
- Outside of the package we must use qualified names: e.g. MyPackage.Class A or import members of the package then use simple names.

To import, the first line of the program would be:

import MyPackage.Class A;
import MyPackage.Class B;
or
import MyPackage.*;

Then use simple names.

Arguments vs. Parameters

Parameter: the variable input by a method.
e.g. public static double areaOfCircle (double radius)

double y = 0;
double x = 1;
y = MyUtility.areaOfCircle(x);

The value of x should be unchanged after this operation. This is known as "passing by value". Within the method, a local copy of x is made.

That's the end of the story for non-object types.

The primitive types are not objects.

Object types:

Variable name (the value of the variable) is the location in memory where the object is stored: i.e. a pointer in some sense.

See Rectangle API in document e.g. Rectangle f = new Rectangle (3,4);

Knowing f gives access to f.width and f.height (via the Rectangle API);

"Passing by value" - a method is not allowed to change the value of the parameter in the calling method. Therefore a method cannot change the address of an object parameter.

A method, however, can change the contents of that memory address (if allowed by that API).

e.g. public static void twice(Rectangle x)
//intended to double width and height

Rectangle r = new Rectangle (3,4);
twice(r); //twice is allowed to change the value of r.width and r.height via the API.
//twice is not allowed to substitute r for a different object.

Wednesday, January 9, 2008

CSE 1030 Lecture 9/Jan/2008

Static Class Features

You can prepend the keyword "static" to either a variable or a method.

This variable or method is associated with the class and not with an object.

A class with only static members is called a utility.

Utilities and Constructors

If a utility has no constructor, it is legal to create an object of that type.

All classes have a default constructor automatically (with no parameters).

You can override the default constructor with our own default constructor, and make the constructor private.



Other notes:

It is perfectly legal to include a constructor in a utility class, however it does not much a lot of sense to instantiate one. This is why we commonly make the default constructor, if and when we include it, private.

I will also eventually post code from class up here, but I'd encourage writing it on your own.

Monday, January 7, 2008

CSE 1030 Lecture 7/Jan/2007

Writing Classes


The class is the basic unit of Java programming. Everything in Java is an object, where each object is of a class type.

A class is a data type definition. The object is a variable of the class type. e.g. String (class).

String s = "This is a string";

Another way is that the class is a category of objects. e.g. Car (class) can have objects My Toyota and Your Ford. These objects are of type Car.

Class Definition Syntax

We have already used:

public class My Program
{
public static void main(String[] args)
{
system.out.println ("Hello World");
}
}


We now use:

public class ClassName
{
//Attributes, variables contained either by the class itself or by objects of the class type.

//Constructors: special methods called when an object is created.

//Other methods: manipulate the attributes.

}


What is public?

This is an access specifier, it determines what other classes can access this resource. Another access specifier is private.

public: anyone can use this resource.
private: only methods within the class itself can access the resource.

There are another two: protected and (blank), i.e. no access specifier.

The ability to make resources private, or specify their access is fundamental to object-oriented programming.

It obscures details, abstracting away complexity, prevents other programs from messing with the internals of the class, and forces other programmers to interact with the class in a manner you specify. This is, in effect, the Application Programming Interface (API).

What is "static"?

A static feature of a class is a feature associated with the class is a feature associated with the class type itself, not with any particular object.

Example, the Car class: all are fueled by Gasoline (static attribute). My Toyota has a fuel level of a quarter tank and Your Ford has a fuel level of a half tank. This "fuel level" is a non-static attribute.

Example, the Jet class: all are fueled by JetA (static attribute). Air Canada Airbus has a fuel level of a full tank and Bill Gates' Private Jet has a fuel level of three-quarters of a tank. This "fuel level" is a non-static attribute.

Declaration of Access Specifiers

(access specifier) static (type of attribute) (attribute name) = initialized value;

Examples: public static String fuel = "Gasoline";
public static int x = 0;
public static double y = 1.2;


In the fuel example, the type of fuel will never change. However fuel is a public attribute that can be changed by other classes. We therefore use the final keyword to ensure this attribute is not writable , and the initial value becomes mandatory.

public static final String fuel = "Gasoline";


Static constructor?

Makes no sense, there's no such thing.


Static method?

Just fine. Write static after the access specifier as in public static void main(...)

They cannot access non-static attributes of an object.


Utility class

A class where every feature is static.



Here is a temporary link to a test video by Louis.

Sunday, January 6, 2008

Putty: An SSH Client

Want to program at home?

I use SSH at home.

Here is a link for putty, and here is the download page if you're lazy.

As for the configuration:
HOST: red.cs.yorku.ca



Once you are connected:
Login as: cse6XXX
password: xxxxxx



Where the login is your CSE(PRISM) username and password is the password to your CSE (PRISM) account (and not 6 x's). You will not see *'s usually, as when entering a password in Windows, however your password is being entered.

There is also documentation on putty here.

You can also log into your PRISM account by the link in the welcome message.

If you want to stay here and still see the links (i.e. without using your browsers Back button), hold the Shift key while clicking the link, and the corresponding page should pop up in another window.