Skip to content

Programming by Design

If you're not prepared to be wrong, you'll never come up with anything original. – Sir Ken Robinson

  • About
  • Java-PbD
  • C-PbD
  • ASM-PbD
  • Algorithms
  • Other

CISS-111 Project 4

Posted on September 7, 2020March 13, 2025 By William Jojo
CISS-111-Project
CISS-111 Project 4

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


Learning outcomes

  • Working with enumerations.
  • Working with inheritance and polymorphism.
  • Designing subclasses.
  • Overriding inherited methods.
  • Confirmation program produces desired results.

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 (scoop of ice cream, if you will) is comprised of a type and a flavor and is as follows. You need to create the constructor and toString() method.

Confection

This will be implemented by a single constructor that accepts both the type and flavor as parameters.

Confection.java
public class Confection {

    private ConfectionType type;
    private Flavor flavor;

    // ...
}

Template for the Confection class.


Important Note!
When working on Cone, Dish and Sundae, do not be concerned with accessors and mutators. Focus on the constructors, toString() and the add() methods required by the interface IceCream.

Next you will create classes for the 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

Cone.java
public class Cone implements IceCream {

    // RULES: only one scoop and no toppings

    private ConeType type;
    private Confection content = null;

    // ...
}

Template for the Cone class.

Dish

Dish.java
import java.util.Vector;

public class Dish implements IceCream {

    // RULES: Up to 2 scoops and no toppings.

    protected Vector<Confection> scoops;

    // ...
}

Template for the Dish class.

Sundae

The Sundae is an extension of Dish, therefore we inherit the scoops from Dish.

Sundae.java
import java.util.Vector;

public class Sundae extends Dish{

    // RULES: Up to 5 scoops and up to 3 toppings.

    private Vector<Toppings> toppings;

    // ...
}

Template for the Sundae class.


You may change the enumeration contents to suit your taste – if you want to add PopRocks to the Toppings, have at it. If you feel that Eggnog ice cream is disgusting (ironic, but okay), substitute another or change the whole palette – make a Stewart’s or Ben and Jerry’s hit list.

However, you are NOT allowed to change the order types or the rules governing them. So, do not modify the code in Project4.java, below.

The Project 4 main method source code is provided here to test your classes (you should not need to change this!):

Project4.java
import java.util.Vector;

public class Project4 {

    public static int rand(int i) {
        return (int)(Math.random() * i);
    }

    public static void main(String[] args) {

        // Take note of the Vector type!
        Vector<IceCream> 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(toppings.length)]);
        order.add(sundae);

        /*
        Print the contents - each object has its own toString().
        This includes the objects filled with other objects.
         */
        System.out.println("\nThe order contains:");
        for (IceCream i : order)
            System.out.println(i);
    }
}

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

Cone already has Sorbet(Chocolate) (1 scoop only.)
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 project to the Learning Management System as Confection.java, Cone.java, Dish.java and Sundae.java. You need not submit the enumerations provided unless you have changed them. I am always looking for new flavors to try!

Post navigation

❮ Previous Post: Overview
Next Post: Assembly Language With NASM ❯

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Copyright © 2018 – 2025 Programming by Design.