Chapter 2 Lecture 2-3: Loop Figures and Constants reading:

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

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 1 Class constants and scope reading: 2.4 self-check: 28 exercises: 11 videos: Ch. 2 #5.
Week 2 ______ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
CS305j Introduction to ComputingNested For Loops 1 Topic 6 Nested for Loops "Complexity has and will maintain a strong fascination for many people. It.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs.
Building Java Programs
Derived from Building Java Programs by Stuart Reges & Marty Stepp
Building Java Programs Chapter 2 Primitive Data and Definite Loops.
CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-3: Loop Figures and Constants reading: self-checks: 27 exercises:
1 CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators.
1 BUILDING JAVA PROGRAMS CHAPTER 2 Pseudocode and Scope.
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:
Building Java Programs Chapter 2 Primitive Data and Definite Loops.
Nested for loops.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
CSE 143 Lecture 10 Recursion reading: slides created by Marty Stepp and Hélène Martin
10/9/07 ______ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
Topic 6 loops, figures, constants Based on slides bu Marty Stepp and Stuart Reges from "Complexity has and will maintain.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
CS 112 Introduction to Programming Loop Examples; Variable Scoping; Nested Loops; Yang (Richard) Yang Computer Science Department Yale University 208A.
1 Building Java Programs Chapter 2: Primitive Data and Definite Loops These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may.
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:
10/9/07 ______ < Moo? > \ ^__^ \ (oo)\_______ (__)\ )\/\
Building Java Programs Chapter 2
Chapter 2 Programming Basics.
Building Java Programs
CSCI 161 – Introduction to Programming I William Killian
Building Java Programs Chapter 2
Building Java Programs Chapter 2
Primitive data, expressions, and variables
CSc 110, Spring 2017 Lecture 5: Constants and Parameters
Topic 7 Nested Loops Case Study
CSc 110, Autumn 2016 Lecture 5: Loop Figures and Constants
Building Java Programs
CSc 110, Spring 2017 Lecture 4: Nested Loops and Loop Figures
Building Java Programs
CSc 110, Spring 2018 Lecture 7: input and Constants
Building Java Programs
Variables variable: A piece of the computer's memory that is given a name and type, and can store a value. Like preset stations on a car stereo, or cell.
Building Java Programs
CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants
Building Java Programs
Building Java Programs
Topic 6 loops, figures, constants
Building Java Programs
CSE 190D, Winter 2013 Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Chapter 2 Programming Basics.
Primitive data, expressions, and variables
CSE 142 Lecture Notes Global Constants, Parameters, Return Values
Building Java Programs
Building Java Programs
Building Java Programs
Suggested self-checks:
Building Java Programs
Drawing complex figures
Building Java Programs
Building Java Programs
slides created by Marty Stepp
Building Java Programs
CIS 110: Introduction to Computer Programming
Presentation transcript:

Chapter 2 Lecture 2-3: Loop Figures and Constants reading: 2.4 - 2.5 12/8/2019 CSE 142, Spring 2013 Chapter 2 Lecture 2-3: Loop Figures and Constants reading: 2.4 - 2.5 1

Drawing complex figures Use nested for loops to produce the following output. Why draw ASCII art? Real graphics require a lot of finesse ASCII art has complex patterns Can focus on the algorithms #================# | <><> | | <>....<> | | <>........<> | |<>............<>|

Development strategy Recommendations for managing complexity: 1. Design the program (think about steps or methods needed). write an English description of steps required use this description to decide the methods 2. Create a table of patterns of characters use table to write your for loops #================# | <><> | | <>....<> | | <>........<> | |<>............<>|

1. Pseudo-code pseudo-code: An English description of an algorithm. Example: Drawing a 12 wide by 7 tall box of stars print 12 stars. for (each of 5 lines) { print a star. print 10 spaces. } ************ * *

2. Tables A table for the top half: Compute spaces and dots expressions from line number line spaces line * -2 + 8 dots 4 * line - 4 1 6 2 4 3 8 12 line spaces dots 1 6 2 4 3 8 12 #================# | <><> | | <>....<> | | <>........<> | |<>............<>|

Scaling the mirror Let's modify our Mirror program so that it can scale. The current mirror (left) is at size 4; the right is at size 3. We'd like to structure the code so we can scale the figure by changing the code in just one place. #================# | <><> | | <>....<> | | <>........<> | |<>............<>| #============# | <><> | | <>....<> | |<>........<>|

Limitations of variables 12/8/2019 Limitations of variables Idea: Make a variable to represent the size. Use the variable's value in the methods. Problem: A variable in one method can't be seen in others. public static void main(String[] args) { int size = 4; topHalf(); printBottom(); } public static void topHalf() { for (int i = 1; i <= size; i++) { // ERROR: size not found ... public static void bottomHalf() { for (int i = size; i >= 1; i--) { // ERROR: size not found 13

Class constants class constant: A fixed value visible to the whole program. value can be set only at declaration; cannot be reassigned Syntax: public static final type name = value; name is usually in ALL_UPPER_CASE Examples: public static final int DAYS_IN_WEEK = 7; public static final double INTEREST_RATE = 3.5; public static final int SSN = 658234569;

Observations about constant 12/8/2019 Observations about constant The constant can change the "intercept" in an expression. Usually the "slope" is unchanged. public static final int SIZE = 4; for (int space = 1; space <= (line * -2 + (2 * SIZE)); space++) { System.out.print(" "); } It doesn't replace every occurrence of the original value. for (int dot = 1; dot <= (line * 4 - 4); dot++) { System.out.print(".");