Derived from Building Java Programs by Stuart Reges & Marty Stepp

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

1 Calling within Static method /* We can call a non static method from a static method but by only through an object of that class. */ class Test1{ public.
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)
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 | || ||
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.
Back to Basics in CS1 and CS2 Stuart Reges University of Washington.
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 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.
BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past.
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:
Building Java Programs Chapter 2 Primitive Data and Definite Loops.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Building java programs, chapter 3 Parameters, Methods and Objects.
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.
Copyright 2010 by Pearson Education Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1.
CS305j Introduction to Computing Parameters and Methods 1 Topic 8 Parameters and Methods "We're flooding people with information. We need to feed it through.
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:
Building Java Programs
Building Java Programs Chapter 2
Exercise Java programming
Building Java Programs
CSCI 161 – Introduction to Programming I William Killian
Building Java Programs Chapter 2
Building Java Programs
Adapted from slides by Marty Stepp, Stuart Reges & Allison Obourn.
Building Java Programs Chapter 2
CSc 110, Spring 2017 Lecture 5: Constants and Parameters
CSc 110, Autumn 2016 Lecture 5: Loop Figures and Constants
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
CSc 110, Spring 2017 Lecture 4: Nested Loops and Loop Figures
Redundant recipes Recipe for baking 20 cookies:
Building Java Programs
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
Building Java Programs
Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.
CSE 142 Lecture Notes Global Constants, Parameters, Return Values
Building Java Programs
Building Java Programs
Suggested self-checks:
Building Java Programs
Drawing complex figures
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 2 Lecture 2-3: Loop Figures and Constants reading:
Presentation transcript:

Derived from Building Java Programs by Stuart Reges & Marty Stepp Nested for Loops Derived from Building Java Programs by Stuart Reges & Marty Stepp

Example output to produce: hourglass +------+ |\ /| | \ / | | \/ | | /\ | | / \ | |/ \| +------+

Hourglass: Decomposition +------+ |\ /| | \ / | | \/ | | /\ | | / \ | |/ \| +------+

Hourglass: Pseudocode +------+ draw line |\ /| | \ / | draw top half | \/ | | /\ | | / \ | draw bottom half |/ \| +------+ draw line

Hourglass: Pseudo Code Draw a solid line Draw the top half of the hourglass Draw the bottom half of the hourglass

main( ) looks like public class DrawFigure { public static void main (String [] args) { drawLine(); drawTop (); drawBottom (); drawLine (); } public static void drawLine () { code SOLVE EACH METHOD INDEPENDENTLY

Solve for the solid line Write a + on the output line Write 6 – on the output line Go to a new output line public static void drawLine( ) { System.out.print ("+"); for (int i=1; i <= 6; i++) { System.out.print (“-”); } System.out.println (“+”); Translated into a static method

Solve for Top Half of Hourglass for (each of 3 lines) { write a bar on the output line. write some spaces on the output line. write a backslash on the output line. write a slash on the output line. go to a new line of output. } To help you… create a table to determine the output of spaces

Table for Top Half of hourglass Line Spaces 1 4 2 3

Refining pseudocode for Top Half of Hourglass for (line going from 1 to 3) { write a bar on the output line. write (line – 1) spaces on the output line. write a backslash on the output line. write (6 - 2 * line) spaces on the output line. write a slash on the output line. go to a new line of output. }

drawTop () // produces the top half of the hourglass figure public class drawTop { for (int line = 1; line <= 3; line++) { System.out.print (“|”); for (int i = 1; i <= (line-1); i++) { system.out.print(“ “); } System.out.print (“\\”); for (int i = 1; i <= (6-2*line); i++) { system.out.print(“ “); System.out.print (“/”); System.out.print (“|”);

Using a Class Constant // refers to height of two halves public static final int SUB_HEIGHT = 3; public static void drawLine( ) { System.out.print ("+"); for (int i=1; i <= (2*SUB_HEIGHT); i++) { System.out.print (“-”); } System.out.println (“+”);

drawTop ( ) using constant // produces the top half of the hourglass figure public class drawTop { for (int line = 1; line <= SUB_HEIGHT; line++) { System.out.print (“|”); for (int i = 1; i <= (line-1); i++) { system.out.print(“ “); } System.out.print (“\\”); for (int i = 1; i <= ((2*SUB_HEIGHT)-2*line); i++) { system.out.print(“ “); System.out.print (“/”); System.out.print (“|”);

Rocket: Assignment #3 Write a java program to produce this output. The height is 3; allow for the height to vary (with multiples of 3). /**\ //**\\ ///**\\\ ////**\\\\ /////**\\\\\ +=*=*=*=*=*=*+ |../\..../\..| |./\/\../\/\.| |/\/\/\/\/\/\| |\/\/\/\/\/\/| |.\/\/..\/\/.| |..\/....\/..|

Rocket: Assignment #3 Decomposition /**\ //**\\ ///**\\\ ////**\\\\ /////**\\\\\ +=*=*=*=*=*=*+ |../\..../\..| |./\/\../\/\.| |/\/\/\/\/\/\| |\/\/\/\/\/\/| |.\/\/..\/\/.| |..\/....\/..|

Rocket: Assignment #3 pseudocode /**\ //**\\ ///**\\\ ////**\\\\ /////**\\\\\ +=*=*=*=*=*=*+ |../\..../\..| |./\/\../\/\.| |/\/\/\/\/\/\| |\/\/\/\/\/\/| |.\/\/..\/\/.| |..\/....\/..| drawCone ( ); drawLine ( ); drawM ( ); drawW ( ); drawLine ( ); drawW ( ); drawM ( ); drawLine ( ); drawCone ( );

In-Class Exercise Create pseudocode for drawCone( ); Create output table for drawCone( ); Write code for drawCone( ); Note: Use a constant for HEIGHT. Compute height and length based on HEIGHT. Note: Top of rocket and booster of rocket are the same shape.