1 / 34

Lecture 6

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 Loop. One of the most important and interesting topics in this course.

durhamd
Download Presentation

Lecture 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 importantand 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 contains2 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 drawTwithout 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) When we run a program, it always starts from main method. public class Test { public static void main(String[] args) { } }

  8. Review (main method) When we run a program, it always starts from 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); } }

  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 endof 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 aspublic, 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 methodshould be closedby 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 arerequired aftermethod name The method mayreceive 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 writea 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 errormakes me sick3 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 parametersshould be the same asthat 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 runa 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 thesestatements4 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 useany variable namefor 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 printednumber when you usethis 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. 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 ); } } }

  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 Turtle { public void drawPicture() { for(int k=0; k < 20; k++) { drawSquare(100); turn(20); } } } public class Test { public static void main(String[] args) { World w = new World(); Turtle t = new Turtle(w); t.drawPicture() } }

  33. Exercise 2 (Another way) public class Turtle { public void drawPicture() { for(int k=0; k < 20; k++) { drawSquare( k * 10 ); turn(20); } } } public class Test { public static void main(String[] args) { World w = new World(); Turtle t = new Turtle(w); t.drawPicture() } }

  34. Exercise3 (another way) 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 ); } } } public class Test { public static void main(String[] args) { World w = new World(); Turtle t = new Turtle(w); t.drawPicture() } }

More Related