Question

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 to consume the newline after your product cost. Otherwise, your program will skip the next input statement. The code should look like this:

productCost = input.nextDouble();
input.nextLine(); // Consume newline

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class AverageProductCost {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double average = 0, count = 0, productCost;
        String name;
        while (true) {
            System.out.print("Enter product name: ");
            name = input.nextLine();
            if(name.equalsIgnoreCase("stop")) {
                break;
            }
            System.out.print("Enter product cost: ");
            productCost = input.nextDouble();
            input.nextLine(); // Consume newline
            if(productCost >= 100) {
                average += productCost;
                count++;
            }
        }
        average /= count;
        System.out.println("Average cost of items is " + average);
    }

}

Enter product name: pl Enter product name: Enter product cost: Enter product name: Average cost of items is 137.5 Process fin

Add a comment
Know the answer?
Add Answer to:
Create a java program which will prompt a user to input a product and its cost....
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
  • This question has already been answered however I do not understand the particulars of the answer....

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

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

  • Write a Java application that prompts the user for pairs of inputs of a product number...

    Write a Java application that prompts the user for pairs of inputs of a product number (1-5), and then an integer quantity of units sold (this is two separate prompts for input values). You must use a switch statement and a sentinel-controlled loop (i.e. a loop that stops execution when an out of range value, such as -1, is input). All 15 items below are for a single purchase. There are five sets of inputs as follows: Product 1    1...

  • Write a Java program that will create an array of Strings with 25 elements. Input Strings...

    Write a Java program that will create an array of Strings with 25 elements. Input Strings from the user and store them in the array until either the user enters “quit” or the array is full. HINT: the sentinel value is “quit” all lowercase. “Quit” or “QUIT” should not stop the loop. HINT: you have to use the equals method (not ==) when you are comparing two Strings. After the input is complete, print the Strings that the user entered...

  • A class Scanner in Java can be used to get user input and its methods can...

    A class Scanner in Java can be used to get user input and its methods can be used after the following statement is executed: Scanner input = new Scanner(System.in); One of its methods nextDouble() returns the next double value from the keyboard. Now you are requested to write a method enterHeight() which displays the message "Input height: " and uses the method input.nextDouble() to get the user input. The process should be repeated until the input is non-negative. Finally the...

  • (a) A class Scanner in Java can be used to get user input and its methods...

    (a) A class Scanner in Java can be used to get user input and its methods can be used after the following statement is executed: Scanner input = new Scanner(System.in); One of its methods nextDouble() returns the next double value from the keyboard. Now you are requested to write a method enterHeight() which displays the message "Input height: " and uses the method input.nextDouble() to get the user input. The process should be repeated until the input is non-negative. Finally...

  • Overview: In this lab you are to write a Java program to prompt the user for...

    Overview: In this lab you are to write a Java program to prompt the user for an integer indicating a year and then print the calendar for that year. Objective: This lab's objective is to exercise you with the use of Java’s control statements. You are required to use exactly one while statement, one for statement and one switch statement. You will also practice on how to use some basic input methods of the Scanner class and some formatting techniques...

  • Write a Java program to meet the following requirements: 1. Prompt the user to enter three...

    Write a Java program to meet the following requirements: 1. Prompt the user to enter three strings by using nextLine(). Space can be part of the string). ( Note: your program requires using loop: for-loop, while-loop or do-while-loop to get the input String ) 2. Write a method with an input variable (string type).The method should return the number of lowercase letters of the input variable. 3. Get the number of the lowercase letters for each user input string by...

  • In Java Main method Your main program will prompt the user for a test name and...

    In Java Main method Your main program will prompt the user for a test name and the number of scores the user will enter. After creating an arraylist to hold the scores, prompt the user for all the scores to hold in the arraylist. After all the scores are entered, then repeat back the score & letter grade. GradeBook Object Create an instance of a GradeBook object and pass the test name and arraylist to set the instance variables. At...

  • You will write three static methods to manipulate an input String in different ways using various...

    You will write three static methods to manipulate an input String in different ways using various String methods. You need to provide the code for each of the three static methods in class StringPlay (requirements for each listed below). You will also change the control statement in the test harness to allow for variations in the sentinel value. You need to modify the loop control condition in Lab09.java so that user inputs of ‘finish’, “FINISH”, “FiniSH”, “fINISH”, etc. will end...

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