Complete Chapter 6 Programming Activity 1 Using while Loops

Complete Chapter 6, Programming Activity 1: Using \"while\" Loops. Make sure you study the Programming Activity 6-1 Guidance document

Programming Activity 6-1 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. Pseudocode for this assignment ------------------------------ You can find the following in this weeks outline: Processing user input (using a while loop) (pseudocode): initialize variables read first item // Priming read while (item != sentinel value) { process item read next item // Update read } output results More specifically for 6-1 ------------------------- initialize variables A key local variable is a reference to an item of type Item. Another useful variable is a double to store the price of an item. Another useful variable is a constant for the DIVIDER sentinel value. Finally, you will need a double to hold the running total of the items. Do a priming read to get the next (first) item and its price. Your while loop condition should check that the current item\'s price is not equal to the DIVIDER sentinel value. Here is an outline for your while loop body: Update the total with the current item\'s price. Make the call to animate. Get the next item and its price. After the end of the while loop: Output the results. Programming Activity 6-1 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. Pseudocode for this assignment ------------------------------ You can find the following in this weeks outline: Processing user input (using a while loop) (pseudocode): initialize variables read first item // Priming read while (item != sentinel value) { process item read next item // Update read } output results More specifically for 6-1 ------------------------- initialize variables A key local variable is a reference to an item of type Item. Another useful variable is a double to store the price of an item. Another useful variable is a constant for the DIVIDER sentinel value. Finally, you will need a double to hold the running total of the items. Do a priming read to get the next (first) item and its price. Your while loop condition should check that the current item\'s price is not equal to the DIVIDER sentinel value. Here is an outline for your while loop body: Update the total with the current item\'s price. Make the call to animate. Get the next item and its price. After the end of the while loop: Output the results.

Need the file in Java

Solution

import java.util.ArrayList;
import java.util.Scanner;

public class LoopDemo {

   public static void main(String[] args) {
       String itemname = \"\";
       double price = 0;
       int count = 0;
       ArrayList<Item> list = new ArrayList<Item>();
       Scanner scanner = new Scanner(System.in);
       final int DIVIDERSENTINELVALUE = 6;
       while (price != DIVIDERSENTINELVALUE) {
           System.out.println(\"to exit to add the items type price=6\");
           count++;
           System.out.println(\"enter the item name\");
           itemname = scanner.next();
           System.out.println(\"enter price of the item\");
           price = scanner.nextDouble();
           Item item = new Item(itemname, price);
           list.add(item);
           System.out
                   .println(\"do you want to add another item or not type y/n only\");
           String choice = scanner.next();
           if (choice.equals(\"n\"))

               break;

       }
       for (Item item : list) {
           System.out.println(item);
       }

       System.out.println(\"to find the total price\");
       double totalprice = 0;

       for (Item item : list) {
           totalprice = totalprice + item.getPrice();
       }
       System.out
               .println(\"the total price of all the items is :\" + totalprice);

   }

}

to exit to add the items type price=6
enter the item name
milk
enter price of the item
25
do you want to add another item or not type y/n only
y
to exit to add the items type price=6
enter the item name
soda
enter price of the item
26
do you want to add another item or not type y/n only
n
Item [itemName=milk, price=25.0]
Item [itemName=soda, price=26.0]
to find the total price
the total price of all the items is :51.0

Complete Chapter 6, Programming Activity 1: Using \
Complete Chapter 6, Programming Activity 1: Using \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site