Personal tools
You are here: Home → Comp 170 Introduction to OOP → Course Materials → Some C# Code, Fragments, Examples → Truth Tables are Helpful
Navigation
Log in


Forgot your password?
 
Document Actions

Truth Tables are Helpful

by Dr. William L. Honig — last modified 2014-10-20 06:33 PM

Learn to use truth tables to understand logic and control flow in your programs

Click here to get the file

Size 2.2 kB - File type text/x-csharp

File contents

/*Comp 170 Standard C# Header
 *Assignment name: Implementing Truth Tables with Boolean Logic
 *LUC id and Name whonig Dr. W L Honig
 *Date 9Sep14
 *
 *Get more practice with Booleans.  
 *The main uses the facade design pattern to get all user input.  It includes a sentinel loop
 *so that the user can give as many inputs they wish
 *
 *Questions: None
 * 
 * 
 */
using System;

namespace TruthTables
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			bool a;
			bool b;
			bool result;
			string inputLine;
			string[] inputValues;
			const string SENTINEL_VALUE = "STOP";

			Console.WriteLine ("Welcome to my truth table tester");

			Console.WriteLine ("Enter two Boolean values as True or False or STOP to quit");
			inputLine = Console.ReadLine();
			inputLine = inputLine.Trim();
			while ( inputLine != SENTINEL_VALUE )
			{
				inputValues = inputLine.Split( ' ' );
				a = bool.Parse( inputValues[0] );
				b = bool.Parse ( inputValues[1] );
				result = TruthTable1110( a, b);
				Console.WriteLine ( "Truth table applied to " + a + " and " + b + " computes " + result );
		 		Console.WriteLine ("Enter two Boolean values as True or False or STOP to quit");
				inputLine = Console.ReadLine();
				inputLine = inputLine.Trim();

			}
			Console.WriteLine ("Thank you for using the truth table program.  What logic function was that?");

		}
		/* TO DO:  Your job is to add two more functions for some other truth tables
		 * Pick any two *other than* AND OR NOT
		 * Implement a method for each of your two to replace TruthTable1110 below (change the name)
		 * Update the truth table with the ones your implement
		 * 
		 * Be sure to test your program by modifying the facade main above
		 * You can test your methods one at a time or both at once.
		 */

		public static bool TruthTable1110 (bool a, bool b)
		{
			/* This function implements a NAND gate according to this truth table
			 * 
			 *    A    B   Result
			 *    F    F   T
			 *    T    F   T
			 *    F    T   T
			 *    T    T   F
			 * 
			 */

			//if ( a && b ) return false;
			//else return true;
			return (!a && !b) || (a && !b) || (!a && b);
		}
	}
}
« October 2027
Su Mo Tu We Th Fr Sa
12
3456789
10111213141516
17181920212223
24252627282930
31
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: