CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

C Functions. What are they? In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
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.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Methods Liang, Chapter 4. What is a method? A method is a way of running an ‘encapsulated’ series of commands. System.out.println(“ Whazzup ”); JOptionPane.showMessageDialog(null,
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Method exercises Without IF. Setup Create one new project to hold all of these. In that project, create an empty class called WorkIt to hold all of your.
Introduction to Methods
CH1 – A 1 st Program Using C#. Program Set of instructions which tell a computer what to do. Machine Language Basic language computers use to control.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
The Java Programming Language
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
ECE122 Feb. 22, Any question on Vehicle sample code?
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Classes - Intermediate
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
CIS 338: VB Variables Dr. Ralph D. Westfall April, 2011.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
CS 106 Introduction to Computer Science I 10 / 10 / 2007 Instructor: Michael Eckmann.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Functions + Overloading + Scope
Chapter 7: User-Defined Functions II
Java Course Review.
Java Primer 1: Types, Classes and Operators
Programming Language Concepts (CIS 635)
CSC240 Computer Science III
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Object Oriented Programming (OOP) LAB # 5
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Java Programming Language
Functions Imran Rashid CTO at ManiWeber Technologies.
Methods/Functions.
Corresponds with Chapter 5
Presentation transcript:

CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010

What is a Method? a block of code that does something has a name and parentheses after name curly brackets enclose statement(s) in body usually has header declarations, and often has arguments inside the parentheses public static void main (String [] args) { System.out.println("Hi!"); }

Declarations - type of access modifiers before method name public – any other class can use method main method must be public private – only methods in same class can use it can leave out the access modifier defaults to friendly – can be used by other classes in same "package" (a package is somewhat like a directory)

Declarations - static static means unchanging all objects created from this class will use this same method there will not be additional copies of a static class method if don't use word static, each object created from this class will have its own instance (copy) of method

Declarations – void or [type] return value type void means doesn't return any data if not void, need to identify the type of data the method will return public static long cubeIt(int x) // int arg { // returns a long integer return x * x * x; }

Arguments (args) the parentheses often enclose "argument(s)" (data) that the method uses, identified by data type public void say(String what) // argument { System.out.println(what); // used here } // from Hello, world! Person class codeHello, world!

Arguments - 2 Java is "strongly typed," so the data type of every variable must be declared somewhere in a Java class argument data types are declared within method's parentheses public static double raised(int salary, double pct) { return (100 + pct) / 100.0) * salary; }

Calling a Method from within its class method name([arguments, if has any]) ; say("Hi!"); // literal argument from another object or classclass [object].[method name]([arguments]) ; myObject.cube(num); // variable argument myObject.raised(empSalary, 5); //both types Math.sqrt(4); // literal in a class method

Calling a Method - 2 arguments must be in right order static double raise(double salary, double pct) raise (wages, increase) ; // correct order raise(7, 50000) ; // logical error argument names don't need to match myObject.raise(pay, increase) ; // declared in method as // (double salary, double pct)

"Signature" a method's "signature" is a pattern: number, order, and types of arguments can have 0 [called with [name]( );] to many an "overloaded" method has more than one signature e.g., can be called with different patterns of arguments: (); (int x); (int x, double y); (int x, char y);

Types of Java Programs applications class that can run without any other programs (often used with other classes) class(es) that doesn't run by itself, but is used by another application class applet class runs inside a web browser (the browser actually is a program) // [PgDn] class

Types of Java Programs - 2 servlet runs on a server and makes it possible to generate web pages with database content programmer can create a Java Server Page (JSP), which then gets compiled into a servlet (Blackboard pages are servlets) servlets are part of the Java Enterprise Edition (JDK + Java EE bundle)JDK + Java EE bundle

Main Method one class in a Java application must have a main( ) method but a class used by an application doesn't need a main( ) method main method must be declared as: public static void

Main Method - 2 main method runs first when application starts it can call methods in its own class, and/or use methods in other classes methods it calls in its own class must be static variables that are not inside any method in the class must also be static

Main Method - 3 main method has String[ ] argument(s) means that you can "pass" one or more words as inputs into class when you run it public class MyProgram { public static void main (String[ ] args) [in DOS after class has been compiled] C:\>[path] java MyProgram Le 19 // 2 args

Using Extra Methods in a Class saves typing (or pasting) can use a separate method, rather than keep repeating same block of code in main method makes code easier to read and maintain smaller programs code is organized into blocks //Notes Page

Exercise following pattern in DemoMethods.java, fill in the blanks in the handout to:DemoMethods.java declare 3 numbers and one String as member variables (above main method), using meaningful variable names assign values to three of these variables in the loadData method (leave one of the numeric variables without a value

Exercise (part 2) following pattern in DemoMethods.java, write the following 4 methods:DemoMethods.java print a literal value with say( method add 2 numbers together and store them in a member variable (above main method) print this calculated member variable multiply two numbers and print result with some identifying text