1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.

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.
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 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
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.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 2: Primitive Data and Definite Loops.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
Building Java Programs
Derived from Building Java Programs by Stuart Reges & Marty Stepp
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
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.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-3: Loop Figures and Constants reading: self-checks: 27 exercises:
CS 112 Introduction to Programming Graphics; Animation Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
1 BUILDING JAVA PROGRAMS CHAPTER 2 Pseudocode and Scope.
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:
Building Java Programs Chapter 2 Primitive Data and Definite Loops.
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
Nested for loops.
Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
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.
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:
CSE 143 Lecture 9: introduction to recursion reading: 12.1.
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.
10/9/07 ______ < Moo? > \ ^__^ \ (oo)\_______ (__)\ )\/\
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
CSCI 161 – Introduction to Programming I William Killian
Building Java Programs Chapter 2
Building Java Programs
Computing Adjusted Quiz Total Score
Building Java Programs Chapter 2
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
Building Java Programs
CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants
Topic 6 loops, figures, constants
Building Java Programs
CSE 190D, Winter 2013 Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Suggested self-checks:
Building Java Programs
Drawing complex figures
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 2 Lecture 2-3: Loop Figures and Constants reading:
Presentation transcript:

1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS

2 MANAGING COMPLEXITY: PSEUDOCODE

3 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 #================# | <><> | | <>....<> | | <> <> | |<> <>| | <> <> | | <>....<> | | <><> | #================#

4 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

5 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 print a star } print 12 stars ************ * ************

6 PSEUDO-CODE ALGORITHM 1. Line #, 16 =, # 2. Top half | spaces (decreasing) <> dots (increasing) <> spaces (same as above) | 3. Bottom half (top half upside-down) 4. Line #, 16 =, # #================# | <><> | | <>....<> | | <> <> | |<> <>| | <> <> | | <>....<> | | <><> | #================#

7 METHODS FROM PSEUDOCODE public class Mirror { public static void main(String[] args) { line(); topHalf(); bottomHalf(); line(); } public static void topHalf() { for (int line = 1; line <= 4; line++) { // contents of each line } public static void bottomHalf() { for (int line = 1; line <= 4; line++) { // contents of each line } public static void line() { //... }

8 2. TABLES A table for the top half: Compute spaces and dots expressions from line number linespacesdots linespaces line * dots4 * line

9 3. WRITING THE CODE Useful questions about the top half: What methods? (think structure and redundancy) Number of (nested) loops per line? #================# | <><> | | <>....<> | | <> <> | |<> <>| | <> <> | | <>....<> | | <><> | #================#

10 PARTIAL SOLUTION // Prints the expanding pattern of <> for the top half of the figure. public static void topHalf() { for (int line = 1; line <= 4; line++) { System.out.print("|"); for (int space = 1; space <= (line * ); space++) { System.out.print(" "); } System.out.print("<>"); for (int dot = 1; dot <= (line * 4 - 4); dot++) { System.out.print("."); } System.out.print("<>"); for (int space = 1; space <= (line * ); space++) { System.out.print(" "); } System.out.println("|"); }