lecture 6. what is the difference in pictures ???

34
Lecture 6

Post on 22-Dec-2015

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 6. What is the difference in pictures ???

Lecture 6

Page 2: Lecture 6. What is the difference in pictures ???

What is the difference in pictures ???

Page 3: Lecture 6. What is the difference in pictures ???

Today’s topic

• Let’s discuss about the most common errors when writing a program– Based on my experience and what I found

from you guys

• Control program flow

– LoopOne of the most importantand interesting topics in this course

Page 4: Lecture 6. What is the difference in pictures ???

Review (sub-methods) public class Turtle {

public void drawLineAt( int x, int y, int len) { penUp();

moveTo( x, y); penDown();

forward(len); penUp();

}

public void drawT( int len ) {

drawLineAt(400, 200, len); turn( 90 ); drawLineAt(350, 200, len);

} }

Turtle class contains2 methods in this example

Methods can be used within a method

If we run

t.drawT( 50 )

Page 5: Lecture 6. What is the difference in pictures ???

Review (sub-methods) public class Turtle {

public void drawLineAt( int x, int y, int len) { penUp();

moveTo( x, y); penDown();

forward(len); penUp();

}

public void drawT( int len ) {

drawLineAt(400, 200, len); turn( 90 ); drawLineAt(350, 200, len);

} }

Why is this good???

What is the benefit???

What if we write drawTwithout using drawLineAt

Page 6: Lecture 6. What is the difference in pictures ???

Review (sub-methods) public class Turtle {

public void drawT( int len ) { penUp();

moveTo( 400, 200 ); penDown();

forward(len); penUp(); turn( 90 );

penUp(); moveTo( 350,

200 ); penDown();

forward(len); penUp();

} }

Do you like this?

- We can avoid to write repeatedly shown statements

- We can wrap them up as a method

- Also, it is easier to understand how program works

Page 7: Lecture 6. What is the difference in pictures ???

Review (main method)

public class Test{

public static void main(String[] args){

}}

When we run a program, it always starts from main method.

Page 8: Lecture 6. What is the difference in pictures ???

Review (main method)

public class Test{

public static void main(String[] args){

World w = new World();Turtle a = new Turtle(w);Turtle b = new Turtle(w);a.moveTo(100,100);b.moveTo(300,300);

}}

When we run a program, it always starts from main method.

Page 9: Lecture 6. What is the difference in pictures ???

What is wrong in this program???

public class Turtle{

public void drawSquare(){

forward(100)turn(90)forward(100)turn(90)forward(100)turn(90)forward(100)

}}

Semicolon at the endof each statement

Page 10: Lecture 6. What is the difference in pictures ???

What is wrong in this program???

Public Class Turtle{

Public Void drawSquare(){

forward(100);turn(90);forward(100);turn(90);forward(100);turn(90);forward(100);

}}

Key word such aspublic, class, void, etc

Case sensitive!!!

Textbook Page 63

Page 11: Lecture 6. What is the difference in pictures ???

What is wrong in this program???

public class Turtle{

public void drawSquare(){

forward(100);turn(90);forward(100);turn(90);forward(100);turn(90);forward(100);

}

A pair of bracketsIf open, then close itA class or a method

should be closedby brackets

Page 12: Lecture 6. What is the difference in pictures ???

What is wrong in this program???

public class Turtle{

public void drawSquare{

forward(100);turn(90);forward(100);turn(90);forward(100);turn(90);forward(100);

}}

Parenthesis arerequired aftermethod name

The method mayreceive parameters

Page 13: Lecture 6. What is the difference in pictures ???

How about this???

public class Turtle{public void drawSquare(){

forward(100);turn(90);forward(100);turn(90);forward(100);turn(90);

forward(100);}

}

It works!!!But please do not write

a program like this.

Hard to read (understand)Please be artistic.

Page 14: Lecture 6. What is the difference in pictures ???

How about this???

public class Turtle{

public void drawSquare(){

forward(100);turn(90);forward(100);turn(90);forward(100);turn(90);forward(100);turn(90);

.}

}

Suffering!!!

Finding this errormakes me sick

3 days

But we have to find

Page 15: Lecture 6. What is the difference in pictures ???

More …

public class Turtle{ public void drawSquare( int

len ) {

forward(len); turn(90);

forward(len); turn(90);

forward(len); turn(90); forward(len);

}}

public class Test{ public static void main(String[]

args) { World w = new World();

Turtle t = new Turtle(w); t.drawSquare(); }}

The number of parametersshould be the same as

that of method definition

Page 16: Lecture 6. What is the difference in pictures ???

More …

public class Turtle{

public void drawSquare() {

// omit…..

}

public void drawSquare( int len )

{ // omit

….. }

}

public class Test{ public static void main(String[]

args) { World w = new World();

Turtle t1 = new Turtle(w); Turtle t2 = new Turtle(w);

t1.drawSquare(50); t2.drawSquare();

}}

This is fine

Page 17: Lecture 6. What is the difference in pictures ???

• Keys to be a good programmer

– Keep writing a program (Every day!!!!!)

– Try to find the errors by yourself• Experience will train you more than just being helped

Page 18: Lecture 6. What is the difference in pictures ???

Repetition Statements (Loops)

• Repetition statements allow us to runa statement or a block of statements multiple times

• Often we call them as loops

Page 19: Lecture 6. What is the difference in pictures ???

Repetition Statements (Loops)

• Java has three kinds of repetition statements:

for loop

while loop

do loop

• The programmer should choose the appropriate loop for the situation

Page 20: Lecture 6. What is the difference in pictures ???

Example of for Loop

public class Turtle{

public void drawSquare( int len ) {

for(int i=0; i < 4; i++) { forward( len );

turn(90); }

} }

Repeat thesestatements

4 times

Count starts from 0Add one for each repeat

4 is not included

Page 21: Lecture 6. What is the difference in pictures ???

Example of for Loop

public class Turtle{

public void printNumber() {

int num = 20;

for(int i=0; i < 10; i++) {

System.out.pritnln( num ); }

} } How many 20’s are printed out?

Page 22: Lecture 6. What is the difference in pictures ???

Example of for Loop

public class Turtle{

public void printNumber() {

int num = 20;

for(int i=5; i < 10; i++) {

System.out.pritnln( num ); }

} } How about this?

Page 23: Lecture 6. What is the difference in pictures ???

Example of for Loop

public class Turtle{

public void printNumber() {

int num = 20;

for(int a=0; a < 10; a++) {

System.out.pritnln( num ); }

} }

You can change and useany variable namefor a loop counter

Page 24: Lecture 6. What is the difference in pictures ???

Example of for Loop

public class Turtle{

public void printNumber( int num ) {

for(int i=0; i < 10; i++) {

System.out.pritnln( num ); }

} }

You can specify the printednumber when you use

this method

Page 25: Lecture 6. What is the difference in pictures ???

• Exercise!!!

Page 26: Lecture 6. What is the difference in pictures ???

You may have this method already

public class Turtle{ public void drawSquare( int

len ) {

forward(len); turn(90);

forward(len); turn(90);

forward(len); turn(90); forward(len);

}}

Page 27: Lecture 6. What is the difference in pictures ???

Exercise1

public class Test{ public static void main(String[] args) { World w = new World();

Turtle t = new Turtle(w);

for(int k=0; k < 20; k++){

t.drawSquare(100);t.turn(20);

}}

}

Page 28: Lecture 6. What is the difference in pictures ???

Exercise2

public class Test{ public static void main(String[] args) { World w = new World();

Turtle t = new Turtle(w);

for(int k=0; k < 20; k++){

t.drawSquare( k * 10 );t.turn(20);

}}

}

Page 29: Lecture 6. What is the difference in pictures ???

• setPenColor(Color.xxx)where xxx will be RED, GREEN, BLUE, etc

(textbook page 43)

Note: please add a following line at a top of the class to use the Color method

import java.awt.*;

• getXPos()will get the position of x axis

• getYPos()will get the postion of y axis

Page 30: Lecture 6. What is the difference in pictures ???

Exercise 3

import java.awt.*;

public class Test{ public static void main(String[] args) { World w = new World();

Turtle t = new Turtle(w);

t.setPenColor( Color.RED );

for(int k=0; k < 10; k++){ t.penUp(); t.moveTo( t.getXPos()+5, t.getYPos()

+5 ); t.penDown(); t.drawSquare( k * 10 );}

}}

Page 31: Lecture 6. What is the difference in pictures ???

Go back to Exercise1

public class Test{ public static void main(String[] args) { World w = new World();

Turtle t = new Turtle(w);

for(int k=0; k < 20; k++){

t.drawSquare(100);t.turn(20);

}}

}

Can be written as a method

Page 32: Lecture 6. What is the difference in pictures ???

Exercise 1 (Another way)

public class Test{ public static void

main(String[] args) { World w = new

World(); Turtle t = new Turtle(w);

t.drawPicture()

}}

public class Turtle{ public void drawPicture() {

for(int k=0; k < 20; k++)

{

drawSquare(100);

turn(20);}

}}

Page 33: Lecture 6. What is the difference in pictures ???

Exercise 2 (Another way)

public class Test{ public static void

main(String[] args) { World w = new

World(); Turtle t = new Turtle(w);

t.drawPicture()

}}

public class Turtle{ public void drawPicture() {

for(int k=0; k < 20; k++) {

drawSquare( k * 10 );

turn(20);

}}

}

Page 34: Lecture 6. What is the difference in pictures ???

Exercise3 (another way)

public class Test{ public static void

main(String[] args) { World w = new

World(); Turtle t = new Turtle(w);

t.drawPicture()

}}

public class Turtle{ public void drawPicture() {

setPenColor( Color.RED );

for(int k=0; k < 10; k++) { penUp();

moveTo( getXPos()+5, getYPos()+5 );

penDown(); drawSquare( k * 10 );

} }}