Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 2 Wanda M. Kunkle.

Slides:



Advertisements
Similar presentations
Week 4 – Functions Introduction. Functions: Purpose Breaking a large problem into a series of smaller problems is a common problem- solving technique.
Advertisements

1 ICS102: Introduction To Computing King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
Computers Are Your Future
LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered.
CS105 INTRODUCTION TO COMPUTER CONCEPTS INTRO TO PROGRAMMING Instructor: Cuong (Charlie) Pham.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
Introduction to Computers and Programming Introduction to Methods in Java.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Converting Between Numbers and Characters CSC 1401: Introduction to Programming with Java Week 4 – Lecture 3 Wanda M. Kunkle.
Repetition Structures: Nested Loops CSC 1401: Introduction to Programming with Java Week 6 – Lecture 2 Wanda M. Kunkle.
1 More About Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 3 Wanda M. Kunkle.
Recursion Formatting Decimal Numbers CSC 1401: Introduction to Programming with Java Week 9 – Lectures 1 & 2 Wanda M. Kunkle.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
Introduction to Computer Programming CSC 1401: Introduction to Programming with Java Lecture 2 Wanda M. Kunkle.
CM143 - Web Week 2 Basic HTML. Links and Image Tags.
Chapter 1 Introduction to C Programming. 1.1 INTRODUCTION This book is about problem solving with the use of computers and the C programming language.
Topic R3 – Review for the Final Exam. CISC 105 – Review for the Final Exam Exam Date & Time and Exam Format The final exam is 120-minutes, closed- book,
Selection Structures: Switch CSC 1401: Introduction to Programming with Java Week 4 – Lecture 1 Wanda M. Kunkle.
Program Design Divide and Conquer –Divide the program into separate tasks Functional Decomposition Top-Down Design –Divide an overall problem into discrete.
Handling Large Projects  Project Decomposition  Stepwise Refinement Functional Decomposition Design Decomposition  Work Breakdown Structures (WBS)
The Java String Type CSC 1401: Introduction to Programming with Java Week 7 – Lecture 1 Wanda M. Kunkle.
Chapter 4 Sec. 4.1, 4.2, 4.4 Procedures (User-defined)
More on Recursion Averting Program Crashes CSC 1401: Introduction to Programming with Java Week 9 – Lecture 3 Wanda M. Kunkle.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
1 Chapter 2 Problem Solving Techniques INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD.
The Project AH Computing. Functional Requirements  What the product must do!  Examples attractive welcome screen all options available as clickable.
CS102 Introduction to Computer Programming
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 02.
1 CSC 221: Introduction to Programming Fall 2012 course overview  What did you set out to learn?  What did you actually learn?  Where do you go from.
1 CSC103: Introduction to Computer and Programming Lecture No 13.
Top-Down Design and Modular Development
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
UniMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Lecture 1.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Problem Solving using the Science of Computing MSE 2400 EaLiCaRA Spring 2015 Dr. Tom Way.
Introduction to Method. Example Java Method ( 1 ) The 2 types (kinds) of methods in Java Class methods Instance methods Methods can do more work than.
The Programming Process Define the problem* Make or buy software? Design the program * Code (write) the program Test (debug) the program Document the.
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.
Sahar Mosleh California State University San MarcosPage 1 JavaScript Basic.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 1.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
JavaScript 101 Lesson 6: Introduction to Functions.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Programming what is C++
Topic: Programming Languages and their Evolution + Intro to Scratch
Introduction to Recursion
Unit-1 Introduction to Java
CMSC201 Computer Science I for Majors Lecture 11 – Program Design
Functions CIS 40 – Introduction to Programming in Python
JavaScript.
©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Problem Solving Techniques
Assignment 4 For this assignment refer to the notes on MATLAB from an MIT course that you can find here (or at the original website, lectures 14, 15, 16).
Looping and Random Numbers
CS105 Introduction to Computer Concepts
The Programming Process
CS105 Introduction to Computer Concepts Intro to programming
CMSC201 Computer Science I for Majors Lecture 12 – Program Design
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 2 Wanda M. Kunkle

