Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete Chapter 6, Programming Activity 2: Using for Loops. Make sure you study the Programming Activity 6-2 Guidance document. Programming Activity 6-2 Guidance ================================= For

Complete Chapter 6, Programming Activity 2: Using "for" Loops. Make sure you study the Programming Activity 6-2 Guidance document. Programming Activity 6-2 Guidance ================================= For this activity, you are totaling item prices. You must get each item, get its price, and add that price to the total. When you have the final total, you output it in the format specified. Purpose of "for" loop --------------------- You must understand the purpose of the "for" loop in this assignment. The "for" loop is to iterate through the items and update a total. The "for" loop should not be the only thing in the function. It needs appropriate local variables declared BEFORE it begins. That means BEFORE the "for" statement line. You output the total AFTER the "for" loop finishes. That means AFTER its closing brace. That is why local variables, like the total, must be declared BEFORE the "for" loop is entered. If you declared the total variable inside the "for" loop, then it could not be referenced after it. Pseudocode for this assignment ------------------------------ You can find the following in this weeks outline: Calculate a total (using a for loop) (pseudocode) total = 0 // There is no need for a priming read. for (int i = 1; i <= number of items; i++) { read next number // Update read total += number } output total Students who have not taken time to learn about pseudocode, mistakenly think that it is real code and try to use it verbatim. Then, when it does not even compile, they get confused. It is called pseudocode because it is not real, final code. It requires work to turn it into real code. Some fragments of it may match real code. Other parts simply outline what to do. More specifically for 6-2 ------------------------- initialize variables The above pseudocode only shows a total variable. Remember that this is pseudocode. In Java every variable must have a type. You need other variables besides the double for the total. You need a reference to an item of type Item. Another useful variable is a double to store the price of an item. Your for loop must iterate once for each item. Your for loop condition must make use of the parameter numberOfItems, which is automatically passed into the function you are in by the framework. Here is an outline for your for loop body: Get the next item and its price. Update the total with the current item's price. Make the call to animate. After the end of the for loop: Output the results. Need the file in Java with more than one file where one is a cashier program.

/* ***** Student writes the body of this method ***** */ // // The parameter of this method, numberOfItems, // represents the number of items in the cart. The // user will be prompted for this number. // // Using a for loop, calculate the total price // of the groceries for the cart. // // The getNext method (in this Cashier class) returns the next // item in the cart, which is an Item object (we do not // know which item will be returned; this is randomly generated). // getNext does not take any arguments. its API is // Item getNext() // // As the last statement of the body of your for loop, // you should call the animate method. // The animate method takes one parameter: a double, // which is your current subtotal. // For example, if the name of your variable representing // the current subtotal is total, your call to the animate // method should be: // animate(total); // // The getPrice method of the Item class // returns the price of the Item object as a double. // The getPrice method does not take any arguments. Its API is // double getPrice() // // After you have processed all the items, display the total // for the cart in a dialog box. // Student code starts here:

Their are already other files that contain the information for the items in the cart. can someone please write the for loop for this problem.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David M. Kroenke, David Auer

11th Edition

B003Y7CIBU, 978-0132302678

More Books

Students also viewed these Databases questions

Question

How do books become world of wonder?

Answered: 1 week ago

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago