Commit 178073fd authored by michael lundquist's avatar michael lundquist
Browse files

Adding method shells from the assignment

parent 9679b5ee
Pipeline #4110 failed with stage
in 1 minute and 21 seconds
...@@ -25,10 +25,12 @@ public class Application { ...@@ -25,10 +25,12 @@ public class Application {
} }
/** /**
* validates the brackets of a char[] * validates the brackets of a mathematical expression
* *
* @param input a char[] to validate * @param input a char[] math expression to validate
* @return true if it's valid, else false
*/ */
//TODO: test this
public static boolean isValid(char[] input) { public static boolean isValid(char[] input) {
Stack s = new Stack(); Stack s = new Stack();
......
...@@ -82,4 +82,25 @@ public class ArrayQueue { ...@@ -82,4 +82,25 @@ public class ArrayQueue {
} }
} }
/**
* Inserts the specified element into this queue if it is possible to do
* so immediately without violating capacity restrictions. When using a
* capacity-restricted queue, this method is generally preferable to
* add(Object), which can fail to insert an element only by throwing
* an exception
*
* @param e the element to add
* @return true if the elementwas added to this queue, else false
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this queue
* @throws NullPointerException if the specified element is null and this queue does not permit null
* elements
* @throws IllegalArgumentException -if some property of this element
* prevents it from being added to this queue
*/
//TODO write this
public boolean offer(Object e){
return false;
}
} }
\ No newline at end of file
...@@ -21,6 +21,31 @@ public class ArrayStack { ...@@ -21,6 +21,31 @@ public class ArrayStack {
data = new Object[initialCapacity]; data = new Object[initialCapacity];
} }
/**
* Returns the 1-based position where an object is on this stack. If the
* object o occurs as an item in this stack, this method returns the
* distance from the top of the stack of the occurrence nearest the top
* of the stack; the topmost item on the stack is considered to beat
* distance 1. The equals method is used to compare o to the items in
* this stack.
*
* @param o the desired object
* @return the 1-based position from the top of the stack where the
* object is located; the return value -1 indicates that the object
* is not on the stack.
* @author Michael Lundquist
*/
//TODO
public int search(Object o){
int distance = 1;
//search for the element and increment distance
if(distance <= data.size()){
return distance;
}else{
return -1;
}
}
public void ensureCapacity(int minimumCapacity){ public void ensureCapacity(int minimumCapacity){
Object[] biggerArray; Object[] biggerArray;
......
...@@ -2,6 +2,7 @@ package com.ds.fifthassignment; ...@@ -2,6 +2,7 @@ package com.ds.fifthassignment;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.NoSuchElementException;
public class LinkedListQueue implements Iterable{ public class LinkedListQueue implements Iterable{
//JAVA-LinkedList implementation of Queue. //JAVA-LinkedList implementation of Queue.
...@@ -39,6 +40,17 @@ public class LinkedListQueue implements Iterable{ ...@@ -39,6 +40,17 @@ public class LinkedListQueue implements Iterable{
} }
/**
* Retrieves, but does not remove, the head of this queue.
* This method throws an exception if this queue is empty.
*
* @return the head of this queue
* @throws NoSuchElementException if the queue is empty
*/
//TODO make sure this works.
public Object element() throws NoSuchElementException{
return head;
}
public boolean isEmpty() { public boolean isEmpty() {
return ( head==null ? true : false); return ( head==null ? true : false);
......
...@@ -5,14 +5,16 @@ import java.util.LinkedList; ...@@ -5,14 +5,16 @@ import java.util.LinkedList;
import javax.management.RuntimeErrorException; import javax.management.RuntimeErrorException;
import java.util.EmptyStackException;
public class LinkedListStack implements Iterable{ public class LinkedListStack implements Iterable{
//JAVA-LinkedList implementation of Stack. //JAVA-LinkedList implementation of Stack.
private LinkedList data; private LinkedList data;
private Object top; private Object top;
public LinkedListStack() { public LinkedListStack() {
data= new LinkedList(); data = new LinkedList();
top=null; top = null;
} }
public void push(Object x) { public void push(Object x) {
...@@ -24,15 +26,55 @@ public class LinkedListStack implements Iterable{ ...@@ -24,15 +26,55 @@ public class LinkedListStack implements Iterable{
if(data.isEmpty()) { if(data.isEmpty()) {
throw new RuntimeException("StackUnderflow!"); throw new RuntimeException("StackUnderflow!");
} }
else { Object out = top; else {
Object out = top;
data.removeLast(); data.removeLast();
if(!data.isEmpty()) top = data.getLast(); if(!data.isEmpty()) top = data.getLast();
else top = null; else top = null;
return out; return out;
} }
}
/**
* Looks at the object at the top of this stack without
* removing it from the stack
*
* @return the object at the top of the stack
* @author Michael Lundquist
* @throws EmptyStackException if the stack is empty
*/
//TODO make sure this works
public Object peek() throws EmptyStackException{
if(top == null){
throw new EmptyStackException();
}
return top;
} }
/**
* Returns the 1-based position where an object is on this stack. If the
* object o occurs as an item in this stack, this method returns the
* distance from the top of the stack of the occurrence nearest the top
* of the stack; the topmost item on the stack is considered to beat
* distance 1. The equals method is used to compare o to the items in
* this stack.
*
* @param o the desired object
* @return the 1-based position from the top of the stack where the
* object is located; the return value -1 indicates that the object
* is not on the stack.
* @author Michael Lundquist
*/
//TODO make sure this works
public int search(Object o){
int distance = 1;
//search for the element and increment distance
if(distance <= data.size()){
return distance;
}else{
return -1;
}
}
public boolean isEmpty() { public boolean isEmpty() {
return ( top==null ? true : false); return ( top==null ? true : false);
...@@ -41,6 +83,4 @@ public class LinkedListStack implements Iterable{ ...@@ -41,6 +83,4 @@ public class LinkedListStack implements Iterable{
public Iterator iterator() { public Iterator iterator() {
return data.iterator(); return data.iterator();
} }
} }
\ No newline at end of file
package com.ds.fifthassignment; package com.ds.fifthassignment;
import java.util.NoSuchElementException;
//linkedlist implementation of Queue developed from scratch. //linkedlist implementation of Queue developed from scratch.
public class Queue { public class Queue {
...@@ -27,6 +29,18 @@ public class Queue { ...@@ -27,6 +29,18 @@ public class Queue {
} }
} }
/**
* Retrieves, but does not remove, the head of this queue.
* This method throws an exception if this queue is empty.
*
* @return the head of this queue
* @throws NoSuchElementException if the queue is empty
*/
//TODO make sure this works
public Object element() throws NoSuchElementException{
return head;
}
public Object remove(){ public Object remove(){
if(this.isEmpty()) return null; if(this.isEmpty()) return null;
......
package com.ds.fifthassignment; package com.ds.fifthassignment;
import java.util.EmptyStackException;
//linkedlist implementation of Stack developed from scratch. //linkedlist implementation of Stack developed from scratch.
/**
* Taken from class
*
* @author Michael Lundquist G00737340
* email: mlundqu2@masonlive.gmu.edu
*/
public class Stack { public class Stack {
private Node top; private Node top;
...@@ -24,6 +33,46 @@ public class Stack { ...@@ -24,6 +33,46 @@ public class Stack {
} }
} }
/**
* Looks at the object at the top of
* this stack without removing it from the stack
*
* @return the object at the top of the stack
* @author Michael Lundquist
* @throws EmptyStackException if the stack is empty
*/
//TODO make sure this work
public Object peek() throws EmptyStackException{
if(top == null){
throw new EmptyStackException();
}
return top.getData();
}
/**
* Returns the 1-based position where an object is on this stack. If the
* object o occurs as an item in this stack, this method returns the
* distance from the top of the stack of the occurrence nearest the top
* of the stack; the topmost item on the stack is considered to beat
* distance 1. The equals method is used to compare o to the items in
* this stack.
*
* @param o the desired object
* @return the 1-based position from the top of the stack where the
* object is located; the return value -1 indicates that the object
* is not on the stack.
* @author Michael Lundquist
*/
//TODO
public int search(Object o){
int distance = 1;
//search for the element and increment distance
if(distance <= data.size()){
return distance;
}else{
return -1;
}
}
public Object pop(){ public Object pop(){
if(this.isEmpty()) return -1; if(this.isEmpty()) return -1;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment