CSC152 2005S, Class 4: An Introduction to Java Admin: * Apologies about reading * Readings can generally be found in the Espresso site * Readings for Monday available later today or early tomorrow * Sam absent on Monday and Tuesday. Cassie teaches. Apologies. Overview: * Theoretical background * Detour: Packages * Running Java programs * Your first program Non-Quiz: * What is an object-oriented programming language? * A language in which you programs are combinations of objects * Objects combine data and capabilities * What key features do you expect in such a language? * Encapsulation - Grouping and Protection * Why do we use them? * We can reuse objects and methods that apply to objects * Protection * Classes vs. objects * Class provides the general features * Object provides the specifics * We need to know how to: * Tell an object to do something * Learn the capabilities of an object * Learn how to refer to objects * Learn how to make objects * Learn how to make objects interact * Learn how to define new classes in terms of other classes * Define classes * Also: Where does the program start? On to Java * Java is a popular object-oriented programming language * Developed at Sun microsystems * Designed for large-scale programming * Designed for embedded devices * Ugly syntax, based on the C programming language Important Java ideas for this course: * The source code for each class usually gets its own file, named ClassName.java * Names follow the "start with capital letter, capitalize each word * Related classes are shoved into packages * Package names are usually multi-part with the parts separated by dots * Multipart names allow us to distingush different packakges greenbe1.hw2 kellylor.hw2 * Official standard looks a lot more like backwards domain names edu.grinnell.cs.rebelsky.cs152.2005s.hw2 * On our linux systems, the names correspond to directories edu.grinnell.cs.rebelsky should be in rebelsky within cs within grinnell within edu within SOME SPECIFIED STARTING POINT package rebelsky.introjava; LOOK, we're working in package rebelsky.introjava Form: package name.of.package SEMICOLON public class First WE'RE DEFINING A CLASS, NAMED First, anyone can use it { START OF CLASS DEFINITION public static void main(String[] args) throws Exception { In Java, non-parameter naming is typically a two-step process * "Declare" the name - Say what kind of things we name KindOfThing name; * "Assign" the name - Say what it names name = ... java.io.PrintWriter pen; pen = new java.io.PrintWriter(System.out, true); new ClassName creates a new object java.lang.String greeting; greeting = "Hello"; pen.println(greeting); EVERY LINE MUST BE TERMINATED BY A SEMICOLON } // main(String[]) } // class First END OF CLASS DEFINITION To tell an object to do soemthing name DOT thingtodo OPENPAREN params CLOSEPAREN "fred simplify yourself" fred.simplify(); "x, add yourself to y" x.add(y); Awful Java convention: Programs start with a method named main which *must* have the form above public static void main(String[] args) throws Exception How do we get this damn thing to work, anyway? * First, translate .java to .class /opt/jdk1.5.0/bin/javac ClassName.java * Next, "execute" the appropriate class /opt/jdk1.5.0/bin/java package.name.ClassName