truth table program
I wrote this program for school, but am not sure what I am doing wrong? Any suggestions?
Thanks.
//Truth Table 3
public class TruthTable3a
{
public static void main(String[] args)
{
String [] gateType=
{"AND","OR","XOR","NAND","NOR"};
for (int i=0; i < gateType.length; i++)
{
System.out.println("Truth Table for "+gateType+" gate");
System.out.println("Input1\tInput2\tOutput");
System.out.println("--");
truth(gateId);
}
}
public static boolean getOutput(boolean in1, boolean in2, int gateId)
{
boolean output = false;
switch (gateId)
{
case 0 : output = !in1;
break;
case output = (in1 && in2);
break;
case output = (in1 || in2);
break;
case output = ((in1 && !in2) || (!in1 && in2));
break;
case output = !(in1 && in2);
break;
case output = !(in1 || in2);
break;
}
return output;
}
}
output = getOutput(bInput1,bInput2,i);
}
Three things you did wrong:- you didn't use the code tags when posting code- you forgot to tell us what it's supposed to do- and what it does instead
Its supposed to output tables:
Truth Table for AND gate
Input1Input2Output
--
falsefalsefalse
falsetruefalse
truefalsefalse
truetruetrue
Truth Table for OR gate
Input1Input2Output
--
falsefalsefalse
falsetruefalse
truefalsefalse
truetruetrue
Truth Table for XOR gate
Input1Input2Output
--
falsefalsefalse
falsetruefalse
truefalsefalse
truetruetrue
Truth Table for NAND gate
Input1Input2Output
--
falsefalsefalse
falsetruefalse
truefalsefalse
truetruetrue
Truth Table for NOR gate
Input1Input2Output
--
falsefalsefalse
falsetruefalse
truefalsefalse
truetruetrue
the problem is they are all the same. Its not changing to the proper gate.
//Truth Table 3
public class TruthTable3a
{
public static void main(String[] args)
{
String [] gateType=
{"AND","OR","XOR","NAND","NOR"};
for (int i=0; i < gateType.length; i++)
{
System.out.println("Truth Table for "+gateType[i]+" gate");
System.out.println("Input1\tInput2\tOutput");
System.out.println("--");
truth(gateId);
}
}
public static boolean getOutput(boolean in1, boolean in2, int gateId)
{
boolean output = false;
switch (gateId)
{
case 0 : output = !in1;
break;
case output = (in1 && in2);
break;
case output = (in1 || in2);
break;
case output = ((in1 && !in2) || (!in1 && in2));
break;
case output = !(in1 && in2);
break;
case output = !(in1 || in2);
break;
}
return output;
}
}
output = getOutput(bInput1,bInput2,i);
}
Hi,Shouldn't e.g. case output = (in1 && in2);be case 1: output = (in1 && in2); break;Kaj
What do you mean "all the same"? Post the actual output.
And repost your code inside [code] and [/code] tags with decent intdentation, so it's readable.
And put a bunch of print statements in so you can see what it's doing each step of the way. Obviously you think it will do one thing, and it's actually doing something else, so you need to examine what's happening at each step.
That does not compile.
mlka at 2007-7-16 1:54:52 >

nope, it sure don't...- MaxxDmg...- ' He who never sleeps... '
yeah, its supposed to be:
public class TruthTable3b
{
public static void main(String[] args)
{
String [] gateType=
{"AND","OR","XOR","NAND","NOR"};
for (int i=0; i < gateType.length; i++)
{
System.out.println("Truth Table for "+gateType[i]+" gate");
System.out.println("Input1\tInput2\tOutput");
System.out.println("--");
truth(gateType);
}
}
}
public static boolean getOutput(boolean in1, boolean in2, int gateId)
{
boolean output = false;
switch (gateId)
{
case 0 : output = !in1;
break;
case 1 : output = (in1 && in2);
break;
case 2 : output = (in1 || in2);
break;
case 3 : output = ((in1 && !in2) || (!in1 && in2));
break;
case 4 : output = !(in1 && in2);
break;
case 5 : output = !(in1 || in2);
break;
}
return output;
}
still doesnt work right though.
> truth(gateType);And where's this "truth" method? And why do you invoke it 5 times identically (without passing 'i' to it)?
Look at the array
{"AND","OR","XOR","NAND","NOR"};
Now look at the swtich:
case 0 /*first element in array*/: output = !in1;
mlka at 2007-7-16 1:54:52 >
