Question

4G,盃29% 7:26 Expert Q&A Create a java program which will prompt a user to input a product and its cost. Using a while loop th

This question has already been answered however I do not understand the particulars of the answer. for an example why are we importing an average class I thought that we could solve the problem just inputting the scanner and doing the import command at the top. Also at the bottom of the problem they increment the counter I thought you increment the counter at the top. Is there any way someone could do a detailed step-by-step explanation of the actual program? For your own interest the question-and-answer have already been posted so I don't think there's any need to do an actual solution but you could cut and paste the solution and explain it step-by-step

의8 Answer 1 of 1 port java.uti1.Scanner; blic class AverageProductCost i public static void main(Stringl] args){ Scanner inpu

의8 Ltou al 32% 9:42 Answer 1 of 1 貝+ System. out.print(Enter product name: ) name = input.nextLine(); if(name.equalsIgnoreC

Answer 1 of 1 input.nextLine); 1/ Consume newline if(productCost >- 100) average t- productCost count+ average / count; Syste

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I had tried to simply the solution .All your Doubts will be solved and is easy than previous answers you had seen.

List - It is Collection class which is similar to array , but the only difference is the worry about the size of list is not there, i.e size is not fixed like array.

Program :-

import java.util.*;   
import java.util.Scanner.*;

class HomeworkLib1
{
   public static void main(String[] args) // Main method
   {

// Declaration of variables
       int i;
       double sum=0;
       Scanner sc1=new Scanner(System.in); // Create Scanner object for String input
       Scanner sc2=new Scanner(System.in); // Create Scanner object for Double input
       Scanner sc3=new Scanner(System.in); // Create Scanner object for Integer input


       List<Double> l1=new ArrayList<Double>(); // Creating a List


       while(true) // Start of while
       {
           System.out.println("Enter 1) Enter Details of Another Product \n 2) Exit"); // Ask User for Choice
           int n=sc3.nextInt(); // Scan the choice and store it in n


           switch(n) // Start of Switch
           {
               case 1:
               {
                   System.out.println("Enter the Cost of the Product"); // Ask User for Cost
                   double cost=sc2.nextDouble(); // Scan the input and store it in cost
                   if(cost>=100) // Check if cost is Greater than or equal to 100
                   {
                       l1.add(cost); // Add the cost of the Product to the List
                       System.out.println("Enter Product Name"); // Ask for Product name
                       String product=sc1.nextLine(); // Scan the input and store it in product variable
                   }// end of if
                   else
                   {
                       System.out.println("Cost Less Than 100"); // if cost is less than 100 print the message
                   }// end of else
                   break; // break the switch case loop
               } // end of case 1


               case 2: // User don't want to add other product
               {
                   break; // break the switch case loop
               } end of case 2


               default: // if choice is not valid ,by default this case is executed
               {
                   System.out.println("Invalid Entry");
               } // end of default


           } // end of switch


           if(n==2){ // If the user don't want to add another product
               break; // break from while loop
           } // end of if


       }// end of while


       for(i=0;i<l1.size();i++) // loop to calculate average of product Cost
       {
           sum=sum + l1.get(i); // l1.get(i) will get the element from list at index i and will add it to sum
       } // end of for


       System.out.println("Average of Cost is :$" +sum); // print the Average of cost


   } // end of main method


} // end of class

Add a comment
Answer #2

Solution:

I have given line by line explanation by giving commenting line in the program. In case of any problem please do the comment.

//import java.util package to use Scanner class to take user input

import java.util.*;

//class definition

class AverageProductCost

{

public static void main(String args[])

{

Scanner sc = new Scanner(System.in); //Scanner class for taking input

String product_name; //declare product_name

double product_cost, average; //declare product_cost and average

double total = 0.0; //declare total to find the total cost of all product whose cost is >= 100

int count = 0; //declare count to keep track of entered products whose cost is greater than or equal to $100

//infinite while loop that break only when user enters stop

while(true)

{

//prompt the user to input product name

System.out.println("Enter product name:");

product_name = sc.nextLine();

//break the loop when user enters the string stop

if(product_name.equalsIgnoreCase("stop"))

{

break;

}

//prompt the user to input product cost

System.out.println("Enter product cost:");

product_cost = sc.nextDouble();

//consume newline

sc.nextLine();

//condition to check product cost >= 100 or not

if(product_cost >= 100)

{

//if condition is true then add the product cost to total to find total cost

total = total + product_cost;

//increase count whenever the product cost is >= 100

//note: we didn't increase the count at the top because we have to increase the count only when cost is >= 100

count++;

}

}

//calculate the average by dividing the total cost by count(total product whose cost is >= 100)

average = total/count;

//print average of the product cost

System.out.println("Average cost of the items is: "+average);

}

}

Please give thumnbsup, if you like it. Thanks.

Add a comment
Know the answer?
Add Answer to:
This question has already been answered however I do not understand the particulars of the answer....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a java program which will prompt a user to input a product and its cost....

    Create a java program which will prompt a user to input a product and its cost. Using a while loop that will run until the sentinel value of “stop” (any case) is entered, the program will look for items with a cost greater than or equal to $100.00. The program will find the average cost of those items. Note: immediately after your statement containing input.nextDouble() you will need to add the following input.nextLine() statement. This will cause the input scanner...

  • Lab 7 Example Output Products that cost $100.00or mone Enter the product ordered type stop to...

    Lab 7 Example Output Products that cost $100.00or mone Enter the product ordered type stop to end: flat screen TV Enter the cost of the product ordered: 599 90 Enter the product ordered-type stop to end: HD cable Enter the cost of the product ordered: 2502 Enter the product ordered - type stop to end: laptop Enter the cost of the product ordered: 1202.25 Enter the product ordered type stop to end: external drive Enter the cost of the product...

  • Java Programming Question

    After pillaging for a few weeks with our new cargo bay upgrade, we decide to branch out into a new sector of space to explore and hopefully find new targets. We travel to the next star system over, another low-security sector. After exploring the new star system for a few hours, we are hailed by a strange vessel. He sends us a message stating that he is a traveling merchant looking to purchase goods, and asks us if we would...

  • Need step by step explanation with the formula used.   Following is my question that has been answered: I received the following answer to the questions. However, I do not understand what formula was...

    Need step by step explanation with the formula used.   Following is my question that has been answered: I received the following answer to the questions. However, I do not understand what formula was used in part b and c. Could anyone help me to understand the solution with more details? Thanks! Specify each of the following. (a) The conditional distribution of X,, given that X2-X2 for the joint distribution with, μ1-0,P2-2, σ11-2, σ22-1, and P12:5 (b) The conditional distribution of...

  • I already sent this question twice and the answer I was given was wrong twice! The...

    I already sent this question twice and the answer I was given was wrong twice! The correct answers are at the top of the question! Thank you very much!!☺️ Problem 10-3A Estimated product warranty liabilities LO4 CHECK FIGURES: 2. $1,750; 3. $40,600; 4. $40,145 On November 10, 2017, Singh Electronics began to buy and resell scanners for S perpetual system to account for inventories. The scanners are covered under a warranty that requires the company to replace any non-working scanner...

  • Below is the code for the class shoppingList, I need to enhance it to accomplish the...

    Below is the code for the class shoppingList, I need to enhance it to accomplish the challenge level as stated in the description above. public class ShoppingList {   private java.util.Scanner scan; private String[] list; private int counter; public ShoppingList() { scan = new java.util.Scanner(System.in); list = new String[10]; counter = 0; } public boolean checkDuplicate(String item) { for(int i = 0; i < counter; i++) { if (list[i].equals(item)) return true; } return false; } public void printList() { System.out.println("Your shopping...

  • Voltswagon plc produces and sells cars. In the past, the company has been very successful, however,...

    Voltswagon plc produces and sells cars. In the past, the company has been very successful, however, increased competition and complaints about car reliability have started to reduce company profits. A new Finance Director has recently hired a consultant to review the existing production processes. The consultant has identified the following areas for consideration: (i) Quality costs Balanced Scorecard REQUIRED: Please note: a maximum word count applies to parts (b), (c) and (d) of this question ONLY. There is no maximum...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

  • write a C++program to analyze a small subset of the data that has been collected. See...

    write a C++program to analyze a small subset of the data that has been collected. See file universities.txt .Use precisely seven parallel arrays: one for name of university, one for state, one for city, one for yearly tuition, one for enrollment, one for average freshman retention, and one for the percent of students who graduate with in six years. Note that the percentage of student accepted is not stored.An output file is opened in main() and remains open until the...

  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT