Presentation is loading. Please wait.

Presentation is loading. Please wait.

Derived from Building Java Programs by Stuart Reges & Marty Stepp

Similar presentations


Presentation on theme: "Derived from Building Java Programs by Stuart Reges & Marty Stepp"— Presentation transcript:

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

2 Example output to produce: hourglass
|\ /| | \ / | | \/ | | /\ | | / \ | |/ \|

3 Hourglass: Decomposition
|\ /| | \ / | | \/ | | /\ | | / \ | |/ \|

4 Hourglass: Pseudocode
draw line |\ /| | \ / | draw top half | \/ | | /\ | | / \ | draw bottom half |/ \| draw line

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

6 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

7 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

8 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

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

10 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. }

11 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 (“|”);

12 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 (“+”);

13 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 (“|”);

14 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). /**\ //**\\ ///**\\\ ////**\\\\ /////**\\\\\ +=*=*=*=*=*=*+ |../\..../\..| |./\/\../\/\.| |/\/\/\/\/\/\| |\/\/\/\/\/\/| |.\/\/..\/\/.| |..\/....\/..|

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

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

17 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.


Download ppt "Derived from Building Java Programs by Stuart Reges & Marty Stepp"

Similar presentations


Ads by Google