Download presentation
Presentation is loading. Please wait.
Published byJessie Hawkins Modified over 9 years ago
1
Creative Commons Attribution Non-Commercial Share Alike License http://creativecommons.org/licenses/by-nc- sa/3.0/http://creativecommons.org/licenses/by-nc- sa/3.0/ Original Developer: Beth Simon, 2009 bsimon@cs.ucsd.edu
2
CSE8A Lecture3 TURN IN INTEGRITY FORMS NOW! (pass to aisle) SIT WITH YOUR DISCUSSION GROUP –Come to the front if you don’t know where they are TODO: –Contact your pair programming partner via email or find one on the discussion forum Post the 6 hours you have reserved to work together –Prepare for quiz on Friday –Work on PSA1 WITH YOUR PARTNER! Expect to spend 6 hours! Read next class: pages 60-89, take the online reading quiz. CLICKERS OUT! LAPTOPS? http://up.ucsd.edu
3
By the end of today’s class you should be able to… LG4: Be able to track a turtle’s path by reading and tracing code LG5: Identify common errors in Java code and use computing terminology to describe them LG6: Read and modify a method to perform a given task with a Turtle. Specifically understand the use of a parameter to a method. LG7: Know which methods get stored in Turtle.java and which get stored in their own files (and why). LG8: Describe return values of methods and identify ones from methods you have used
4
Lab and Pair Programming Lab was a blast! What’s it about? –Not just “typing in code” but –ASKING QUESTIONS about what that code is doing and why! –Everyone should ask at least one question in lab every day. What is pair programming? –Driver/Navigator Why pair programming? –People who pair in a first programming course, do BETTER in a second programming course. Note: your pair partner in lab is DIFFERENT (and changes each week) than for homework
5
What effect do parameters have? (Draw the turtle actions, assuming declared at 25,25 in a world declared with parameters 200, 100) maria.turnLeft(); maria.forward(10); jose.turn(-45); jose.forward(10); raul.turn(90); raul.forward(200); Turtle.turn(405); Turtle.forward(-10); Can you do LG4 now?
6
Javadocs for the Turtle class (know these) penUp penDown show hide moveTo setName getName
7
Why is this code incorrect? (pick all that apply – there are no assumptions for this code) A.Nothing is incorrect B.Return type is wrong C.Parameter is used incorrectly D.turnLeft should be turnRight E.use of turtle1 is incorrect public bethsSquare(int size) turtle1.turnLeft(); turtle1.forward(100); turtle1.turnLeft(); turtle1.forward(100); turtle1.turnLeft(); turtle1.forward(100); turtle1.turnLeft(); turtle1.forward(100); 1)SOLO VOTE (20 sec) 2)Discuss in team (1 min) 3)GROUP VOTE (15 sec)
8
Fix the code (write on the slide crossing out incorrect stuff and adding stuff to make it correct) public void bethsSquare(int size) turtle1.turnLeft(); turtle1.forward(100); turtle1.turnLeft(); turtle1.forward(100); turtle1.turnLeft(); turtle1.forward(100); turtle1.turnLeft(); turtle1.forward(100);
9
Quick Comparison: Other methods with parameters that you know System.out.println(“This is a parameter of String type); int x = 45; System.out.println(x); //Param of int type turtle1.setName(“George”); turtle2.moveTo(45,115); Now, back to the bethsSquare method… public void bethsSquare(int size)
10
What’s the right way to “call” that method to draw a square? 1)SOLO VOTE (60 sec) 2)Discuss in team (2 min) 3)GROUP VOTE (15 sec) World w = new World(); Turtle t = new Turtle(10,10, w); t = bethsSquare(50); World w = new World(); Turtle t = new Turtle(10,10, w); t.bethsSquare(); World w = new World(); Turtle t = new Turtle(10,10, w); t.bethssquare(); World w = new World(); Turtle t = new Turtle(10,10, w); t = bethssquare(50); A B C D E None of the above
11
Storing methods in the correct file We just wrote 2 methods. They each have to be stored in a different place. bethsSquare is a method that can be called on a Turtle so it needs to be in the Turtle class ( Turtle.java ) –t.bethsSquare(50); Then we wrote code to “test out” our Turtle method to “act on” a specific turtle. –That needs to go in a method that we can make up a name for… like SpecialTester.java
12
So we might in Dr. Java open a new file and put this in it. public class SpecialTester { public static void main(String []args) { World w = new World(); Turtle t = new Turtle(10,10, w); t.bethsSquare(); } And Save As…?
13
For PSA3: Where are files stored and WHY? 1) drawShape –A method which acts on a turtle object to make it complete a set of steps to make a closed form shape of your choosing 2) createShapes –Directs 4 different turtles in calling the drawShape method to make 4 nested copies of your shape 1 2 12 1 and 2
14
Writing Methods: Rules and Why Methods are a way of “encapsulating” (putting together in one place/one name) a series of code statements. They are “wrapped” in their –Method header (visibility, return type, name, parameter list) –And { and } enclose all the statements that we want to have done when we “call” that method WHY? –We do this when we have a set of statements that we may want to have done multiple times (and possibly for multiple objects/turtles)
15
Why Do Methods Get Stored in Turtle.java? If a method describes a “general action” a turtle could reasonably be expected to do –It goes in the Turtle class If it’s a specific “application” that is just a “one off” need –It’s its own program (createShapes, myWackyTurtleProgram, etc) Classes (like Turtle and Picture) are made up of –Data about them –Methods/Actions that can be taken on them
16
Pictures! DEMO You NEED TO try typing in the code to (for Pictures) –Create –Show –Open –Explore
17
What types are returned by these method calls? A) main main Picture String B) Turtle Turtle Picture String 1)SOLO VOTE (60 sec) 2)Discuss in team (2 min) 3)GROUP VOTE (30 sec) 1) turtle1.turnLeft(); 2) turtle1.getName(); 3) FileChooser.pickAFile(); 4) new Picture(); B) void void Picture String C) void void String Picture E) None Of The Above
18
What is a return value?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.