nested for loops derived from building java programs by stuart reges & marty stepp

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

Upload: miranda-marshall

Post on 24-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

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

Nested for Loops

Derived from Building Java Programs by Stuart Reges & Marty Stepp

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

Example output to produce: hourglass

+------+|\ /|| \ / || \/ || /\ || / \ ||/ \|+------+

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

Hourglass: Decomposition

+------+|\ /|| \ / || \/ || /\ || / \ ||/ \|+------+

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

Hourglass: Pseudocode

+------+ draw line|\ /|| \ / | draw top half

| \/ |

| /\ |

| / \ | draw bottom half

|/ \|

+------+ draw line

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

Hourglass: Pseudo Code

• Draw a solid line

• Draw the top half of the hourglass

• Draw the bottom half of the hourglass

• Draw a solid line

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

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

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

Solve for the solid line

• Write a + on the output line• Write 6 – on the output line• Write a + 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

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

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 some spaces on the output line.write a slash on the output line.write some spaces on the output line.write a bar on the output line.go to a new line of output.

}

To help you… create a table to determine the output of spaces

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

Table for Top Half of hourglass

Line Spaces Spaces Spaces

1 0 4 0

2 1 2 1

3 2 0 2

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

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.

write (line – 1) spaces on the output line.

write a bar on the output line.

go to a new line of output.

}

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

drawTop ()

// produces the top half of the hourglass figurepublic 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 (“/”);for (int i = 1; i <= (line-1); i++) {

system.out.print(“ “);}System.out.print (“|”);

}}

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

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

}

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

drawTop ( ) using constant// produces the top half of the hourglass figurepublic 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 (“/”);for (int i = 1; i <= (line-1); i++) {

system.out.print(“ “);}System.out.print (“|”);

}}

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

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

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

Rocket: Assignment #3 Decomposition

/**\ //**\\

///**\\\ ////**\\\\ /////**\\\\\ +=*=*=*=*=*=*+ |../\..../\..| |./\/\../\/\.| |/\/\/\/\/\/\| |\/\/\/\/\/\/| |.\/\/..\/\/.| |..\/....\/..| +=*=*=*=*=*=*+ |\/\/\/\/\/\/| |.\/\/..\/\/.| |..\/....\/..| |../\..../\..| |./\/\../\/\.| |/\/\/\/\/\/\| +=*=*=*=*=*=*+ /**\ //**\\ ///**\\\ ////**\\\\ /////**\\\\\

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

Rocket: Assignment #3 pseudocode

/**\ //**\\

///**\\\ ////**\\\\ /////**\\\\\ +=*=*=*=*=*=*+ |../\..../\..| |./\/\../\/\.| |/\/\/\/\/\/\| |\/\/\/\/\/\/| |.\/\/..\/\/.| |..\/....\/..| +=*=*=*=*=*=*+ |\/\/\/\/\/\/| |.\/\/..\/\/.| |..\/....\/..| |../\..../\..| |./\/\../\/\.| |/\/\/\/\/\/\| +=*=*=*=*=*=*+ /**\ //**\\ ///**\\\ ////**\\\\ /////**\\\\\

drawCone ( );

drawLine ( );drawM ( );drawW ( );

drawLine ( );

drawLine ( );

drawM ( );

drawCone ( );

drawW ( );

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

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.