How to resolve the algorithm Ternary logic step by step in the Java programming language
How to resolve the algorithm Ternary logic step by step in the Java programming language
Table of Contents
Problem Statement
In logic, a three-valued logic (also trivalent, ternary, or trinary logic, sometimes abbreviated 3VL) is any of several many-valued logic systems in which there are three truth values indicating true, false and some indeterminate third value.
This is contrasted with the more commonly known bivalent logics (such as classical sentential or boolean logic) which provide only for true and false.
Conceptual form and basic ideas were initially created by Łukasiewicz, Lewis and Sulski.
These were then re-formulated by Grigore Moisil in an axiomatic algebraic form, and also extended to n-valued logics in 1945.
Note: Setun (Сетунь) was a balanced ternary computer developed in 1958 at Moscow State University. The device was built under the lead of Sergei Sobolev and Nikolay Brusentsov. It was the only modern ternary computer, using three-valued ternary logic
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Ternary logic step by step in the Java programming language
Enum Trit
:
This enum represents the three values: true (TRUE), maybe (MAYBE), and false (FALSE). It defines the behavior of logical operators like and
, or
, tIf
(conditional implication), not
(logical negation), and equals
(equivalence) for these values.
Method and(Trit other)
:
- Returns
other
if the currentTrit
isTRUE
. - Returns
MAYBE
if the currentTrit
isMAYBE
andother
is notFALSE
. - Returns
FALSE
otherwise.
Method or(Trit other)
:
- Returns
TRUE
if the currentTrit
isTRUE
. - Returns
MAYBE
if the currentTrit
isMAYBE
andother
isTRUE
. - Returns
other
otherwise.
Method tIf(Trit other)
:
- Returns
other
if the currentTrit
isTRUE
. - Returns
MAYBE
if the currentTrit
isMAYBE
andother
isTRUE
. - Returns
TRUE
if the currentTrit
isFALSE
.
Method not()
:
- Returns
FALSE
if the currentTrit
isTRUE
. - Returns
MAYBE
if the currentTrit
isMAYBE
. - Returns
TRUE
if the currentTrit
isFALSE
.
Method equals(Trit other)
:
- Returns
other
if the currentTrit
isTRUE
. - Returns
MAYBE
if the currentTrit
isMAYBE
. - Returns the negation of
other
if the currentTrit
isFALSE
.
Main Method:
- Prints the truth table for the
not
operator for eachTrit
value. - Prints the truth table for the
and
,or
,tIf
,not
, andequals
operators for all pairs ofTrit
values.
This code demonstrates the behavior of three-valued logic, where each proposition can have the values true, maybe, or false. It defines logical operators that work consistently with these values, allowing for more nuanced reasoning than in traditional two-valued logic.
Source code in the java programming language
public class Logic{
public static enum Trit{
TRUE, MAYBE, FALSE;
public Trit and(Trit other){
if(this == TRUE){
return other;
}else if(this == MAYBE){
return (other == FALSE) ? FALSE : MAYBE;
}else{
return FALSE;
}
}
public Trit or(Trit other){
if(this == TRUE){
return TRUE;
}else if(this == MAYBE){
return (other == TRUE) ? TRUE : MAYBE;
}else{
return other;
}
}
public Trit tIf(Trit other){
if(this == TRUE){
return other;
}else if(this == MAYBE){
return (other == TRUE) ? TRUE : MAYBE;
}else{
return TRUE;
}
}
public Trit not(){
if(this == TRUE){
return FALSE;
}else if(this == MAYBE){
return MAYBE;
}else{
return TRUE;
}
}
public Trit equals(Trit other){
if(this == TRUE){
return other;
}else if(this == MAYBE){
return MAYBE;
}else{
return other.not();
}
}
}
public static void main(String[] args){
for(Trit a:Trit.values()){
System.out.println("not " + a + ": " + a.not());
}
for(Trit a:Trit.values()){
for(Trit b:Trit.values()){
System.out.println(a+" and "+b+": "+a.and(b)+
"\t "+a+" or "+b+": "+a.or(b)+
"\t "+a+" implies "+b+": "+a.tIf(b)+
"\t "+a+" = "+b+": "+a.equals(b));
}
}
}
}
You may also check:How to resolve the algorithm Continued fraction step by step in the Swift programming language
You may also check:How to resolve the algorithm Sorting algorithms/Shell sort step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Elementary cellular automaton/Random number generator step by step in the Rust programming language
You may also check:How to resolve the algorithm Long year step by step in the МК-61/52 programming language
You may also check:How to resolve the algorithm Prime numbers whose neighboring pairs are tetraprimes step by step in the Nim programming language