Modules (Methods) Functions and Procedures Parameters...Output.

Slides:



Advertisements
Similar presentations
Procedural programming in Java
Advertisements

Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
JAVA BASICS SYNTAX, ERRORS, AND DEBUGGING. OBJECTIVES FOR THIS UNIT Upon completion of this unit, you should be able to: Explain the Java virtual machine.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
©2004 Brooks/Cole Chapter 6 Methods. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Methods We've already seen examples of using methods.
Access to Names Namespaces, Scopes, Access privileges.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Writing Methods. Create the method Methods, like functions, do something They contain the code that performs the job Methods have two parts.
Hello, world! Dissect HelloWorld.java Compile it Run it.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Defining Classes and Methods Chapter 4.1. Key Features of Objects An object has identity (it acts as a single whole). An object has state (it has various.
Introduction to Methods
Hello AP Computer Science!. What are some of the things that you have used computers for?
Laboratory Study October, The very first example, traditional "Hello World!" program: public class first { public static void main (String[ ]
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.
The Java Programming Language
Copyright © Curt Hill Java Looking at our first console application in Eclipse.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.
Week 2 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Procedural programming in Java Methods, parameters and return values.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
3. Methods. Programming Paradigms Procedural Programming ‘Imperative’ assignment used to create state, and procedures manipulate state. E.g., C, Assembly,
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 and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
By Mr. Muhammad Pervez Akhtar
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Methods.
OOP Basics Classes & Methods (c) IDMS/SQL News
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Programs and Models Almost all computer programs model some artifact – Artifact: product of civilization, either concrete or abstract, simple or complex.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Comp1004: Building Better Objects I Methods. Coming up Methods and Parameters – Why Parameterise? – Call by value, call by reference Return Types – Methods.
Suppose we want to print out the word MISSISSIPPI in big letters.
Namespaces, Scopes, Access privileges
Methods.
Writing Methods.
Classes, Encapsulation, Methods and Constructors (Continued)
CS110D Programming Language I
Namespaces, Scopes, Access privileges
Programs and Classes A program is made up from classes
6 Methods: A Deeper Look.
Scope of variables class scopeofvars {
Notes from Week 5 CSE 115 Spring 2006 February 13, 15 & 17, 2006.
Java Looking at our first console application in Eclipse
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Instructor: Alexander Stoytchev
Corresponds with Chapter 5
Presentation transcript:

Modules (Methods) Functions and Procedures Parameters...Output

Procedures and Functions Some of our pseudocode rules translate into firm rules in Java: –Functions return a single value Value returned can be any complex data type (including an Object) –Procedures can “return” information by changing an object that is passed in via a parameter. Other rules are style issues: –Functions having no side effects –Functions not doing I/O

Java Methods Single construct for both procedures and functions: When a function is called for, specify the appropriate return type before the method name public float average (float fNum1, float fNum2) { float fReturnVal; fReturnVal = (fNum1 + fNum2)/ 2; return fReturnVal; } // of average to specify a procedure, make the return type void (More later…)

Writing Methods A Java requirement: --All methods belong to an object (or class). --Name of object (or class) must be unambiguous when the method called. --To run a program, there must be a class (whose name is the name-of-the-program), containing a special method called main: a class method, not an instance method visible to all nothing returned Method name for command line parameters public static void main (String[ ] argv)

Method Overloading Sometimes more than one method is required to do the same job. In Pseudocode, we frequently used a helper module Procedure Convert(Lhead iot in Ptr toa LLNode) Procedure ConvertHelper(Lhead iot Ptr toa LLNode, NewHead iot Ptr toa LLNode) In Java, multiple methods can have the same name as long as the number, type or order of their parameters is different public void Convert(LLNode Lhead) public void Convert(LLNode Lhead, LLNode NewHead)

Method Signatures “The signature of a method consists of the name of the method and the number and types of formal parameters to the method. A class may not declare two methods with the same signature, or a compile time error occurs.” Java Language Specification s Method overloading occurs where identically named methods have different parameter (number, type or order NOT name!) public int getCube(int iNum){ return iNum * iNum * iNum; } public int getCube(float fNum){ return (int)(fNum * fNum * fNum); } public int getCube(double dNum){ return (int) (dNum * dNum *dNum); }

Methods: Common Mistakes public float average (float fNum1, float fNum2, float fNum3); { float fReturnVal; fReturnVal = (fNum1 + fNum2 + fNum3)/ 3; return (fReturnVal); } // of average Note ending semicolon -- results in unhelpful error message

Parameters inJava only has in parameters Initially this will appear to be a big change. It is a common feature in many languages. We can get information out of a module via two techniques –Return something from a function –Pass in a reference to some object and inside the method modify the object [side effect]

Printing to Screen Pseudocode: print ( ) Java: System.out.print( ); System.out.println( ); Inserts a newline after the printing is complete

Printing to Screen int x = 10; System.out.println(5); System.out.println( ); System.out.println(“Hello World”); System.out.println(“x = “ + x); System.out.println(“y = “ + 5); System.out.print(“All on one “); System.out.println(“line”); 5 Hello World x = 10 y = 5 All on one line