Results 1 to 1 of 1
Thread: Truth Table Homework Problem
 02132012, 06:46 AM #1Member
 Join Date
 Jan 2012
 Posts
 7
 Rep Power
 0
Truth Table Homework Problem
I was given the following homework problem : gather user input for x,y,z, and F (x,y,z). After that, display the truth table from the input, and output the sum of products expansion on the screen. The sum of products looks at rows where the output is 1: if the xinput is 1, it prints X, if it is 0, it prints X'. The same goes for Y and Z. If there are two rows with ones (let's say 0 0 0 1 and 1 1 1 1), the sum of products expansion should be X'Y'Z' + XYZ. My program compiles and runs, it outputs the correct truth table, however, it doesn't display the right sum of products expansion, and I cannot seem to figure out why. Any help would be appreciated, I think it is something very simple that I am not seeing, but I can be wrong. Here is my code, thanks in advance!
Java Code:import java.util.Scanner; public class TruthTables { public static void main (String args [] ) { Scanner in = new Scanner (System.in) ; int [] xrow = new int [8]; int [] yrow = new int [8]; int [] zrow = new int [8]; int [] output = new int [8]; String [] sumOfProduct = new String [24]; int outputOnes = 0; for (int i = 0; i < 8; i++) { System.out.println ("Enter the value for x please: "); xrow [i] = in.nextInt(); while (xrow[i] > 1  xrow[i] < 0) { System.out.println ("This input for x is invalid. Enter 0 or 1 and try again: "); xrow [i] = in.nextInt(); } System.out.println ("Enter the value for y please: "); yrow [i] = in.nextInt(); while (yrow[i] > 1  yrow[i] < 0) { System.out.println ("This input for y is invalid. Enter 0 or 1 and try again: "); yrow [i] = in.nextInt(); } System.out.println ("Enter the value for z please: "); zrow [i] = in.nextInt(); while (zrow[i] > 1  zrow[i] < 0) { System.out.println ("This input for z is invalid. Enter 0 or 1 and try again: "); zrow [i] = in.nextInt(); } System.out.println ("Enter the output for the function please: "); output [i] = in.nextInt (); while (output[i] > 1  output[i] < 0) { System.out.println ("This is invalid. Enter 0 or 1 and try again: "); output[i] = in.nextInt(); } if (output[i] == 1) { outputOnes++; } for (int k = 0; k < outputOnes; k++) { if (output [i] == 1) { if (xrow[i] == 1) { sumOfProduct[i] = "X"; } if (xrow[i] == 0) { sumOfProduct[i] = "X'"; } if (yrow[i] == 1) { sumOfProduct[i + 1] = "Y"; } if (yrow [i] == 0) { sumOfProduct[i + 1] = "Y'"; } if (zrow[i] == 1) { sumOfProduct[i + 2] = "Z"; } if (zrow[i] == 0) { sumOfProduct[i + 2] = "Z'"; } } } } System.out.println ("X Y Z F(X,Y,Z) "); for (int i = 0; i < 8; i++) { System.out.println ( xrow[i]+ " " + yrow[i] + " " + zrow[i] + " " + output[i]); } System.out.println ("Sum of Products Expansion : "); if (outputOnes > 0) { int i = 0; while (i < outputOnes) { if ( i > 0) { System.out.print ( " + "); } System.out.print (sumOfProduct[i] + sumOfProduct[i+1] + sumOfProduct[i+2]); i++; } } } }
Similar Threads

Letter counter homework problem
By Djgnl in forum New To JavaReplies: 15Last Post: 09242011, 03:37 AM 
Help with Homework Problem
By Holy_Zombies in forum New To JavaReplies: 6Last Post: 10052010, 07:26 AM 
Decision structure truth table
By JMaste in forum New To JavaReplies: 2Last Post: 07142010, 08:31 AM 
Problem using thread +rmi in my homework
By IbrahimAbbas in forum Threads and SynchronizationReplies: 10Last Post: 04142008, 10:24 PM 
Homework PREREQUISITE Problem
By ChrisC in forum New To JavaReplies: 7Last Post: 11272007, 06:36 AM
Bookmarks