Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Flower class with two instance variables: String name and double cost . Flower has a single constructor that takes a String and a

Write a Flower class with two instance variables: String name and double cost. Flower has a single constructor that takes a String and a double parameter and sets the instance variables from them. Flower also has getter methods for both instance variables, but does not have any setter methods. Finally, Flower has a public String toString() method that returns a String containing the name of the flower and its cost.

Flower will be a non-public class in the same input file as the next class Florist, below.

Copy the FlowerCounter program as the base for a class Florist that computes the cost of flowers sold at a flower stand.

As in FlowerCounter there are 5 kinds of flowers, petunia, pansy, rose, violet, and carnation, which cost, respectively, 50, 75, \$1.50, 50, and 80 per flower.

Florist creates an array flowers of Flower objects containing this information; use 5 assignment statements to create the objects. ThenFlorist should print those objects in a for or for-each loop.

Your program should read the name of a flower and the quantity (number of flowers) desired by a customer in a loop; when the user types quit, exit the loop. Locate the flower in the flowers array using each Flower objects getName() method, and use that objects getCost() method to obtain the cost per flower. At the end, print the number of flowers bought and the total cost of the sale (you have to accumulate this information in the loop).

Hints: use the same process as in the Flower Counter homework to read in the name of the flower, and, if the user did not type quit, compare their flowers name against those in the array be sure to deal with the case where the user wants to buy a flower whose name doesnt exist in the flowers array, and if so print the same message as in Flower Counter.

IN JAVA;

/* DO NOT MARK THIS CLASS PUBLIC! */

class Flower

{

// a simple class to represent a flower to sell

/* declare the instance variables, name and cost, below this comment: */

/* write a simple constructor for the Flower class that sets these variables */

/* write getters for the name and cost below this comment: */

// finally, write a toString method that produces this output:

// name: , cost: $

/* write that toString method below this comment */

}

public class Florist

{

// Compute the cost of flowers sold at a flower stand

/* first, copy the FlowerCounter main method into Florist here: */

// at the top of the main method, create a Flower array flowers and

// put the 5 flowers from the FlowerCounter class into that array,

// filling in the names and costs of its five individual flowers;

// replace the name and cost arrays with that Flower array

/* your code to do this goes below this comment: */

// everythng else in Florist is the same as in FlowerCounter,

// except that in order to determine if the flower name the user

// enters is one the florist sells you have to use the getName

// getter method on each of the Flower array elements; if the

// name matches, pull out its cost using the getCost method for

// that same Flower array element, otherwise print the error message

// again, if the user types quit end the loop and print the required

// summary information

/* make the required modification(s) in the loop as required above */

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Is this investment worthwhile? Why or why not?

Answered: 1 week ago