Question

For this assignment, you will apply what you learned in analyzing for, while, and do-while loops...

For this assignment, you will apply what you learned in analyzing for, while, and do-while loops by writing these statements yourself. The Java™ program you write should do the following:

  • Display a pyramid of asterisks onscreen (i.e., a nested for loop)
  • Display the integers 10 to 1 in decreasing order, one number per line (i.e., a while/do-whlie loop)
  • Add 7 until the sum becomes greater than 157, at which point the program should display both the sum and the number of 7s added

Complete this assignment by doing the following:

  1. Download and unzip the linked Week 3 Coding Assignment Zip File.
  2. Add comments to the code by typing your name and the date in the multi-line comment header.
  3. Replace the following lines with Java™ code as directed in the file:
  • LINE 1
  • LINE 2
  • LINE 3
  1. Comment each line of code you add to explain what you intend the code to do and why you chose each type of loop.
  2. Test and modify your Java™ program until it runs without errors and produces the results as described above.

Note: Refer to this week's analyzing code assignment if you need help.

/**************************************************************************************
* Program: PRG/420 Week 3
* Purpose: Week 3 Coding Assignment
* Programmer: TYPE YOUR NAME HERE
* Class: PRG/420
* Creation Date: TYPE TODAY'S DATE HERE
******************************************************************************************
* Program Summary: For, while, and do-while loops; nested loops
*
* For this assignment, you will add code to create:
*
* a for loop nested inside another for loop
* a while loop
* a do-while loop

*****************************************************************************************/

package prg420week3_codingassignment;