2 Problem Solving in General Divide and conquer Divide and conquer –Fundamental approach to human problem solving –Process consisting of two stages: 1.Decomposition 2.Composition

3 Problem Solving in General Divide and conquer Divide and conquer 1.Decomposition  Divide a complex problem into simpler problems  Solve each simple problem one at a time 2.Composition  Combine the solutions to the simple problems to …  Form a solution to the complex problem

4 Problem Solving in Java How do we implement divide and conquer in Java? How do we implement divide and conquer in Java? 1.Decomposition  Use stepwise programming to break a larger task into smaller tasks which can be programmed one at a time (e.g., single statements)

5 Problem Solving in Java How do we implement divide and conquer in Java? How do we implement divide and conquer in Java? 2.Composition  Combine the smaller program units to form a complete program using composition mechanisms.  Composition mechanisms that you have used so far are: –Structured statements  if, if-else, switch, for, while, do-while –Data structures  Objects such as input, output, and String  Text files –Methods  read, readline, readint, readdouble, writeln, etc.

6 Methods in Java A method is a piece of code that performs a specific task. A method is a piece of code that performs a specific task. There are two main types of methods in Java: There are two main types of methods in Java: –Built-in  A built-in method is a method written by the authors of a programming language for use by other programmers.  A built-in method you have seen is Math.pow (If you recall, I used it in a demo program, powers.java) powers.java –Programmer-defined  A programmer-defined method is a method written by a programmer for his own use.  Some programmer-defined methods you have used are read, readint, and writeln

7 Methods in Java The author of your text further divides methods into two general categories: The author of your text further divides methods into two general categories: –Functions  A function is a method that performs a task and returns a value. –Procedures  A procedure is a method that performs a task but does not return a value.

8 Built-in Methods Java’s creators at Sun Microsystems have provided us with many built-in methods. Java’s creators at Sun Microsystems have provided us with many built-in methods. For now, we’ll just look at some of the built-in math methods, which can be viewed at: For now, we’ll just look at some of the built-in math methods, which can be viewed at: – html htmlhttp://java.sun.com/javase/6/docs/api/index. html

9 Sample Program Now let’s look at a sample program that demonstrates some of these methods: Now let’s look at a sample program that demonstrates some of these methods: –mathFunctionDemo1.java mathFunctionDemo1.java

10 Programmer-Defined Methods The program we just looked at contains a lengthy section of code at the beginning that does nothing more than display a menu of options to the user. The program we just looked at contains a lengthy section of code at the beginning that does nothing more than display a menu of options to the user. This “clutters up” the program, detracting from its main purpose, which is to demonstrate the built-in math functions. This “clutters up” the program, detracting from its main purpose, which is to demonstrate the built-in math functions. Would it not be better to move this code elsewhere and simply call it as needed, as we do with the math functions? Would it not be better to move this code elsewhere and simply call it as needed, as we do with the math functions?

11 Programmer-Defined Methods This, in essence, is the purpose of methods. This, in essence, is the purpose of methods. Programmer-defined methods provide us with a means of defining routines that we, as programmers, need to use repeatedly without repeating the code. Programmer-defined methods provide us with a means of defining routines that we, as programmers, need to use repeatedly without repeating the code.

12 Sample Program The following sample program demonstrates a programmer-defined method that our author would classify as a procedure: The following sample program demonstrates a programmer-defined method that our author would classify as a procedure: –mathFunctionDemo2.java mathFunctionDemo2.java

13 Sample Program The following sample program demonstrates a programmer-defined method that our author would classify as a function: The following sample program demonstrates a programmer-defined method that our author would classify as a function: –ClassifyDayOfWeek.java ClassifyDayOfWeek.java –It improves upon the original DaysOfWeek.java program. DaysOfWeek.java

14 Friday’s Lecture More on methods … More on methods …