Question

Create a two new gate classes, one called NorGate the other called NandGate. NandGates work like...

Create a two new gate classes, one called NorGate the other called NandGate. NandGates work like AndGates that have a Not attached to the output. NorGates work lake OrGates that have a Not attached to the output.Create a series of gates that prove the following equality NOT (( A and B) or (C and D)) is that same as NOT( A and B ) and NOT (C and D). Make sure to use some of your new gates in the simulation. in python.

class LogicGate:

    def __init__(self,n):

        self.name = n

        self.output = None

    def getName(self):

        return self.name

    def getOutput(self):

        self.output = self.performGateLogic()

        return self.output

class BinaryGate(LogicGate):

    def __init__(self,n):

       LogicGate.__init__(self,n)

        self.pinA = None

        self.pinB = None

    def getPinA(self):

        if self.pinA == None:

            return int(input("Enter Pin A input for gate "+self.getName()+"-->"))

        else:

            return self.pinA.getFrom().getOutput()

    def getPinB(self):

        if self.pinB == None:

            return int(input("Enter Pin B input for gate "+self.getName()+"-->"))

        else:

            return self.pinB.getFrom().getOutput()

    def setNextPin(self,source):

        if self.pinA == None:

            self.pinA = source

        else:

            if self.pinB == None:

               self.pinB = source

            else:

               print("Cannot Connect: NO EMPTY PINS on this gate")

class AndGate(BinaryGate):

    def __init__(self,n):

       BinaryGate.__init__(self,n)

    def performGateLogic(self):

        a = self.getPinA()

        b = self.getPinB()

        if a==1 and b==1:

            return 1

        else:

            return 0

class OrGate(BinaryGate):

    def __init__(self,n):

       BinaryGate.__init__(self,n)

    def performGateLogic(self):

        a = self.getPinA()

        b = self.getPinB()

        if a ==1 or b==1:

            return 1

        else:

            return 0

class UnaryGate(LogicGate):

    def __init__(self,n):

       LogicGate.__init__(self,n)

        self.pin = None

    def getPin(self):

        if self.pin == None:

            return int(input("Enter Pin input for gate "+self.getName()+"-->"))

        else:

            return self.pin.getFrom().getOutput()

    def setNextPin(self,source):

        if self.pin == None:

            self.pin = source

        else:

           print("Cannot Connect: NO EMPTY PINS on this gate")

class NotGate(UnaryGate):

    def __init__(self,n):

       UnaryGate.__init__(self,n)

    def performGateLogic(self):

        if self.getPin():

            return 0

        else:

            return 1

class Connector:

    def __init__(self, fgate, tgate):

        self.fromgate = fgate

        self.togate = tgate

       tgate.setNextPin(self)

    def getFrom(self):

        return self.fromgate

    def getTo(self):

        return self.togate

def main():

   g1 = AndGate("G1")

   g2 = AndGate("G2")

   g3 = OrGate("G3")

   g4 = NotGate("G4")

   c1 = Connector(g1,g3)

   c2 = Connector(g2,g3)

   c3 = Connector(g3,g4)

  print(g4.getOutput())

main()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Step 1: Change the code with mine code (which is extended one)

<==================(this line is not in the code)==================>

class LogicGate:
def __init__(self,n):
self.name = n
self.output = None
def getName(self):
return self.name
def getOutput(self):
self.output = self.performGateLogic()
return self.output

class BinaryGate(LogicGate):
def __init__(self,n):
LogicGate.__init__(self,n)
self.pinA = None
self.pinB = None

def getPinA(self):
if self.pinA == None:
return int(input("Enter Pin A input for gate "+self.getName()+"-->"))
else:
return self.pinA.getFrom().getOutput()
def getPinB(self):
if self.pinB == None:
return int(input("Enter Pin B input for gate "+self.getName()+"-->"))
else:
return self.pinB.getFrom().getOutput()

def setNextPin(self,source):
if self.pinA == None:
self.pinA = source
else:
if self.pinB == None:
self.pinB = source
else:
print("Cannot Connect: NO EMPTY PINS on this gate")

class AndGate(BinaryGate):
def __init__(self,n):
BinaryGate.__init__(self,n)
def performGateLogic(self):
a = self.getPinA()
b = self.getPinB()
if a==1 and b==1:
return 1
else:
return 0

class OrGate(BinaryGate):
def __init__(self,n):
BinaryGate.__init__(self,n)
def performGateLogic(self):
a = self.getPinA()
b = self.getPinB()
if a ==1 or b==1:
return 1
else:
return 0

class UnaryGate(LogicGate):
def __init__(self,n):
LogicGate.__init__(self,n)
self.pin = None
def getPin(self):
if self.pin == None:
return int(input("Enter Pin input for gate "+self.getName()+"-->"))
else:
return self.pin.getFrom().getOutput()

def setNextPin(self,source):
if self.pin == None:
self.pin = source
else:
print("Cannot Connect: NO EMPTY PINS on this gate")

class NotGate(UnaryGate):
def __init__(self,n):
UnaryGate.__init__(self,n)
def performGateLogic(self):
if self.getPin():
return 0
else:
return 1

class NorGate(BinaryGate):
def __init__(self,n):
BinaryGate.__init__(self,n)
def performGateLogic(self):
if (not self.getPinA()) and (not self.getPinB()):
return 1
else:
return 0

class NandGate(BinaryGate):
def __init__(self,n):
BinaryGate.__init__(self,n)
def performGateLogic(self):
if self.getPinA() and self.getPinB():
return 0
else:
return 1
  
class Connector:
def __init__(self, fgate, tgate):
self.fromgate = fgate
self.togate = tgate
tgate.setNextPin(self)
def getFrom(self):
return self.fromgate
def getTo(self):
return self.togate

def main():
#this is implementation of first Case
#NOT (( A and B) or (C and D))
a = AndGate("A")
b = AndGate("B")
e = OrGate("E")
g = NorGate("G")

c1 = Connector(a,e)
c2 = Connector(b,e)
c7 = Connector(e,g)
print(g.getOutput())
print("Now check the second case given")
#this is implementation of first Case
#NOT( A and B ) and NOT (C and D)
a = NandGate("A")
b = NandGate("B")
e = AndGate("E")
c1 = Connector(a,e)
c2 = Connector(b,e)
print(e.getOutput())

main()

<=====================>

Step 2: Run your code.

Here is my sample

Any problem ask me in the comment section.

Thumbs up is appreciated.

Add a comment
Know the answer?
Add Answer to:
Create a two new gate classes, one called NorGate the other called NandGate. NandGates work like...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am currently facing a problem with my python program which i have pasted below where i have to design and implement python classes and record zoo database and takes user input as query. the error i...

    I am currently facing a problem with my python program which i have pasted below where i have to design and implement python classes and record zoo database and takes user input as query. the error i am recieving says that in line 61 list index is out of range. kindly someone help me with it as soon as possible. Below is the program kindly check and correct it. Thanks! class Animal: def __init__(self, name, types, species, mass): self.name=name self.type=types...

  • I am currently facing a problem with my python program which i have pasted below where...

    I am currently facing a problem with my python program which i have pasted below where i have to design and implement python classes and record zoo database and takes user input as query. the error i am recieving says that in line 61 list index is out of range. kindly someone help me with it as soon as possible. Below is the program kindly check and correct it. Thanks! class Animal: def __init__(self, name, types, species, mass): self.name=name self.type=types...

  • PYTHON The provided code in the ATM program is incomplete. Complete the run method of the...

    PYTHON The provided code in the ATM program is incomplete. Complete the run method of the ATM class. The program should display a message that the police will be called after a user has had three successive failures. The program should also shut down the bank when this happens. [comment]: <> (The ATM program allows a user an indefinite number of attempts to log in. Fix the program so that it displays a message that the police will be called...

  • Score Analysis creating a class named "Student" in python and use matplotlib package

    class students:       count=0       def __init__(self, name):           self.name = name           self.scores = []           students.count = students.count + 1       def enterScore(self):                     for i in range(4):                 m = int(input("Enter the marks of %s in %d subject: "%(self.name, i+1)))                self.scores.append(m)             def average(self):                 avg=sum(self.scores)/len(self.scores)                           return avg               def highestScore(self):                           return max(self.scores)         def display(self):                          print (self.name, "got ", self.scores)                      print("Average score is ",average(self))                          print("Highest Score among these courses is",higestScore(self))          name = input("Enter the name of Student:")  s = students(name)  s.enterScore()  s.average()  s.highestScore()  s.display()So I need to print two things, first one is to compute the average score of these courses and second one is to print out the course name with the highest score among these courses. Moreover I need to import matplotlib.pyplot as plt to show the score figure. I barely could pull the code this...

  • PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment...

    PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment the lines please so I can understand. There are short and med files lengths for each the list of names/ids and then search id file. These are the input files: https://codeshare.io/aVQd46 https://codeshare.io/5M3XnR https://codeshare.io/2W684E https://codeshare.io/5RJwZ4 LinkedList ADT to store student records(code is below). Using LinkedList ADT instead of the Python List. You will need to use the Student ADT(code is below) Imports the Student class...

  • PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please...

    PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please so I can understand LinkedList ADT: class myLinkedList:     def __init__(self):         self.__head = None         self.__tail = None         self.__size = 0     def insert(self, i, data):         if self.isEmpty():             self.__head = listNode(data)             self.__tail = self.__head         elif i <= 0:             self.__head = listNode(data, self.__head)         elif i >= self.__size:             self.__tail.setNext(listNode(data))             self.__tail = self.__tail.getNext()         else:             current = self.__getIthNode(i - 1)             current.setNext(listNode(data,...

  • PYTHON QUESTION... Building a Binary Tree with extended Binary Search Tree and AVL tree. Create a...

    PYTHON QUESTION... Building a Binary Tree with extended Binary Search Tree and AVL tree. Create a class called MyTree with the methods __init__(x), getLeft(), getRight(), getData(), insert(x) and getHeight(). Each child should itself be a MyTree object. The height of a leaf node should be zero. The insert(x) method should return the node that occupies the original node's position in the tree. Create a class called MyBST that extends MyTree. Override the method insert(x) to meet the definitions of a...

  • Python3 programming help needed LAB*: Program: Online shopping cart (continued) Need the below code edited to run prope...

    Python3 programming help needed LAB*: Program: Online shopping cart (continued) Need the below code edited to run properly for the lab.. class ItemPurchase:     def __init__(self, nameitem='none', item_prc=0, item_quntity=0, item_descrp = 'none'):         self.nameitem = nameitem         self.item_prc = item_prc         self.item_quntity = item_quntity         self.item_descrp = item_descrp     def print_itemvaluecost(self):              string = '{} {} @ ${} = ${}'.format(self.nameitem, self.item_quntity, self.item_prc(self.item_quntity * self.item_prc))         valuecost = self.item_quntity * self.item_prc         return string, valuecost     def print_itemdescription(self):         string = '{}: {}'.format(self.nameitem, self.item_descrp)         print(string , end='\n')         return string class Shopping_Cart:     #Parameter...

  • Python3 programming help needed LAB*: Program: Online shopping cart (continued) Need the below code edited to...

    Python3 programming help needed LAB*: Program: Online shopping cart (continued) Need the below code edited to run properly for the lab.. class ItemPurchase:     def __init__(self, nameitem='none', item_prc=0, item_quntity=0, item_descrp = 'none'):         self.nameitem = nameitem         self.item_prc = item_prc         self.item_quntity = item_quntity         self.item_descrp = item_descrp     def print_itemvaluecost(self):              string = '{} {} @ ${} = ${}'.format(self.nameitem, self.item_quntity, self.item_prc(self.item_quntity * self.item_prc))         valuecost = self.item_quntity * self.item_prc         return string, valuecost     def print_itemdescription(self):         string...

  • in python and according to this #Creating class for stack class My_Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def Push(self, d): self.items.append(d) def Po...

    in python and according to this #Creating class for stack class My_Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def Push(self, d): self.items.append(d) def Pop(self): return self.items.pop() def Display(self): for i in reversed(self.items): print(i,end="") print() s = My_Stack() #taking input from user str = input('Enter your string for palindrome checking: ') n= len(str) #Pushing half of the string into stack for i in range(int(n/2)): s.Push(str[i]) print("S",end="") s.Display() s.Display() #for the next half checking the upcoming string...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT