Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Java program to demonstrate the use of inheritance and polymorphism. This program will also use the Vector class and a few enumerations. ConfectionType.java

Write a Java program to demonstrate the use of inheritance and polymorphism. This program will also use the Vector class and a few enumerations.

ConfectionType.java

public enum ConfectionType { FrozenYogurt, Gelato, HardPack, Sorbet; } 

Flavor.java

public enum Flavor { Vanilla, Chocolate, Strawberry, CrumbsAlongTheMohawk, BlackRaspberry, CakeBatter, Eggnog, CottonCandy, CookieDough; } 

Toppings.java

public enum Toppings { ChocolateSprinkles, HotFudge, Caramel, MarshmallowCreme } 

ConeType.java

public enum ConeType { Cake, Waffle, Sugar, Pretzel, GlutenFree; } 

IceCream.java

public interface IceCream { public void add(Confection c); public void add(Toppings t); } 

A general confection is comprised of a type and a flavor and is as follows. You need to create the constructor and toString() method.

Confection

Template for the Confection class.

Confection.java

public class Confection { private ConfectionType type; private Flavor flavor; // ... } 

**** Next you will create types of ice cream orders you could place at an ice cream stand. Each type of order has rules about what it may contain.

*Cone

Template for the Cone class.

Cone.java

public class Cone implements IceCream { private ConeType type; private Confection content = null; // ... } 

*Dish

Template for the Dish class.

Dish.java

import java.util.Vector; public class Dish implements IceCream { protected Vector scoops; // ... } 

*Sundae

Template for the Sundae class.

Sundae.java

import java.util.Vector; public class Sundae extends Dish{ private Vector toppings; // ... } 

A main test.java source code is provided here to test your classes:

test.java

import java.util.Vector; public class test { public static int rand(int i) { return (int)(Math.random() * i); } public static void main(String[] args) { // Take note of the Vector type! Vector order = new Vector<>(); // This is just like Coins and PaperCurrency ConfectionType[] types = ConfectionType.values(); Flavor[] flavors = Flavor.values(); Toppings[] toppings = Toppings.values(); int x; // Code to test the Cone Class Cone cone = new Cone(ConeType.Waffle); for (x = 0; x < 2 ; x++) cone.add(new Confection(types[rand(types.length)], flavors[rand(flavors.length)])); cone.add(Toppings.ChocolateSprinkles); order.add(cone); // Code to test the Dish Class Dish dish = new Dish(); for (x = 0; x < 3 ; x++) dish.add(new Confection(types[rand(types.length)], flavors[rand(flavors.length)])); dish.add(Toppings.MarshmallowCreme); order.add(dish); // Code to test the Sundae Class Sundae sundae = new Sundae(); for (x = 0; x < 6 ; x++) sundae.add(new Confection(types[rand(types.length)], flavors[rand(flavors.length)])); for (x = 0; x < 4 ; x++) sundae.add(toppings[rand(types.length)]); order.add(sundae); /* Print the contents - each object has its own toString(). This includes the objects filled with other objects. */ System.out.println(" The order contains:"); for (IceCream i : order) System.out.println(i); } } 

A sample run using the test.java code would look like:

Cone already has Sorbet(Chocolate) A Cone may not have toppings. A Dish may only have 2 scoops maximum. A Dish cannot have toppings. Perhaps you want a Sundae? A Sundae may only have 5 scoops maximum. A Sundae may only have 3 toppings maximum. The order contains: Waffle cone with Sorbet(Chocolate) Dish with 2 scoops:Gelato(Vanilla) Gelato(CottonCandy) Sundae with 5 scoops:FrozenYogurt(CottonCandy) FrozenYogurt(CrumbsAlongTheMohawk) HardPack(BlackRaspberry) Sorbet(Chocolate) FrozenYogurt(BlackRaspberry) and 3 toppings: HotFudge Caramel MarshmallowCreme 

Pay very close attention to the output. It will guide you regarding the rules of how many scoops and toppings each type may have.

Submit the assigenmet files as Cone.java, Dish.java and Sundae.java. You do not need to submit the enumerations provided unless you have changed them.

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

Advanced Database Systems

Authors: Carlo Zaniolo, Stefano Ceri, Christos Faloutsos, Richard T. Snodgrass, V.S. Subrahmanian, Roberto Zicari

1st Edition

155860443X, 978-1558604438

More Books

Students also viewed these Databases questions

Question

=+organizations and employees differently.

Answered: 1 week ago