Results 1 to 6 of 6
  1. #1
    fasck is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default in need of help (easy)

    The problem is easy to solve, i cant figure it out myself tho...:D

    i got a table with boolean, it got two vars p and q (p = false q = true)

    I want to make the true and the false 0 and 1 instead of p and q

    p and q is the var for the true false.

    Anyways, i know it has something to do with casting, conversion or the if to get.

    the conversion should be boolean to int right? kinda lost...

    Help is greatly appreciated

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You can't cast: boolean values don't have any intrinsic numerical value or vice versa.

    "conversion" is an extremely vague term. Certainly you want to start with false and end up with zero, so you could say the false value is changed or converted. But this just begs the question: how?

    You are left with using an if statement. Try that. If you get stuck, post your code and describe what goes wrong (compiler error or output that differs from what you want etc).

    If you have studied the ternary ?:, it does something similar to if.

  3. #3
    fasck is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    Heres the code

    As you see on the first line i have tried with a if statement but i dont know how to connect it with the f = 0

    Java Code:
    // Project 2-2: a truth table for the logical operators.
    class LogicalOpTable {
    	public static void main(String args[]) {
    	
    	 boolean p, q;
    	 int t, f;
    	 
    	 t = 1; f = 0;
    	 
    	 
    	 System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
    	 
    	[B]p = true; q = false;[/B]
    	[B]if(p==true); f=0; [/B]
            System.out.print(p + "\t" + q +"\t");
    	System.out.print((p&q) + "\t" + (p|q) + "\t");
    	System.out.println((p^q) + "\t" + (!p));
    	
    	p = true; q = false;
    	System.out.print(p + "\t" + q +"\t");
    	System.out.print((p&q) + "\t" + (p|q) + "\t");
    	System.out.println((p^q) + "\t" + (!p));
    	
    	p = false; q = true; 
    	System.out.print(p + "\t" + q +"\t");
    	System.out.print((p&q) + "\t" + (p|q) + "\t");
    	System.out.println((p^q) + "\t" + (!p));
    	
    	p = false; q = false;
    	System.out.print(p + "\t" + q +"\t");
    	System.out.print((p&q) + "\t" + (p|q) + "\t");
    	System.out.println((p^q) + "\t" + (!p)); 
    }
    }
    Last edited by fasck; 12-30-2009 at 08:21 PM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    if(p==true); f=0;

    Close, but using an if statement it would be

    Java Code:
    if(p) {
        f = 0; 
    } else {
        f = 1;
    }

    Notice how if(p) is the same as if(p==true). And how the value of f has to be set for either option. (by the way, didn't you say you wanted zero, not one, for false?)

    One way of achieving what you are trying to do would be to use a separate method to do the conversion.

    Java Code:
    public class BooleanDisplay {
        public static void main(String[] args) {
            boolean p, q;
            
            System.out.println("P\tQ\tXOR");
    
            p = true;
            q = false;
            System.out.println(asInt(p) + "\t" + asInt(q) + "\t" + asInt(p^q));
    
            p = false;
            q = false;
            System.out.println(asInt(p) + "\t" + asInt(q) + "\t" + asInt(p^q));
        }
        
        static int asInt(boolean b) {
            if(b) {
                return 1;
            } else {
                return 0;
            }
                // same thing
            //return b ? 1 : 0;
        }
        
            // not used, but it could be!
        static void display(boolean p, boolean q, boolean r) {
            System.out.println(asInt(p) + "\t" + asInt(q) + "\t" + asInt(r));
        }
    }

  5. #5
    fasck is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    Thanks alot mate =)

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Similar Threads

  1. Easy task
    By Srcee in forum New To Java
    Replies: 4
    Last Post: 12-24-2009, 10:25 PM
  2. how easy it is?
    By ron87 in forum New To Java
    Replies: 0
    Last Post: 04-01-2009, 06:36 PM
  3. Not so easy is it.
    By Roy Gardiner in forum Introductions
    Replies: 0
    Last Post: 10-24-2008, 04:59 PM
  4. What does this mean (Very Easy)
    By Zebra in forum New To Java
    Replies: 6
    Last Post: 05-01-2008, 01:46 PM
  5. Easy question
    By JavaNoob in forum New To Java
    Replies: 10
    Last Post: 08-03-2007, 10:28 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
  •