You are on page 1of 22

12/28/2013 11:44:00 PM

Lecture 1 & 2: Goals of CS 61B
1. Learning efficient data structures and algorithms 2. Designing and Writing Large Programs 3. Understanding and designing data abstraction and interfaces. 4. Java Object-Oriented Programming

Object: A repository of data

Classes: a particular type of object Method: Procedure, function that operates upon an object Inheritance: A class may inherit properties from a more general class Polymorphism: One method that works on at least two classes, even if the classes need different implementations.
o For instance, “addItem” method works on every kind of List, though adding to a shoppingList is different from adding to a shoppingCart.

Object-Oriented: each object knows its own class
o A shoppingCart and shoppingList knows which implementation applies to it.

Variables:

Variables are either Primitive or Non-Primitive Reference:  Variable that references or points to an object Example: String myString.  That chunk of memory is named “x” o x = 1.  Allocates a chunk of memory for you that is capable of storing an “int-sized” thing.   Does not create a String object Creates a reference variable that can then point to a String object  myString = new String(). These must be declared with their type o int x. .

takes a String parameter for the constructor to read and . new String(s) a.     looks where s points Reads String Constructs new String with copy of characters Makes s2 reference new String 3 string constructors: 1. “Yow!” 3. new String() constructs an empty string (0 characters) 2.   “new String()” is a constructor “ = “ assignment o causes myString to reference the new String object Example 2: s2 = new String(s).

toUppercase(). String s4 = “*”.makes a copy of the String object that “s” references.concat(“*”). String s3 = s2.concat(“!!”). .  Same as s4 = “*” + s + “*”. Rule: Constructors always have the same name as the object that they construct Methods s2 = s.concat(s2).  Same as s3 = s2 + “!!”.

I/O Classes and objects  Objects in System class for interacting with a user  System.in is an InputStream object that reads from the keyboard readLine is defined on BufferedReader objects How do we construct a BufferedReader?  Use InputStreamReader .out references a PrintStream object that outputs to the screen  System.

io.How do we construct InputStreamReader?  With InputStream How do we construct InputStream?  System. . BufferedReader  Compose into entire lines of text InputStreamReader  Compose into characters (2 bytes long) InputStream  Raw data *remember.*. JVM always runs the main method first *remember to do this  import java.in is one.

 .in)).class SimpleIO { o public static void main(String[] args) throws Exception{  BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.

A method is associated with its particular class.   . For example. 2013 Grading ------. 5. and so on down to D.10 pts Labs There are 200 points total you can earn in this course. additem might operate on any List. Object  An object is a repository of data. For instance. Polymorphism  The ability to have one method call work on several classes of objects. There is NO CURVE. 4. 185+ points is an A+. Class A class is a type of object Many objects of the same class might exist. of which a ShoppingList is just one type. ------. MyList might record your shopping 2. MyList and YourList may both be ShoppingList objects 3. 175-184 is 70 pts Projects an A. 25 pts Midterm II Late projects are penalized 1% of your score for every 50 pts Final Exam two hours by which you miss the deadline.  Sometimes a method is associated with a family of classes.(85-94).  For example. Method  A procedure or function that operates on an object or a class. Inheritance  A class may inherit properties from a more general class.61B Lecture 1 CS 61B: Lecture 1 12/28/2013 11:44:00 PM Wednesday. the ShoppingList class inherits from the List class the property of storing a sequence of items.200 pts Create a new project for each assignment The Language of Object-Oriented Programming 1. if MyList is a ShoppingList object. 25 pts Midterm I Late homeworks and labs will NOT be accepted. even if those classes need different implementations of the method call. January 23. period. 20 pts Homeworks broken down at left. for instance.

Java Java allows you to store data in variables. There are two ways to get classes of objects to play with:   Define your own Use pre-defined classes that Java has already made o For example. but you must first declare them and specify their type. even though adding an item to a ShoppingList is completely different from adding an item to a ShoppingCart. x = 1. 6. Declaring a Variable: Allocating a chunk of memory (which can be used to reference something) Variables are used not only to store numbers. Note: This does not create a string object! . String myString. which Java calls int. Object-Oriented  Each object knows its own class and which methods can manipulate objects in that class. (2) It names the variable (or chunk of memory) “x”. Java has the built-in class called String. but also to reference objects. one line of code might be able to call the addItem method on every kind of List. Python: x = 1 Scheme: (let ((x 1))) Java: int x.  This Java declaration does two things:  (1) Allocates a chunk of memory large enough to store an  integer.  Each ShoppingList and Shoppingcart knows which implementation of addItem applies to it.For example.

  Instead. it declares a variable (chunk of memory) myString does not reference anything initially Currently. Assume the following code is in a file called HelloWorld. The disadvantage is that you must wait for it to compile. Note: Unlike Scheme and Matlab. Java programs must be compiled first before you can run them. we can make it reference an object. The advantage is a program that is faster than one written in Scheme. myString references to an empty chunk of memory However. myString = new String().Java . Compilation converts your written code to a machine readable bytecode.

which can be a string. System.out to address the output stream. System The Objects are: args. because other classes may have variables called out too. with their own meanings.Classes The Classes are: HelloWorld. world” Code Analysis The innermost line:  System.out. o Hence we can invoke “println” from any PrintStream class object.out. world”  In truth. String. the first two of these are references to objects The Methods are: main. and “Hello. .out” o “println” takes one parameter. “println” is a method (procedure) of the class PrintStream. including “system.   “main” is a method in the “HelloWorld” class. println  The println method prints its parameter  The main method prints the string “Hello.  We have to write System. world”)  System.println(“Hello. o The characters that are sent through out find their way to your computer screen System is a class that happens to have out as a variable (among other variables).out references an object which has the class PrintStream o A Prinstream is a path by which characters can be output by a program.

out references an object of the class “Prinstream.” which contains the method “println.” . Analyze the Diagram In the “System” class.” The HelloWorld class contains the method “main.The HelloWorld class knows how to do the “main” method. system. just like the PrintStream class knows how to do the “println” operation.

sleeker  Portability The Way Java Works The goal is to write one application (in this case a Party invite) which will run on multiple platforms (whatever your friends may happen to have). 2. which checks for errors and won’t let you compile until it is satisfied that everything will run correctly. The compiled bytecode is platform-independent. Multiple Virtual Machines .Sierra-Bates 12/28/2013 11:44:00 PM Pages 1-9: Breaking the Surface Java is appealing in the following ways:  Write once/ run anywhere  Faster. Source:  Create a source document using an established protocol. Compiler  Run your source document through a compiler. 4. coded into Java bytecode. Output  The compiler creates a new document. in this case Java.  Any machine capable of running Java will be able to interpret/translate this bytecode into something that it can run. 1. 3.

class.java by using javac  If there are no errors. Output (code)  This is the compiled code: Party. but what they all do have is a virtual Java machine created through software engineering on each of their various devices. 1. and runs the program.class 4. you will get a second document called Party. Code Structure in Java .class file. Your friends don’t have a physical Java machine. What You’ll Do in Java You will write a source document. 3. Source  Save as Party. Compiler  Compile Party. This file consists of bytecode. The virtual Java machine reads and runs the bytecode. Java Virtual Machine  Run the program by starting the Java Virtual Machine (JVM) with the Party. The JVM translates the bytecode into something the underlying platform understands. compile it into Java bytecode using the Javac compiler. and run the compiled bytecode on a Java virtual machine.java 2.

Class must go between a pair of {…} What Goes in a Class? .  A tiny application may hold just one class.   A class represents a piece of your program.What Goes in a Source File?  A source file holds class definitions.

. Methods must be declared in a class. in the Dog class.  Every statement must end with a semicolon. What Goes in a Method?  The body of a method is essentially a set of instructions for how that method should be performed. the method Bark will hold instructions for how the Dog should bark.  This body must go in a set of {…}.    A class holds methods. o Must be within {…} Think of methods as functions For example.

the JVM runs everything between the {…} of your main method. . not per class). Remember. every Java program must have at least one class and one main method (one main for the entire program.Anatomy of a Class Note: When the JVM starts running. Then it looks for a particular method that looks exactly like  public static void main ([]String args) { // your code goes here } Next. it looks into the “class” you gave it at the command line.

The main() method is where the program starts running. Running a program means telling a Java Virtual Machine to load the class and to start executing its main() method. and to run until all the code in main is finished.Writing a Class with a Main Note:  In Java. You’ll type your source code and save it as a .java file. everything goes in a class.class document.   . but it will be compiled into a new document and saved as a .

Compiler (importance) In Summary:  JVM runs the bytecode that is produced by the compiler  The compiler is responsible for checking the original .Pages 18-19: JVM vs.java file for most mistakes in syntax Page 84: Declaring and Initializing Instance Variables .

 For example: o Int size. A value can be assigned to the variable at the same time that the variable is declared  int size = 420.Recall A variable declaration needs at least at least a name and a type. In other words:  DECLARING = giving a variable a NAME and a TYPE  INITIALIZING = assigning a variable a VALUE However When you do not initialize an instance variable. o String name.  String name = “Donny”.0  Booleans  o False References o Null For example: . what happens when you call a getter method? -> What is the default value of an uninitiated variable or a variable that has not been assigned a value? If you don’t explicitly assign/instantiate a variable or you don’t call a setter method…instance variable still has a value!  Integers o 0  Floating Point o 0.