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 need help understanding this programming assignment. I do not understand it at all. Someone provided...

    I need help understanding this programming assignment. I do not understand it at all. Someone provided me with the code but when I run the code on eclipse it gives an error. Please explain this assignment to me please. Here is the code: import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class TextEditor { public static List<String> lines = new ArrayList<String>(); public static void main(String[] args) throws IOException { Scanner s = new...

  • PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java,...

    PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java, Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times....

  • I have already posted the question, however, only four parts have been answered with missing data,...

    I have already posted the question, however, only four parts have been answered with missing data, as well as not correct. Kindly, requesting you to help as much as possible. I have an exam and I need an explanation. Problem! A mount. Assets 1.00.000 Goodwil Amount 60,000 Liabilities Equity shares of $ 10 each Rosoves 20.000 Ficed Assets at cost Profit and loss account Secured loan Sundry Creditors Provision for action 30,000 Stock 80,000 Sundry deblors 50.000 Advances 20.000 Cash...

  • I understand that the picture has the answer to it. However, I'm wanting help understanding why t...

    I understand that the picture has the answer to it. However, I'm wanting help understanding why that's the answer/how they got to the answer. How did we go from APk=(5K^(-1/4)L^(1/2)) to Q=5k^(3/4)L^(1/2)? Why do you then proceed to add the exponents together and essentially ignore the other parts of the problem completely (as to arrive at the answer of 5/4)? Furthermore, how did they get the "n" looking thing in the parenthesis before the "K" and "L" ? What does...

  • 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...

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
ADVERTISEMENT