1 More About Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 3 Wanda M. Kunkle.

Slides:



Advertisements
Similar presentations
Introduction to Programming
Advertisements

Lecture 10 Methods COMP1681 / SE15 Introduction to Programming.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
1 Fall 2009ACS-1903 Methods – Ch 5 A design technique referred to as stepwise refinement (or divide and conquer, or functional decomposition) is used to.
Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 2 Wanda M. Kunkle.
Boolean Expressions Conditional Statements & Expressions CSC 1401: Introduction to Programming with Java Lecture 4 – Part 2 Wanda M. Kunkle.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
Selection Structures: Switch CSC 1401: Introduction to Programming with Java Week 4 – Lecture 1 Wanda M. Kunkle.
TA: Nouf Al-Harbi NoufNaief.net :::
Parameters, Arguments, Local Variables, and Scope CSC 1401: Introduction to Programming with Java Week 8 – Lecture 1 Wanda M. Kunkle.
The Java String Type CSC 1401: Introduction to Programming with Java Week 7 – Lecture 1 Wanda M. Kunkle.
Introduction to Methods
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Comments are for people Header comments supply basic information about the artifact.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Functions Art &Technology, 3rd Semester Aalborg University Programming David Meredith
CSC 204 Programming I Loop I The while statement.
The Java Programming Language
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
Procedural programming in Java Methods, parameters and return values.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Chapter 3 Object Interaction.  To construct interesting applications it is not enough to build individual objects  Objects must be combined so they.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology- George Koutsogiannakis 1.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
1 ICS103 Programming in C Lecture 8: Functions I.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Methods: functions & procedures. Top-down programming Divide-and-conquer technique Divide-and-conquer technique Problem is broken down into a series of.
An introduction to arrays, continued. Recall from last time… public static void main ( String args[] ) { //define number of rooms final int N = 100; //define.
Methods CSCI 1301 What is a method? A method is a collection of statements that performs a specific task. Pre-Defined methods: available in Java library.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Expressions Methods if else Statements Loops Potpourri.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
CSCI S-1 Section 8. Coming Soon Problem Set Four (72 + 5/15 points) – Tuesday, July 21, 17:00 EST.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Midterm preview.
CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition.
Lecture 4b Repeating With Loops
Chapter 7 Top-Down Development
The Selection Structure
Starting Out with Java: From Control Structures through Objects
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
CS 200 Loops Jim Williams, PhD.
Chapter 6 – Methods Topics are:
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Loops CGS3416 Spring 2019 Lecture 7.
Methods/Functions.
ITM 352 Functions.
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

1 More About Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 3 Wanda M. Kunkle

2 Why Use Methods? The main method ( public static void main (String[] args) ) of the program should focus on the primary tasks of the program. The main method ( public static void main (String[] args) ) of the program should focus on the primary tasks of the program. Methods make it possible to use code repeatedly without typing the code repeatedly. Methods make it possible to use code repeatedly without typing the code repeatedly. Methods facilitate code reuse by allowing us to use the code by simply invoking (calling) the method in which it has been placed. Methods facilitate code reuse by allowing us to use the code by simply invoking (calling) the method in which it has been placed. Methods can be placed in their own separate classes for use with many different Java programs. Methods can be placed in their own separate classes for use with many different Java programs. Methods support modular programming, an approach to programming in which programs are constructed from reusable components (modules), each of which performs a specific task. Methods support modular programming, an approach to programming in which programs are constructed from reusable components (modules), each of which performs a specific task.

3 How Do We Define Methods? 1. Decide what task the method is to perform. 2. Choose a name for the method that is descriptive of the task. 3. Determine what values (if any) must be passed into the method so that it can perform its task. 4. Determine what value (if any) the method must return. 5. Write the code to carry out the method’s task.

4 Problem Write a method to determine whether or not a character ch is a lowercase letter. Write a method to determine whether or not a character ch is a lowercase letter.

5 Defining a Method to Solve a Problem 1. Decide what task the method is to perform. –The method is to determine whether or not a character ch is a lowercase letter.

6 Defining a Method to Solve a Problem 2. Choose a name for the method that is descriptive of the task. –Suggestions?

7 Defining a Method to Solve a Problem 3. Determine what values (if any) must be passed into the method so that it can perform its task. –Answer? (Hint: There’s only one value. What is it?)

8 Defining a Method to Solve a Problem 4. Determine what value (if any) the method must return. –Now we have to make some decisions.  How do we want the method to indicate that a letter is upper- or lowercase? –Return a String: “lowercase” or “uppercase” –Return a boolean: true if lowercase, false otherwise –Return an integer: 1 if lowercase, 0 otherwise  I know what approach I’d use. What approach would you use?

9 Defining a Method to Solve a Problem 5. Write the code to carry out the method’s task. –Once again we have to make some decisions.  What decision structures should we use? –if –switch  Depending upon our choice of decision structure, do we need to use boolean expressions? –ch == ‘a’ || ch == ‘b’ || … || ch == ‘z’  Do we need to use any local variables (variables declared inside the method)?  I know what choices I’d make. What choices would you make?

10 Possible Solution The method shown below: // Classify a letter as lower- or uppercase and // return the result of the test static boolean isLowerCase(char ch) { boolean isLower = false; if ('a' <= ch && ch <= 'z') isLower = true; return isLower; } // end isLowerCase The method shown below: // Classify a letter as lower- or uppercase and // return the result of the test static boolean isLowerCase(char ch) { boolean isLower = false; if ('a' <= ch && ch <= 'z') isLower = true; return isLower; } // end isLowerCase

11 Sample Program Now let’s look at a sample program that uses this method: Now let’s look at a sample program that uses this method: –DetermineLetterCase.java DetermineLetterCase.java

12 General Form of a Java Method static boolean isLowerCase(char ch) { boolean isLower = false; if ('a' <= ch && ch <= 'z') isLower = true; return isLower; } // end isLowerCase static boolean isLowerCase(char ch) { boolean isLower = false; if ('a' <= ch && ch <= 'z') isLower = true; return isLower; } // end isLowerCase Function heading Function definition (i.e., the function itself) Function nameFunction return type Function parameter Function body (i.e., the code)

13 Changing the subject …

14 One More String Method (You’ll need it for today’s lab.) startsWith(prefix) startsWith(prefix) –Returns true if the calling String object starts with prefix; otherwise, returns false –Example:  output out = new output(); String professor_name = “Dr. Johnson”; if (professor_name.startsWith(“Dr.")) out.writeln("\nYou have a Ph.D."); else out.writeln("\nYou do not have a Ph.D."); // What do you think is displayed?

15 String Methods Recall that the String methods can be viewed at: Recall that the String methods can be viewed at: – Just look for the String class under All Classes in the lower left-hand frame. Just look for the String class under All Classes in the lower left-hand frame.

16 One more thing …

17 Have a nice break! Have a nice break!