Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Beginners Problem Boolean to Int?

    Hi there, I recently bought Java: A Beginners Guide in an attempt to learn my first programming language, I completed the first chapter no problems but a question in chapter 2 has me stumped.
    Try This 2-2, Display a Truth Table: Create a program that displays the truth table for Javas Logical Operators. I did this by following the example in the book:

    Java Code:
     
    class LogicalOpTable { 
    	public static void main(String[]args) {
    	
    	boolean p, q;
    	
    	System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
    		
    	p = true;
    	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 = 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));
    
    	}
    }
    Thats grand, but the last part of the question "modify the program so that it displays 1's and 0's rather than true or false" is where I run into problems.

    I have tried lots of different ways to do this none being successful. The one that makes most sense to me is trying to cast incompatible types, which I have tried but the complier says they are inconvertible types.

    Java Code:
    class LogicalOpTable { 
    	public static void main(String[]args) {
    	
    	boolean p, q;
    	int i;
    	
    	System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");
    		
    	p = true;
    	q = true;
    	i = (int)( p ); System.out.print(i); //Complier says inconvertible types.
    	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));
    
    	}
    }
    I can't find anything in the previous two chapters to help me solve this. it actually says in chapter 2 boolean and int are not compatible. The author of this book is mocking me! So I need someone to give me a hint and point me in the right direction. Is it possible to solve this problem by casting incompatible types or are my barking up the wrong tree? Should I be trying if statements instead? Something like if(true)System.out.print(1). I just want a hint, not a complete answer. Thanks for taking the time to help me!
    Last edited by ShiftyBiscuits; 08-11-2013 at 04:25 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Beginners Problem Boolean to Int?

    Quote Originally Posted by ShiftyBiscuits View Post
    Something like if(true)System.out.print(1).
    You're very very close; have you learned how to use else in conjunction with if?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: Beginners Problem Boolean to Int?

    Quote Originally Posted by DarrylBurke View Post
    You're very very close; have you learned how to use else in conjunction with if?

    db
    Thank you for your quick reply, I know about if{}else{} but that isn't covered until Chapter 3 so I thought this must not be the solution the author is looking for since this question is asked in chapter 2. Is it possible to do it with the if statement alone? The if statement was covered in chapter 1 so this might be what the author is looking for?
    Last edited by ShiftyBiscuits; 08-11-2013 at 05:10 PM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Beginners Problem Boolean to Int?

    Well, that if-else can also be written as two if statements ...

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: Beginners Problem Boolean to Int?

    Ah I see, I will give the if statement approach another go. Thanks for your help.

Similar Threads

  1. Swing boolean/String problem
    By zhider in forum New To Java
    Replies: 5
    Last Post: 10-23-2012, 05:06 AM
  2. Boolean Method Problem
    By Bonfy in forum New To Java
    Replies: 32
    Last Post: 01-23-2012, 01:21 AM
  3. Java boolean problem
    By javagangster in forum New To Java
    Replies: 1
    Last Post: 04-04-2011, 12:24 PM
  4. problem on expressing a boolean method
    By b.m in forum New To Java
    Replies: 9
    Last Post: 12-12-2010, 06:17 PM
  5. A total beginners problem
    By mbezel in forum New To Java
    Replies: 1
    Last Post: 02-15-2010, 10:07 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
  •