• Post Reply Bookmark Topic Watch Topic
  • New Topic

Logic Gates Simulators  RSS feed

 
Abdulhameed Usman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please guys help me figure out this Assignment.

Logic Gates Simulator Write a program, in Java, that simulates the following logic gates: AND, OR, NOT, XOR, NOR, NAND  Your program should display a numbered list of gates and allow the user to choose a gate. It should accept binary input (not more than 2) and produce correct output based on the gate’s behaviour.  Each gate will be a method in your class (you should have one class for all the gates LogicGatesSimulator and a tester class).  Upload the two Java files in a single zipped folder  Make sure the input is correct.  You may want to allow the user to keep testing until he/she decides to exit.  You can use console based application.  There is a bonus point for GUI.  HINT: start simple and build up. e.g. i) Basic class ii) add a method iii) return a value
 
Tim Moores
Saloon Keeper
Posts: 4036
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you have so far, and where are you stuck making progress? If you haven't started yet, what ideas have you had?
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice to see this hint at the bottom:

HINT: start simple and build up. e.g. i) Basic class ii) add a method iii) return a value


in other words, baby steps.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is nicely described assignment.

I'd suggest first to write down for yourself a truth tables for each logical operator. Have you done that before?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, remember that an inverter or NOT gate only takes one input.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
You might not need truth tables, but you will need to work out the formulae for the gates. For example the output of an AND gate is i₁ ∧ i
using true for an input and false for no input. You would do well to write the formulae for all those gates, and then their Java® syntax equivalents.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One another thing:

Once you get logic gate AND method implemented, you'll be able to reuse it in NAND, because NAND will be inverted result of AND output. In different words, NAND method will call AND method. Same as with OR and NOR.
 
Abdulhameed Usman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't done it before I have no idea on how to do it
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abdulhameed Usman wrote:I haven't done it before I have no idea on how to do it

I doubt you'd be given assignments without first being taught or given materials from which you can get the proper foundational knowledge to be able to complete the assignment. We won't do your homework for you here. You'll need to go back over whatever you have already covered in class and figure out how to use what you've learned to at least make an attempt at solving this problem yourself.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case go from silicon‑based technology to carbon‑based technology with many centuries' experience.


Pencil, paper, and most important


ERASER (to destroy the evidence when you get it wrong ‍)
Write down what the inputs will be for an AND gate, and how you are going to use the formula to get the output. You can read more about AND gates on Wikipedia. I am going to have to warn you about making a method to mimic an AND gate. When you have managed it, it will look much simpler and easier than you thought.

Before you implement the methods, consider what their inputs will be. Can you simply use true/false?
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abdulhameed Usman wrote:I haven't done it before I have no idea on how to do it


Which bit are you stuck on?

Can you at least put a skeleton class together with (as suggested in the HINT) a single method?
 
Abdulhameed Usman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys, you being helpful I will try my best
 
Abdulhameed Usman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I going to instantiate each gate before before returning each value
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your instructions specifically state that "Each gate will be a method in your class (you should have one class for all the gates LogicGatesSimulator and a tester class)"
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I was wrong to suggest you to start from the truth tables. After all, maybe that wasn't so bad idea.

Do you know the differences between those logical operators, AND, OR, XOR? Their truth tables? You need to know them well before you move on.
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abdulhameed Usman wrote:Am I going to instantiate each gate before before returning each value


Why do you want to instantiate each gate ? maybe you could show us the code you have written so far so that we can help you out better ?

Here is what we want from you :
 
Ajinkya Ghonge
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abdulhameed Usman wrote:Am I going to instantiate each gate before before returning each value


Hi, welcome to coderanch. I would suggest that you followed the approach that Java was based on - object oriented approach. It is clearly mentioned that you require two classes. Now one of the class is you LogicGateSimulator class. Think of the properties of this class. How many instance variables would you want for this class? How many methods would you want for this class. Also I assume you know the role of && || and ^ operators in Java, if not be sure to find it out.

Your second class (Tester)would be a mere class to take input. just write a main method take three inputs from your console (two inputs and one specifying the type of operation to be performed on them) and call your LogicGateSimulator class method with. Think over it a bit and let us know if you are facing any issue.
 
Abdulhameed Usman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I know what && and || stands for & & =AND, ||=OR
 
Abdulhameed Usman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Vilda yes I know AND has 1 high output when both inputs are high
OR has 1 high output when one or more of its input is high
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great. So please try as suggested earlier, create one method for the start with AND or OR operator, which takes 2 arguments and gives you an output of operation result.

What arguments data type do you think would be an appropriate for those methods?
 
Abdulhameed Usman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As in how Argument data type??
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abdulhameed Usman wrote:As in how Argument data type??
Please check what is a method signature. And this tutorial. Please research a lot about your problem, about non known words to you yet. Once you have all information crammed and written on your note book, please ask which parts exactly are not clear about your assignment.
 
Abdulhameed Usman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK thanks man
 
Abdulhameed Usman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh you mean the primitive data type
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abdulhameed Usman wrote:Oh you mean the primitive data type
Yes, primitive. Which one? There are few of them.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not need to use primitives, but if you read the whole of this discussion you will find somebody has already given a suggestion for possible types of input.
 
Abdulhameed Usman
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Vilda integer
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use 1=on 0=off, but you would have to explore which operators you would apply. Don't let any other numbers into your methods.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Great. So please try as suggested earlier, create one method for the start with AND or OR operator, which takes 2 arguments and gives you an output of operation result.

I don't want to criticise, or confuse Abdulhameed too much; but there is an alternative way of looking at this:

Create an empty Gate class.

Why? Because that's the one thing you know right at the start: A "gate" is something that processes a binary digit (or stream) in a specific way.
The 'NAND' (or 'AND' or 'XOR') simply tells you how it does it; but nothing is going to happen without the gate itself.

Then you can decide how you want to implement it. And there are at least two distinct choices on how to proceed even then:

1. You can regard 'NAND' as a method (or procedure - ie, nand()), and implement one for each type of process.

2. You can regard a Gate as simply something that "processes binary digits", and:
(a) Give it a single abstract method called processBits().
(b) Create subtypes of Gate (eg, 'NAND') that override that method to do whatever the particular type of Gate actually does.

And there is no 'right' or 'wrong' about either approach. I'd say that #2 is more "objective" - and probably the one that people like Grady Booch and the writers of Java wanted us to be aware of, because it isn't as immediately obvious - but it's also arguably overkill for a problem like this.

However, if you don't know about the second one, you might miss out what may be the best solution of all (at least in Java):
Create an interface called Gate (or re-label your class), and add an enum called LogicGate that implements Gate, with members called 'AND', 'NAND, 'XOR' etc.

HIH

Winston
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abdulhameed Usman,

Ok mate, don't stress out about the mark now, that is not that important. Better is to try to understand this discrete mathematic's topic well.
Winston gave you a full bunch of good suggestions, but that is too early probably and your assignment instructions asking you to do it in slightly different way Write down to your notes this topic's URL to keep Winston's post as a reference, so you could come back to it and read it after a year or so.

Lets move on, when you were reading posts, you missed some good hints. Cambell Ritchie in his 2nd post mentioned, what kind of inputs (as method arguments/parameters) you could use for the start to ease the kick off.
Campbell Ritchie wrote:Can you simply use true/false?
That was a few days ago, Thursday. You must not need to use true or false, but that could be a good point to start for you in order to start getting an idea, so you could satisfy the bottom instruction a bit later.
Posted instructions wrote:It should accept binary input

You demonstrated, that you know how in Java AND and OR operators are expressed (do you know their "equivalent" bitwise operators?). Can you try to [1]build a method, [2]which accepts two parameters as its inputs, [3]uses one of logical operators for calculations, [4]produces an output (your posted instruction's last sentence says it should return a value, but for a start you can just print out result from the method).
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:. . . Cambell Ritchie in his 2nd post mentioned, . . .
Can you simply use true/false?
. . .
I also could use 1 and 0, or even a pair of objects called ON/OFF or similar.

You need to try one of those combinations and see if you can't get it to work. There are different operators which you can use on true/false or 1/0; remind yourself of the range of operators available. Another hint: the names of the operators will help you pick them.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
I don't want to criticise, or confuse Abdulhameed too much; but there is an alternative way of looking at this:

Create an empty Gate class.

Why? Because that's the one thing you know right at the start: A "gate" is something that processes a binary digit (or stream) in a specific way.
The 'NAND' (or 'AND' or 'XOR') simply tells you how it does it; but nothing is going to happen without the gate itself.
<snip other bits>


The requirements are pretty clear how this should be written.
I think any deviation is going to result in getting it wrong.

From the requirements in the first post:

Each gate will be a method in your class (you should have one class for all the gates LogicGatesSimulator and a tester class)


So we're talking 6 methods, one per logic gate, with however many parameters that particular gate requires.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:From the requirements in the first post:
Each gate will be a method in your class (you should have one class for all the gates LogicGatesSimulator and a tester class)
So we're talking 6 methods, one per logic gate, with however many parameters that particular gate requires.

Yup, you're quite right. I missed that.

@Abdulhameed: Forget my post for the moment, and follow the instructions you've been given.
It might be worth remembering that it's not the ONLY way to do it though.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!