Georgia Institute of Technology Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah Branch King Abdulaziz University.

Slides:



Advertisements
Similar presentations
Introduction to Object-Oriented Programming in Alice and Java
Advertisements

TOPIC 13 OBJECTS IN MEMORY, PASSING PARAMETERS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial.
Lecture 4. Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// …
01-Intro-Object-Oriented-Prog-Alice1 Barb Ericson Georgia Institute of Technology Aug 2009 Introduction to Object-Oriented Programming in Alice.
Georgia Institute of Technology Introduction to Java part 2 Barb Ericson Georgia Institute of Technology May 2006.
TOPIC 3 INTRODUCTION TO PROGRAMMING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
04-Intro-Object-Oriented-In-Java1 Barb Ericson Georgia Institute of Technology Sept 2009 Introduction to Object-Oriented Programming in Java.
Introduction to Programming Writing Java Beginning Java Programs.
TOPIC 3 INTRODUCTION TO PROGRAMMING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
CPSC1301 Computer Science 1 Chapter 11 Creating Classes part 1.
Introducing Scratch the Cat
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.
Methods in Java CSC1401. Overview In this session, we are going to see how to create class-level methods in Java.
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
CSE8A Lecture3 TODO: –Finish PSA1 individually (no partner!) and turn it in with the bundlePSA1 command GET AN INTERVIEW for PSA1 from a tutor See tutor.
Georgia Institute of Technology Barb Ericson Georgia Institute of Technology May 2006 Teaching Java using Turtles part 2.
Writing Methods Mrs. C. Furman October 9, Drawing a Square World worldObj = new World(); Turtle turtle1 = new Turtle(100, 100, worldObj); turtle1.forward.
Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005.
Java Introduction part 2 CSC1401. Overview In this session, we are going to examine some of the instructions from the previous class in more detail.
Introduction to Programming Writing Java Beginning Java Programs.
Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005.
CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology Simulations Barb Ericson Jan 2005.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology Creating Classes part 2 Barb Ericson Georgia Institute of Technology June 2006.
Georgia Institute of Technology Barb Ericson Georgia Institute of Technology Aug 2006 Introduction to Object-Oriented Programming in Alice and Java.
Creating a Java Application and Applet
Georgia Institute of Technology More on Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples,
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
Georgia Institute of Technology Comic Strip Analysis and Design Inheritance, Abstract Classes, and Polymorphism part 2 Barb Ericson Georgia Institute of.
Georgia Institute of Technology Barb Ericson Georgia Institute of Technology Sept 2006 Introduction to Object-Oriented Programming in Alice and Java.
Georgia Institute of Technology Introduction to Java, and DrJava part 1 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Appendix A Barb Ericson Georgia Institute of Technology May 2006
Manipulating Pictures, Arrays, and Loops part 1
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Introduction to Java part 2
Appendix A Barb Ericson Georgia Institute of Technology May 2006
Manipulating Pictures, Arrays, and Loops part 3
Declaring Variables – Mod 4
Creating and Modifying Text part 2
Barb Ericson Georgia Institute of Technology Dec 2009
Introduction to Object-Oriented Programming in Alice and Java
Introduction to Object-Oriented Programming in Java
Teaching Java using Turtles part 1
Manipulating Pictures, Arrays, and Loops part 2
Georgia Institute of Technology
Barb Ericson Georgia Institute of Technology August 2005
Introduction to Java part 2
Declaring Variables – Mod 4
Teaching Java using Turtles part 2
Teaching Java using Turtles part 3
Declaring Variables – Mod 4
Barb Ericson Georgia Institute of Technology Oct 2005
Introduction to Object-Oriented Programming in Alice and Java
Workshop for Programming And Systems Management Teachers
Introduction to Java part 2
More on Creating Classes
Manipulating Pictures, Arrays, and Loops
More on Creating Classes part 3
Teaching Java using Turtles part 2
Workshop for Programming And Systems Management Teachers
Presentation transcript:

Georgia Institute of Technology Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah Branch King Abdulaziz University Teaching Java using Turtles part 3

Georgia Institute of Technology Learning Goals Explain how to change the pen width and color Show that objects can refuse to do what you ask Create a method to reuse a series of Java statements –Compile it to turn it into something the computer understands –Execute it to try it out –Show how to make it more reusable

Georgia Institute of Technology Setting the Pen Width You can change the width of the trail the pen leaves World world1 = new World(); Turtle turtle1 = new Turtle(world1); turtle1.setPenWidth(5); turtle1.forward(100);

Georgia Institute of Technology Setting the Pen Color Use setPenColor to set the color of the pen turtle1.setPenColor(java.awt.Color.RED); There are several predefined colors –In the package java.awt A package is a group of related classes –In the class Color To use them you can use the full name –java.awt.Color.RED

