DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 1 of 1
  1. #1
    Join Date
    Nov 2005
    Posts
    10

    circular QUEUE HOW TO PRINT!!

    public class Queues
    {
    static Console c;
    public static void main (String[] args)
    {
    c = new Console ();
    StraightLine[] x = new StraightLine [5];
    x [0] = new StraightLine ();
    x [1] = new StraightonRemove ();
    x [2] = new ShiftOnFull ();
    x [3] = new Circular ();
    char t = 'a';
    int n = 1;
    while (t != 'x')
    {
    c.println ("enter your command! x to exit i to insert d to delete r to reset");
    c.setCursor (10, 1);

    t = c.getChar ();
    if (t == 'i')
    {
    x [0].add (n);
    x [1].add (n);
    x [2].add (n);
    x [3].add (n);
    n++;
    if (n == 100)
    n = 1;
    c.println ("Straight Line!");
    x [0].print (c);
    c.println ("Shift on each Remove");
    x [1].print (c);
    c.println ("Shift on full");
    x [2].print (c);
    c.println ("Circular");
    x [3].print (c);
    }
    else if (t == 'd')
    {
    x [0].remove ();
    c.println ("Straight Line!");
    x [0].print (c);
    x [1].remove ();
    c.println ("Shift on each Remove");
    x [1].print (c);
    x [2].remove ();
    c.println ("Shift on full");
    x [2].print (c);
    x [3].remove ();
    c.println ("Circular");
    x [3].print (c);
    }
    else if (t == 'r')
    {
    c.clear ();
    x [0] = new StraightLine ();
    x [1] = new StraightonRemove ();
    x [2] = new ShiftOnFull ();
    x [3] = new Circular ();
    n = 1;
    }
    }

    }
    }

    /*class StraightLine
    Fields
    x- the integer array
    size- physical size of the array
    F- front position in the array
    B- back position in the array

    Methods
    constructors
    add- adds a given element to the array
    remove- removes element from the array
    empty- returns whether or not the queue is empty
    full- returns whether or not the queue is full
    print- prints the queue array
    */

    class StraightLine
    {
    int[] x;
    int size;
    int F;
    int B;

    public StraightLine (int size)
    {
    F = 0;
    B = 0;
    this.size = size;
    x = new int [size];
    }


    public StraightLine ()
    {
    F = 0;
    B = 0;
    size = 10;
    x = new int [size];
    }


    /* method add
    inserts given data into the array
    parameters: data, integer of data to be inserted
    returns:boolean, true if data was successfully added, false if not
    */
    public boolean add (int data)
    {
    if (!full ())
    {
    x [B] = data;
    B++;
    return true;
    }
    else
    return false;
    }


    /* method remove
    removes an element from the array
    parameters:none
    returns:integer of the element value that was removed
    */

    public int remove ()
    {
    int result = 99999999;
    if (!empty ())
    {
    result = x [F];
    F++;
    }
    return result;
    }


    /* method empty
    returns whether the array is empty
    parameters:none
    returns: boolean, true if array is empty, false if not

    */
    public boolean empty ()
    {
    return F == B;
    }


    /* method full
    returns whether or not the array is full
    parameters:none
    returns: boolean, true if the array if full,false if not
    */
    public boolean full ()
    {
    return B == size;
    }


    /* method print
    prints the array
    parameters:Console c
    returns:none(void)
    */
    public void print (Console c)
    {
    String result = "";
    StringBuffer b = new StringBuffer (" ");
    DecimalFormat f = new DecimalFormat ("00");
    int spot = 0;
    for (int i = 0 ; i < F ; i++)
    result += "-- ";
    for (int i = F ; i < B ; i++)
    result += f.format (x [i]) + " ";
    for (int i = B ; i < size ; i++)
    result += "-- ";
    c.println (result);
    b.setCharAt (F * 3, 'F');
    b.setCharAt ((B * 3) + 1, 'B');
    c.print (b.toString ());
    if (full ())
    c.print ("(Full)");
    if (empty ())
    c.print ("(Empty)");
    c.println ("");
    }
    }

    /* class StraightonRemove
    Fields-
    None(inherited only)

    Methods
    constructors
    add- adds a given integer to the array given its value
    remove- to remove element from the queue
    print- prints the queue
    full- returns whether or not the Queue is full
    empty- returns whether or not the Queue is empty
    */

    class StraightonRemove extends StraightLine
    {
    public StraightonRemove (int size)
    {
    super (size);
    if (size < 0)
    size = 10;
    B = 0;
    F = 0;
    this.size = size;
    x = new int [size];
    }


    public StraightonRemove ()
    {
    super (10);
    size = 10;
    B = 0;
    F = 0;
    this.size = size;
    x = new int [size];
    }


    /* method remove
    toremove element from the queue
    parameters:none
    returns:integer,the value of the element that was removed */
    public int remove ()
    {
    boolean more = false;
    int result;
    if (!empty ())
    {
    B--;
    int i = F;
    while (i < B)
    {
    x [i] = x [i + 1];
    i++;
    }
    result = x [B];
    }
    else
    result = 99999999;
    return result;
    }
    }


    /* class ShiftOnFull
    Fields
    none(only inherited)

    Methods
    constructors
    remove- removes from the Queue
    print- prints the Queue
    reallyFull- returns whether or not the array is really full
    */

    class ShiftOnFull extends StraightLine
    {
    public ShiftOnFull (int size)
    {
    super (size);
    B = 0;
    F = 0;
    if (size < 0)
    size = 10;
    this.size = size;
    x = new int [size];
    }


    public ShiftOnFull ()
    {
    B = 0;
    F = 0;
    size = 10;
    this.size = size;
    x = new int [size];
    }


    /* method reallyFull
    returns whether or not the Queue is really full
    parameters:none
    returns: boolean, true if Queue is really full, false if not
    */
    public boolean full ()
    {
    return F == 0 && super.full ();
    }


    public boolean add (int data)
    {

    if (full ())
    return false;
    else
    {
    if (F != 0 && B == size)
    {
    int i = F;
    int pos = 0;

    while (i < B)
    {
    x [pos++] = x [i++];
    System.out.println ("did the moving");
    }
    B = B - F;
    x [B] = data;
    B++;
    F = 0;

    }
    else if (!super.full ())
    {
    super.add (data);
    return true;
    }
    return false;
    }


    }
    }

    class Circular extends StraightLine
    {
    public Circular (int size)
    {
    super (size);
    if (size < 0)
    size = 10;
    this.size = size;
    F = 0;
    B = 0;
    x = new int [size];
    }


    public Circular ()
    {
    size = 10;
    this.size = size;
    F = 0;
    B = 0;
    x = new int [size];
    }


    public boolean full ()
    {
    return B + 1 == F;
    }


    public boolean add (int data)
    {
    if (!full ())
    {
    if (B != F - 1)
    x [B] = data;
    if (B == size - 1)
    B = (B + 1) % size;
    else
    B++;
    return true;
    }
    else
    return false;
    }


    public int remove ()
    {
    int result = 99999999;
    if (!empty ())
    {
    result = x [F];
    F++;
    }
    return result;
    }

    public void print (Console c)
    {
    String result = "";
    StringBuffer b = new StringBuffer (" ");
    DecimalFormat f = new DecimalFormat ("00");
    int spot = 0;
    for (int i = 0 ; i < F ; i++)
    result += "-- ";
    for (int i = F ; i < B ; i++)
    result += f.format (x [i]) + " ";
    for (int i = B ; i < size ; i++)
    result += "-- ";
    c.println (result);
    b.setCharAt (F * 3, 'F');
    b.setCharAt ((B * 3) + 1, 'B');
    c.print (b.toString ());
    if (full ())
    c.print ("(Full)");
    if (empty ())
    c.print ("(Empty)");
    c.println ("");
    }
    /*

    */

    }
    Last edited by xsouldeath; 12-13-2005 at 08:07 PM.

Similar Threads

  1. quick basic codes conversion
    By oranged in forum VB Classic
    Replies: 2
    Last Post: 07-19-2006, 06:50 AM
  2. MAJOR PROBLEM!!!
    By Hamish in forum VB Classic
    Replies: 3
    Last Post: 10-31-2002, 04:19 PM
  3. Print Job properties - can I change them?
    By Dave Goerlich in forum VB Classic
    Replies: 4
    Last Post: 07-31-2001, 11:54 AM
  4. Stored Procedure (Want looping in Cursor)
    By malikmehmood in forum Database
    Replies: 0
    Last Post: 11-20-2000, 05:12 AM
  5. Replies: 2
    Last Post: 03-24-2000, 01:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links