6/25/02 Amir Kamil Topic: Administrivia, Course Overview, OOP Review, Java Intro Administrivia: - About me Amir Kamil 3rd year Engineering Physics and EECS major Techs taken: Math 1B, 53, 54 Physics H7A, H7B, H7C Chem 1B CS 3, 61A, 61B, 61C, 70, 164 EE 40 Email: cs61b-te@cory.eecs.berkeley.edu Webpage: http://inst.eecs.berkeley.edu/~cs61b-te Office Hours? - Course HWs due at midnight on Sundays, individual (you may discuss it with others, but the answers must be yours). Labs due by the end of lab period, recommend you at least look it over before coming to lab. If you are in a cancelled section or want to attend a different lab than the one you are in, ask the TA who's lab you want to attend for permission. You may attend any discussion section, space permitting, and any TA's office hours. If you have questions, first check the newsgroup (ucb.class.cs61b) to see if it has already been answered. If not, post your question. - Section Don't be intimidated from asking questions. Notes will be available on my webpage; they may be plain text or PowerPoint slides. I'll try to have them up before section. It will probably be best if you print out a copy and bring it to discussion. Course Overview: - The goal of CS61B is to teach you how to program, not to teach you Java. If that's what you want, take CS9G. Java is a tool through which 61B is taught. - We will teach you data structures including arrays, lists, heaps, stacks, trees, graphs. - We will also teach you some algorithms such as sorting, algorithmic analysis, and possibly other, related topics. - There are three projects in addition to homeworks and labs. Start early, or you'll regret it. OOP Review: - OOP Vocabulary: Object: A repository of data with access to "methods" to manipulate that data. Class: A type of object. All objects of that type have the same, but independent, "variables" and share the same "methods." Method: A procedure that operates on an object or class. Variable: Names for pieces of data. Variables that are contained within an object are also called "instance variables" or "fields." Inheritance: The ability to extend a class with additional methods or fields without modifying the original class. - An object of class "A" is called an "instance" of "A." - Multiple instances of a class can exist at the same time - they each have their own sets of variables. For example, if you have a class "Computer", you can have as many computers as you want in your program. Say you want to represent 277 Soda in Java. Then you might have 30 or so computers to represent each computer in the lab. They might have variables to represent their state (on, off, crashed), who's using them, and the programs they're running. The beauty of OOP is that you only have to define a single Computer class, and you can have as many independent instances as you need. - Inheritance means you can have "subclasses" of a class. For example, there are many different types of computers, e.g. laptops, desktops, Macs, PCs. They all share much of the same behavior: they can be on, off, or crashed, have different users, and run programs. But they also have differences in behavior. A laptop may be out of battery while a desktop can't be, a PC might be blue-screened while a Mac would just be unresponsive. Inheritance allows them to share many behaviors while differing in others. We will go into more detail on inheritance next time. - Methods are the means through which objects interact with each other. I reluctantly remind you of message passing from 61A, but keep in mind that this is not how OOP is implemented in Java. Methods may require certain "parameters," variables that are passed to the target object when one of its methods is called. Java Introduction: - Class definitions: access class name V V public class Computer { } - Method definitions: access, other modifiers (e.g. static, final) | return type function name | | / parameters V V V V public void main(String[] args) { // This line is the "prototype." } - Variable declarations: type optional initialization | name / | V V V | int value = 3; V Computer comp = new Computer("MSDOS 5.0"); - Primitive types: Java includes some built-in types that are NOT objects. Examples include int, float, boolean, char, and others. Primitive types are initialized with values, while objects are initialized with "new." Also, a variable corresponding to an object is actually a "reference" or "pointer" to the object. - Constructor: public Computer(String os) { } The expression new Computer() creates a new instance of the Computer class, initializes it by calling the Constructor, and returns a reference to the object. - Comments: Anything between "//" and the end of the line is a comment, and will be ignored by the compiler. Anything between "/*" and "*/" is a comment, and will be ignored by the compiler. - /* A simple example of a Java class */ /* Imports classes from external packages */ import java.util.*; public class Computer { int state = 0; // Let 0 = off, 1 = on, -1 = crashed. String os; // The operating system on the computer. String user; // Initially an empty list; no programs running. LinkedList programs = new LinkedList(); /* Turns on the computer, then boots it. */ public void turnOn() { state = 1; boot(); return; } /* Boots the computer */ private void boot() { if (os.equals("Windows 95")) { state = -1; // crash } else { programs.add(os); } } /* Logs a user into the computer */ public void login(String user) { this.user = user; } /* Constructor */ public Computer(String os) { this.os = os; user = "no one"; } } Notes: "void" denotes no return value. In this case, a return statement is unnecessary, but allowed, at the end of the method. "this" refers to the current object, so "this." refers to the instance variable named . It may be necessary to use "this" if variables names from an inner scope obscure instance variables of the same name. Next Time: Control Structures, Arrays, Lists, Inheritance, Running a Program