Georgia Institute of Technology Setting Colors You can change the pen color turtle.setPenColor(java.awt.Color.RED); You can change the turtle color turtle1.setColor(java.awt.Color.BLUE); You can change the turtle’s body color turtle1.setBodyColor(java.awt.Color.CYAN); You can change the turtle’s shell color turtle1.setShellColor(java.awt.Color.RED);

Georgia Institute of Technology Objects can Refuse Turtles won’t move completely out of the boundaries of the world World world2 = new World(); Turtle turtle2 = new Turtle(world2); turtle2.forward(600);

Georgia Institute of Technology Objects send Messages Objects don’t “tell” each other what to do –They “ask” each other to do things Objects can refuse to do what they are asked –The object must protect it’s data Not let it get into an incorrect state A bank account object shouldn’t let you withdraw more money that you have in the account

Georgia Institute of Technology Creating a Method We can name a block of Java statements and then execute them again –By declaring a method in a class The syntax for declaring a method is –visibility returnType name(parameterList) –Visibility determines access Usually public or private The return type is the type of thing returned If nothing is returned use the keyword void –Name the method starting with a lowercase word and uppercasing the first letter of each additional word

Georgia Institute of Technology Example Method public void drawSquare() { this.turnRight(); this.forward(30); this.turnRight(); this.forward(30); this.turnRight(); this.forward(30); this.turnRight(); this.forward(30); } The visibility is public The keyword void means this method doesn’t return a value The method name is drawSquare There are no parameters –Notice that the parentheses are still required The keyword this means the object this method was invoked on

Georgia Institute of Technology Adding a Method to a Class 1. Open file Turtle.java 2. Type the method before the last } // end 3. Compile open files

Georgia Institute of Technology Compile Errors Clicking on the error takes you to the code and highlights it. Case matters in Java! turnright isn’t the same as turnRight

Georgia Institute of Technology Try the New Method Compiling resets the interactions pane –Clearing all variables But you can still use the up arrow to pull up previous statements –You will need to create a world and turtle again World world1 = new World(); Turtle turtle1 = new Turtle(world1); turtle1.forward(50); turtle1.drawSquare(); turtle1.turn(30); turtle1.drawSquare();

Georgia Institute of Technology Saving the Interactions History in DrJava You can save the interactions history into a script –And optionally edit it first before you save it –Click on Tools then on Save Interactions History And then latter load and execute the statements in the script –Click on Tools and Load Interactions History as Script Use the next button to see the next statement and click on the execute button to execute it

Georgia Institute of Technology Better Method to Draw a Square A method to draw a square public void drawSquare() { int width = 30; this.turnRight(); this.forward(width); this.turnRight(); this.forward(width); this.turnRight(); this.forward(width); this.turnRight(); this.forward(width); } We added a local variable for the width –Only known inside the method This makes it easier to change the width of the square But, we still have to recompile to draw a different size square

Georgia Institute of Technology Testing the Better Method Type the following in the interactions pane World world1 = new World(); Turtle turtle1 = new Turtle(world1); turtle1.forward(50); turtle1.drawSquare(); turtle1.turn(30); turtle1.drawSquare(); Or use the saved script if you saved the last interactions history

Georgia Institute of Technology Passing a Parameter public void drawSquare(int width) { this.turnRight(); this.forward(width); this.turnRight(); this.forward(width); this.turnRight(); this.forward(width); this.turnRight(); this.forward(width); } Parameter lists specify the type of thing passed and a name to use to refer to the value in the method The type of this parameter is int The name is width Values are passed by making a copy of the passed value

Georgia Institute of Technology Testing with a Parameter Type the following in the interactions pane World world1 = new World(); Turtle turtle1 = new Turtle(world1); turtle1.forward(50); turtle1.drawSquare(30); turtle1.turn(30); turtle1.drawSquare(50);

Georgia Institute of Technology How Does That Work? When you ask turtle1 to drawSquare(30) turtle1.drawSquare(30); –It will ask the Turtle Class if it has a method drawSquare that takes an int value And start executing that method The parameter width will have the value of 30 during the executing of the method The this keyword refers to turtle1 When you ask turtle1 to drawSquare(50) turtle1.drawSquare(50); –The width will have a value of 50 –The this refers to turtle1 (the object the method was invoked on)

Georgia Institute of Technology Challenges Create a method for drawing a rectangle –Pass the width and height Create a method for drawing an equilateral triangle –all sides have the same length –Pass in the length Create a method for drawing a diamond Create a method for drawing a house –Using the other methods Create a method for drawing a school –Using the other methods

Georgia Institute of Technology Summary To create a method –visibility returnType name(Type name, Type name, …) –Let’s you reuse a block of statements Examples –public drawSquare() –public drawSquare(int width) You can make methods more useful by allowing them to take parameters