Results 1 to 5 of 5
  1. #1
    duster is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default 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;
    }
    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default 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!

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default 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.

  4. #4
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default 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.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

Similar Threads

  1. Using one class object in other package's class
    By vishrut_n_shah in forum New To Java
    Replies: 2
    Last Post: 03-21-2012, 02:09 PM
  2. Java Instructor - Work from your own loactaion
    By timvtc1 in forum Jobs Offered
    Replies: 0
    Last Post: 02-14-2011, 05:21 PM
  3. Replies: 6
    Last Post: 09-20-2010, 07:46 AM
  4. Learning Tree Instructor Opportunities
    By LearningTree in forum Jobs Offered
    Replies: 0
    Last Post: 04-25-2007, 08:33 PM
  5. JSP Instructor Opportunity
    By derrickD in forum Jobs Offered
    Replies: 0
    Last Post: 04-14-2007, 03:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •