WSP 1: Test program (2)

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

public class MyArrayList<E>

{

  private int size; 

  private E[] data;

  private int MAXELEMENTS = 100;

  public MyArrayList() {

               data = (E[])new Object[MAXELEMENTS];

       size = 0; 

  }

 

  public int getMAXELEMENTS(){

              return MAXELEMENTS;

  }

             

   

  

  public boolean checkSpace()

  {

              if (size+1<MAXELEMENTS)

                          return true;

              else

                          return false;

  }

 

  public void add(int index, E e) {  

    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] = 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");

              while (i<this.getSize() && j<param.getSize())

              {

                          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++;

                          }

              }

              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; 

              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;                

  }

 

}

 

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();

            }

}

WSP 1: Test program (1)
Create a test class containing a main() method - Call it Watersort
• The test program first should create single bottle
(StackAsArraylist).
• Create objects of the character glass called red, green and blue.
• Use the following type of static variables:
stooik Cherocter reda new Character
• Put ink in the bottles (Push character objects onto the stack)
• Test the getStockSize() and CheckStackUniform() methods
thoroughly.
WSP 1: Test program (2)
If you succeeded you are now ready to add more bottles
• Carefully think about the game. Can you see that each bottle is
one stack. We need 5 bottles. What type of data structure would
you require when you need S objects of the same class?
WSP 1: Test program (2)
If you succeeded you are now ready to add more bottles
• Carefully think about the game. Can you see that each bottle is
one stack. We need 5 bottles. What type of data structure would
you require when you need 5 objects of the same class?
* You can either use and array or a linked list. In this case the
direct access of an array is much easier to use. So you need an
array of 5 bottles. [(Call the array bottles). We are using the
simple built-in array of Java NOT our own MyArraytist class. [We
want direct access without using accessors and mutators.]
Transcribed Image Text:WSP 1: Test program (1) Create a test class containing a main() method - Call it Watersort • The test program first should create single bottle (StackAsArraylist). • Create objects of the character glass called red, green and blue. • Use the following type of static variables: stooik Cherocter reda new Character • Put ink in the bottles (Push character objects onto the stack) • Test the getStockSize() and CheckStackUniform() methods thoroughly. WSP 1: Test program (2) If you succeeded you are now ready to add more bottles • Carefully think about the game. Can you see that each bottle is one stack. We need 5 bottles. What type of data structure would you require when you need S objects of the same class? WSP 1: Test program (2) If you succeeded you are now ready to add more bottles • Carefully think about the game. Can you see that each bottle is one stack. We need 5 bottles. What type of data structure would you require when you need 5 objects of the same class? * You can either use and array or a linked list. In this case the direct access of an array is much easier to use. So you need an array of 5 bottles. [(Call the array bottles). We are using the simple built-in array of Java NOT our own MyArraytist class. [We want direct access without using accessors and mutators.]
Watersort Introduction (1)
PDF - 1 MB
Water Sort Puzzle (WSP) Part 1
• In Part 1 we do mostly preparation: we are extending the MyArraylist
class developed in SU3 and the StackAsArray class of SUS. You need
to master the Stacks as Array (SUS Session 2) before you start.
• You also need to master the Water Sort Puzzie game (Android, Apple
or Microsoft app stores). Play at least the first 20 levels before you
start.
WSP 1: Modifications (1) UUI
Make the following Additions in the MyArraylist class:
Mease use the piven naming convertiora
We wi mep the code far Pn 1 end ye an ta ver rblero befare sartng wih Periz
MAIN IDEA:
In order to check if a game is completed one needs to check f all the colours in a
bottle are the same. generic version of this method should be added to the
MyArraylist class called
• public boolean checkuniform()
The method should return true if all the fled entries are identical.
• Make sure you have a accessor for the instance variable called:
* pubic int getsioel)
WSP 1: Modifications (2)
Make the following Additions to the StackAsMyArrayList class
• As you know you may only use a few actions of a stack: Push and Pop but
there is another common one called Peek. Peek returns the value of the top
element without removing it.
• You need to create a Peek method:
* public E peek (Make sure you understond the difference between Peek and Pop)
• We are going to add 2 non-typical stack methods (just to make this cool game
work!
public int getstacksine which cals the getsiel) method of the Myaraylist dass
*public booinan checkStackinform) which calls the checkinform) method o
Myarraylist dass
HINT: The toString0 of the stack class calls the tostring() of the MyArr.
class
Transcribed Image Text:Watersort Introduction (1) PDF - 1 MB Water Sort Puzzle (WSP) Part 1 • In Part 1 we do mostly preparation: we are extending the MyArraylist class developed in SU3 and the StackAsArray class of SUS. You need to master the Stacks as Array (SUS Session 2) before you start. • You also need to master the Water Sort Puzzie game (Android, Apple or Microsoft app stores). Play at least the first 20 levels before you start. WSP 1: Modifications (1) UUI Make the following Additions in the MyArraylist class: Mease use the piven naming convertiora We wi mep the code far Pn 1 end ye an ta ver rblero befare sartng wih Periz MAIN IDEA: In order to check if a game is completed one needs to check f all the colours in a bottle are the same. generic version of this method should be added to the MyArraylist class called • public boolean checkuniform() The method should return true if all the fled entries are identical. • Make sure you have a accessor for the instance variable called: * pubic int getsioel) WSP 1: Modifications (2) Make the following Additions to the StackAsMyArrayList class • As you know you may only use a few actions of a stack: Push and Pop but there is another common one called Peek. Peek returns the value of the top element without removing it. • You need to create a Peek method: * public E peek (Make sure you understond the difference between Peek and Pop) • We are going to add 2 non-typical stack methods (just to make this cool game work! public int getstacksine which cals the getsiel) method of the Myaraylist dass *public booinan checkStackinform) which calls the checkinform) method o Myarraylist dass HINT: The toString0 of the stack class calls the tostring() of the MyArr. class
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY