Common Errors in Programming
340 likes | 358 Vues
Explore common errors when writing programs related to control flow and loops with examples from the Turtle class. Learn to identify mistakes and improve coding skills efficiently.
Common Errors in Programming
E N D
Presentation Transcript
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
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 )
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
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
Review (main method) When we run a program, it always starts from main method. public class Test { public static void main(String[] args) { } }
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); } }
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
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
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
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
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.
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
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
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
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
Repetition Statements (Loops) • Repetition statements allow us to runa statement or a block of statements multiple times • Often we call them as loops
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
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
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?
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?
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
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
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); } }
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); } } }
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); } } }
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
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 ); } } }
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
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() } }
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() } }
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() } }