• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

truth table

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm just starting out learning Java and need a little help. I'm supposed to take this truth table and alter it so it displays 1's and 0's instead of true false. I'm assumed to do this I would just need to change the variable type and replace true and false with 1 and 0 but every way I try this does not work. I would just like for someone to point me in the right direction. I would like an explanation so I could understand instead of just an awnser. Thanks in advance



 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lenny Leon:
I'm just starting out learning Java and need a little help. I'm supposed to take this truth table and alter it so it displays 1's and 0's instead of true false. I'm assumed to do this I would just need to change the variable type and replace true and false with 1 and 0 but every way I try this does not work. I would just like for someone to point me in the right direction. I would like an explanation so I could understand instead of just an awnser. Thanks in advance



okay, so break down what !p means...
! = a boolean operator that inverts the value of a boolean...
So...

if p is true then !p ( NOT p ) is false, otherwise...
if p is false then !P ( NOT p ) is true... Hmmm... the opposite...

so if you use integers... then you could look at it like this...

if p is 1 then !p ( NOT p) then p is 0, else
if p is 0 then !p ( NOT p) then p is ???
... you can take it from here...

now you got some of the logic, making it work should be easier...
 
Lenny Leon
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've got it to work (though I don't think its correct) but in order for it to work I removed the ! operator. It won't compile when I leave it in. How do I fix this so it includes the ! operator?

This is the code with the NOT operator still in. It would not compile this way

 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't want to do all your work for you but look at the code below and see if you can work from there.

hope this helps
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The important lesson to learn here is that Nigel did not change the underlying code in the program...he kept the data types the same and simply changed the user interface to display 1's and 0's instead of trues and falses.

You could also then change it to display Y/N, T/F (or, for the really sadistic, A/B or even N/Y.....)

You might want to add a could of static variables:


If you were yo use these in the code, you could change the displays to whatever your heart desired with very little effort.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order to compile, I had to adapt this code below by inserting parenthesis around the ternary operator section. For example - System.out.print((p? 1:0) + "\t"); How did you get around this?

Nigel Browne wrote:I didn't want to do all your work for you but look at the code below and see if you can work from there.

hope this helps


 
Marshal
Posts: 80074
410
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You have a + operator in that line, which has a higher precedence than ?:. So without the () you would offer it the choice between printing 1 and printing "0\t"; those are incompatible types (int and String), so the compiler wouldn't be happy.
What about using % tags
System.out.printf("%d\t", p ? 1 : 0);
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You guys realize this thread is nine years old, right?
 
Ben Don
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but the answer didn't work as written. The problem hasn't changed.
 
Campbell Ritchie
Marshal
Posts: 80074
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain what you expect to happen and what is actually happening.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic