Tag » Object Oriented Programming

Issue Priority Tokens - Application of Priority Queue

PriorityToken.java

public class PriorityToken {
	
	private long[] tokenArray;
	private int numberOfTokens;
	private int arraySize;
	
	public PriorityToken(int arraySize) {
		tokenArray = new long;
		numberOfTokens = 0;
		this.arraySize = arraySize;
	}
	
	public void addToken(long newToken) {
		int j = 0;
		
		if (numberOfTokens == arraySize) {
			System.out.println("Token Array is full, cannot add more Tokens");
		}
		else if (numberOfTokens == 0) {
			tokenArray = newToken;
		}
		else {
			for (j = numberOfTokens - 1; j >= 0; j--) {
				if (newToken > tokenArray[j]) {
					tokenArray = tokenArray[j]; // shift to create space for the new token
				}
				else {
					break;
				}
			}
			tokenArray = newToken; // now add the new token
			numberOfTokens++;
		}
	}
	
	public long peek() {		
		if (numberOfTokens > 0) {
			return tokenArray;
		}
		return 0;
	}
	
	public boolean isFull() {
		if (numberOfTokens == arraySize) {
			return true;
		}
		return false;
	}
	
	public boolean isEmpty() {
		if (numberOfTokens == 0) {
			return true;
		}
		return false;
	}
	
	public long removeToken() {
		return tokenArray[--numberOfTokens];
	}
	
	public void displayTokens() {
		for (int i = 0; i < numberOfTokens; i++) {
			System.out.println(tokenArray[i]);
		}
	}
}
… 170 more words
Arrays

Using interface in C#.Net Part 1

Interface in C#:

  • Interface defines the signature of function or properties. It does not include implementation.
  • The class who implements the interface, it must implements all the members of that interface.
  • 74 more words
Object Oriented Programming

Convert an expression in Infix to Postfix

InfixStack.java

public class InfixStack {
	
	private char[] stackArray;
	private int top;
	private int stackSize;
	
	public InfixStack(int maxSize) {
		stackSize = maxSize;
		stackArray = new char;
		top = -1;
	}
	
	public void push(char ch) {
		if (top == stackSize) {
			System.out.println("Stack is full, cannot add more elements");
		}
		else {
			stackArray[++top] = ch;
		}
	}
	
	public char pop() {
		char ch = ' ';
		if (top == -1) {
			System.out.println("Stack is empty");
			return ch;
		}
		return stackArray;		
	}
	
	public boolean isFull() {
		if (top == stackSize) {
			return true;
		}
		return false;
	}
	
	public boolean isEmpty() {
		if (top == -1) {
			return true;
		}
		return false;
	}
	
	public char peek() {
		return stackArray;
	}
}
… 201 more words
Arrays

Is scala pure object-oriented programming language??

Most of the people have question “Is scala pure object oriented programming language??” if yes then what about functions and primitives.
yes scala is pure object oriented language and functions and primitives are also objects in scala. 324 more words

Shower Idea: Keyboards and Object-Oriented Design

You know that hypnotic thinking that happens in the shower? I do that outside of the shower. I’m not just staring at the wall. That’s the story of why I’m calling this a shower idea (even though I didn’t think of this in the shower). 672 more words

Programming

Example for Inserting an element to an array

InsertName.java


public class InsertName {
	
	private String[] nameArray;
	private int numberOfElements;
	private int arraySize;
	
	public InsertName(int arraySize) {
		nameArray = new String;
		numberOfElements = 0;
		this.arraySize = arraySize;
	}
		
	public void insertElement(String newItem) {
		if (numberOfElements < this.arraySize) {
			nameArray = newItem;
		}
		else {
			System.out.println("The array is full, cannot add new elements.");
		}
	}
	
	public void doInsertionSort(String newName) {
		
		int i,j;
		String temp;
		
		this.nameArray = newName;
		for (i = 1; i < this.numberOfElements; i++) {
			j = i;
			temp = nameArray[i];
			while (j > 0 && (nameArray.compareTo(temp) > 0)) {
				nameArray[j] = nameArray;
				j--;
			}
			nameArray[j] = temp;
		}
	}
	
	public void displayArray() {
		for (int i = 0; i < this.numberOfElements; i++) {
			System.out.println(this.nameArray[i]);
		}
	}
	
	public static void main(String[] args) {
		
		InsertName in = new InsertName(11);
		
		in.insertElement("Cat");
		in.insertElement("Buffalo");
		in.insertElement("Camel");
		in.insertElement("Deer");
		in.insertElement("Lion");
		in.insertElement("Elephant");
		in.insertElement("Giraffe");
		in.insertElement("Rhino");
		in.insertElement("Hippo");
		in.insertElement("Tiger");
		
		in.displayArray();
		System.out.println(" ");
		in.doInsertionSort("Crocodile");
		in.displayArray();
	}
	

}
Arrays

An example for Priority Queue

PriorityQueue.java

/*
 * Demo of Pririty Queue
 */
public class PriorityQueue {
	
	private long[] priorityQueueArray;
	private int numberOfElements;
	private int queueMaxSize;
	
	public PriorityQueue(int arraySize) {
		priorityQueueArray = new long;
		numberOfElements = 0;
		queueMaxSize = arraySize;
	}
	
	public void insertItem(long queueElement) {
		
		int j = 0;
		
		if (numberOfElements == queueMaxSize) {
			System.out.println("Cannot add "+queueElement+", Queue is full.");
		}
		else if (numberOfElements == 0) {
			priorityQueueArray = queueElement;			
		}
		else {
			for (j = numberOfElements - 1; j >= 0; j--) {
				if (queueElement < priorityQueueArray[j]) {
					priorityQueueArray = priorityQueueArray[j];
				}
				else {
					break;
				}
			}
			priorityQueueArray = queueElement;
			numberOfElements++;
		}
	}
	
	public void displayPriorityQueue() {
		for (int  i = 0; i <= numberOfElements-1; i++ ) {
			System.out.println(priorityQueueArray[i]);
		}
	}
}
</code]
<p>PriorityQueueApp.java</p>
1

public class PriorityQueueApp {

	public static void main(String[] args) {
		
		try {
			PriorityQueue pq = new PriorityQueue(6);
			
			pq.insertItem(10);
			pq.insertItem(30);
			pq.insertItem(33);
			pq.insertItem(1);
			pq.insertItem(80);
			pq.insertItem(54);
			pq.insertItem(39);
			
			System.out.println(" ");
			System.out.println("Displaying Queue Items....");
			pq.displayPriorityQueue();
		}
		catch(Exception e) {
			System.out.println("An Exception in main()... ");
		}
	}
}
Arrays