Solved

Printing (or storing)  boolean value of an object

Posted on 2014-12-25
5
147 Views
Last Modified: 2014-12-27
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:

System.out.println(cv.compareBinToHex(binary, hex));

Open in new window


Now, the question is how to make it like this

cv.compareBinToHex(binary, hex);
System.out.println(cv.someMethod());

Open in new window


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

Open in new window

0
Comment
Question by:KPax
  • 2
  • 2
5 Comments
 

Expert Comment

by:greenearth
ID: 40517972
package com.test;

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, hex);
        System.out.println(cv.isVal());

        // 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 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(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;
    }
}
0
 

Expert Comment

by:greenearth
ID: 40517973
I have posted the solution above by encapsulating your boolean value in setter/getters
0
 

Author Comment

by:KPax
ID: 40518196
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?
0
 
LVL 32

Accepted Solution

by:
awking00 earned 500 total points
ID: 40519739
boolean compHex2Bin = cv.compareBinToHex(binary, hex);
System.out.println(compHex@Bin); ==> prints either true or false
0
 

Author Closing Comment

by:KPax
ID: 40519843
Excellent, so simple yet so true!

Please just note that it should be:
 
System.out.println(compHex2Bin);  // ==> prints either true or false

not

System.out.println(compHex@Bin);  // ==> prints either true or false

compHex@Bin is probably typo, i guess :)
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java.io.BufferedReader   readLine()  //returns the entire file ? 4 73
stackato and cloud 4 87
map related example 6 49
servlet example 17 33
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question