Professional Documents
Culture Documents
Lecture 1 & 2: Goals of CS 61B
Lecture 1 & 2: Goals of CS 61B
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
Variables:
Example 2:
s2 = new String(s);
3 string constructors:
1. new String() constructs an
empty string (0 characters)
2. Yow!
3. new String(s)
a. takes a String parameter for
the constructor to read and
Methods
s2 = s.toUppercase();
String s3 = s2.concat(!!);
Same as s3 = s2 + !!;
String s4 =
*.concat(s2).concat(*);
Same as s4 = * + s + *;
*remember to do this
import java.io.*;
class SimpleIO {
o public static void main(String[] args)
throws Exception{
BufferedReader keyboard = new
BufferedReader(new
InputStreamReader(System.in));
61B Lecture 1
CS 61B: Lecture 1
12/28/2013 11:44:00 PM
Wednesday, January 23, 2013
For example, one line of code might be able to call the addItem
method on every kind of List, even though adding an item to a
ShoppingList is completely different from adding an item to a
ShoppingCart.
6. Object-Oriented
Each object knows its own class and which methods can manipulate
objects in that class.
Each ShoppingList and Shoppingcart knows which implementation
of addItem applies to it.
Java
Java allows you to store data in variables, but you must first declare them
and specify their type.
Python: x = 1 Scheme: (let ((x 1)))
Java: int x; x = 1;
String myString;
Note: This does not create a string object!
Note: Unlike Scheme and Matlab, Java programs must be compiled first
before you can run them. Compilation converts your written code to a
machine readable bytecode. The advantage is a program that is faster than
one written in Scheme. The disadvantage is that you must wait for it to
compile.
Classes
The Classes are: HelloWorld, String, System
The Objects are: args, System.out, and Hello, world
In truth, the first two of these are references to objects
The Methods are: main, println
The println method prints its parameter
The main method prints the string Hello, world
Code Analysis
The innermost line:
System.out.println(Hello, world)
The HelloWorld class knows how to do the main method, just like the
PrintStream class knows how to do the println operation.
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, 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).
1. Source:
Create a source document using an established protocol, in this
case Java.
2. Compiler
Run your source document through a compiler, which checks for
errors and wont let you compile until it is satisfied that
everything will run correctly.
3. Output
The compiler creates a new document, coded into Java
bytecode.
Any machine capable of running Java will be able to
interpret/translate this bytecode into something that it can run.
The compiled bytecode is platform-independent.
4. Multiple Virtual Machines
Your friends dont have a physical Java machine, but what they all
do have is a virtual Java machine created through software
engineering on each of their various devices. The virtual Java
machine reads and runs the bytecode.
Anatomy of a Class
Note: When the JVM starts running, it looks into the class you gave it at
the command line. Then it looks for a particular method that looks exactly
like
public static void main ([]String args) {
// your code goes here
}
Next, the JVM runs everything between the {} of your main method.
Remember, every Java program must have at least one class and one
main method (one main for the entire program, not per class).
Page 84:
Declaring and
Initializing
Instance Variables
Recall
A variable declaration needs at least at least a name and a type.
For example:
o Int size;
o String name;
A value can be assigned to the variable at the same time that the
variable is declared
int size = 420;
String name = Donny;
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, 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 dont explicitly assign/instantiate a variable or you dont call
a setter methodinstance variable still has a value!
Integers
o 0
Floating Point
o 0.0
Booleans
o False
References
o Null
For example: