Advertisement
mrScarlett

logic gates Java

Jan 30th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. import java.util.Scanner;
  2. /*Here is the beginning of a program that:
  3.  * Asks the user what logic gate they want to choose
  4.  * Asks the input for input A and B
  5.  * uses a subroutine called andGate that accepts values for input A and B
  6.  * The subroutine then produces the correct output depending on the input
  7.  *
  8.  * Develop the code to:
  9.  * include subroutines for NOT, OR, NOR, NAND and XOR gates
  10.  * ask the user to choose what gat they want to choose and produce the correct output.
  11.  *
  12.  * Extension:
  13.  * Use a loop so that the user can keep choosing gates until END is entered.
  14.  *
  15.  */
  16. public class logicGates
  17. {
  18.     public static int andGate (int a, int b){//AND subroutine
  19.         if (a==1 && b==1){
  20.             return 1;
  21.         }
  22.         else{
  23.             return 1;
  24.     }
  25. }
  26. public static void main (String args[]){
  27.     System.out.print("\u000C");
  28.     String gate;
  29.     int aInput, bInput;
  30.     Scanner input =new Scanner (System.in);
  31.     System.out.println("What is the input for A? ");
  32.     aInput = input.nextInt();
  33.     System.out.println("What is the input for B? ");
  34.     bInput = input.nextInt();
  35.     System.out.println("choose a gate");
  36.     gate = input.next();
  37.     if (gate.equals("and")){
  38.         System.out.println("The output is "+andGate(aInput, bInput)); //what happens with this code?
  39.     }
  40.    
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement