Question

In: Computer Science

I need this Java code translated into C Code. Thank you. //Logical is the main public...

I need this Java code translated into C Code. Thank you.

//Logical is the main public class

public class Logical {
public static void main(String args[]) {
char [][] table= {{'F','F','F'},{'F','F','T'},{'F','T','F'},{'F','T','T'},{'T','F','F'},{'T','F','T'},{'T','T','F'},{'T','T','T'}};

// table contains total combinations of p,q,& r
int totalTrue, totalFalse, proposition;
  
//proposition 1:

proposition=1;
start(proposition);
totalTrue=0;
totalFalse=0;
for(int i=0;i<8;i++)
{
  
{
char o= conjuctive(implecation(negation(table[i][0]),table[i][1]),implecation(table[i][2],table[i][0]));
System.out.println(" "+table[i][0]+" "+table[i][1]+" "+table[i][2]+" "+o);
if(o=='T')
totalTrue++;
else
totalFalse++;
  
}
}
finalOutput(totalTrue,totalFalse,proposition);
System.out.println(" ");
System.out.println(" ");
  

//proposition 2:
proposition=2;
start(proposition);
totalTrue=0;
totalFalse=0;
for(int i=0;i<8;i++)
{
  
{
char o= conjuctive(disjunctive(table[i][0],negation(table[i][1])),disjunctive(table[i][2],negation(implecation(table[i][0],table[i][1]))));
System.out.println(" "+table[i][0]+" "+table[i][1]+" "+table[i][2]+" "+o);
if(o=='T')
totalTrue++;
else
totalFalse++;
  
}
}
finalOutput(totalTrue,totalFalse,proposition);
System.out.println(" ");
System.out.println(" ");
  

//Proposition 3
proposition=3;
start(proposition);
totalTrue=0;
totalFalse=0;
for(int i=0;i<8;i++)
{
  
{
char o= implecation(table[i][0],implecation(negation(conjuctive(table[i][0],negation(table[i][1]))),conjuctive(table[i][0],table[i][1])));
System.out.println(" "+table[i][0]+" "+table[i][1]+" "+table[i][2]+" "+o);
if(o=='T')
totalTrue++;
else
totalFalse++;
  
}
}
finalOutput(totalTrue,totalFalse,proposition);
System.out.println(" ");
System.out.println(" ");
  

//Proposition 4
proposition=4;
start(proposition);
totalTrue=0;
totalFalse=0;
for(int i=0;i<8;i++)
{
  
{
char o= conjuctive(conjuctive(table[i][0],implecation(table[i][0],table[i][1])),negation(table[i][1]));
System.out.println(" "+table[i][0]+" "+table[i][1]+" "+table[i][2]+" "+o);
if(o=='T')
totalTrue++;
else
totalFalse++;
  
}
}
finalOutput(totalTrue,totalFalse,proposition);
  
/* //Proposition 0
proposition=0;
start(0);
totalTrue=0;
totalFalse=0;
for(int i=0;i<8;i++)
{
  
{
char o= conjuctive(table[i][0],conjuctive(table[i][1],negation(table[i][2])));
System.out.println(" "+table[i][0]+" "+table[i][1]+" "+table[i][2]+" "+o);
if(o=='T')
totalTrue++;
else
totalFalse++;
  
}
}
finalOutput(totalTrue,totalFalse,proposition);*/
  
}

//method to check contradiction & tautology
static public void finalOutput(int totalTrue,int totalFalse,int proposition)
{
System.out.println(" "+totalTrue+" combination result in Proposition " +proposition+" being T");
System.out.println(" "+totalFalse+" combination result in Proposition "+proposition+ " being F");
if(totalTrue==8)
System.out.println(" Proposition "+ proposition+ " is a tautology");
else if(totalFalse==8)
System.out.println(" Proposition " + proposition+" is a contradiction");
else
System.out.println(" Proposition "+proposition +" is neither a tautology nor a contradiction");
}
static public void start(int i)
{
System.out.println(" "+"p"+" "+"q"+" "+"r"+" "+"Proposition "+ i);
System.out.println("----------------------------------------------------");
}
// negation method
static public char negation(char x)
{
if(x=='T')
return 'F';
else
return 'T';
}
// disjuctive method
static public char disjunctive(char x, char y)
{
if(x=='T' || y=='T')
return 'T';
else
return 'F';
}
// conjuctive method
static public char conjuctive(char x, char y)
{
if(x=='F' || y=='F')
return 'F';
else
return 'T';
}
// implecation method
static public char implecation(char x,char y)
{
if((x=='T' && y=='T') ||(x=='F' && y=='F') || (x=='F' && y=='T'))
return 'T';
else
return 'F';
}
}

