bluej Programs
1. Write a program to design a truth table.
import java.util.*;
class truthtable
{
Scanner sc= new Scanner(System.in);
int n,r,c,ch;char table[][];
void input()
{
System.out.println("Enter total number of literals:");
n=sc.nextInt();
if(n>=1 && n<=4)
{
r=(int)Math.pow(2,n);
c=n+1;
table=new char[r][c];
}
}
void combination()
{
for(int i=0;i<r;i++)
{
String v=binary(i,n);//System.out.println(v);
for(int j=0;j<c-1;j++)
{
char ch=v.charAt(j);
table[i][j]=ch;
}
}
}
String binary(int i,int n)
{
String val="",v1="",v2="";
while(i!=0)
{
int r=i%2;
val=val+Integer.toString(r);
i=i/2;
}
val=reverse(v2,val,val.length()-1);
for(int x=0;x<(n-val.length());x++)
{
v1+="0";
}
val=v1+val;
return val;
}
String reverse(String v2,String val,int l)
{
if(l==-1)
return v2;
else
{
v2+=val.charAt(l);
return(reverse(v2,val,l-1));
}
}
void ORgate()
{
int f=0;
for(int i=0;i<r;i++)
{
for(int j=0;j<c-1;j++)
{
if(table[i][j]=='1')
{
f=1;break;
}
}
if(f==1)
{
table[i][c-1]='1';
}
else
table[i][c-1]='0';
f=0;
}
}
void ANDgate()
{
int f=0;
for(int i=0;i<r;i++)
{
for(int j=0;j<c-1;j++)
{
if(table[i][j]=='0')
{
f=1;break;
}
}
if(f==1)
{
table[i][c-1]='0';
}
else
table[i][c-1]='1';
f=0;
}
}
void NOTgate()
{
for(int i=0;i<r;i++)
{
if(table[i][0]=='1')
{
table[i][1]='0';
}
else
table[i][1]='1';
}
}
void display(int ch)
{
System.out.println("The truth table is:");
if(n==1)
System.out.println("p\tp");
else if(n==2)
{
if(ch==1)
{
System.out.println("p\tq\tp+q");
}
else if(ch==2)
{
System.out.println("p\tq\tp.q");
}
}
else if(n==3)
{
if(ch==1)
{
System.out.println("p\tq\tr\tp+q+r");
}
else if(ch==2)
{
System.out.println("p\tq\tr\tp.q.r");
}
}
else
{
if(ch==1)
{
System.out.println("p\tq\tr\ts\tp+q+r+s");
}
else if(ch==2)
{
System.out.println("p\tq\tr\ts\tp.q.r.s");
}
}
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.print(table[i][j]+"\t");
}
System.out.println();
}
}
void main()
{
truthtable ob=new truthtable();
ob.input();
ob.combination();
System.out.println("1:OR gate\n2:AND gate\n3:NOT gate\nEnter ur choice:");
ch=sc.nextInt();
switch(ch)
{
case 1:if(ob.n>1)
{ob.ORgate();
ob.display(ch);
}
else
System.out.println("Minimum inputs should be 2 for OR gate");
break;
case 2: if(ob.n>1)
{
ob.ANDgate();
ob.display(ch);
}
else
System.out.println("Minimum inputs should be 2 for AND gate");
break;
case 3:if(ob.n>1)
System.out.println("Minimum and Maximuminputs should be 1 for NOT gate");
else
{
ob.NOTgate();
ob.display(ch);
}
break;
default:
System.out.println("Wrong Input");
}
}
}
Output:
Enter total number of literals: 3 1:OR gate 2:AND gate 3:NOT gate Enter ur choice: 1 The truth table is: p q r p+q+r 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 1 Enter total number of literals: 3 1:OR gate 2:AND gate 3:NOT gate Enter ur choice: 2 The truth table is: p q r p.q.r 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 1 0 1 1 0 0 1 1 1 1 Enter total number of literals: 1 1:OR gate 2:AND gate 3:NOT gate Enter ur choice: 3 The truth table is: p p 0 1 1 0
ADVERTISEMENT
