Workshop for Programming And Systems Management Teachers

Slides:



Advertisements
Similar presentations
ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
Advertisements

Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
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.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Georgia Institute of Technology Introduction to Java part 2 Barb Ericson Georgia Institute of Technology May 2006.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Georgia Institute of Technology Introduction to Media Computation Barb Ericson Georgia Institute of Technology May 2006.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
CPSC1301 Computer Science 1 Chapter 11 Creating Classes part 1.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
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.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Georgia Institute of Technology More on Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
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.
Python programming Using the JES picture functions and defining new functions.
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.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CS1316: Representing Structure and Behavior
Getting and displaying
User-Written Functions
Classes and Objects.
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Introduction to Java part 2
COMP 170 – Introduction to Object Oriented Programming
Java Primer 1: Types, Classes and Operators
Manipulating Pictures, Arrays, and Loops part 3
CS1315: Introduction to Media Computation
Chapter 3: Using Methods, Classes, and Objects
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Creating and Modifying Text part 2
Barb Ericson Georgia Institute of Technology Dec 2009
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 5
Chapter 4 void Functions
Introduction to Java part 2
Introduction to Classes and Objects
Manipulating Pictures, Arrays, and Loops part 3
Introduction to Media Computation
Teaching Java using Turtles part 3
Barb Ericson Georgia Institute of Technology Oct 2005
Manipulating Pictures, Arrays, and Loops
Tonga Institute of Higher Education
Manipulating Pictures, Arrays, and Loops
Introduction to Object-Oriented Programming in Alice
Chapter 9: Value-Returning Functions
Object Oriented Programming in java
Introduction to Java part 2
More on Creating Classes
Manipulating Pictures, Arrays, and Loops
February 2 - 6, 2009 CSE 113 B.
Unit 3: Variables in Java
Barb Ericson Georgia Institute of Technology Oct 2005
Review for Midterm 3.
Corresponds with Chapter 5
Classes and Objects Systems Programming.
Workshop for Programming And Systems Management Teachers
Introduction to Classes and Objects
Presentation transcript:

Workshop for Programming And Systems Management Teachers Chapter 4 Writing Recipes (Methods) Georgia Institute of Technology

Georgia Institute of Technology Learning Goals Understand at a conceptual and practical level How to invoke methods in Java Both class and object How to write methods in Java How to return a value from a method How to specify that a method doesn’t return a value How to pass variables to a method Georgia Institute of Technology

Invoking Methods in Java Use .methodName(parameters) to send a message which will map to a method Examples FileChooser.pickAFile() pickAFile is a message which maps to the pickAFile class method in the class FileChooser “Hi”.toLowerCase() toLowerCase is a message which maps to the toLowerCase object method in the class String Georgia Institute of Technology

Georgia Institute of Technology Naming a Recipe Writing a method is giving a name to a recipe (a series of Java statements) in a class file When you want to execute the series of statements again You use the method name As the name of the message You can also pass parameters To be used as variables in the method Georgia Institute of Technology

Georgia Institute of Technology Naming a Method When we name variables we use Type name; or Type name = expression; When we name methods we use visibility Type name (parameterList) {body} The order is important We call this the syntax of the declaration Example method declarations public void show() private double computeTax(double total) Georgia Institute of Technology

Georgia Institute of Technology Writing a Method There are several visibility keywords The keyword “public” means it can be invoked from inside any class definition The keyword “private” means it can be invoked from within the current class definition The “Type” is the type of value returned Or the keyword “void” if no value is returned The “parameterList” is a list of type and name pairs separated by commas There are other visibility keywords but these are the two you should be using. Georgia Institute of Technology

Declaring a Class Method A class method adds the keyword “static” public static String pickAFile() Class methods can be invoked using the class name FileChooser.pickAFile Use a class method when you are not operating on data in the current object The method may create an object Georgia Institute of Technology

Georgia Institute of Technology Method Body The statements that make up the method are placed in a block between opening and closing curly braces public Type name (parameterList) { statement 1; statement 2; . } Georgia Institute of Technology

Returning from a Method Use the keyword “return” to return a value or object from a method return x; return person; The type of the thing being returned must agree with the declared type public int getNum() { return “3”; // would cause a compile error } Georgia Institute of Technology

Georgia Institute of Technology Class Definitions In Object-oriented programming we break the tasks to be done up and assign them to classes The class that is responsible for the task Method definitions go inside a class definition public class Name { field declarations constructors method declarations } The order of things declared in a class definition doesn’t matter. Some people declare the methods first and then the constructors and fields. Some prefer the fields first, then the constructors, and then the methods. You an actually mix definitions up like declare a field then a method and then another field but it is good practice to group the fields, constructors, and methods. Georgia Institute of Technology

Java Class Definitions In Java each class is defined in its own file With the same name as the class With .java as the extension The FileChooser class definition would be defined in the file FileChooser.java The Picture class definition would be in Picture.java The compiler won’t compile a file with a class name that differs from the file name it is defined in. Georgia Institute of Technology

Writing your First Method How about a method that lets you pick the file name, creates the picture object, and shows it? It could also return the picture object for later use. public static Picture pickAndShow() { String fileName = FileChooser.pickAFile(); Picture picture = new Picture(fileName); picture.show(); return picture; } Georgia Institute of Technology

Georgia Institute of Technology Method Exercise Open DrJava Open the file Picture.java Type the method before the last ‘}’ in the file Compile the file Fix any errors and compile again Invoke the method in the interactions pane Files Pane Definitions Pane The files are in c:\intro-prog-java\bookClasses\ Interactions Pane Georgia Institute of Technology

