KPax
asked on
Printing (or storing) boolean value of an object
So here is sample code to check if hex and binary numbers are the same, Doesn't really matter in this concept. The point is that the return value is boolean, so object holds it. Task should be accomplished without using separate boolean variable to hold boolean value.
With this statement I can print if returned boolean value for object cv is true or not:
Now, the question is how to make it like this
Where someMethod()) would be preferably some if already exist Java object method to achieve this?
Whole code look like this:
With this statement I can print if returned boolean value for object cv is true or not:
System.out.println(cv.compareBinToHex(binary, hex));
Now, the question is how to make it like this
cv.compareBinToHex(binary, hex);
System.out.println(cv.someMethod());
Where someMethod()) would be preferably some if already exist Java object method to achieve this?
Whole code look like this:
public class CheckValue {
public static void main(String[] args) {
// 158
// 10011110
// 9E
String binary = "10011110";
String hex = "9E";
CheckValue cv = new CheckValue();
System.out.println(cv.compareBinToHex(binary, hex));
// Is there any elegnt way to make this work.
// not only for System.out.println but to later acess if the value was
// true or false
// without sepcially assigning boolan variable to hold this value
// cv.compareBinToHex(binary, hex);
// System.out.println(cv.....somtething .... something....);
}
public boolean compareBinToHex(String binary, String hex) {
int nl = convertToBase(binary, 2);
int n2 = convertToBase(hex, 16);
if (nl < 0 || n2 < 0) {
return false;
} else {
return nl == n2;
}
}
private int convertToBase(String number, int base) {
if (base < 2 || (base > 10 && base != 16))
return -1;
int value = 0;
for (int i = number.length() - 1; i >= 0; i--) {
int digit = digitToValue(number.charAt(i));
if (digit < 0 || digit >= base) {
return -1;
}
int exp = number.length() - 1 - i;
value += digit * Math.pow(base, exp);
}
return value;
}
public int digitToValue(char c) {
if (c >= '0' && c <= '9')
return c - '0';
else if (c >= 'A' && c <= 'F')
return 10 + c - 'A';
else if (c >= 'a' && c <= 'f')
return 10 + c - 'a';
return -1;
}
}
I have posted the solution above by encapsulating your boolean value in setter/getters
ASKER
Yes, I know it could be done through Getters/Setters, but is there any other way to get intrinsic value of and Object, in this case it's true/false return value?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent, so simple yet so true!
Please just note that it should be:
System.out.println(compHex 2Bin); // ==> prints either true or false
not
System.out.println(compHex @Bin); // ==> prints either true or false
compHex@Bin is probably typo, i guess :)
Please just note that it should be:
System.out.println(compHex
not
System.out.println(compHex
compHex@Bin is probably typo, i guess :)
public class CheckValue {
private boolean val;
public boolean isVal() {
return val;
}
public void setVal(boolean val) {
this.val = val;
}
public static void main(String[] args) {
// 158
// 10011110
// 9E
String binary = "10011110";
String hex = "9E";
CheckValue cv = new CheckValue();
cv.compareBinToHex(binary,
System.out.println(cv.isVa
// Is there any elegnt way to make this work.
// not only for System.out.println but to later acess if the value was
// true or false
// without sepcially assigning boolan variable to hold this value
// cv.compareBinToHex(binary,
// System.out.println(cv.....
}
public void compareBinToHex(String binary, String hex) {
int nl = convertToBase(binary, 2);
int n2 = convertToBase(hex, 16);
if (nl < 0 || n2 < 0) {
setVal(false);
} else {
setVal(nl == n2);
}
}
private int convertToBase(String number, int base) {
if (base < 2 || (base > 10 && base != 16))
return -1;
int value = 0;
for (int i = number.length() - 1; i >= 0; i--) {
int digit = digitToValue(number.charAt
if (digit < 0 || digit >= base) {
return -1;
}
int exp = number.length() - 1 - i;
value += digit * Math.pow(base, exp);
}
return value;
}
public int digitToValue(char c) {
if (c >= '0' && c <= '9')
return c - '0';
else if (c >= 'A' && c <= 'F')
return 10 + c - 'A';
else if (c >= 'a' && c <= 'f')
return 10 + c - 'a';
return -1;
}
}