// APS101, Winter 2009: Lecture 6 (Jan. 20) // // Review: so far, we've worked with objects in the Interactions pane. // Examples of classes we've seen: Date, String, Integer, Double, JFrame. JFrame j; import javax.swing.JFrame; JFrame j; j = new JFrame(); j.setVisible(true) j.setSize(400, 400) j.setVisible(false) j.setVisible(true) j.setTitle("This is my window!") j.getWidth() j.getHeight() j.getTitle() // Today, you'll be learning how to write your own programs. // Specifically, you'll learn how to write your own classes. // First, let's look at some terminology... // // What is a class? // A class is a blueprint that specifies what an // object of that class is supposed to look like. // (i.e., what variables and methods it's supposed to have.) // // // instruction: action to be performed // Java statement: an instruction written in the Java language // Java program: an organized collection of Java statements // execute: follow the instructions // // // There are three types of methods: // function: returns data of a particular type // procedure: perform a set of actions, but don't return any data // constructor: functions that create the object and return it // ex. of a procedure: setSize, setTitle, etc. // ex. of a function: charAt, getTitle, etc. // // What if we want to customize the JFrame class? // (we wrote OurJFrame.java) // // // NOTES // // We start off with a file called MyFileName.java, // and put all our code in there. // // Then, we compile it, and get MyFileName.class. // Be careful: DrJava makes backups of .java files, // in the form of .java~ (don't open or work with these!) // // What is "this"? // "this" refers to the current object. // so, inside a class, you should always put "this" in front // of instance variables and methods. // ex. this.variableName and this.methodName() // // Arguments vs. Parameters // parameter: an abstract definition of what the input // to a method is supposed to be (what TYPE!). // argument: the actual value of the input that is // given to the method. // ex. String title (param) vs. "This is OUR frame!" (arg) // // Let's try out the new class we wrote! OurJFrame ow = new OurJFrame("This is OUR frame!"); ow.doubleWidth() ow.getOurTitle()