Pages

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"




May 28, 2015

[Python] Don't use "<<" to double a number

As we know, we can use "a << b" to represent "a*(2^b)." But this method is much slower than just using addition, performing "a+=a" for b times. In other words, "<<" operation is much more expensive than "+" operation.

I have performed some simple experiments. The results are shown below.


As we can see, the running time for "<<" operations are much slower than that of "+" operation. It takes more than several hundred times for "<<" to produce the same result as "+" does. Therefore, using "+" operation instead of using "<<" for the same purpose. (Here I am under the restriction of not using multiplication.)

May 25, 2015

Lexicographic order in Java

The following notes are from Udacity-Intro to Java Programming

The term lexicographic order is not defined in the videos. It means the order the computer sorts the strings in. In the computer world, numbers are considered to be smaller than letters of any kind. So they come first. Uppercase letters are smaller than lowercase numbers letters. so "X" comes before "x"

This is another interesting thing about alphabetizing Strings containing numbers in computers. Here is how numbers would be ordered. 1, 11, 12, 13, 14 ... 2, 21, 22, 23 ... Anything that starts with 1 comes before anything that starts with 2. so even 1000 would come before 2.

Thus, numbers (1 < 11 < 12 < ... < 2 < 21 ...) < Uppercase letters < Lowercase letters
Ex:
123 < John < john
lesson1 < lesson10 < lesson2

We can use "compareTo" function to compare the lexicographic order between two strings.
ex: String1.compareTo(string2)
the return value:
0    : if two strings are identical
< 0 : if String1 is lexicographically less than String2
> 0 : if String1 is lexicographically greater than String2

See more detail on 

[Python] Conversion between int and bit

May 18, 2015

May 12, 2015

Call by assignment in Python

When passing a mutable object (list, dictionary, etc.) to a function, it works like "call by reference" that the object will be shared.

When passing a immutable object (int, tuple) to a function, it works like "call by value" that it pass a copy of the original object to a function.

See more detail:
[1] https://docs.python.org/3/faq/programming.html#how-do-i-write-a-function-with-output-parameters-call-by-reference
[2] http://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference