Download presentation
Presentation is loading. Please wait.
1
Lecture 6
2
What is the difference in pictures ???
3
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 – Loop One of the most important and interesting topics in this course
4
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 contains 2 methods in this example Methods can be used within a method If we run t.drawT( 50 )
5
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 drawT without using drawLineAt
6
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
7
Review (main method) public class Test { public static void main(String[] args) { } When we run a program, it always starts from main method.
8
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.
9
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 end of each statement
10
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 as public, class, void, etc Case sensitive!!! Textbook Page 63
11
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 brackets If open, then close it A class or a method should be closed by brackets
12
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 are required after method name The method may receive parameters
13
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.
14
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 error makes me sick 3 days But we have to find
15
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 parameters should be the same as that of method definition
16
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
17
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
18
Repetition Statements (Loops) Repetition statements allow us to run a statement or a block of statements multiple times Often we call them as loops
19
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
20
Example of for Loop public class Turtle { public void drawSquare( int len ) { for(int i=0; i < 4; i++) { forward( len ); turn(90); } Repeat these statements 4 times Count starts from 0 Add one for each repeat 4 is not included
21
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?
22
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?
23
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 use any variable name for a loop counter
24
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 printed number when you use this method
25
Exercise!!!
26
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); }
27
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); }
28
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); }
29
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
30
Exercise3Exercise3 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 ); }
31
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
32
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); }
33
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); }
34
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 ); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.