Computer Science 1026 Topic 3.docx

3 Pages
Unlock Document

Computer Science
Computer Science 1026A/B
Brian Langen

Topic 3: Introducing to Programming Creating Objects  To create an object of a class, a special keyword’ new’ is used o new ClassName(value,value);  By only creating an object, there is no way to refer to it repeatedly. A reference variable is therefore needed. o Class name; o Class name = new Class(value,value); Declaration and referencing done on one command  Creating a world o A world object is an empty canvas with default size of 640 x 480 o New World(); o World world1 = new World (); Creates a world and assigns a reference variable world1 to it.  Creating a turtle o A turtle object lives in a world (must be specified), and by default starts facing north and in the center o A turtle will not move out of the boundaries of the world. If it is commanded to go beyond the world’s boundaries, it will simply move to the respective boundary location, and stop there. o More than one turtle can be created and assigned to a single world. o Turtles’ positions are given as x (horizontal) and y (vertical) values.  Turtle turtle1 = new Turtle(world1); Creates a turtle, and assigns it to world1 o Turtles can be created at different starting positions  Turtle turtle1 = new Turtle (50,50,world1); Forces a turtle to start at (50,50) if possible. o When worlds or turtles are printed out, all their characteristics are given. This is useful for debugging.  System.out.println(world1); Object Method  Method (Object, Instance): A named collection of java statements that perform specific actions on an object  Calling Methods: o A method can be called / invoked for a program when we want it to be executed o An object method can only be executed on an object belonging to the class in which the method was defined  Method Parameters: o A method may take input to act upon o Input is passed into the method in the form of a list of parameters inside the parentheses, when it is invoked o When parameter(s) are missing, either a syntax error or a default action will occur o methodName (paramenter1, parameter2,…)  Return Values: o If a method produces a result, the result is then returned to wherever the method was invoked.  Exception: String Methods o Strings are immutable objects. o String methods that modify a string don’t change the original string, but return another string with the result Methods for Turtles  Moving Turtles o Turtle.forward (); Makes a turtle travel in the direction it is facing. By default, move 100 pixels. o Turtle.backward (); Makes a turtle travel in the opposite direction. o Turtle.moveTo(x,y); Makes a turtle move to a specific pixel.  Turning Turtles o Turtle.turnRight (); Makes a turtle turn 90 degrees to the right. o Turtle.turnLeft (); Makes a turtle turn 90 degrees to the left. o Turtle.turn (); Makes a turtle turn a specific number of angles. o Turtle.turnToFace (); Makes a turtle turn to face another turtle, or a specific point.  Pen o Each turtle has a pen, which leaves a trail as the turtle moves. By default, the pen is down. o When asked to make the turtle return to its default position, make sure to drop the pen back down. o Turtle.penUp(); Picks up the turtle’s pen, so that it leaves no trail. o Turtle.penDown(); Drops a turtle’s pen, so that it leaves a trail.  Misc. o Turtle.hi
More Less

Related notes for Computer Science 1026A/B

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.