Question

In Java with comments. Write a program that computes the cost of flowers sold at a...

In Java with comments.

Write a program that computes the cost of flowers sold at a flower stand. Five kinds of flowers—petunia, pansy, rose, violet, and carnation— are stocked and cost, respectively, 50¢, 75¢, $1.50, 50¢, and 80¢ per flower. Create an array of strings that holds the names of these flowers. Create another array that holds the cost of each corresponding flower. Your program should read the name of a flower and the quantity desired by a customer. Locate the flower in the name array and use that index to find the cost per stem in the cost array. Compute and print the total cost of the sale.

Expected output: "The cost of 10 pansys is $7.5" or "The cost of 6 roses is $9.0" or "The cost of 2 violets is $1.0"

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

Program:

import java.util.*;

class ComputeCost
{
public static void main(String args[])
{
String flowers[]={"petunia", "pansy", "rose", "violet", "carnation"};
double cost[]={50,75,1.50,50,80};
String nameFlower;
int quantity;
double totalCost;
Scanner input=new Scanner(System.in);


System.out.println("\nThe Five Flowers and their costs: ");
System.out.println("\nFlowers\t\tCost");
System.out.println("-------------------------------");


for(int i=0;i<flowers.length;i++)
{
if(i==4)
{
System.out.println(flowers[i]+"\t"+cost[i]);
}
else
{
System.out.println(flowers[i]+"\t\t"+cost[i]);
}
}

System.out.println("\n");
System.out.print("Enter the name of a flower: ");
nameFlower=input.next();

int index=-1;

for(int i=0;i<flowers.length;i++)
{
if(flowers[i].equalsIgnoreCase(nameFlower))
{
index=i;
}
}
if(index==-1)
{
System.out.println("\n"+nameFlower+" is not in the list!!\n");
}
else
{

System.out.print("\nEnter the quantity: ");
quantity=input.nextInt();

if(index==2) //The cost of rose is in dollar
{
totalCost=quantity * cost[index];
System.out.println("\nThe cost of "+quantity+" "+nameFlower+"s is $"+totalCost+"\n");
}
else //Cost is in Cents
{
totalCost=quantity * cost[index];

System.out.println("\nThe cost of "+quantity+" "+nameFlower+"s is $"+totalCost/100+"\n");
}
}
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
In Java with comments. Write a program that computes the cost of flowers sold at a...
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
  • Write a program in a class SandwichCounter that computes the cost of sandwiches sold at a...

    Write a program in a class SandwichCounter that computes the cost of sandwiches sold at a food stand. Three kinds of sandwiches —cheese steak, chicken, and salad— are cost, respectively, $8.5, $7.5, and $7.0 per sandwich. Create an array of strings that holds the names of these sandwiches. Create another array that holds the cost of each corresponding sandwich. Your program should read the name of a sandwich and the quantity desired by a customer. Locate the sandwich in the...

  • JAVA PROGRAM Caitlyn's Crafty Creations computes a retail price for each product as the cost of...

    JAVA PROGRAM Caitlyn's Crafty Creations computes a retail price for each product as the cost of materials plus $14 multiplied by the number of hours of work required to create the product, plus $6 shipping and handling. Caitlyn’s Crafty Creations is having a semi-annual sale now, and everything is 25% off before shipping and handling: Retail price = 0.75 * (cost of materials + (14 * hours of work)) + 6 Create a class that contains a main () method...

  • THIS MUST BE DONE ON VISUAL STUDIO Write a C# program that computes a the cost...

    THIS MUST BE DONE ON VISUAL STUDIO Write a C# program that computes a the cost of donuts at your local Rich Horton’s store. The cost, of course, depends on the number of donuts you purchase. The following table gives the break down: Donuts Purchased Cost per Donut ($) = 15 0.75 A customer can save on the HST (13%) if they buy 12 or more donuts so only charge HST (use a constant) on the purchase if the customer...

  • NOTE: All C++ programs that you write must have comments at the top with the program...

    NOTE: All C++ programs that you write must have comments at the top with the program name, your name, and the pseudocode describing what the program will do. 1. Create a new program that will calculate the total cost for software purchased from a store. Each software package costs $99.00, but discounts are given on the total cost, based on the number of packages purchased. a. Ask the user for the number of packages they want to buy b. Calculate...

  • (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates...

    (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates for an upcoming election. This program needs to allow the user to enter candidates and then record votes as they come in and then calculate results and determine the winner. This program will have three classes: Candidate, Results and ElectionApp Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are...

  • Write a program in java. You are tasked with writing an application that will keep track...

    Write a program in java. You are tasked with writing an application that will keep track of Insurance Policies. Create a Policy class called InsurancePolicies.java that will model an insurance policy for a single person. Use the following guidelines to create the Policy class: • An insurance policy has the following attributes: o Policy Number o Provider Name o Policyholder’s First Name o Policyholder’s Last Name o Policyholder’s Age o Policyholder’s Smoking Status (will be “smoker” or “non-smoker”) o Policyholder’s...

  • For this c++ assignment, Overview write a program that will process two sets of numeric information....

    For this c++ assignment, Overview write a program that will process two sets of numeric information. The information will be needed for later processing, so it will be stored in two arrays that will be displayed, sorted, and displayed (again). One set of numeric information will be read from a file while the other will be randomly generated. The arrays that will be used in the assignment should be declared to hold a maximum of 50 double or float elements....

  • Please Write the following program in c# You are to write an application which will create...

    Please Write the following program in c# You are to write an application which will create a company, add agents (their name, ID, and weekly sales) to that company, and printout the details of all agents in the company. The application will contain three classes: Agent.cs, Company.cs, and CompanyTest.cs (this class is already provided). Flow of the application: The CompanyTest class creates an object of the Company class and reserves space for five agents. At this point if you call...

  • For this lab you will write a Java program that plays the dice game High-Low. In...

    For this lab you will write a Java program that plays the dice game High-Low. In this game a player places a bet on whether the sum of two dice will come up High (totaling 8 or higher), Low (totaling 6 or less) or Sevens (totaling exactly 7). If the player wins, they receive a payout based on the schedule given in the table below: Choice Payout ------ ------ High 1 x Wager Low 1 x Wager Sevens 4 x...

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