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.

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

BUILDING JAVA PROGRAMS CHAPTER 3 PARAMETERS AND OBJECTS.
{ int fun = 99; } Scope refers to whether the variable is seen within a certain context Any variable defined inside of curly brackets, only exists within.
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.
1 Parameters. 2 Repetitive figures Consider the task of drawing the following figures: ************* ******* *********************************** **********
Copyright 2008 by Pearson Education Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs Lecture 3-1: Parameters (reading: 3.1)
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
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:
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
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.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 4: Loop Figures and Constants reading:
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
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];
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Output Programs These slides will present a variety of small programs. Each program has a compound condition which uses the Boolean Logic that was introduced.
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.
Output Programs These slides will present a variety of small programs. Each program has a control structure that was introduced in this chapter. Our concern.
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.
Staples are our staple Building upon our solution.
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs Chapter 3
Lecture 3: Method Parameters
Building Java Programs
CSCI 161 – Introduction to Programming I William Killian
AKA the birth, life, and death of variables.
using System; namespace Demo01 { class Program
Building Java Programs
Building Java Programs
CSCI 161 – Introduction to Programming I William Killian
Building Java Programs
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[]) {
Lecture 3: Method Parameters
Topic 6 loops, figures, constants
Building Java Programs
Building Java Programs
CSc 110, Spring 2018 Lecture 8: input and Parameters
Building Java Programs
Scope of variables class scopeofvars {
CSE 142 Lecture Notes Global Constants, Parameters, Return Values
Building Java Programs
Building Java Programs
Building Java Programs
Drawing complex figures
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

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

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