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 TestDessert's 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.  Write the operation for each step under its comment and submit the file as a response    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 list = new 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       }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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 TestDessert's 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.

 Write the operation for each step under its comment and submit the file as a response 

 

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<Dessert> list = new ArrayList<Dessert>();

           

            // 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

      }

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 6 images

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education