Methods in Java CSC1401. Overview In this session, we are going to see how to create class-level methods in Java.

Slides:



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

TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
TOPIC 13 OBJECTS IN MEMORY, PASSING PARAMETERS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial.
Computer Science 1000 LOGO I. LOGO a computer programming language, typically used for education an old language (1967) the basics are simple: move a.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Lecture 3. Review (What is Class and Object ?) The world of JAVA contains objects The world of JAVA.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
Lecture 4. Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// …
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
01-Intro-Object-Oriented-Prog-Alice1 Barb Ericson Georgia Institute of Technology Aug 2009 Introduction to Object-Oriented Programming in Alice.
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.
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
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.
Creative Commons Attribution Non-Commercial Share Alike License sa/3.0/
Introducing Scratch the Cat
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
COSC 1P02 Introduction to Computer Science 3.1 Cosc 1P02 Week 3 Lecture slides Birthdays are good for you. Statistics show that the people who have the.
Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.
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.
Chapter 4 Procedural Methods. Learning Java through Alice © Daly and Wrigley Objectives Identify classes, objects, and methods. Identify the difference.
Georgia Institute of Technology Barb Ericson Georgia Institute of Technology May 2006 Teaching Java using Turtles part 2.
Copyright © Curt Hill Turtles The beginning of media computation.
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.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
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.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
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.
Variables and Functions Alice. Naming is Important If you get a new pet one of the first things you do is name it Gives you a way to refer to the new.
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.
Georgia Institute of Technology More on Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
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.
Alice and Java Unit 7 1. Day 1  Objective: Gain an introduction to Java and Eclipse  Essential skill: DM-1: Use technology to advance critical thinking.
Georgia Institute of Technology Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah Branch King Abdulaziz University.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
Declaring Variables – Mod 4
Introduction to Object-Oriented Programming in Alice and Java
Introduction to Object-Oriented Programming in Java
Chapter 4 Procedural Methods.
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
Chapter 7 Procedural Methods.
Topics Introduction to Functions Defining and Calling a Function
Barb Ericson Georgia Institute of Technology June 2005
More on Creating Classes
More on Creating Classes part 3
Methods/Functions.
Teaching Java using Turtles part 2
Presentation transcript:

Methods in Java CSC1401

Overview In this session, we are going to see how to create class-level methods in Java

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);

Drawing a Letter How would you use a turtle to draw a large letter T? Process Create a World variable and a World object and a Turtle variable and object. Ask the Turtle object to go forward 100 Ask the Turtle object to pick up the pen Ask the Turtle object to turn left Ask the Turtle object to go forward 25 Ask the Turtle object to turn 180 degrees Ask the Turtle object to put down the pen Ask the Turtle object to go forward 50

Drawing a T World world1 = new World(); Turtle turtle1 = new Turtle(world1); turtle1.forward(100); turtle1.penUp(); turtle1.turnLeft(); turtle1.forward(25); turtle1.turn(180); turtle1.penDown(); turtle1.forward(50);

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

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

Adding a Method to a Class Open the file Turtle.java (found on the P: drive) Save it on your J: drive (wherever you have been saving your TurtleTester program) Type the method before the last } // end Compile the Turtle.java file, but do not run it

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

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();

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

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();

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

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);

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)

Revisiting turtle spelling Creating methods to spell my name The V was not easy to create – let’s figure out how to do it!

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

Summary You can create objects from classes in Alice and Java Each object needs a unique way to reference it In Java we call this declaring a variable You can create new methods visibility returnType name(Type name, Type name, …) Let’s you reuse a block of statements

Assignment Read Media Computation Chapter 3, Section 5 Methods Parameters and arguments Also, please look at the Javadoc for the Turtle class, available at: p:\scooper\BookClasses1.5\doc\Turtle.html