public class PRG420Week3_CodingAssignment {

public static void main(String[] args) {

// The following code should print asterisks: 1 on line 1, 2 asterists on line 2,
// 3 on line 3, 4 on line 4... for as many lines as the variable linesOfAsterisks.
// To do this, we can use 2 nested for loops. The first for loop is coded for you.
// You will need to add another for lop, NESTED INSIDE the first, that prints
// a certain # of asterisks based on the # of times the loop code has been executed.
// The result should look like this:
// *
// **
// ***
// ****
// *****
// etc.
int linesOfAsterisks = 5;
for (int i = 1; i <= linesOfAsterisks; i++) { // for each line...

// LINE 1. ADD A NESTED FOR LOOP THAT DISPLAYS ONE ASTERISK ON LINE 1, TWO ASTERISKS ON LINE 2, 3 ASTERISKS ON LINE 3, ETC.

System.out.println();
}

//////////////////////////////////////////////////////////////////////
// Add a while or do-while loop that displays the numbers from 10 to 1 in that order, like so:
// 10
// 9
// 8
// 7
// ...
// 1
//////////////////////////////////////////////////////////////////////


int num=10;

//LINE 2. ADD A LOOP THAT DISPLAYS NUMBERS 10 TO 1 IN DECREASING ORDER (HINT: DECREMENT OPERATOR)

///////////////////////////////////////////////////////////////////////
// Write a loop that adds 7s one at a time until the sum becomes > 157.
// Then print out both the sum and the number of 7s that were aded.
// Write a while or do-while loop, whichever you think is most appropriate.
//////////////////////////////////////////////////////////////////////////

int sum = 0;
int numberOfSevens = 0;

//LINE 3. ADD ANOTHER LOOP THAT ADDS 7s UNTIL SUM > 157. THEN DISPLAY SUM AND NUMBER OF SEVENS ADDED.

}


0 0
Add a comment Improve this question Transcribed image text
Answer #1


package prg420week3_codingassignment;

public class PRG420Week3_CodingAssignment {

public static void main(String[] args) {

// The following code should print asterisks: 1 on line 1, 2 asterists on line 2,
// 3 on line 3, 4 on line 4... for as many lines as the variable linesOfAsterisks.
// To do this, we can use 2 nested for loops. The first for loop is coded for you.
// You will need to add another for lop, NESTED INSIDE the first, that prints
// a certain # of asterisks based on the # of times the loop code has been executed.
// The result should look like this:
// *
// **
// ***
// ****
// *****
// etc.
int linesOfAsterisks = 5;
for (int i = 1; i <= linesOfAsterisks; i++) { // for each line...

// LINE 1. ADD A NESTED FOR LOOP THAT DISPLAYS ONE ASTERISK ON LINE 1, TWO ASTERISKS ON LINE 2, 3 ASTERISKS ON LINE 3, ETC.
for (int j = 0; j < i; j++) {// run until * = row number
System.out.print("*"); // print
}
System.out.println();// break line
}

//////////////////////////////////////////////////////////////////////
// Add a while or do-while loop that displays the numbers from 10 to 1 in that order, like so:
// 10
// 9
// 8
// 7
// ...
// 1
//////////////////////////////////////////////////////////////////////


int num=10;

//LINE 2. ADD A LOOP THAT DISPLAYS NUMBERS 10 TO 1 IN DECREASING ORDER (HINT: DECREMENT //OPERATOR)
while(num!=0){ //run till num not become 0
System.out.println(num--);// print num
}
///////////////////////////////////////////////////////////////////////
// Write a loop that adds 7s one at a time until the sum becomes > 157.
// Then print out both the sum and the number of 7s that were aded.
// Write a while or do-while loop, whichever you think is most appropriate.
//////////////////////////////////////////////////////////////////////////

int sum = 0;
int numberOfSevens = 0;

//LINE 3. ADD ANOTHER LOOP THAT ADDS 7s UNTIL SUM > 157. THEN DISPLAY SUM AND NUMBER OF SEVENS //ADDED.
while(sum < 157){ // rum until sum > 157
System.out.println("Sum: "+sum+" Number of Sevens: "+numberOfSevens);//print
sum+=7; // add 7 to 7 each time
numberOfSevens++; // count
}
}
}

/* PLEASE UPVOTE */

/* OUTPUT */

run:
*
**
***
****
*****
10
9
8
7
6
5
4
3
2
1
Sum: 0 Number of Sevens: 0
Sum: 7 Number of Sevens: 1
Sum: 14 Number of Sevens: 2
Sum: 21 Number of Sevens: 3
Sum: 28 Number of Sevens: 4
Sum: 35 Number of Sevens: 5
Sum: 42 Number of Sevens: 6
Sum: 49 Number of Sevens: 7
Sum: 56 Number of Sevens: 8
Sum: 63 Number of Sevens: 9
Sum: 70 Number of Sevens: 10
Sum: 77 Number of Sevens: 11
Sum: 84 Number of Sevens: 12
Sum: 91 Number of Sevens: 13
Sum: 98 Number of Sevens: 14
Sum: 105 Number of Sevens: 15
Sum: 112 Number of Sevens: 16
Sum: 119 Number of Sevens: 17
Sum: 126 Number of Sevens: 18
Sum: 133 Number of Sevens: 19
Sum: 140 Number of Sevens: 20
Sum: 147 Number of Sevens: 21
Sum: 154 Number of Sevens: 22
BUILD SUCCESSFUL (total time: 0 seconds)

Add a comment
Know the answer?
Add Answer to:
For this assignment, you will apply what you learned in analyzing for, while, and do-while loops...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program...

    Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program will calculate and display the total of all numbers up to a specific number (entered by the user). Only positive numbers are allowed. Part B: User-controlled loop Part A Input Validation loop Part A: Summation loop Examples: When 5 is entered the program will calculate the total as 1+2+...+5 and display 15. When 2 is enterered the program will calculate the total as 1+2...

  • /************************************************************************************ * Program: PRG/420 Week 5 * Purpose: Week 5 Coding Assignment * Programmer: TYPE YOUR...

    /************************************************************************************ * Program: PRG/420 Week 5 * Purpose: Week 5 Coding Assignment * Programmer: TYPE YOUR NAME HERE * Class: PRG/420 * Creation Date: TYPE TODAY'S DATE HERE ************************************************************************************* * Program Summary: * This program converts a given date to a string. * The code includes exception handling for a ParseException. ************************************************************************************/ package prg420week5_codingassignment; import java.util.*; // wildcard to import all the util. classes import java.text.*; // wildcard to import all the text classes public class PRG420Week5_CodingAssignment { public static...

  • Java programming: Identify the true statements regarding Java loops. Select ALL that apply. A. A do-while...

    Java programming: Identify the true statements regarding Java loops. Select ALL that apply. A. A do-while loop always runs at least once. B.Loops can be nested in other loops to any depth as desired. C. While loops are best when the number of loop iterations can be predetermined. D. A while loop might not run at all. E. For loops are more susceptible to endless loop coding errors than while loops.

  • I need this code in java. Loops do just what they sound like they should -...

    I need this code in java. Loops do just what they sound like they should - they perform a statement again and again until a condition is fulfilled. For this lab you will be practicing using loops, specifically a for loop. The for loop takes the form of: for(/*variable initialization*/;/*stop condition*/; /*increment*/){ //statements to be done multiple times } Task Write an application that displays every perfect number from 2 through 1,000. A perfect number is one that equals the...

  • In this assignment, you will write a program in C++ which uses files and nested loops...

    In this assignment, you will write a program in C++ which uses files and nested loops to create a file from the quiz grades entered by the user, then reads the grades from the file and calculates each student’s average grade and the average quiz grade for the class. Each student takes 6 quizzes (unknown number of students). Use a nested loop to write each student’s quiz grades to a file. Then read the data from the file in order...

  • In java, write a program that gets 10 integer numbers from the user using user input,...

    In java, write a program that gets 10 integer numbers from the user using user input, and then calculates and display the sum of the numbers that have been read.   Program Requirements: Write the program in three versions with three loops. Put all three loops in the main method of your source code. version1:  use a while loop. version2:  use a do-while loop. version 3:  use a for loop. For each version, use a loop to input 10 int numbers from the user...

  • In java, write a program that gets 10 integer numbers from the user using user input,...

    In java, write a program that gets 10 integer numbers from the user using user input, and then calculates and display the sum of the numbers that have been read.   Program Requirements: Write the program in three versions with three loops. Put all three loops in the main method of your source code. The program must be in one file. version1:  use a while loop. version2:  use a do-while loop. version 3:  use a for loop. For each version, use a loop to...

  • Topics c++ Do While loops cin.ignore Description Redo the last assignment with the following modifications: 1....

    Topics c++ Do While loops cin.ignore Description Redo the last assignment with the following modifications: 1. When the program asks the user to enter a student name, the user will enter the full name (e.g. John Smith). Make changes to the program to accommodate that. 2. When the program asks the user to enter the test score, the user will enter the test score as before. However, make changes to the program so that it will check that the score...

  • C++ please 27.5 Loops (nested)**: Sum a given number of integers A user will enter an...

    C++ please 27.5 Loops (nested)**: Sum a given number of integers A user will enter an initial number, followed by that number of integers. Output those integers' sum. Repeat until the initial number is O or negative. Ex: If the user enters 3 96 1 0, the output is 16 Ex: If the user enters 396125 3 0, the output is 16 Hints: Use a while loop as an outer loop. Get the user's initial number of ints before the...

  • Please do this in C++ using only for loops, while loops, and do while loops. No...

    Please do this in C++ using only for loops, while loops, and do while loops. No arrays. Use for loop, while loop, and do while loops to generate a table of decimal numbers, as well as the binary, octal, and hexadecimal equivalents of the decimal numbers in the range 1-256 Note: See Appendix for number systems The program should print the results to the Console without using any string formats Design Details: The Main program should prompt the user for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT