410 likes | 421 Views
Learn how to create various shapes and patterns using nested loops in a program. From basic lines of asterisks to checkerboards and rectangles, explore different design possibilities.
E N D
Nesting Loops (That’s loops inside of loops. Why not?)
Consider a line of asterisks: column *********************... How can I do this in a program? row
Consider a line of asterisks:*************… final intnumberCols = 20; for (intcol=1; col<=numberCols; col++) { System.out.print( "*" ); } System.out.println();
Say I want rows and rows of this line. **************… **************… **************… . . . final int numberRows = 5; final int numberCols = 20;
//output all of the lines for (int row=1; row<=numberRows; row++) { //output one line for (intcol=1; col<=numberCols; col++) { System.out.print( "*" ); } System.out.println(); }
Say instead we want: * * * * … * * * * … . . . Note space between asterisks.
for (int row=1; row<=numberRows; row++) { for (intcol=1; col<=numberCols; col++) { System.out.print( "* " ); } System.out.println(); } space
Say we want a checkerboard: for (int row=1; row<=numberRows; row++) { for (intcol=1; col<=numberCols; col++) { System.out.print( "* " ); } System.out.println(); }
Say we want a checkerboard: for (int row=1; row<=numberRows; row++) { for (intcol=1; col<=numberCols; col++) { System.out.print( "* " ); } System.out.println(); } Can mod (%) help us somehow?
final int width = 8; //output all of the rows for (int r=1; r<=width; r++) { //output all of the columns within the row for (int c=1; c<=width/2; c++) { if ( (r%2) == 0 ) { System.out.print( " *" ); } else { System.out.print( "* " ); } } System.out.println(); }
final int width = 8; //output all of the rows for (int r=1; r<=width; r++) { //output all of the columns within the row for (int c=1; c<=width/2; c++) { if ( (r%2) == 0 ) System.out.print( " *" ); else System.out.print( "* " ); } System.out.println(); }
final int width = 8; //output all of the rows for (int r=1; r<=width; r++) { //output all of the columns within the row for (int c=1; c<=width/2; c++) { if ( (r%2) == 0 ) System.out.print( " *" ); else System.out.print( "* " ); } System.out.println(); } What are the possible values of r % 2? How many values are possible with r % 2? What data type has exactly this many values?
final int width = 8; booleanbumpOver = false; //output all of the rows for (int r=1; r<=width; r++) { //output all of the columns within the row for (int c=1; c<=width/2; c++) { if (bumpOver) System.out.print( " *" ); else System.out.print( "* " ); } System.out.println(); bumpOver = !bumpOver; } Use a boolean instead of mod.
final int width = 8; booleanbumpOver = false; //output all of the rows for (int r=1; r<=width; r++) { //output all of the columns within the row for (int c=1; c<=width/2; c++) { if (bumpOver) System.out.print( " *" ); else System.out.print( "* " ); } System.out.println(); bumpOver = !bumpOver; } Why width/2?
/* * file : CheckerBoard.java * author: george j. grevera, ph.d. * desc. : program to output a checkerboard (problem 4.1, p. 74. */ public class CheckerBoard { public static void main ( String[] s ) { //declare variables & initialize i/o //ouput startup message System.out.println( "here's the checkerboard:\n" ); final int width = 8; booleanbumpOver = false; //output all of the rows for (int r=1; r<=width; r++) { //output all of the columns within the row for (int c=1; c<=width/2; c++) { if (bumpOver) System.out.print( " *" ); else System.out.print( "* " ); } System.out.println(); bumpOver = !bumpOver; } //finish up System.out.println( "\n\nadios" ); } } The complete program.
Generate a rectangle. ********** * * * * <- rows * * ********** ^ cols
Generate a rectangle. ********** * * * * <- rows * * ********** ^ cols Note that the first and last lines are the same; also note that all of the middle lines are the same.
Generate a rectangle. ********** * * * * <-rows * * ********** ^ cols So our algorithm is: • Output the first line. • Output the intervening lines. • Output the last line (just like the first).
Generate a rectangle. ********** * * * * <-rows * * ********** ^ cols //Output the first line. for (int c=0; c<cols; c++) { System.out.print( "*" ); } System.out.println();
Generate a rectangle. ********** * * * * <-rows * * ********** ^ cols //Output the intervening lines. for (int r=0; r<rows-2; r++) { System.out.print( "*" ); for (int c=0; c<cols-2; c++) { System.out.print( " " ); } System.out.println( "*" ); }
Generate a rectangle. ********** * * * * <-rows * * ********** ^ cols //Output the last line (just like the first). for (int c=0; c<cols; c++) { System.out.print( "*" ); } System.out.println();
final int rows = 10; //define number of rows final int cols = 20; //define number of columns //Output the first line (just like the last). for (int c=0; c<cols; c++) { System.out.print( "*" ); } System.out.println(); //Output the intervening lines. for (int r=0; r<rows-2; r++) { System.out.print( "*" ); for (int c=0; c<cols-2; c++) { System.out.print( " " ); } System.out.println( "*" ); } //Output the last line (just like the first). for (int c=0; c<cols; c++) { System.out.print( "*" ); } System.out.println();
Repeatedly prompt until user enters something valid. final int minimum = 0; intinputValue = minimum-1; while (inputValue < minimum) { inputValue = in.nextInt(); }
Repeatedly prompt until user enters something valid. final int minimum = 0; intinputValue = minimum-1; boolean invalid = true; while (invalid) { inputValue = in.nextInt(); if (inputValue >= minimum) { invalid = false; } }
Repeatedly prompt until user enters something valid. final int minimum = 0; intinputValue = minimum-1; boolean valid = false; while (!valid) { inputValue = in.nextInt(); if (inputValue >= minimum) { valid = true; } }
Shakespearean insults • p. 75 of Per Brinch Hansen’s Java book (if time permits). • Thou goatish, hasty-witted ratsbane! • Thou loggerheaded, doghearted dewberry! • Thou churlish, tickle-brained pigeon-egg! • Our task is to prompt the user for the number of these to output, and then output that number of insults.
Shakespearean insults • Thou goatish, hasty-witted ratsbane! • Thou loggerheaded, doghearted dewberry! • Thou churlish, tickle-brained pigeon-egg! always always pick one pick one pick one
Shakespearean insults algorithm • Prompt user for number of insults. • Read in the number of insults. • Output that number of insults. • Output “Thou ”. • Pick and output a word from column 1. • Output “,”. • Pick and output a word from column 2. • Output “ ”. • Pick and output a word from column 3. • Output “!” w/ newline.
Shakespearean insults algorithm //Prompt user for number of insults. //Read in the number of insults. //Output that number of insults. //Output “Thou ”. //Pick and output a word from column 1. //Output “,”. //Pick and output a word from column 2. //Output “ ”. //Pick and output a word from column 3. //Output “!” w/ newline.
Shakespearean insults algorithm Scanner in = new Scanner( System.in ); //Prompt user for number of insults. System.out.print( "Enter the number of insults: " ); //Read in the number of insults. final int count = in.nextInt();
Shakespearean insults algorithm //Output that number of insults. for (int i=0; i<count; i++) { //Output “Thou ”. //Pick and output a word from column 1. //Output “, ”. //Pick and output a word from column 2. //Output “ ”. //Pick and output a word from column 3. //Output “!” w/ newline. }
//Output that number of insults. for (inti=0; i<count; i++) { //Output “Thou ”. System.out.print( “Thou ” ); //Pick and output a word from column 1. ? //Output “, ”. System.out.print( “, ” ); //Pick and output a word from column 2. ? //Output “ ”. //Pick and output a word from column 3. ? //Output “!” w/ newline. System.out.println( “!” ); }
//Output that number of insults. Random rnd = new Random(); for (inti=0; i<count; i++) { System.out.print( “Thou ” ); //Pick and output a word from column 1. ? System.out.print( “, ” ); //Pick and output a word from column 2. ? System.out.print( “ ” ); //Pick and output a word from column 3. ? System.out.println( “!” ); }
//Output that number of insults. Random rnd = new Random(); for (inti=0; i<count; i++) { System.out.print( “Thou ” ); //Pick and output a word from column 1. switch (rnd.nextInt(10)+1) { case 1: System.out.print( “artless”) break; case 2: System.out.print( “bootless”); break; … default: System.out.print( “loggerheaded”); break; } //end switch
//Output that number of insults. Random rnd = new Random(); for (inti=0; i<count; i++) { System.out.print( “Thou ” ); //Pick and output a word from column 1. switch (rnd.nextInt(10)+1) { case 1: System.out.print( “artless”) break; case 2: System.out.print( “bootless”); break; … default: System.out.print( “loggerheaded”); break; } //end switch System.out.print( “, ” ); //Pick and output a word from column 2. ? System.out.print( “ ” ); //Pick and output a word from column 3. ? System.out.println( “!” ); }
//Pick and output a word from column 1. switch (rnd.nextInt(10)+1) { case 1: System.out.print( “artless”) break; case 2: System.out.print( “bootless”); break; … default: System.out.print( “loggerheaded”); break; } //end switch System.out.print( “, ” ); //Pick and output a word from column 2. switch (rnd.nextInt(10)+1) { case 1: System.out.print( “beetle-headed”) break; case 2: System.out.print( “clay-brained”); break; … default: System.out.print( “weather-bitten”); break; } //end switch System.out.print( “ ” ); //Pick and output a word from column 3. …
//Output that number of insults. Random rnd = new Random(); for (inti=0; i<count; i++) { System.out.print( “Thou ” ); //Pick and output a word from column 1. switch (rnd.nextInt(10)+1) { case 1: System.out.print( “artless”) break; case 2: System.out.print( “bootless”); break; … default: System.out.print( “loggerheaded”); break; } //end switch System.out.print( “, ” ); //Pick and output a word from column 2. switch (rnd.nextInt(10)+1) { case 1: System.out.print( “beetle-headed”) break; case 2: System.out.print( “clay-brained”); break; … default: System.out.print( “weather-bitten”); break; } //end switch System.out.print( “ ” ); //Pick and output a word from column 3. ? System.out.println( “!” ); } And similarly here.