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
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.
java codes stack bro man ako pasabot bro
'toinkz! la mn u ngtel gud...hehehe
Java Convert Binary to Decimal,Binary to Decimal
'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
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
so tell me, why Stack is needed here?Code:int decimalValue = 10; String strBinValue = Integer.toBinaryString(decimalValue); String strOctalValue = Integer.toOctalString(decimalValue); String strHexValue = Integer.toHexString(decimalValue);
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
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;
}
}
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.
.\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.....
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
kinghanlan i-specify jud ang type:Code:Stack stack = new Stack();
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.Code:Stack <String> stack = new Stack <String> ();
here's my code with the appropriate fixes:
Balances.java
testBalances.javaCode: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; } }
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"); } } }
Similar Threads |
|