The aim of Part 2 is to set up the puzzle for the player to solve in Part 3.

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

WATERSORT PROGRAM - JAVA

•The aim of Part 2 is to set up the puzzle for the player to solve in Part 3.

IMPORTANT NOTE: We are not going to start with 2 empty bottles, but rather with 8 empty slots spread over the 5 bottles as explained in the intro video.

Start with 5 empty bottles. Use a random number generator to fill bottles one slot at a time with the colours while keeping 8 slots empty – there is a total of 5*4 = 20 slots – so if we will 12 slots, 8 are free.

•Advantage: Easy to create a puzzle with a good mix

•Disadvantage: With more colours and bottles and only 2 open bottles for more advanced versions of the game, the result might not be solvable.

Start with three sorted bottles. In the strategy the idea is to load three bottles with uniform colour and then move ink around for a number of moves until the bottles are mixed up.

•Advantage: Result is always solvable since the bottles are created in a reversed-game strategy.

•Disadvantage: It is hard to develop an algorithm which will reach the bottoms of the bottles. We tried this by moving on item from every bottle in rounds. But it still took more than 100 moves to obtain a good mix in the bottles.

•You need to extend your WaterSort program to implement one of these strategies. It is a good idea to play around with both – please use Random numbers in both cases.

•Important: To add ink to a bottle (strategy 1) or to move ink from one bottle to another (strategy 2) or you need to use the stack operators.

•You need to ensure that you are not spilling ink! (now you understand why you needed the getSize()) method in Part 1.

•You only need to submit one of the two strategies. Your program should have output of 5 scrambled bottles.

public class StackAsMyArrayList<E>

{

MyArrayList<E> theStack; public StackAsMyArrayList()

{ theStack = new MyArrayList<E>();

}

public void push(E newElement) //insert at end of array!

{

if (!theStack.checkSpace()) throw new IndexOutOfBoundsException ("Stack out of bounds"); theStack.add(theStack.getSize(),newElement);

}

public E pop() //remove end of array { E temp = null; boolean isDone = false; if (theStack.getSize() > 0) temp=theStack.remove(theStack.getSize()-1); return temp; // temp will be null in special case of empty list } public String toString()

{

return theStack.toString(); } }//end class public class MyArrayList<E>

{

private int size; // Number of elements in the list private E[] data; private int MAXELEMENTS = 100; /** Create an empty list */ public MyArrayList()

{

data = (E[])new Object[MAXELEMENTS];// cannot create array of generics size = 0; // Number of elements in the list

}

public int getMAXELEMENTS()

{

return MAXELEMENTS; } public boolean checkSpace()

{

if (size+1<MAXELEMENTS) return true; else return false;

}

public void add(int index, E e)

{

// Ensure the index is in the right range

if (index < 0 || index > size)

throw new IndexOutOfBoundsException ("Index: " + index + ", Size: " + size); 

for (int i = size - 1; i >= index; i--) data[i + 1] = data[i];

data[index] data[index] = e;

size++;

}

public boolean contains(Object e)

{

for (int i = 0; i < size; i++) if (e.equals(data[i])) return true; return false;

}

public E get(int index)

{

if (index < 0 || index >= size)

throw new IndexOutOfBoundsException ("Index: " + index + ", Size: " + size); return data[index]; } public E remove(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException ("Index: " + index + ", Size: " + size); E e = data[index];

 

for (int j = index; j < size - 1; j++) data[j] = data[j + 1]; data[size - 1] = null;

size--; return e; } public void clear()

{ size = 0;

}

public MyArrayList<E> merge(MyArrayList<E> param)

{

int i=0; 

 int j=0;

int k=0;

MyArrayList<E> returnArray = new MyArrayList(); if (this.getSize() ==0)

return param;

if (param.getSize()==0) return this; if ((this.getSize()+ param.getSize()) > MAXELEMENTS) throw new IndexOutOfBoundsException ("Combined list out of bounds"); // traverse both list until one list is completely done while (i<this.getSize() && j<param.getSize()) { // Compare single value from each list and copy smallest into result if (((Comparable)data[i]).compareTo(param.data[j]) <0) { returnArray.data[k]= this.data[i]; k++; i++; } else { returnArray.data[k]=param.data[j]; k++; j++; } } // copy remainder of the array if (i < this.getSize()) { for (i=i;i<getSize();i++) //for starts at current position { returnArray.data[k]= this.data[i]; k++; } } if (j < param.getSize()) { for (j=j;j<param.getSize();j++) { returnArray.data[k]=param.data[j]; k++; } } returnArray.size = k; // set size of return array return returnArray; } public String toString() { String result="["; for (int i = 0; i < size; i++) { result+= data[i]; if (i < size - 1) result+=", "; } return result.toString() + "]"; } public int getSize() { return size; } public boolean sortList() { E hold; for (int i = 0; i < size-1; i++) { for (int j = 0; j<size-1; j++) { if(((Comparable)data[j]).compareTo(data[j+1])>0) { hold= data[j+1]; data[j+1]=data[j]; data[j]=hold; } } } return true; } }

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Time complexity
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