Results 1 to 5 of 5
- 10-09-2012, 03:39 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 3
- Rep Power
- 0
In your lab05, replace instructor’s Tokenizer class and MyStackQueue package with you
Objective:
The objective of this lab is to get you some experience in processing strings character by
character and in implementing stacks and queues in a class package.
The programming assignment:
In your lab05, replace instructor’s Tokenizer class and MyStackQueue package with your own.
Requirements:
1. You must use an array to implement your queue.
2. You must use a linked list to implement your stack.
3. You must use the following frame work to implement your tokenizer.
class Tokenizer {
private char [] Buf;
private int cur;
Tokenizer(String infixExpression) {
Buf = infixExpression.toCharArray();
cur = 0;
}
Token nextToken() {
1. Skip blanks.
2. if (cur>=Buf.length) return null;
3. If the next character is a digit, keep reading until a non-digit is read.
Convert the string of digits into an integer.
String Digits = new String(Buf, start, len);
int num = Integer.valueOf(Digits).intValue();
Create and return an operand.
4. Otherwise, use the next character to create and return an operator.
}
}
class Tokenizer {
private char[] Buf;
private int cur;
Tokenizer(String infixExpression) {
Buf = infixExpression.toCharArray();
cur = 0;
}
Token nextToken() {
int bufLength = Buf.length;
Object obj = null;
// ignore blank space
while (cur < bufLength && Buf[cur] == ' ') {
cur++;
}
// if given string having only space return null
if (cur >= Buf.length)
return null;
StringBuilder value = new StringBuilder();
// Iterate through each element of string array and construct an string
// for consecutive digits
while (cur < bufLength && Buf[cur] <= '9' && Buf[cur] >= '0') {
value.append(Buf[cur]);
cur++;
}
// if digits are there convert all digits as an integer value and create
// operand
if (value.length() > 0) {
obj = new Operand(Integer.parseInt(value.toString()));
}
// if at cur position no digit is present then create operand with the
// same non digit value
else {
obj = new Operator(Buf[cur]);
cur++;
}
return ((Token) (obj));
}
}
package StackAndQueue;
public class Queue {
private int front;
private int rear;
private int capacity;
private Object S[];
public Queue() {
front = 0;
rear = -1;
capacity = 100;
S = new Object[capacity];
}
public boolean isEmpty() {
return S[front] == null;
}
public void enqueue(Object obj) {
int insertionPoint = (rear + 1) % capacity;
if (S[insertionPoint] == null) {
S[insertionPoint] = obj;
rear = insertionPoint;
} else {
System.out.println("Queue capacity is full");
}
}
public Object dequeue() {
if (S[front] != null) {
Object obj = S[front];
S[front] = null;
front = (front + 1) % capacity;
return obj;
} else {
System.out.println("Queue is empty");
return null;
}
}
public String toString() {
StringBuilder state = new StringBuilder("[");
for (int i = front; i < capacity; i++) {
if (S[i] != null) {
state.append(S[i] + ", ");
}
}
for (int i = 0; i < front; i++) {
if (S[i] != null) {
state.append(S[i] + ", ");
}
}
if (state.length() > 1) {
state.delete(state.length() - 2, state.length() - 1);
}
state.append("]");
return state.toString();
}
}
package StackAndQueue;
import java.util.LinkedList;
import java.util.List;
public class Stack {
List<Object> linkedList = null;
public Stack() {
linkedList = new LinkedList<Object>();
}
public boolean isEmpty() {
return linkedList.size() == 0;
}
public void push(Object obj) {
linkedList.add(obj);
}
public Object pop() {
int topIndex = linkedList.size() - 1;
if (topIndex >= 0) {
Object obj = linkedList.get(topIndex);
linkedList.remove(topIndex);
return obj;
} else {
return null;
}
}
public Object top() {
int topIndex = linkedList.size() - 1;
if (topIndex >= 0) {
return linkedList.get(topIndex);
} else {
return null;
}
}
}
- 10-09-2012, 03:58 PM #2
Re: In your lab05, replace instructor’s Tokenizer class and MyStackQueue package with
That is absolutely not how this works. Please see the link in my signature on asking questions the smart way before you post again.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 10-09-2012, 04:55 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: In your lab05, replace instructor’s Tokenizer class and MyStackQueue package with
This is the second one from this user.
Please do not ask for code as refusal often offends.
- 10-09-2012, 10:51 PM #4
Member
- Join Date
- Oct 2012
- Location
- Tempe, Arizona
- Posts
- 77
- Blog Entries
- 12
- Rep Power
- 0
Re: In your lab05, replace instructor’s Tokenizer class and MyStackQueue package with
WAY to much code to read without code tags. Around each snippet of code, use code tages; [code] ....code....[/code] This makes it so much easier to read the code.
- 10-10-2012, 12:01 AM #5
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
Re: In your lab05, replace instructor’s Tokenizer class and MyStackQueue package with
Similar Threads
-
Using one class object in other package's class
By vishrut_n_shah in forum New To JavaReplies: 2Last Post: 03-21-2012, 02:09 PM -
Java Instructor - Work from your own loactaion
By timvtc1 in forum Jobs OfferedReplies: 0Last Post: 02-14-2011, 05:21 PM -
Replace ClassLoader with custom class loader in Java Plugin
By pwlodarczak in forum Java AppletsReplies: 6Last Post: 09-20-2010, 07:46 AM -
Learning Tree Instructor Opportunities
By LearningTree in forum Jobs OfferedReplies: 0Last Post: 04-25-2007, 08:33 PM -
JSP Instructor Opportunity
By derrickD in forum Jobs OfferedReplies: 0Last Post: 04-14-2007, 03:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks