Friday, March 28, 2014

Lecture 2 | Programming Methodology (Stanford)


Hii all , can't wait to attend the second lecture with Prof. Mehran. Let's start!

Lecture Notes:

- we saw karel and tried to play with it using a small program.
- we've to start solving, check  Assignment 1 PDF. and Project Files.
- Be sure that you installed Eclipse Standard and JRE before starting this Assignment.

karel
karel is a robot needs to be controled to finish its tasks. we are going to use karel to enter the programming world.

commands that karel understands easily:
- move          -> move one step 
- turnLeft      -> turn 90 degree to the left
- pickBeeper  -> catch a candy
- putBeeper   -> put a candy from its candy bag

these commands called methods in programming world. we use them to control karel in its virtual world. 

first, we tried a simple program that control karel.


                                              2

Import stanford projects:

- File -> Import -> Existing Projects in Workspace -> select archive file .. browse .. select your project. this is the program code in case you want to give it a try.
   move( );
   pickBeeper( );
   move( );
   turnLeft( );
   move( );
   turnRight( );
   move( );
   putBeeper( );
   move( );
as you can see each method ended with ( ). this is important to be a valid command for karel. also at the end of each statement/command in programming world you need to end it with a semicolon ' ; '.

Run Method

we put all these method in a bigger method called run which is the start point of karel. karel read run method line by line and achieve it.



            4 

 Tabs

 you need to take care of putting tabs for code readability.

How is eclipse organized ?

                              حشؤنشل

each project in eclipse contains a set of packages. each package contains a set of classes. each class contains a set of methods.

why ?- it's a way for organizing your code, but it's also made like this to use it easily in the future. don't worry if you can't understand it clearly, soon we will try everything practically.  

what does extends means?

 it means that you're going to use other properties from another class. our program needs to extend from karel to use its commands.

what is import stanford.karel.* ?

telling the program to get everything related to karel from stanford library.

creating New Methods : TurnRight ( ) , TurnAround( )

we need to add a new method as karel doesn't have this behavior. 

private void turnRight ( ){
          turnLeft( );      
          turnLeft( );
          turnLeft( );
}

private void turnAround( ) {
          turnLeft( );      
          turnLeft( ); 
}

after that we found out that there is another class called superkarel which contains more methods, so we decided to extends superkarel instead of karel.

SNOOZE story :D
this was an introductory story to a programming concept called "For Loop", which means doing something multiple times as much as you want.

For loop syntax
for ( int i = 0 ; i < Ntimes ; i++){

   body that will be executed Ntimes
}

while loop syntax 
when you want to do something many times but you do know how much, you can use a while loop. karel want to move till it hits a wall.
while ( frontIsClear ) {

        body


IF condition 
in case you want to execute a command once upon a condition.
if( beeperPresent ) {
    
    pickBeeper( );
}
else {

    putBeeper( );
}

Nested If
also you can put another condition inside the if command.

Readable code
write a code for people to read not only for computers to execute.

the last minutes of the lecture was very FUNNY :D:D ,The End for today :), see you in the next lecture! and i will try to think out loud while solving my first assignment :).

No comments:

Post a Comment