Question

In: Computer Science

For this assignment, you will be analyzing the Java™ code in the linked Week 3 Analyze...

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:

  1. What is the output of the program as it is written?
  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?

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

Solutions

Expert Solution

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


Related Solutions

For this assignment, you will apply what you learned in analyzing Java™ code so far in...
For this assignment, you will apply what you learned in analyzing Java™ code so far in this course by writing your own Java™ program. The Java™ program you write should do the following: Accept user input that represents the number of sides in a polygon. Note: The code to do this is already written for you. If input value is not between 3 and 5, display an informative error message If input value is between 3 and 5, use a...
For this assignment, you will apply what you learned in analyzing a simple Java™ program by...
For this assignment, you will apply what you learned in analyzing a simple Java™ program by writing your own Java™ program. The Java™ program you write should do the following: Display a prompt on the console asking the user to type in his or her first name Construct the greeting string "Hello, nameEntered!" Display the constructed greeting on the console Complete this assignment by doing the following: Download and unzip the linked zip file. Add comments to the code by...
Assignment Content Resource: ****************************CODE PASTED BELOW******************************* For this assignment, you will develop Java™ code that relies...
Assignment Content Resource: ****************************CODE PASTED BELOW******************************* For this assignment, you will develop Java™ code that relies on localization to format currencies and dates. In NetBeans, copy the linked code to a file named "Startercode.java". Read through the code carefully and replace all occurrences of "___?___" with Java™ code. Note: Refer to "Working with Dates and Times" in Ch. 5, "Dates, Strings, and Localization," in OCP: Oracle® Certified Professional Java® SE 8 Programmer II Study Guide for help. Run and debug...
The Week 7 Case Study Assignment is an individual assignment that requires you to analyze a...
The Week 7 Case Study Assignment is an individual assignment that requires you to analyze a select group of alternative industries to determine which is most likely to perform best over the next 12 months. Factors to consider when comparing the industry groups include how the current and prospective economic conditions over the next year will affect them and the current and prospective domestic and global supply and demand conditions in their markets. Review briefly the list of industries below...
3.) Analyze the Java Program LinkRotator RUN the code and add the screenshot of the window...
3.) Analyze the Java Program LinkRotator RUN the code and add the screenshot of the window and output. Describe what code change you would make if you wanted each link to be displayed for 2.5 seconds. _______________________________________________________________ Below is the "LinkRotator" java program: 1: package com.java24hours; 2: 3: import java.awt.*; 4: import java.awt.event.*; 5: import java.io.*; 6: import javax.swing.*; 7: import java.net.*; 8:    9: public class LinkRotator extends JFrame 10: implements Runnable, ActionListener { 11: 12: String[] pageTitle =...
Using JAVA and NETBEANS Assignment Content For this assignment, you will develop "starter" code. After you...
Using JAVA and NETBEANS Assignment Content For this assignment, you will develop "starter" code. After you finish, your code should access an existing text file that you have created, create an input stream, read the contents of the text file, sort and store the contents of the text file into an ArrayList, then write the sorted contents via an output stream to a separate output text file. Copy and paste the following Java™ code into a JAVA source file in...
In this assignment, you will be practicing the Java OOP skills we've learned this week in...
In this assignment, you will be practicing the Java OOP skills we've learned this week in implementing a customized mutable Array class. Please read the following requirements carefully and then implement your own customized Array class: === 1) Basic (100' pts total) === Your Array class must provide the following features: --1.1) (20' pts) Two constructors. One takes the input of the initialized capacity (int) and create the underlying array with that capacity; the other takes no input and creates...
In this assignment, you will be practicing the Java OOP skills we've learned this week in...
In this assignment, you will be practicing the Java OOP skills we've learned this week in implementing a customized mutable Array class. Please read the following requirements carefully and then implement your own customized Array class: === 1) Basic (100' pts total) === Your Array class must provide the following features: --1.1) (20' pts) Two constructors. One takes the input of the initialized capacity (int) and create the underlying array with that capacity; the other takes no input and creates...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of the following sorting methods (Insertion Sort, Selection Sort, Quick Sort, and Merge Sort) to sort ArrayList of objects using Comaprable interface. (60 points)
This is a programming assignment!!! Start with the Assignment3.java source code posted to Canvas. This code...
This is a programming assignment!!! Start with the Assignment3.java source code posted to Canvas. This code outputs the programmer’s name , prompts the user to enter two numbers (integers) and outputs their sum. Note: When you run the program, it expects you to enter two integer values (a counting or whole number like 1, 216, -35, or 0) Make the following changes/additions to the code: On line 11, change the String myName from “your full name goes here!!!” to your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT