Question
For this discussion assignment, we will be working with the ArrayList data structure. To demonstrate working with ArrayLists, we will be working with four source
For this discussion assignment, we will be working with the ArrayList data structure. To demonstrate working with ArrayLists, we will be working with four source files: a Dessert class, two classes that extend Dessert called IceCream and Cake, and a TestDessert class. The TestDessert class contains a main() method that declares an ArrayList to hold Dessert objects. This ArrayList is referenced by the variable named "list".
Examine TestDesserts main() method and notice the four comments. You are to implement code that accomplishes the tasks described in each step.
For step 1, you are to populate the ArrayList with 10 IceCream and Cake objects. These objects should be inserted into the ArrayList at random. This means each run of the program should produce an ArrayList with different proportions of IceCream and Cake objects. After this operation, the ArrayList should contain 10 total objects (IceCream and Cake objects). Display the ArrayList after the operation.
For step 2, if the first and last dessert in the ArrayList are different (one is IceCream and the other is Cake), make the last dessert the same as the first dessert. Display the ArrayList after the operation.
For step 3, at every other index (0, 2, 4, etc.), if the dessert is not a Cake object, make it a Cake object. Display the ArrayList after the operation.
For step 4, remove all the cake objects from the ArrayList. Display the ArrayList after the operation.
Your TestDessert class with the required steps must compile. Therefore, you will also need to copy the Dessert, IceCream, and Cake files to your computer so TestDessert can work with them.
Copy the code for the TestDessert class (starting with the import java.util.*; statement) and paste it into a new file named LastName_FirstName_Discussion5.java (note that you will need to change the name of the class from TestDessert to the name of the file itself, once you rename it). Write the operation for each step under its comment and submit the file as a response to this discussion.
public class Dessert { }
public class IceCream extends Dessert {
public String toString() {
return "Ice Cream"; } }
public class Cake extends Dessert {
public String toString() {
return "Cake"; } }
import java.util.*;
public class TestDessert {
public static void main(String[] args) {
ArrayList
// Step 1: populate list with a total of 10 IceCream
// and Cake objects at random indexes
// Step 2: If the first and last desserts are different,
// make the last dessert the same as the first dessert
// Step 3: At every other index, if the dessert is not
// cake, make it cake
// Step 4: Remove all cakes
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started