How to resolve the algorithm Ternary logic step by step in the Java programming language

Published on 12 May 2024 09:40 PM

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 current Trit is TRUE.
  • Returns MAYBE if the current Trit is MAYBE and other is not FALSE.
  • Returns FALSE otherwise.

Method or(Trit other):

  • Returns TRUE if the current Trit is TRUE.
  • Returns MAYBE if the current Trit is MAYBE and other is TRUE.
  • Returns other otherwise.

Method tIf(Trit other):

  • Returns other if the current Trit is TRUE.
  • Returns MAYBE if the current Trit is MAYBE and other is TRUE.
  • Returns TRUE if the current Trit is FALSE.

Method not():

  • Returns FALSE if the current Trit is TRUE.
  • Returns MAYBE if the current Trit is MAYBE.
  • Returns TRUE if the current Trit is FALSE.

Method equals(Trit other):

  • Returns other if the current Trit is TRUE.
  • Returns MAYBE if the current Trit is MAYBE.
  • Returns the negation of other if the current Trit is FALSE.

Main Method:

  • Prints the truth table for the not operator for each Trit value.
  • Prints the truth table for the and, or, tIf, not, and equals operators for all pairs of Trit 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