The input to the program will consist of eight groups of four Boolean values (0 or 1). Each group represents a row in the truth table defining the Boolean function. As in a truth table, the first three Boolean values in the group are values of x, y, and z; the last Boolean value in the group represents the result of the expression. Input to the program may be read from a file or entered through console prompts.
The output of the program will consist of (1) the resulting truth table containing four columns (x, y, z and F) with appropriate labels, and (2) a sum-of-products expansion of the variables x, y, and z.
I thought it was going well but i cant figure out the arrays and its telling me "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9 at sumofproducts.SumOfProducts.main(SumOfProducts.java:45)Java Result: 1"
Heres my code:
package sumofproducts;
import java.util.Scanner;
public class SumOfProducts {
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];
int k = 0;
while (k<8){
System.out.print("What is x input of line " + (k+1) + "?");
xrow[k]=in.nextInt();
System.out.print("What is y input of line " + (k+1) + "?");
yrow[k]=in.nextInt();
System.out.print("What is z input of line " + (k+1) + "?");
zrow[k]=in.nextInt();
System.out.print("What is output of line " + (k+1) + "?");
output[k]=in.nextInt();
k++;
}
System.out.println("X Y Z "+"F(x,y,z)");
int o = 0;
while(o<8){
System.out.println(xrow[o]+" "+yrow[o]+" "+zrow[o]+" "+output[o]);
o++;
}
int u = 0;
String[] sumofprod = new String[24];
int numofones=0;
while(u<25){
if(output[u]==1){
numofones++;
if(xrow[u]==1){
sumofprod[u]="X";
}
if(xrow[u]==0){
sumofprod[u]="X'";
}
if(yrow[u]==1){
sumofprod[u+1]="Y";
}
if(yrow[u]==0){
sumofprod[u+1]="Y'";
}
if(zrow[u]==1){
sumofprod[u+2]="Z";
}
if(zrow[u]==0){
sumofprod[u+2]="Z'";
}
}
u++;
u++;
u++;
}
System.out.print("Sum of Products Expansion:");
if (numofones>0){
int m=0;
while(m<numofones){
if(m>0){
System.out.print(" + ");
}
System.out.print(sumofprod[m]+sumofprod[m+1]+sumofprod[m+2]);
m++;
}
}
else{
System.out.print("1");
}
}
}
Edited by Alyn, 14 October 2011 - 07:48 AM.
added code tag


Sign In
Create Account

Back to top