Solutions

Expert Solution

Please find the answer below, all the details are mentioned in the comments. Conversion is done as per the code provided in the question.

Logical.c

#include<stdio.h>

//function declarations
void start();
char negation(char x);
char conjuctive(char x, char y);
char implecation(char x,char y);
void finalOutput(int totalTrue,int totalFalse,int proposition);
char disjunctive(char x, char y);

//main method to execute the functions
void main(){
char table[8][3]= {{'F','F','F'},{'F','F','T'},{'F','T','F'},{'F','T','T'},{'T','F','F'},{'T','F','T'},{'T','T','F'},{'T','T','T'}};
// table contains total combinations of p,q,& r
int totalTrue, totalFalse, proposition;

//proposition 1:
proposition=1;
start(proposition);
totalTrue=0;
totalFalse=0;

for(int i=0;i<8;i++){
{
char o= conjuctive(implecation(negation(table[i][0]),table[i][1]),implecation(table[i][2],table[i][0]));
printf(" %c %c %c %c\n",table[i][0],table[i][1],table[i][2],o);
if(o=='T')
totalTrue++;
else
totalFalse++;
}
}
finalOutput(totalTrue,totalFalse,proposition);
printf(" \n");
printf(" \n");

//proposition 2:
proposition=2;
start(proposition);
totalTrue=0;
totalFalse=0;

for(int i=0;i<8;i++){
{
char o= conjuctive(disjunctive(table[i][0],negation(table[i][1])),disjunctive(table[i][2],negation(implecation(table[i][0],table[i][1]))));
printf(" %c %c %c %c\n",table[i][0],table[i][1],table[i][2],o);
if(o=='T')
totalTrue++;
else
totalFalse++;

}
}

finalOutput(totalTrue,totalFalse,proposition);
printf(" \n");
printf(" \n");

//Proposition 3
proposition=3;
start(proposition);
totalTrue=0;
totalFalse=0;
for(int i=0;i<8;i++){
{
char o= implecation(table[i][0],implecation(negation(conjuctive(table[i][0],negation(table[i][1]))),conjuctive(table[i][0],table[i][1])));
printf(" %c %c %c %c\n",table[i][0],table[i][1],table[i][2],o);
if(o=='T')
totalTrue++;
else
totalFalse++;

}
}
finalOutput(totalTrue,totalFalse,proposition);
printf(" \n");
printf(" \n");

//Proposition 4
proposition=4;
start(proposition);
totalTrue=0;
totalFalse=0;
for(int i=0;i<8;i++){
{
char o= conjuctive(conjuctive(table[i][0],implecation(table[i][0],table[i][1])),negation(table[i][1]));
printf(" %c %c %c %c\n",table[i][0],table[i][1],table[i][2],o);
if(o=='T')
totalTrue++;
else
totalFalse++;

}
}
finalOutput(totalTrue,totalFalse,proposition);

}//main method end

//function definitions
//method to check contradiction & tautology
void finalOutput(int totalTrue,int totalFalse,int proposition){
printf(" %d combination result in Proposition %d being T\n",totalTrue,proposition);
printf(" %d combination result in Proposition %d being F\n",totalFalse,proposition);
if(totalTrue==8)
printf(" Proposition %d is a tautology\n",proposition);
else if(totalFalse==8)
printf(" Proposition %d is a contradiction\n",proposition);
else
printf(" Proposition %d is neither a tautology nor a contradiction\n",proposition);
}

void start(int i){
printf(" p q r Proposition %d\n",i);
printf("----------------------------------------------------\n");
}

// negation method
char negation(char x){
if(x=='T')
return 'F';
else
return 'T';
}

// disjuctive method
char disjunctive(char x, char y){
if(x=='T' || y=='T')
return 'T';
else
return 'F';
}
// conjuctive method
char conjuctive(char x, char y){
if(x=='F' || y=='F')
return 'F';
else
return 'T';
}
// implecation method
char implecation(char x,char y){
if((x=='T' && y=='T') ||(x=='F' && y=='F') || (x=='F' && y=='T'))
return 'T';
else
return 'F';
}


Output:

Please let us know in the comments if you face any problems.


Related Solutions

I need this code translated from C++ to Java. Im personally still trying to learn Java,...
I need this code translated from C++ to Java. Im personally still trying to learn Java, so if you can include screenshots of your IDE/output that would be helpful. Much appreciated! #include <iostream> #include <string> using namespace std; class pizza { public:    string ingrediants, address;    pizza *next;    pizza(string ingrediants, string address)    {        this->address = address;        this->ingrediants = ingrediants;        next = NULL;    } }; void enqueue(pizza **head, pizza **tail, pizza...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main {...
I need this Java code transform to Python Code PROGRAM: import java.util.Scanner; public class Main { static int count=0; int calculate(int row, int column) { count++; if(row==1&&column==1) { return 0; } else if(column==1) { return ((200+calculate(row-1,column))/2); } else if(column==row) { return (200+calculate(row-1,column-1))/2; } else { return ((200+calculate(row-1,column-1))/2)+((200+calculate(row-1,column))/2); }    } public static void main(String[] args) { int row,column,weight; Main m=new Main(); System.out.println("Welcome to the Human pyramid. Select a row column combination and i will tell you how much weight the...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");   ...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public...
I need to translate my java code into C code. import java.util.Scanner; class CS_Lab3 { public static void main( String args[] ) { Scanner input = new Scanner( System.in ); // create array to hold user input int nums[] = new int[10]; int i = 0, truthCount = 0; char result = 'F', result2 = 'F'; // ask user to enter integers System.out.print("Please Enter 10 Different integers: "); // gather input into array for ( i = 0; i <...
(This is for java) I need to rewrite this code that uses a while loop. public...
(This is for java) I need to rewrite this code that uses a while loop. public class Practice6 {      public static void main (String [] args) {         int sum = 2, i=2;        do { sum *= 6;    i++;    } while (i < 20); System.out.println("Total is: " + sum); }
Translate the following C code to MIPS assembly. The main function and subfunction are translated to...
Translate the following C code to MIPS assembly. The main function and subfunction are translated to two separate .asm files. Finish the assembly code segment for the above requirement. int main() { int x=2; int y=1; int z=0; z=Subfunc(x,y); printf(“Value of z is: %d”, z); } int Subfunc(int x, int y) { int t1=0; t1=x+y+100; return t1;} File 1: .data str: .asciiz "The value of z:" .text #.globl main main: addi $s0, $0,2 #x addi $s1, $0,1 #y addi $s2,...
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
Define the meaning of following java code in public void refer, line by line. thank you...
Define the meaning of following java code in public void refer, line by line. thank you public class LRUCache {     // store keys of cache     static Deque<Integer> dq;     // store references of key in cache     static HashSet<Integer> map;     // maximum capacity of cache     static int csize;        LRUCache(int n)     {         dq = new LinkedList<>();         map = new HashSet<>();         csize = n;     }     public void refer(int x)     {         if (!map.contains(x)) {             if (dq.size() == csize) {                 int last = dq.removeLast();                 map.remove(last);...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to use all methods, also switch statements. Write a Temperature class. The class will have three conversion methods: toCelsius(), toKelvin() and toFahrenheit(). These methods will return a Temperature in those three scales equal to the this temperature. Note that the value of this is not changed in these conversions. In addition to these three conversion methods the class will have methods add(Temperature), subtract(Temperature), multiply(Temperature), and...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import the required classes import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BirthdayReminder {       public static void main(String[] args) throws IOException {        // declare the required variables String sName = null; String names[] = new String[10]; String birthDates[] = new String[10]; int count = 0; boolean flag = false; // to read values from the console BufferedReader dataIn = new BufferedReader(new...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT