Building Java Programs

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
BUILDING JAVA PROGRAMS CHAPTER 3 PARAMETERS AND OBJECTS.
Nested for loops Cont’d. 2 Drawing complex figures Use nested for loops to produce the following output. Why draw ASCII art? –Real graphics require a.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs Lecture 3-1: Parameters (reading: 3.1)
Introduction to Computers and Programming Introduction to Methods in Java.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
Copyright 2008 by Pearson Education 1 Class constants and scope reading: 2.4 self-check: 28 exercises: 11 videos: Ch. 2 #5.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs.
Topic 7 parameters Based on slides bu Marty Stepp and Stuart Reges from "We're flooding people with information. We.
Building Java Programs
Engineering 1020 Introduction to Programming Peter King Winter 2010.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Building Java Programs Chapter 3 Parameters and Objects Copyright (c) Pearson All rights reserved.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-3: Loop Figures and Constants reading: self-checks: 27 exercises:
1 BUILDING JAVA PROGRAMS CHAPTER 2 Pseudocode and Scope.
Building Java Programs Parameters and Objects. 2 Redundant recipes Recipe for baking 20 cookies: –Mix the following ingredients in a bowl: 4 cups flour.
Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 4: Loop Figures and Constants reading:
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
Copyright 2010 by Pearson Education Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 2: Primitive Data and Definite Loops.
CS 112 Introduction to Programming Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Copyright 2009 by Pearson Education Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1 self-check: #1-6 exercises: #1-3 videos: Ch.
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs Chapter 3
Lecture 3: Method Parameters
Chapter 7 User-Defined Methods.
Building Java Programs
CSCI 161 – Introduction to Programming I William Killian
using System; namespace Demo01 { class Program
Building Java Programs
Building Java Programs
CSCI 161 – Introduction to Programming I William Killian
Building Java Programs
TO COMPLETE THE FOLLOWING:
CSc 110, Spring 2017 Lecture 5: Constants and Parameters
Topic 7 parameters "We're flooding people with information. We need to feed it through a processor. A human must turn information into intelligence or.
Building Java Programs
Redundant recipes Recipe for baking 20 cookies:
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
class PrintOnetoTen { public static void main(String args[]) {
Building Java Programs
Building Java Programs
Building Java Programs
Scope of variables class scopeofvars {
CSE 142 Lecture Notes Global Constants, Parameters, Return Values
Building Java Programs
Building Java Programs
Scope scope: The part of a program where a variable exists. From its declaration to the end of the { } braces A variable declared in a for loop exists.
Building Java Programs
Drawing complex figures
Building Java Programs
CSE 341 Lecture 11 b closures; scoping rules
Building Java Programs
Generic Programming.
Building Java Programs
Building Java Programs
Methods/Functions.
CIS 110: Introduction to Computer Programming
Corresponds with Chapter 5
Building Java Programs
Presentation transcript:

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

Scope scope: The part of a program where a variable exists. From its declaration to the end of the { } braces A variable declared in a for loop exists only in that loop. A variable declared in a method exists only in that method. public static void example() { int x = 3; for (int i = 1; i <= 10; i++) { System.out.println(x); } // i no longer exists here } // x ceases to exist here x's scope i's scope

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. main line ******* 7 ************* 13

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

5 2 9 5 2 9 Output: 2 and 4 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)); 5 2 9 Output: 2 and 4 9 and 3 5 2 9 Output: 2 and 4

2 9 5 2 9 5 Output: 9 and 3 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)); 2 9 5 Output: 2 and 4 9 and 3 2 9 5 Output: 9 and 3

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 + ")";