class WaterSort {             Character top = null;             // create constants for colors             static Character red= new Character('r');             static Character blue = new Character('b');             static Character yellow= new Character('g');             // Bottles declaration                                     public static void showAll( StackAsMyArrayList bottles[])             {                        for (int i = 0; i<=4; i++)                         {                                     System.out.println("Bottle "+ i+ ": " + bottles[i]);                         }             }                 public static void main(String args[])     {                        //part 1                        //create the bottle                         StackAsMyArrayList bottleONE = new StackAsMyArrayList();             System.out.println("\nbottleONE:"+ bottleONE+ " size:" + bottleONE.getStackSize()+" uniform? "+ bottleONE.checkStackUniform());                         bottleONE.push(red);                         bottleONE.push(red);                         bottleONE.push(red);             System.out.println("\nbottleONE:"+bottleONE+ " size:" + bottleONE.getStackSize()+" uniform? "+ bottleONE.checkStackUniform());                         bottleONE.push(blue);             System.out.println("\nbottleONE:"+bottleONE+ " size:" + bottleONE.getStackSize()+" uniform? "+ bottleONE.checkStackUniform());                        bottleONE.pop();                        bottleONE.pop();             System.out.println("\nbottleONE:"+bottleONE+ " size:" + bottleONE.getStackSize()+" uniform? "+ bottleONE.checkStackUniform());                        // Bottles declaration                                               // Part 2                        // Bottles declaration                         StackAsMyArrayList bottles[] = new StackAsMyArrayList[5];                         //You can do this with a for also                         bottles[0] = new StackAsMyArrayList();                         bottles[1] = new StackAsMyArrayList();                         bottles[2] = new StackAsMyArrayList();                         bottles[3] = new StackAsMyArrayList();                         bottles[4] = new StackAsMyArrayList();                                                  // Fill 3 bottles with specific colors                         // need not be uniform colours - just easy to do it with a for                         for (int i = 0; i<4; i++)                         {                                     bottles[0].push(red);                                     bottles[1].push(blue);                                     bottles[2].push(yellow);                                     bottles[3].push(yellow);                                     bottles[4].push(blue);                         }                         // show all bottles                         showAll(bottles);       } } From this program , mix up ink , start with five empty bottles,  use random  number generator to fill bottles one slot at a time with the colors while  keeping  8 slots empty. There is a total of 5×4 =20 slots ,so if we fill 12 slots 8 slots  are free.

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

 

class WaterSort {

            Character top = null;

            // create constants for colors

            static Character red= new Character('r');

            static Character blue = new Character('b');

            static Character yellow= new Character('g');

            // Bottles declaration

           

           

            public static void showAll( StackAsMyArrayList bottles[])

            {

                       for (int i = 0; i<=4; i++)

                        {

                                    System.out.println("Bottle "+ i+ ": " + bottles[i]);

                        }

            }

           

    public static void main(String args[])

    {

                       //part 1

                       //create the bottle

                        StackAsMyArrayList bottleONE = new StackAsMyArrayList<Character>();

            System.out.println("\nbottleONE:"+ bottleONE+ " size:" + bottleONE.getStackSize()+" uniform? "+ bottleONE.checkStackUniform());

                        bottleONE.push(red);

                        bottleONE.push(red);

                        bottleONE.push(red);

            System.out.println("\nbottleONE:"+bottleONE+ " size:" + bottleONE.getStackSize()+" uniform? "+ bottleONE.checkStackUniform());

                        bottleONE.push(blue);

            System.out.println("\nbottleONE:"+bottleONE+ " size:" + bottleONE.getStackSize()+" uniform? "+ bottleONE.checkStackUniform());

                       bottleONE.pop();

                       bottleONE.pop();

            System.out.println("\nbottleONE:"+bottleONE+ " size:" + bottleONE.getStackSize()+" uniform? "+ bottleONE.checkStackUniform());

                       // Bottles declaration

                      

                       // Part 2

                       // Bottles declaration

                        StackAsMyArrayList bottles[] = new StackAsMyArrayList[5];

                        //You can do this with a for also

                        bottles[0] = new StackAsMyArrayList<Character>();

                        bottles[1] = new StackAsMyArrayList<Character>();

                        bottles[2] = new StackAsMyArrayList<Character>();

                        bottles[3] = new StackAsMyArrayList<Character>();

                        bottles[4] = new StackAsMyArrayList<Character>();

                        

                        // Fill 3 bottles with specific colors

                        // need not be uniform colours - just easy to do it with a for

                        for (int i = 0; i<4; i++)

                        {

                                    bottles[0].push(red);

                                    bottles[1].push(blue);

                                    bottles[2].push(yellow);

                                    bottles[3].push(yellow);

                                    bottles[4].push(blue);

                        }

                        // show all bottles

                        showAll(bottles);

 

    }

}

From this program , mix up ink , start with five empty bottles,  use random  number generator to fill bottles one slot at a time with the colors while  keeping  8 slots empty. There is a total of 5×4 =20 slots ,so if we fill 12 slots 8 slots  are free.

 

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Random Class and its operations
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