Method to Pick And Play a Sound Write a method to pick and play a sound First pick a file name Create the sound object Tell the sound object to play Return the sound object How do you do each of these things? What class does this method need to be declared in? Georgia Institute of Technology

Pick and Play Sound Method Exercise Write the following method In the Sound.java file Before the last curly brace Then compile it And invoke it by Sound.pickAndPlay(); public static Sound pickAndPlay() { String fileName = FileChooser.pickAFile(); Sound sound = new Sound(fileName); sound.play(); return sound; } Georgia Institute of Technology

Names are Important to People Go back and change the names you use for variables in the method Does it change what the method does? Use names that are understandable To you To others Use names that aren’t too long thisIsAVeryLongVariableName is a pain to type It doesn’t matter to the computer what names you use. Good names make your code easier for people to understand what you are doing. Georgia Institute of Technology

Method to Show A Picture Write a method to show a particular picture specify the file name create the picture object using the file name ask the picture object to show itself return the picture object How do you do each of those things? What class should the method be in? Georgia Institute of Technology

Showing A Particular Picture public static Picture showPicture() { String myFile = "FILENAME"; Picture myPicture = new Picture(myFile); myPicture.show(); return myPicture; } Georgia Institute of Technology

Slashes and Backslashes We have seen file names with backslashes ‘\’ in them As a result of FileChooser.pickAFile() However we can’t just use backslashes in string objects They have a special meaning “\t” is tab “\n” is newline There are two ways to fix this problem Use a slash instead ‘/’ Use double backslashes ‘\\’ Georgia Institute of Technology

Using the Interaction Pane If you aren’t sure about how to do something you want to do in a method Try it out in the Interactions pane Use System.out.println(expression) to see the result Copy it to the Definitions pane Select what you want to copy Control-C to copy it Control-V to paste it You can also copy text from the definitions pane to the interactions pane. Georgia Institute of Technology

Passing a Parameter to a Method We wouldn’t want to create a new method for every picture we want to show That would be a huge number of methods Instead let’s take the file name as a parameter (input variable) Making the method more general (reusable) Parameters specify their type and name Just like a variable declaration They are used like variables in the method We also don’t want to have to edit the program and recompile to change the file name for each file we want to create a picture from. Georgia Institute of Technology

Showing a Picture Given the Name Write a method to show a picture when the method is passed the file name to use. Need to pass in a file name Need to create the picture object using the file name Need to tell the picture object to show itself Need to return the picture object How do we do those things? What class should this method be defined in? Georgia Institute of Technology

Georgia Institute of Technology showNamed Method Add the method to the Picture.java file Compile the Picture.java file Try the method in the interactions pane Picture.showNamed(“c:/intro-prog-java/mediasources/katie.jpg”); public static Picture showNamed(String fileName) { Picture myPicture = new Picture(fileName); myPicture.show(); return myPicture; } Try showNamed() with different file names. You can use FileChooser.pickAFile() to get the file names to use. Georgia Institute of Technology

How Does showNamed Work? When you type Picture.showNamed(“c:/intro-prog-java/mediasources/katie.jpg”); The compiler checks that the Picture class has a class method showNamed that takes a string object. At run time the method is executed and the input variable fileName refers to the input string object during execution of the method The name is only in use during the method Georgia Institute of Technology

Georgia Institute of Technology Scope Parameters or variables declared in a method have method scope The names are known inside the method They are not known outside the method Can use the same variable names in the interactions pane Or in other methods Georgia Institute of Technology

Georgia Institute of Technology playNamed Exercise Try to do a similar method in the Sound.java class to play a sound It should take a file name as input It should create the sound object It should tell the sound object to play It should return the created sound object Georgia Institute of Technology

Georgia Institute of Technology Show a Passed Picture How would you write a method that takes a picture object as an input variable? What type would the input variable be? What should you name it? How would you get the picture object to show? Does it need to return anything? Georgia Institute of Technology

Georgia Institute of Technology showPicture Method public static void showPicture(Picture myPicture) { myPicture.show(); } How is this different from just calling picture.show()? Which one is a class method? Which one is an object method? Which way is better? Georgia Institute of Technology

Georgia Institute of Technology Summary Methods are declared inside of class definitions Methods declarations have the following syntax visibility Type name (parameterList) { code} Class methods have the keyword static Methods that do not return any value use the keyword void Georgia Institute of Technology