ANSWER TO ABOVE QUESTION
package com.company;
import java.util.Scanner;
class My_Array_List<E>
{
private int size; // Number of elements in the list
private E[] data;
private int maximum_elements = 100;
/** Create an empty list */
public My_Array_List() {
data = (E[])new Object[maximum_elements];// cannot create array of generics
size = 0; // Number of elements in the list
}
public boolean checkUniform()
{ if(size==0){return true;}
E temp=this.data[0];
for (int i = 1; i < size; i++)
{ if (!temp.equals(data[i])) {return false;}
}
return true;
}
public int getMAXELEMENTS(){
return maximum_elements;
}
public boolean checkSpace()
{
if (size+1<maximum_elements)
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);
// Move the elements to the right after the specified index
for (int i = size - 1; i >= index; i--)
data[i + 1] = data[i];
// Insert new element to data[index]
data[index] = e;
// Increase size by 1
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];
// Shift data to the left
for (int j = index; j < size - 1; j++)
data[j] = data[j + 1];
data[size - 1] = null; // This element is now null
// Decrement size
size--;
return e;
}
public void clear()
{
size = 0;
}
public My_Array_List<E> merge(My_Array_List<E> param)
{
int i=0; //counter in calling array
int j=0; // counter in param array
int k=0; // counter in return array
My_Array_List<E> returnArray = new My_Array_List();
if (this.getSize() ==0) // same as if (size==0)
return param;
if (param.getSize()==0)
return this;
if ((this.getSize()+ param.getSize()) > maximum_elements)
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;
}
}
class StackAsMyArrayList<E>
{
My_Array_List<E> theStack;
public StackAsMyArrayList()
{ theStack = new My_Array_List<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 boolean checkStackUniform()
{
return theStack.checkUniform();
}
public int getStackSize()
{
return theStack.getSize();
}
public String toString()
{
return theStack.toString();
}
}//end class
public class Watersort{
static Character red= new Character('r');
static Character green= new Character('g');
static Character blue= new Character('b');
// as we dont create obj of water sort thats why we are using it as static
//declaring a arrayu of objects of class StackAsMyArrayList;
static StackAsMyArrayList[] bottle;
//n will strore the number of bottles
static int n;
//this function is to print all the botlle ink
static public void ShowAll()
{
System.out.println();
System.out.println("Printing all the bottles");
for(int i=0;i<n;i++)
{
System.out.println(bottle[i].toString());
}
}
public static void main(String []args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of bottle you want to fill");
n=sc.nextInt();
// making an array of obj of size n
bottle=new StackAsMyArrayList[n];
for(int i=1;i<=n;i++)
{ System.out.println("-----------------------------------------");
System.out.println("filling bottle "+ i);
bottle[i-1]=new StackAsMyArrayList();
char temp;
int k;
System.out.println("Enter the number of water glass you want to add");
k=sc.nextInt();
while(k>0)
{
System.out.println("Color of water glass you want to add r/g/b :");
temp = sc.next().charAt(0);
//using switch
switch(temp)
{ case 'r':
bottle[i-1].push(red);
break;
case 'g':
bottle[i-1].push(green);
break;
case 'b':
bottle[i-1].push(blue);
break;
}
//showing no of glass added
System.out.println("no of glass added " + bottle[i-1].getStackSize());
//Checking that bottle is uniform or not . if it is uniform then we show it as true else false
System.out.println ("Checking that bottle is uniform or not :: " + bottle[i-1].checkStackUniform());
System.out.println(bottle[i-1].toString());
System.out.println();
k--;
}
}
// function to print all the bottle color
ShowAll();
}
}
Comments
Leave a comment