1 / 17

Nested for Loops

Nested for Loops. Derived from Building Java Programs by Stuart Reges & Marty Stepp. Example output to produce: hourglass. +------+ | /| | / | | / | | / | | / | |/ | +------+. Hourglass: Decomposition. +------+ | /| | / |

mweidner
Télécharger la présentation

Nested for Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Nested for Loops Derived from Building Java Programsby 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 • Draw a solid line

  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 • 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

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

  9. Table for Top Half of hourglass

  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. write (line – 1) spaces on the output line. write a bar 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 (“/”); for (int i = 1; i <= (line-1); i++) { 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 (“/”); for (int i = 1; i <= (line-1); i++) { 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.

More Related