Monday, June 27, 2011

reversing a String

Native recursion:
-remove first character
-reverse the res of the string
-add (former) first character to the end of the string


Steps to develope a recursive algorithm:

1) Can I divide the problem at hand into smaller problems of the same type?
2) given a sufficiently small instance of the problem, is it simple (or trivial) to find the answer?

--> BASE CASES <--

3) given answers to the smaller problems,
can I reassemble the answers to get an answer for the whole problem?

Wednesday, June 22, 2011

June 22, 2011

ArrayList
- a generic class, should be restricted for use
Array List tmpCards = new ArrayList ()
.
.
.
tmpCards.add(anotherCard);
.
.
.
Card[] allCards = new Card[tmpCards.size()]
allCards= tmpCards.toArray(allCards);


How to check if Equal?

String s1 = "abc"
String s2 = "abc"

'==' tests equality of reference

if (s1 == s2) or s1.equals(s2)
System.out.println ("equal")
else
System.out.println("not equal")

the answer is equal!

why?
even
String s3 = "a" + "b" + "c"
if (s1==s3) .....
will be equal

but
String s4 ;
s4 = "b";
s4 = "a" + s4;
s4 += "c" ;
if (s1 == s4)....

NO it is not equal

but 

s1.equals(s4)

Yes it is equal!



Recursion

GNU ::= GNU is not Unix
"recursive acronym"

----------------

name list ::= name, namelist | name



Fibonacci Numbers
F(0) = 0
F (1) = 1
F(k) = F(k-1) + F(k-2)
int fibonacci (int k) {
if (k==0) return(0);
if(k==1) return(1);
return(fibonacci(k-1) + fibonacci(k-2));
}

Monday, June 13, 2011

June 13th , 2011

Polymorphism & inheritance
Design Principles:

* A derived class should have an ' is-a, plus ' relationship to the superclass.
- The derived class is an instance of the superclass, plus same additional attributed & methods.
* Push common attributes & methods as high as possible in the hierarchy.
*If the derived class needs to manipulate attributes in superclass, use the methods of the superclass where possible.

Java Keywords:
'super' refers to the immediate superclass
'this' refers to the current class

Wednesday, June 8, 2011

June 8th , 2011

Posted a list of topics to course website.
Notes and code for i/o posted on course website.


I/O - Input & Output

Friday, June 3, 2011

June 3st - 2011

public class sailboat extendets watercraft
{



public stringtostring(){
string assembply;
final double poundspertonne= 2204/62262;
double pounds = getDisplacement()*poundsPerTonne;
assembly += "masks, displacement";
assembly += Double.toString(pound);
assembly += " pounds." ;
return (assembly);



Question 12:52 PM
Sailboar laser2 = new Sailboat();
laser2.toString();



choice of overload is made at compile time based on signaure of the method
choice of override is made at runtime based on type of object