Building Java Programs

Similar documents
Building Java Programs

Building Java Programs

Topic 7 parameters. Based on slides from Marty Stepp and Stuart Reges from

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

CSc 110, Autumn 2016 Lecture 6: Parameters. Adapted from slides by Marty Stepp and Stuart Reges

Building Java Programs Chapter 3

Redundant recipes. Building Java Programs Chapter 3. Parameterized recipe. Redundant figures

Building Java Programs

Admin. CS 112 Introduction to Programming. Counting Down: Code Puzzle. Counting Down: Code Puzzle

CS 112 Introduction to Programming

CS 112 Introduction to Programming

CSE 201 Java Programming I. Smart Coding School website:

CSE 142, Summer 2015

CSE 142, Summer 2014

Building Java Programs

clicker question Comments Using comments Topic 3 static Methods and Structured Programming

Java Programming. What is a program? Programs Recipes. Programs as Recipes 8/16/12. Pancakes In one bowl. mix: 1½ cup flour

Static Methods & Decomposition

Building Java Programs

CS 112 Introduction to Programming

Recap: Structure of a Java program CS 112 Introduction to Programming. A Foundation for Programming Why Introduce Static Methods?

Topic 3 static Methods and Structured Programming

CS 112 Introduction to Programming

CS 112 Introduction to Programming

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Parameters. Repetitive figures. A solution? Parameterization. Declaring parameterized methods. Generalizing methods. Readings: 3.1

Building Java Programs

Java Programming. What is a program? Programs as Recipes. Programs Recipes 8/20/15. Pancakes In one bowl

Entering the world of Javatar

Topic 6 loops, figures, constants

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

public Twix() { calories = 285; ingredients = "chocolate, sugar, cookie, caramel"; }

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

CS 106A, Lecture 7 Parameters and Return

Building Java Programs

Building Java Programs

CIS 110: Introduction to Computer Programming

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

CSEN 202 Introduction to Computer Programming

Building Java Programs Chapter 2

CSc 110, Spring Lecture 2: Functions. Adapted from slides by Marty Stepp and Stuart Reges

Section 2.2 Your First Program in Java: Printing a Line of Text

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Recap: Assignment as an Operator CS 112 Introduction to Programming

true false Imperative Programming III, sections , 3.0, 3.9 Introductory Programming Control flow of programs While loops: generally Loops

Two figures that have the exact same shape, but not necessarily the exact same size, are called similar

Repetition with for loops

Lecture 5: Methods CS2301

Building Java Programs

Loops. CSE 114, Computer Science 1 Stony Brook University

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

CS 112 Introduction to Programming

Lecture 1: Basic Java Syntax

Building Java Programs

Computer Science is...

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Building Java Programs

Lecture 19: Recursion

Computer Science is...

Graded Project. HTML Coding

CS 161: Object Oriented Problem Solving

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Grade 7 Math LESSON 14: MORE PROBLEMS INVOLVING REAL NUMBERS TEACHING GUIDE

Procedural decomposition using static methods

CS 161: Object Oriented Problem Solving

CIS 110 Introduction To Computer Programming. October 5th, 2011 Exam 1. Review problems

Introduction to Computer Programming

Building Java Programs

Lecture Set 4: More About Methods and More About Operators

Notes - Recursion. A geeky definition of recursion is as follows: Recursion see Recursion.

Admin. CS 112 Introduction to Programming. Recap. Example: Nested Loop. Example: Rewrite

Write a program which converts all lowercase letter in a sentence to uppercase.

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

CP122 CS I. Chapter 1: Introduction

Building Java Programs

Building Java Programs

CSE 143 Lecture 10. Recursion

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Last Class. While loops Infinite loops Loop counters Iterations

Full file at

Midterm Review Session

Give one example where you might wish to use a three dimensional array

Building Java Programs

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

CS1150 Principles of Computer Science Loops (Part II)

Top-Down Program Development

CIS 110 Introduction To Computer Programming. October 5th, 2011 Exam 1. Answer key

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Methods Summer 2010 Margaret Reid-Miller

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

CIS 110 Introduction To Computer Programming. October 5th, 2011 Exam 1. Answer key for review problems

Topic 6 Nested for Loops

Lecture 14. 'for' loops and Arrays

Lecture Set 4: More About Methods and More About Operators

Object Oriented Programming. Java-Lecture 1

Algorithms: The recipe for computation

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Transcription:

Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1

(I think this is a made up language like Visual Basic but not quite.) 2

Promoting reuse Programmers build increasingly complex applications Enabled by existing building blocks, e.g. methods The more general a building block, the easier to reuse Abstraction: focusing on essential properties rather than implementation details Algebra is all about abstraction Functions solve an entire class of similar problems 3

Redundant recipes Recipe for baking 20 cookies: Mix the following ingredients in a bowl: 4 cups flour 1 cup butter 1 cup sugar 2 eggs 40 oz. chocolate chips... Place on sheet and Bake for about 10 minutes. Recipe for baking 40 cookies: Mix the following ingredients in a bowl: 8 cups flour 2 cups butter 2 cups sugar 4 eggs 80 oz. chocolate chips... Place on sheet and Bake for about 10 minutes. 4

Parameterized recipe Recipe for baking 20 cookies: Mix the following ingredients in a bowl: 4 cups flour 1 cup sugar 2 eggs... Recipe for baking N cookies: Mix the following ingredients in a bowl: N/5 cups flour N/20 cups butter N/20 cups sugar N/10 eggs 2N oz. chocolate chips... Place on sheet and Bake for about 10 minutes. parameter: A value that distinguishes similar tasks. 5

Redundant figures Consider the task of printing the following lines/boxes: ************* ******* *********************************** ********** * * ********** ***** * * * * ***** 6

A redundant solution public class Stars1 { public static void main(string[] args) { lineof13(); lineof7(); lineof35(); box10x3(); box5x4(); public static void lineof13() { for (int i = 1; i <= 13; i++) { System.out.print("*"); System.out.println(); public static void lineof7() { for (int i = 1; i <= 7; i++) { System.out.print("*"); System.out.println(); public static void lineof35() { for (int i = 1; i <= 35; i++) { System.out.print("*"); System.out.println();... This code is redundant. Would variables help? Would constants help? What is a better solution? line - A method to draw a line of any number of stars. box - A method to draw a box of any size. 7

Parameterization parameter: A value passed to a method by its caller. Instead of lineof7, lineof13, write line to draw any length. When declaring the method, we will state that it requires a parameter for the number of stars. When calling the method, we will specify how many stars to draw. 7 main line ******* 13 line ************* 8

Declaring a parameter Stating that a method requires a parameter in order to run public static void <name> (<type> <name>) { <statement>(s); Example: public static void saypassword(int code) { System.out.println("The password is: " + code); When saypassword is called, the caller must specify the integer code to print. 9

Passing a parameter Calling a method and specifying values for its parameters <name>(<expression>); Example: public static void main(string[] args) { saypassword(42); saypassword(12345); Output: The password is 42 The password is 12345 10

Parameters and loops A parameter can guide the number of repetitions of a loop. public static void main(string[] args) { chant(3); public static void chant(int times) { for (int i = 1; i <= times; i++) { System.out.println("Just a salad..."); Output: Just a salad... Just a salad... Just a salad... 11

How parameters are passed When the method is called: The value is stored into the parameter variable. The method's code executes using that value. public static void main(string[] args) { chant(3); chant(7); public static void chant(int times) { for (int i = 1; i <= times; i++) { System.out.println("Just a salad..."); 37 12

Common errors If a method accepts a parameter, it is illegal to call it without passing any value for that parameter. chant(); // ERROR: parameter value required The value passed to a method must be of the correct type. chant(3.7); // ERROR: must be of type int Exercise: Change the Stars program to use a parameterized method for drawing lines of stars. 13

Stars solution // Prints several lines of stars. // Uses a parameterized method to remove redundancy. public class Stars2 { public static void main(string[] args) { line(13); line(7); line(35); // Prints the given number of stars plus a line break. public static void line(int count) { for (int i = 1; i <= count; i++) { System.out.print("*"); System.out.println(); 14

Multiple parameters A method can accept multiple parameters. (separate by, ) When calling it, you must pass values for each parameter. Declaration: public static void <name>(<type> <name>,..., <type> <name>) { <statement>(s); Call: <name>(<exp>, <exp>,..., <exp>); 15

Multiple parameters example public static void main(string[] args) { printnumber(4, 9); printnumber(17, 6); printnumber(8, 0); printnumber(0, 8); public static void printnumber(int number, int count) { for (int i = 1; i <= count; i++) { System.out.print(number); System.out.println(); Output: 444444444 171717171717 00000000 Modify the Stars program to draw boxes with parameters. 16

Stars solution // Prints several lines and boxes made of stars. // Third version with multiple parameterized methods. public class Stars3 { public static void main(string[] args) { line(13); line(7); line(35); System.out.println(); box(10, 3); box(5, 4); box(20, 7); // Prints the given number of stars plus a line break. public static void line(int count) { for (int i = 1; i <= count; i++) { System.out.print("*"); System.out.println();... 17

Stars solution, cont'd.... // Prints a box of stars of the given size. public static void box(int width, int height) { line(width); for (int line = 1; line <= height - 2; line++) { System.out.print("*"); for (int space = 1; space <= width - 2; space++) { System.out.print(" "); System.out.println("*"); line(width); 18

Value semantics value semantics: When primitive variables (int, double) are passed as parameters, their values are copied. Modifying the parameter will not affect the variable passed in. public static void strange(int x) { x = x + 1; System.out.println("1. x = " + x); public static void main(string[] args) { int x = 23; strange(x); System.out.println("2. x = " + x);... Output: 1. x = 24 2. x = 23 19

A "Parameter Mystery" problem public class ParameterMystery { public static void main(string[] args) { int x = 9; int y = 2; int z = 5; mystery(z, y, x); mystery(y, x, z); public static void mystery(int x, int z, int y) { System.out.println(z + " and " + (y - x)); 20

Strings string: A sequence of text characters. String <name> = "<text>"; String <name> = <expression resulting in String>; Examples: String name = "Marla Singer"; int x = 3; int y = 5; String point = "(" + x + ", " + y + ")"; 21

Strings as parameters public class StringParameters { public static void main(string[] args) { sayhello("marty"); Output: String teacher = "Bictolia"; sayhello(teacher); public static void sayhello(string name) { System.out.println("Welcome, " + name); Welcome, Marty Welcome, Bictolia Modify the Stars program to use string parameters. Use a method named repeat that prints a string many times. 22

Stars solution // Prints several lines and boxes made of stars. // Fourth version with String parameters. public class Stars4 { public static void main(string[] args) { line(13); line(7); line(35); System.out.println(); box(10, 3); box(5, 4); box(20, 7); // Prints the given number of stars plus a line break. public static void line(int count) { repeat("*", count); System.out.println();... 23

Stars solution, cont'd.... // Prints a box of stars of the given size. public static void box(int width, int height) { line(width); for (int line = 1; line <= height - 2; line++) { System.out.print("*"); repeat(" ", width - 2); System.out.println("*"); line(width); // Prints the given String the given number of times. public static void repeat(string s, int times) { for (int i = 1; i <= times; i++) { System.out.print(s); 24