Page 1 of 1

Fruit Baskets Sam has a small business whereby she sells fruit baskets that can be ordered for delivery to family and fr

Posted: Tue Jul 12, 2022 8:12 am
by answerhappygod
Fruit Baskets
Sam has a small business whereby she sells fruit baskets thatcan be ordered for delivery to family and friends. Your job is tocreate an application that simulates part of Sam's business.
Unfortunately, at the moment Sam's ability to offer variety isseverely limited. She can only put 3 fruit items in her baskets(each basket contains exactly 3 pieces of fruit). She can also swapthe pieces of fruit out for new types. However, it's an`all-or-nothing' operation. If a customer decides to change thetype of fruit, they must change all three pieces.
Create two object classes and one driver class for thisassignment. Write a Java class Fruit that stores a name and priceper unit.
Write a second Java class FruitBasket that holds 3 Fruitobjects. The total price for a FruitBasket is total of the unitprice for each of the three Fruit objects, plus a base price of$10.00 that applies to all fruit baskets.
You should develop two constructors for the FruitBasketclass.
ThefirstconstructorforFruitBasketshouldacceptthreeparameters:3Fruitobjects.
Thesecondconstructorshouldacceptsixparameters:thenamesandpricesof3fruit. This constructor creates the relevant Fruit objects as partof creating a FruitBasket.
FruitBasket has a getTotalPrice( ) method, which adds togetherthe base price of the basket with the unit price of each fruit.
FruitBasket has a format( ) method that lists the names of thefruit in this FruitBasket in a sentence suitable for displaying tothe user. Do not include price information.
FruitBasket has a toString( ) method that displays the objectinformation in our standard format.
FruitBasket has a swapFruit( ) method that takes 3 Fruit objectsas parameters and replaces the fruit in the basket with these newFruit objects.
Write a driver class TestFruitBasket that creates 2 FruitBasketobjects and prints out the details and total price of each. Makesure you test both FruitBasket constructors. That is, create oneFruitBasket object with the first constructor and the other objectwith the second constructor.
Have your driver class get the names and unit prices of fruitfrom the user.
Have your driver class test the toString( ) method forFruitBasket. Hint: you will also need a toString( ) method forFruit. This is similar to how the toString( ) methods for Circleand Point are written, as described in Module 5. Make sure youconform to the toString() result formats shown in the sample outputprovided below.
Have your driver class test the swapFruit( ) method. You can“hard-code” the Fruit objects in the main method. That is, you donot have to gather more information from the user for thistest.
Display the fruit names and cost of this basket before and afterthe fruit has been swapped.
Note: Later in the term we will learn how to create lists ofobjects, but for now you will need to create 3 separate Fruitinstance variables in your FruitBasket class.
Helpful Hint: You will need to create quite a few methods foryour FruitBasket class, so it is important that you practiceincremental development. Write one method at a time in your Fruitand FruitBasket classes. Each time you do, add a relevant test toyour main method. Compile, test and debug that code before youattempt to move on to the next method. This will save you plenty oftime in the long run.
Sample Output: The following is an example of what the outputfrom the driver program might resemble. Use different test valueswhen testing your own program.
---------- Make your first fruit basket ---------- Enter thename of the first fruitorangeEnter the
unit price for orange name of the second fruit unit price forapple
0.75Enter theappleEnter the0.25Enter the name of the third fruitpineappleEnter the price for pineapple4.25The first fruit basket:This fruit basket contains a(n) orange, a(n) apple and a(n)pineappleThe total price for the first fruit basket is $15.25 TestingtoString()FruitBasket[f1=Fruit[name=orange, price=0.75], f2=Fruit[name=apple,price=0.25], f3=Fruit[name=pineapple, price=4.25]]
---------- Make a second fruit basket---------- Enter the nameof the first fruitmangoEnter the unit price for mango
2.50Enter the name of the second fruitbananaEnter the unit price for banana1.25Enter the name of the third fruitmelonEnter the unit price for melon6.00The second fruit basket:This fruit basket contains a(n) mango, a(n) banana and a(n)melonThe total cost for the second fruit basket is $19.75After swapping out the fruit in the second basket:This fruit basket contains a(n) strawberry, a(n) blueberry and a(n)papayaThe total cost for the second fruit basket is$27.650000000000002