Escolar Documentos
Profissional Documentos
Cultura Documentos
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.
Variables:
Variables are either Primitive or Non-Primitive Reference: Variable that references or points to an object Example: String myString;
Does not create a String object Creates a reference variable that can then point to a String object
new String() is a constructor = assignment o causes myString to reference the new String object
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
makes a copy of the String object that s references. Rule: Constructors always have the same name as the object that they construct
I/O Classes and objects Objects in System class for interacting with a user System.out references a PrintStream object that outputs to the screen 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
How do we construct InputStreamReader? With InputStream How do we construct InputStream? System.in is one. 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
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
Grading ------- 10 pts Labs There are 200 points total you can earn in this course, 20 pts Homeworks broken down at left. 185+ points is an A+, 175-184 is 70 pts Projects an A, and so on down to D- (85-94). There is NO CURVE. 25 pts Midterm I Late homeworks and labs will NOT be accepted, period. 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. ------- 200 pts Create a new project for each assignment
The Language of Object-Oriented Programming 1. Object An object is a repository of data. For example, if MyList is a ShoppingList object, MyList might record your shopping 2. Class A class is a type of object Many objects of the same class might exist, for instance, MyList and YourList may both be ShoppingList objects 3. Method A procedure or function that operates on an object or a class. A method is associated with its particular class. Sometimes a method is associated with a family of classes. For instance, additem might operate on any List, of which a ShoppingList is just one type. 4. Inheritance A class may inherit properties from a more general class. For example, the ShoppingList class inherits from the List class the property of storing a sequence of items. 5. Polymorphism The ability to have one method call work on several classes of objects, even if those classes need different implementations of the method call.
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;
This Java declaration does two things: (1) Allocates a chunk of memory large enough to store an integer, which Java calls int. (2) It names the variable (or chunk of memory) x.
Declaring a Variable: Allocating a chunk of memory (which can be used to reference something) Variables are used not only to store numbers, but also to reference objects. 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, Java has the built-in class called String.
Instead, it declares a variable (chunk of memory) myString does not reference anything initially
Currently, myString references to an empty chunk of memory However, we can make it reference an object. myString = new String();
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) System.out references an object which has the class PrintStream o A Prinstream is a path by which characters can be output by a program. 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). We have to write System.out to address the output stream, because other classes may have variables called out too, with their own meanings. println is a method (procedure) of the class PrintStream. o Hence we can invoke println from any PrintStream class object, including system.out o println takes one parameter, which can be a string.
The HelloWorld class knows how to do the main method, just like the PrintStream class knows how to do the println operation.
Analyze the Diagram In the System class, system.out references an object of the class Prinstream, which contains the method println. The HelloWorld class contains the method main.
Sierra-Bates
12/28/2013 11:44:00 PM
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.
What Youll Do in Java You will write a source document, compile it into Java bytecode using the Javac compiler, and run the compiled bytecode on a Java virtual machine. 1. Source Save as Party.java 2. Compiler Compile Party.java by using javac If there are no errors, you will get a second document called Party.class. This file consists of bytecode. 3. Output (code) This is the compiled code: Party.class 4. Java Virtual Machine Run the program by starting the Java Virtual Machine (JVM) with the Party.class file. The JVM translates the bytecode into something the underlying platform understands, and runs the program. Code Structure in Java
What Goes in a Source File? A source file holds class definitions. A tiny application may hold just one class. A class represents a piece of your program. Class must go between a pair of {}
A class holds methods. Methods must be declared in a class. o Must be within {} Think of methods as functions For example, in the Dog class, the method Bark will hold instructions for how the Dog should bark.
What Goes in a Method? The body of a method is essentially a set of instructions for how that method should be performed. This body must go in a set of {}. Every statement must end with a semicolon.
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).
Writing a Class with a Main Note: In Java, everything goes in a class. Youll type your source code and save it as a .java file, but it will be compiled into a new document and saved as a .class document. 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. The main() method is where the program starts running.
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: