I. The while loop You have learned in lecture about the while loop, that allows you to write code that repeats a set of

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

I. The while loop You have learned in lecture about the while loop, that allows you to write code that repeats a set of

Post by answerhappygod »

I The While Loop You Have Learned In Lecture About The While Loop That Allows You To Write Code That Repeats A Set Of 1
I The While Loop You Have Learned In Lecture About The While Loop That Allows You To Write Code That Repeats A Set Of 1 (62.01 KiB) Viewed 34 times
I The While Loop You Have Learned In Lecture About The While Loop That Allows You To Write Code That Repeats A Set Of 2
I The While Loop You Have Learned In Lecture About The While Loop That Allows You To Write Code That Repeats A Set Of 2 (72.02 KiB) Viewed 34 times
I The While Loop You Have Learned In Lecture About The While Loop That Allows You To Write Code That Repeats A Set Of 3
I The While Loop You Have Learned In Lecture About The While Loop That Allows You To Write Code That Repeats A Set Of 3 (61.16 KiB) Viewed 34 times
I The While Loop You Have Learned In Lecture About The While Loop That Allows You To Write Code That Repeats A Set Of 4
I The While Loop You Have Learned In Lecture About The While Loop That Allows You To Write Code That Repeats A Set Of 4 (57.38 KiB) Viewed 34 times
I. The while loop You have learned in lecture about the while loop, that allows you to write code that repeats a set of statements. For this part of the lab, you'll use a nested while loop (one inside of another), to generate a very simple ASCII art image. ASCll art is a graphic design technique for drawing pictures made up of the 95 printable characters that you can type on a standard keyboard. From far away, ASCII art looks like a normal (ish) art drawing (Figure I a), but if you look closely, you'll notice that an ASCII art image is made up letters, numbers. characters, ctc. (Figure Ib). (Images courtesy of http:/len.wikipedia.org/wiki/ASCII art) CSt10, Tarians flerians. CWT. Labs; while and for hepe Ruse t af 5
s lab you won't be creating images that are as complicated as the one in Figure 1 (such images take a great of planning and practice). Instead, you'll begin by writing a program that generates a triangle, made up of cters chosen by user. See Figure 3 for the output that your program will generate. To complete this part of b : Create a new java file. Type the text that is shown in Figure 2 into the editor panel of the new java file that you just created. Add/modify the comments accordingly, so that your name is listed as an author, and so that you include a description of the program. As always, be generous with using comments. Save the file as AsciiTriangle.java in your lab5 folder. Compile the program and run it. Make sure that the program outputs what you see in Figure 3. If you have typed any part of the code incorrectly (and introduced a syntax error, you'll have to fix the error, until your code is error-free). Figure 2: Java code to create triangle ASCII art image.
I1. The for loop Another loop control structure, in addition to the while and do-while loops, is the for loop, which you've secn in lecture already. The for loop has a very similar functionality to the while loop, bat the syntax is different. In this part of the lab, you'll recreate the triangle ASCII program that you wrote for the previous part of this lab, but you'll use a for loop instead of a whale loop. To complete this part of the lab: 1. Create a new file, ForLoopdscilAr.jawa inside of your lab5 folder. 2. Import Java's Scanner functionality; at the top of your file, write: import java . ut i. . Scanner; 3. Write a main method, and inside of it, set up a Scanner that is listening to the keyboard, and print to the screen the following questions: How tall is the triangle? Use the Scanner's nextint () method, to retricve from the keyboard, the number input by the user, which you should save into a variable triangleHeight of type int. 4. To output the "percent" characters (\%). you'll need to use fwo for-loops, an outer loop and an inner loop: a. The outer loop should have an iterator variable. row, set to 0 in the initialization step. The update step should be row++, and the loop should iterate as long as zow <= triangleHe ight. Therefore, this loop will perform as many iterations as the number that was input by the user. The outer for loop should look like the following: for fint row =0; rou x of the triangle, you print x asterisks. The inner loop uses the value of the zow variable, to determine how many columns of asterisks to print. The println is needed to generate a new line after cach row of the triangle is printed. your inner for loop (the loop INSIDE the body of the outer loop), should be: for (int colunn =0, columt \& rowz columnt+1 1 CSI10, Tanara Harrien. CWU, Lab 5; while and for loge Pape 3 of 5
5. Complete the program by making sure that the main and class sections are correctly closed using the closing curly brackets. Compile and run your program. Make sure that your program reproduces the output that is shown in Figures 4. III. The for loop; deerementing In this part of the lab, you'll again print a triangle, but this time, an upside-down triangle. See Figure 5 , for example output. The code for this program is nearly identical to the program for printing a right-side-up triangle. The only difference is that the for loop decrements. To complete this part of the lab: 1. Using jGRASP, create a new file, UpsideDonv Triangle java, inside of your lab5 folder. 2. Import the Scanner functionality, at the top of your java ffle. Write a main method, and inside of it, create a Scanner that is listening to the keyboard. 3. Print to the screen the following questions: How tall is the upsidendown triangle? 4. Use the Scanner's nextint () method, to retricve from the keyboard, the number input by the user, which you should save into a variable triangleHel ght of type int. 5. To output the percent characters (\%), you'l again need two for-loops, an outer loop and an inner loop. The "first" row of the triangle, however, should have as many columns as the number that is placed into the variable triangleHeight. Then, each successive row should have one fewer asterisk. a. The outer loop should have an iterator variable, row, set to triangleHe 1ght in the initialization step. The update step should be row −∗and ​ and the loop should iterate as long as row >0. Therefore, this loop will perform as many iterations as the number that was input by the user, but it is different than the for loop that you wrote for part 1 of this lab, because the "first" row has the most columns of asterisks, and each successive row has one fewer. for fint row = trianglelfeight f row >0,7 rowe 1 1 b. Inside of the body of the outer for loop, write ANOTHER for loop, which is identical to the inner for loop that you wrote for part II of this lab. 6. Compile and run your program. Make sure that your program can reproduce the output in Figure 5. Page 4 of 4
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply