170 likes | 208 Views
Learn how to create an hourglass pattern using nested for loops in Java, demonstrated through code samples and step-by-step explanations.
E N D
Nested for Loops Derived from Building Java Programsby 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 • Draw a solid line
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 • 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
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
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. }
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 (“|”); } }
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 (“/”); for (int i = 1; i <= (line-1); i++) { 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.