CSC152 2005F, Class 04: An Introduction to Java (1) Admin: * New class member: Tom; senior Biol. Chem / Econ major (prewealth) who believes computers are useful. We expect to prove him wrong. * Convo tomorrow. Extra credit. Why go? * Today's lab continues on Friday * Questions on yesterday's lab? * New Web site questions? Overview: * Review * High-level discussion of Java * Questions on the readings * Lab! Yesterday's lab: * Emacs doesn't believe in dialog boxes Review: What is an Object-Oriented Language? * An OOL is a language that uses objects as the primary building blocks of programs * Objects are described by classes * Each object has some information (aka fields, data, attributes) and ways to process that information (aka methods, functions, procedures) Example: * "Numbers" is a class * Each number has a value (field, more or less) * Each number can be printed on the screen * Each number can be added to, subtracted from, multiplied by, etc. other similar kinds of numbers * "Rational Numbers" is a class closely related to numbers * Every rational number is a number * The "value" of a rational number is traditionally specified with two parts, a numerator and denominator * To add two rational numbers (creating a new rational number), a/b and c/d = (ad + bc)/bd Key Tasks in Object-Oriented Languages * Specify classes: * Each class goes in a file named CLASS.java (e.g., Rational.java) * Classes begin with the words PROTECTION class NAMEOFCLASS { } public class Rational { int numerator; int denominator; public Rational add(Rational other) { ... } } * Instantiate objects: new NAMEOFCLASS ( PARAMETERS ) E.g., new Rational(1,2) ; new Rational("1/2") Typical programs: * Describe names * Create objects * Tell objects to do things * Tell objects to do something (invoke methods): NAMEOFOBJECT.NAMEOFMETHOD(PARAMS) screen.display(rat.toString()) * Encapsulate/protect: Later * Begin: Create a special kind of class (Sam calls it a "main class") That class has a method called "main" "main" must look as follows: public static void main(String[] args) throws Exception { STUFFYOUWANTTODO } * Inherit: * Polymorphism: