Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: stack

  1. #1

    Default stack


    Any one of u here knows how to convert decimal to binary,decimal to octal,decimal to hexa using stack.I don't have any idea at all

    Decimal Value: 85
    Octal Value: 125
    Hexadecimal Value: 55
    Binary Value: 1010101
    Last edited by skeptic_rob; 08-23-2010 at 04:13 PM.

  2. #2

  3. #3
    java codes stack bro man ako pasabot bro

  4. #4
    'toinkz! la mn u ngtel gud...hehehe
    Java Convert Binary to Decimal,Binary to Decimal

  5. #5
    'stack! huh! miagi ko ana C prog, but nw la njud ko krmember ana...

    'if naa n u idea s stack, den kbalo na u unsaon ang code s conversion process,
    'then mix it all... kaya mu yan bro.... hehehe

  6. #6
    Stack is a Data Structure that has a LIFO rule (Last In First Out). In C/C++ Programming, Stacks can be implemented using Array or Pointers. Since in Java, pointers are not supported, the only way you can implement Stacks is through Arrays.

    But what intrigues me regarding the (assignment)requirement is that, it involves number system conversion. I don't know how it relates to Stack? Clearly one can convert a value from one number system to another without having to use Stacks.

    Code Snippets
    Code:
    int decimalValue = 10;
    String strBinValue = Integer.toBinaryString(decimalValue);
    String strOctalValue = Integer.toOctalString(decimalValue);
    String strHexValue = Integer.toHexString(decimalValue);
    so tell me, why Stack is needed here?

    Source:
    Integer: toBinaryString(int intValue) : Integerjava.langJava by API
    Integer: toHexString(int intValue) : Integerjava.langJava by API
    Integer: toOctalString(int intValue) : Integerjava.langJava by API

  7. #7
    import java.util.*;
    public class Balance{
    public Boolean isBalance(String expr){
    Stack stack = new Stack();

    boolean result= false;
    int expr[] =new int [10];

    for(int x =0; x < expr.length();x++){
    if(expr.charAt(x) == '(')
    stack.push(new Character(expr.charAt(x)));
    else{
    if(stack.isEmpty()!= true)
    stack.pop();
    else
    return false;
    }
    }

    if(stack.isEmpty()==true)
    result = true;
    else
    result =false;


    return result;

    }
    }

  8. #8
    main
    ====

    public class testBalances{
    public static void main(String[]args){
    String str = "(((())))";
    Balances bal = new Balances();

    if(bal.isBalances(str)==true)
    System.out.println("OK");
    else
    System.out.println("Not Ok");
    }

    }
    Last edited by skeptic_rob; 08-28-2010 at 10:57 AM.

  9. #9
    .\Balances.java:7: expr is already defined in isBalances(java.lang.String)
    int expr[] =new int [10];
    ^
    .\Balances.java:9: cannot find symbol
    symbol : method length()
    location: class int[]
    for(int x =0; x < expr.length();x++){
    ^
    .\Balances.java:10: cannot find symbol
    symbol : method charAt(int)
    location: class int[]
    if(expr.charAt(x) == '(')
    ^
    .\Balances.java:11: cannot find symbol
    symbol : method charAt(int)
    location: class int[]
    stack.push(new Character(expr.charAt(x)));
    ^
    .\Balances.java:11: internal error; cannot instantiate Character(char) at java.lang.Character to ()
    stack.push(new Character(expr.charAt(x)));
    ^
    Note: .\Balances.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    5 errors
    ---------------------------------------------------------
    mao ni mo gawas nga error bro sa java textpad compiler
    --- i need ur help mga bro.....

  10. #10
    sa main bro kay imong gigamit nga method is isBalances() unya ang naa sa Balances class kay isBalance(). then ang variable name nga expr kay kaduha nimo gigamit, 1st as a string parameter sa isBalance(), 2nd kay as an int array sulod sa isBalance().

    steps to resolve ani kay...

    1. correct the isBalance() naming issues. use the exact name sa method nga naa sa Balances class.
    2. remove the expr int array kay wala man siya actual gamit.
    3. instead of

    Code:
    Stack stack = new Stack();
    kinghanlan i-specify jud ang type:

    Code:
    Stack <String> stack = new Stack <String> ();
    actually ok ra na kung jdk 1.4 ang gamiton, pero since pag-release sa jdk 1.5, kinghanlan na i-specify ang stack type for safety reasons.

    here's my code with the appropriate fixes:

    Balances.java
    Code:
    import java.util.Stack;
    
    public class Balances
    {
    	public Boolean isBalance(String expr)
    	{
    		Stack <String> stack = new Stack <String> ();
    
    		boolean result = false;
    
    		for(int x = 0; x < expr.length(); x++)
    		{
    			if(expr.charAt(x) == '(')
    			{
    				stack.push("(");
    			}
    			else
    			{
    				if(stack.isEmpty() != true)
    				{
    					stack.pop();
    				}
    				else
    				{
    					return false;
    				}
    			}
    		}
    
    		if(stack.isEmpty() == true)
    		{
    			result = true;
    		}
    		else
    		{
    			result = false;
    		}
    
    		return result;
    	}
    }
    testBalances.java
    Code:
    public class testBalances
    {
    	public static void main(String[] args)
    	{
    		String str = "(((())))";
    		Balances bal = new Balances();
    
    		if(bal.isBalance(str) == true)
    		{
    			System.out.println("OK");
    		}
    		else
    		{
    			System.out.println("Not Ok");
    		}
    	}
    }

  11.    Advertisement

Page 1 of 2 12 LastLast

Similar Threads

 
  1. Looking For: Speed STacks
    By samanthapooh in forum Collectibles, Memorabilias & Hobbies
    Replies: 9
    Last Post: 12-23-2009, 10:51 PM
  2. Lf: Speed stacks
    By aldincio in forum Hobbies & Crafts
    Replies: 2
    Last Post: 12-10-2009, 03:56 AM
  3. Stacks: Help!!!
    By Thelastairbender in forum Programming
    Replies: 6
    Last Post: 09-08-2009, 09:17 AM
  4. Unbelievable Dice Stacking!
    By chelseacathy in forum Music & Radio
    Replies: 0
    Last Post: 12-23-2008, 01:12 AM
  5. For Sale: AM2 CPU Stack Cooler Set
    By bahiista in forum Computers & Accessories
    Replies: 0
    Last Post: 11-03-2008, 12:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top