Truth Tables (Kotlin)

1

It's like this for the first time here, I'm learning to use the programming language Kotlin, I have to develop a Boolean formula with true or false, I already do it in Java and it stayed like this

    private static final boolean VALUES[] = new boolean[]{true,false};
public static void main(String args[])
{
    System.out.println("a\t\tb\t\tc\t\td\t\t(avb)\t(c^d)\t(avb) ->  (c^d)\t 'xor'(avb) -> !(cvd) \t !(avb) != (a=b) -> !(cvd)\r\n" + 
            "");
    for( boolean a : VALUES )               
    {
        for( boolean b: VALUES )
        {
            for( boolean c: VALUES )
            {
                for( boolean d: VALUES )
                {
                    for( boolean e: VALUES )
                    {
                        for( boolean f: VALUES )
                        {
                            System.out.println(a+"\t"+"\t"+b+"\t"+"\t"+c+"\t"+"\t"+d+"\t"+"\t"+or(a,b)+"\t"+and(c,d)+"\t\t"
                                    +conditional(or(a,b),and(c,d))+"\t"+"\t"+xor(e,f)+"\t"+"\t"+disequal(or(a,b),xor(e,f)));

                        }
                    }
                }
            }
        }
    }
}
public static boolean or( boolean x , boolean y )
{
    return x || y;
}
public static boolean and( boolean x , boolean y )
{
    return x && y;
}
public static boolean xor( boolean x , boolean y )
{
    return x ^ y;
}
public static boolean conditional( boolean x , boolean y )
{
    return !(x && y);

}
public static boolean bicontional( boolean x , boolean y )
{
    return x == y;
}   

public static boolean disequal ( boolean x , boolean y )
{
    return x != y;
}

Now I want to implement a similar formula in Android Studio with Kotlin and it has stayed that way

class MainActivity : AppCompatActivity() {



override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val textview:TextView = findViewById(R.id.textview) as TextView



    val a = true;
    val b = false;



    val a1 = a && a
    val a2 = a && b
    val a3 = b && a
    val a4 = b && b


    val b1 = a || a
    val b2 = a || b
    val b3 = b || a
    val b4 = b || b


    val c1 = a xor a
    val c2 = a xor b
    val c3 = b xor a
    val c4 = b xor b

    val d1 = !a and b
    val d2 = a and !b


    val myString0 = "Tabla de AND \n";
        val myString2 = "$a  y $a = $a1 \n";
        val myString3 =  "$a  y $b = $a2 \n";
        val myString4 =  "$b  y $a = $a3 \n";
        val myString5=  "$b  y $b = $a4 \n";


    val myString6 = "\n";
    val myString7 = "Tabla de OR \n";

    val myString8 = "$a  y $a = $b1 \n";
    val myString9 =  "$a  y $b = $b2 \n";
    val myString10 =  "$b  y $a = $b3 \n";
    val myString11 =  "$b  y $b = $b4 \n";

    val myString12 = "\n";
    val myString13 = "Tabla de XOR \n";

    val myString14 = "$a  y $a = $c1 \n";
    val myString15 =  "$a  y $b = $c2 \n";
    val myString16 =  "$b  y $a = $c3 \n";
    val myString17 =  "$b  y $b = $c4 \n";



    val myString18 = "\n";
    val myString19 = "Tabla de NOT \n";

    val myString20 =  "$a  y $b = $d1 \n";
    val myString21 =  "$a  y $b = $d2 \n";




    textview.setText( myString0 + myString2 + myString3 + myString4 + myString5 +  myString6 + myString7 + myString8 + myString9 + myString10 + myString11 +myString12 +myString13 + myString14 + myString15
     + myString16 + myString17 + myString18 + myString19 + myString20 + myString21)

    textview.setMovementMethod(ScrollingMovementMethod())
}

Kotlin's code shows the truth tables I want to create my own boolean formula similar to the first, which I must change in my Kotlin code already investigated and I can not find

You will be grateful for all kinds of help from you

Thanks

    
asked by Kevin Paúl Montealegre Melo 01.09.2018 в 21:28
source

1 answer

0

The direct translation of your Java code to Kotlin is:

private val VALUES = booleanArrayOf(true, false)
fun main(args: Array<String>) {
    println("a\t\tb\t\tc\t\td\t\t(avb)\t(c^d)\t(avb) ->  (c^d)\t 'xor'(avb) -> !(cvd) \t !(avb) != (a=b) -> !(cvd)\r\n" + "")
    for (a in VALUES) {
        for (b in VALUES) {
            for (c in VALUES) {
                for (d in VALUES) {
                    for (e in VALUES) {
                        for (f in VALUES) {
                            println(a.toString() + "\t\t" + b + "\t\t" + c + "\t\t" + d + "\t\t" + or(a,b) + "\t" + and(c, d) + "\t\t" + conditional(or(a, b), and(c, d)) + "\t\t" + xor(e,f) + "\t\t" + disequal(or(a, b), xor(e, f)))
                        }
                    }
                }
            }
        }
    }
}

fun or(x: Boolean, y: Boolean): Boolean {
    return x || y
}

fun and(x: Boolean, y: Boolean): Boolean {
    return x && y
}

fun xor(x: Boolean, y: Boolean): Boolean {
    return x xor y
}

fun conditional(x: Boolean, y: Boolean): Boolean {
    return !(x && y)

}

fun bicontional(x: Boolean, y: Boolean): Boolean {
    return x == y
}

fun disequal(x: Boolean, y: Boolean): Boolean {
    return x != y
}

This code has been done by Android Studio only with its Java class converter to Kotlin, to use it you just have to right click on the class (from the project explorer) and then on the option to convert to Kotlin class , or directly use the keyboard shortcut "Ctrl + Alt + Mayus + K".

    
answered by 20.11.2018 / 12:33
source