Waiting for answer This question has not been answered yet. You can hire a professional tutor to get the answer.

QUESTION

2. What improvement(s) could be made to this code to make the output clearer or more accurate, or to make the code easier to maintain?

   What improvement(s) could be made to code to make the output clearer more accurate, to make the code easier to maintain? I cannot think of anything other than the tax is hard coded into the code. Please provide suggestions on how to make this code better.

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

* 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);

  }

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question