It's not a secret anymore!
The moose likes Beginning Java and the fly likes how to create a boolean truth table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to create a boolean truth table" Watch "how to create a boolean truth table" New topic

how to create a boolean truth table

Pete Tyo
Ranch Hand

Joined: May 11, 2005
Posts: 38
I am rather confuse on truth tables or how to create them. Take for example the following code segement

okay this is something I need to understand for a college class. I took there code made a program out of it and added a,b,c,d as boolean so I could set them to true or false. But there has to be another way to figure this out and I am under the impression it has something to do with truth tables. Any help would be greatly appreciated....


M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
i'm not really sure what you mean by a "truth table". i've always thought that was a piece of paper with a grid on it. along the sides of the grid, you'd write in the values of two different variables (one for each side), and inside the grid squares, you'd write in whether a third variable (a boolean) should be "true" or "false" when the other two variables had the values corresponding to that square's coordinates.

what you've got here seems like a four-dimensional grid, where each dimension is a boolean variable (a through d), and the values in the grid squares would be the strings "1" through "6". the only real complication seems to be in the cases where the variable c is false, or d is true; that's your first big "if" block near the top. all other cases seem more straight-forward.

actually writing out your truth table would take, hm, four actual grids if you keep to one variable per axis. if you compress it a bit (more than one variable per axis) you could shrink the piece(s) of paper you'll need, at the cost of making it a bit harder to read. it might prove useful, though.
[ May 15, 2005: Message edited by: M Beck ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I recall doing this in school with Karnaugh Maps and Gray Code. The combination allows you to find the minimum number of tests to cover a wide number of conditions. Google for them and see if you find anything interesting. HEre's one set of university notes that gets into it part way through:

I have some REXX code around from the 90s that will generate code (any language from templates) if you tell it which boxes are shaded in your Karnaugh map. Send private e-mail with your e-mail address if you're interested.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Ah, Karnaugh maps. Fondly remembering P.J. Plauger's old "Programming on Purpose" column in Computer Language magazine. Those were the days.

[Jess in Action][AskingGoodQuestions]
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1044
Well if you want to make a truth table, just put a for loop around your big if/elseif/else block of logic and set a,b,c,&d at the start of the loop. Your existing println() calls will fill in the last table column.

It's been my experience tha truth tables and Karnaugh maps were usually used when the result of your fuction was a boolean, so that they were just filled with T's and F's. In this case you could either make six separate tables, one for each possible out put, or you caould make three separate ones, one for each bit in the output integer.

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The "Programming on Purpose" columns were collected into a fantastic little book by the same name. Highly recommended for the state of the art of design ... right before objects. The essays on design styles like Top Down, Bottom Up, Left to Right, Right to Left, Inside Out and Outside In are timeless. I became parted from my copy. Hope those were right!

One word of warning on using this kind of logic reduction ... the resulting code can be nearly impossible to translate back into business terms. I used it a couple times and kept the graph as a comment.

Here's one input to my little code generator showing all the possible rectangles in a Karnaugh map up to 4 variables and what they imply ...
I agree. Here's the link:
subject: how to create a boolean truth table
It's not a secret anymore!