In: Computer Science
For this assignment, you will be analyzing the Java™ code in the linked Week 3 Analyze Assignment Zip File, and predicting the results. You will also examine both the code and the output for inconsistencies and clarity. This Java™ code includes examples of for, while, and do-while loops.
Carefully read through the code line by line, then answer the following questions in a Microsoft® Word document:
Note: You do not have to make the improvements in the Java™ program, although you certainly may. For this assignment, simply describe the things you see that would need to be improved to elevate the code and output to a more professional level. For the code, consider variable names and hardcoding. For the output, consider formatting/punctuation, repetition, accuracy of information, and wording.
/**************************************************************************************
* Program: PRG/420 Week 3
* Purpose: Week 3 Analyze Assignment
* Programmer: Iam A. Student
* Class: PRG/420
* Creation Date: 10/22/17
******************************************************************************************
* Program Summary: For, while, and do-while loops
*
* This program demonstrates the syntax for the for, while, and
do-while loops. It also
* contains comments that explain why a programmer would use a for
loop over a while or do-while
* loop.
*
* Notice the increment operator (i++) and also notice the copious
use of println() statements.
* Using System.out.println() is an excellent way to debug your
code--especially if your loop code
* is giving unexpected results.
*****************************************************************************************/
package PRG420Week3_AnalyzeAssignment;
public class PRG420Week3_AnalyzeAssignment {
public static void main(String[] args) {
// for loops are a good choice when you have a specific number of
values
// you want to iterate over and apply some calculation to.
System.out.println("FOR LOOP - Here are the taxes on all 10
prices:");
double taxRate = 0.08;
for (int price=1; price<=10; price++) {
System.out.println("The 8% tax on " + price + " dollar(s) is " +
"$" + (price * taxRate));
}
System.out.println(""); // Leave a blank space
// while loops are a good choice when you're looking through a pile
of values
// and want to execute some logic while some condition is
true.
// while loops MAY OR MAY NOT EVER EXECUTE, depending on the
counter value.
int dollars=1;
System.out.println("WHILE LOOP - Here are the taxes on prices less
than $5:");
while (dollars < 5) {
System.out.println("The 8% tax on " + dollars + " dollar(s) is $" +
(dollars * taxRate));
dollars++;
}
System.out.println(""); // Leave a blank space
// do-while loops are also a good choice when you're looking
through a pile of values
// and want to execute some logic while some condition is
true.
// do while loops ALWAYS EXECUTE AT LEAST ONCE, no matter what the
counter value.
// For example, in the code below, we want to print out the tax
only on those
// amounts smaller than $1. But because we're using the do-while
loop, the code
// will execute the body of the loop once before it even checks the
condition! So
// we will get an INCORRECT PRINTOUT.
dollars=1;
System.out.println("DO-WHILE LOOP - Here are the taxes on prices
less than $1:");
do {
System.out.println("The 8% tax on " + dollars + " dollar(s) is $" +
(dollars * 0.08));
dollars++;
} while (dollars < 1);
}
}
The out put produced by the program:
Here we are printing the 8% value as hard coded instead of that we can take it into a constant variable and use it so that in future even if you change the it will easy to update because if we update at one place than that will change every where