Pages

Showing posts with label Data Structure. Show all posts
Showing posts with label Data Structure. Show all posts

Jun 10, 2015

[CS61B-2015] Notes for class 5-Binary and Bits



8 primitive types of Java:

  • The 5 integer types
    • Exact representations of integers.
    • byte, short, int, long, char.
  • The boolean type
  • The 2 floating point types
    • Approximate representation of reals.
    • float, double.
Integer:











bit operations:

Jun 8, 2015

[CS61B-2015] Notes for class 4-Testing



https://berkeley-cs61b.github.io/public_html/materials/lab/lab2/lab2.html

import org.junit.Test;
import static org.junit.Assert;
assertEquals(expected, actual);

To make testing output more concise
1. Annotate each test with @org.junit.Test
2. Make methods non-static
3. jh61b.junit.textui.runClasses

example:
https://github.com/Berkeley-CS61B/lectureCode/blob/master/lec4/live1/TestSort.java


Jun 7, 2015

[CS61B-2015] Notes for class 3




A variable name in Java is a name for a simple container.
The 9 things that can go in simple containers:

  • primitive values(byte, short, int, long, float, double, boolean, char
  • references to Objects (an arrow), references may be null

Java is "Pass by value" (in some case, the "value" is a reference?)


Jun 5, 2015

[CS61B-2015] Notes for class 2-Defining and using classes



Static vs. Non-static
A class may have a mix of static and non-static members.

  • A variable or method defined in a class is also called a member of that class.
  • Static members are accessed using class name, e.g. ClassName.StaticMember.
  • Non-static members cannot be invoked using class name: Dog.makeNoise().
  • Static methods must access instance variables via a specific instance, e.g. an object.
  • Static members are shared in a class.

Jun 4, 2015

[CS61B] Notes for class 5-Linked lists




Array-based lists
Advantage: very fast access of each item
2 disadvantages:
    (1) Insert item at beginning or middle takes time proportional to length of array.
    (2) Array has a fixed length.                        

Linked lists (a recursively data type)
Made up of nodes. Each node has:
(1) an item
(2) a reference to next node in list

Linked lists vs. Array lists
Advantages of linked lists:
(1) Inserting item into middle of linked lists takes constant time, if you have a ref to previous node.
(2) List can keep growing until memory runs out.

Disadvantage of linked lists: Finding n-th item of a linked list takes time proportional to n. (Start at head, walk n-1 nodes.)

The "public" and "private" keywords
Private method or field: invisible and inaccessible to other classes.
Why?
(1) To prevent data form being corrupted by other classes.
(2) You can improve the implementation without causing other classes to fail.


The interface of a class: prototypes for public methods, plus description of their behaviours.

Abstract Data Type (ADT): A class with well-defined interface, but implementation details are hidden from other classes.

Invariant: A fact about a data structure that is always true. "A Date object always stores a valid date." Enforced by allowing access only through method calls.
If we don't have any restriction of a field, than we could declare this filed to "public"; Otherwise, we should declare it as "private."

Not all classes are ADTs! Some classes are data storage units, no invariants; field can be public.

Double-Linked Lists
Insert and delete items at both ends in constant running time.

Sentinel: A special node that does not represent an item. It points to the first and last nodes.













[CS61B] Notes for class 6-Stack frames




The heap stores all objects, including all arrays and all class variables.

The stack stores all local variables, including parameters.

When a method is called, Java creates a stack frame (aka activation record); stores the parameters and local variables.

Stack of tack frame.


Garbage collection only performs on heap.

When a method finishes, its stack frame is erased.

Method "Thread.dumpStack()": Prints a stack trace of the current thread to the standard error stream. This method is used only for debugging.

Parameter Passing
Java passes all parameters by value: copied.
When parameter is a reference, the reference is copied, but the object is shared.





Jun 3, 2015

[CS61B] Notes for class 4-Iteration and array




Declare arraies:
int[] a, b[]; //a is 1D array, b is 2D array

Constant:
"final" keyword: value that can never be changed.
BAD: if (month == 2)
GOOD: public final static int FEBRUARY = 2;
              if (month == FEBRUARY)

Jun 1, 2015

[CS61B] Notes for class 3-types, conditionals, iteration and arrays




Primitive types: (don't have to create an object to store it)
byte : 8-bit integer    => -128~127
short:16-bit integer   => -32,768~32,767
int    : 32-bit integer  => -2,147,483,648 ~ 2,147483647
long : 64-bit
float : 32-bit
double: a 64-bit floating-point number
boolean: "true" or "false" ("True" or "False" in Python)
char: a character, 2byte, non-signed

e.g.
long x = 43L;

double & float values must have a decimal point:
double x = 10.0;
float x = 43.9f;

                               Object types                            Primitive types
-------------------------------------------------------------------------------------------
Contains a              reference                                   value
How defined?        class definition                          built into Java
How created?         "new"                                        "6", "3.4", "true", etc.
How initialized?    constructor                                default (usually zero)
How used?            method                                      operators("+", "*",...)


Function: methods declared to return a non-void type.

char c = new char[4];
c[4] = 'x'; //Run-Time error

The size of an array cannot be changed once it has been created.


May 31, 2015

[CS61B] Notes for class 2-Defining classes




Java gives a default constructor that takes no parameter, does no initializing. However, if we write a constructor, the default constructor goes away.

Static field: a single variable shared by a whole class of objects. Also called class variables.
e.g. System.in & System.out are static fields.

Static method: Doesn't implicitly pass an object as a parameter.
main() is always static.
IMPORTANT: In a static method, THERE IS NO "this".


May 30, 2015

[CS61B] Notes for class 1-Using objects



Object: A repository of data.

Class: Type of object.

Method: Procedure or function that operates on an object.
e.g. addItem: adds an item to any ShoppingList object

Inheritance: A class may inherit properties from a more general class.
e.g. ShoppingList inherits from List the property of storing a sequence of items.

Polymorphism: One method works on several classes; even if the classes need different implementations.
e.g. "addItem" method on every kind of List, though adding item to a ShoppingList is different from a ShoppingCart.

Object-Oriented: Each object knows its own class and methods.
e.g. Each ShoppingList and ShoppingCart knows which implementation applies to it.


Java
Variables: You must declare them and their type.
Variables also used to reference objects.

e.g.
String myString; 
//Only declare a String type reference variable that can point to a String object.
//It doesn't create an object.

String myString = new String();
//"new String()" is a constructor call. It creates a String object.
//assignment "=" causes myString to reference the String object.


Objects & Constructors
If an object is not referenced by a variable, it will eventually be deleted (garbage collection).
Constructors always have same name as their class, except "stuffinquotes".

3 ways to construct a string object:
1. new String()
2. "test"
3. new String("test")

Strings are immutable.

Some object reference examples:
e.g.
String s1 = new String();
s1 = "test";
String s2 = s1; //s1 and s2 reference to the same object
s2 = new String(s1); //s1 and s2 reference to different objects

e.g.
String s1; //null pointer (reference to nothing)
String s2 = s1; //s2 is also a null pointer
s1 = "test"; //while s2 still is a null pointer

e.g.
String s1;
String s2 = s1;
s1 = "test";
s2 = "test"; //Java will make s2 point to the same object that s1 pointed because their value is the same.
s2 = new String(s1); //force Java to create a copy of that String object.
s2 = s1;
s1 = "another test"; //now s2 = "test" while s1 = "another test"