CS136, Lecture 1

Take attendance, hand out syllabus and Java syntax.


Course web page is at http://www.cs.williams.edu/~kim/cs136/


TA's: Adrian Ludwig and Snehal Patel. Snehal will have evening hours Sunday from 7 to 11pm and Monday from 8:30 to 10:30pm.
Assumed background: 134 equivalent - Pascal (or willingness to learn), records, multi-dim'l arrays, recursion, taste of pointers.


Focus in course on developing tools to solve wide range of problems.

Key idea in algorithm design is design of data structures and algorithms to work on them.

Also interested in analyzing how well a program works - efficiency and correctness.

Introduce new ideas in programming language support for problem solving:

Important characteristics of good code

Programming language characteristics needed to support this:

Style very important. Readable software necessary for correctness, optimization, and maintainability.


CS is science of abstraction and computation

Create right model for problem and devise appropriate mechanizable techniques to solve it.

Data and operations are tightly intertwined.

Idea of Abstract Data Types (ADT) - create your own data types with powerful operations.

All the user has to know is what is effect of operations, not how they are done.

E.g. - Data base is good example - representation hidden, but operations available to you.

Object-oriented programming takes one step further.

Objects bundled with their operations (methods)
Ask object to perform action (send message) - it responsible for knowing how.

Write post-its in Java

Use preconditions and postconditions to specify what actual procedures and functions do (as opposed to how they do it).

If create powerful types of objects with methods at beginning then actual program is easy. This is our goal.


Main foci of course:

  1. Use of abstraction - objects and classes.

  2. Interfaces provide (abstract) specification of operations

  3. classes provide concrete data representations and algorithms for operations.

  4. Correctness - testing and formal verification - preconditions and postconditions

  5. Analysis of complexity

  6. Language features to support writing of correct, efficient, and reusable code.

Use lots of examples of interesting problems.

Focus on use of applets which can be viewed and run in web browsers (easy to convert to applications as well).


Object-oriented programming:

A Class describes the possible objects of an ADT

An object consists of collection of features

Interfaces behave like the types of objects.
Provide specification (type info) of methods (but not bodies).


Example (in an object-oriented pseudo-code)
Interface EmployeeSpec
    GetName(): String
    GetWkPay(): Integer

Class Employee
Implements EmployeeSpec
Instance variables
    name: string
    birthDate: date
Constructor
    Employee(emp_name: string; emp_bday: date)  // Constructor of objects
                 name <-- emp_name;
                 birthDate <-- emp_bday;
Methods
    GetName(): string   return name
    getAge(): integer   return (Today - birthDate)
    getWkPay(): real    return 0.0

Note that Employee implements EmployeeSpec because it has at least the methods required.

If boss represents an object of class Employee, then can write:

	boss.getName() 
to send message to boss to return his/her name.

Inheritance is used to define new classes from old ones (incremental modification) by defining subclasses of existing classes:

Class HourlyEmployee
Superclass Employee
Instance variables
    hourlySalary: real
Constructor
    HourlyEmployee(empName: string; empBDay: date; hrlyPay: real) 
             name <-- empName;
         birthDate <-- empBDay;
         hourlySalary <-- hrlyPay;
Methods
    setHourlyPay(newSalary : real)  hourlySalary <-- newSalary
    getWkPay: real                  return (hourlySalary * 40)
	
{Note have over-ridden old getWkPay, but getName & getAge as above}


Class ExemptEmployee
Superclass Employee
Instance variables
    yearlySalary: real
Constructor
    ExemptEmployee(empName: string; empBDay: date; yrlyPay: real) 
             name <-- empName;
         birthDate <-- empBDay;
         yearlySalary<-- yrlyPay;
Methods
    setAnnualPay(salary : real) yearlySalary <-- new_salary
    getWkPay(): real                return (yearlySalary / 52)

Objects in subclass treated as though elements of class as well! If subclass does not provide method to respond to message, then use appropriate one from superclass (or its superclass, etc.)

In particular, subclasses also implement any interface declared for superclass.

Note that same method name can exist in different classes - form of polymorphism.

An hourly employee will respond differently from an exempt employee when getWkPay message is sent.

When want to add a new kind of employee, simply add a new subclass of Employee and only provide routines which differ from those of Employee.