Computer Science Canada ripple carry adder |
Author: | Insectoid [ Thu Oct 09, 2008 12:57 pm ] | ||
Post subject: | ripple carry adder | ||
Hi, I decided to try to make a ripple carry adder. Because I'm like that, I decided to do it recursively. I am rather new to arrays in Java, which I wanted to use, and so I did. This is my first time using a non-static method, and it is a little bit over my head. The adder works (I did a half-adder and a full adder, now I'm adjusting the full adder to create my ripple carry adder, so the math bit works). The problem? Lots of them. Mostly syntax, but I can't find the problems. It says I need identifiers where there are identifiers, that I'm missing ';' on lines that have them, etc. Right now, it only has static input, so true = 1 and false = 0. Eventually I'll add in a binary converter, but right now, I want what I have to work. Here's my code:
|
Author: | [Gandalf] [ Thu Oct 09, 2008 3:24 pm ] |
Post subject: | RE:ripple carry adder |
You're declaring variables in the scope of main() and then trying to use the in adder(). You'll have to pass them to adder() as parameters or make them instance variabeles and make an object of your class or make them static. [/rushed post] |
Author: | wtd [ Thu Oct 09, 2008 3:40 pm ] | ||
Post subject: | RE:ripple carry adder | ||
Also worth noting:
Once you "return out_C" that's the end of execution for the method. The conditional is not evaluated. |
Author: | Insectoid [ Thu Oct 09, 2008 3:47 pm ] | ||||
Post subject: | RE:ripple carry adder | ||||
@ wtd-well...thanks. All I had to go on for this was a rushed explanation by my teacher about functions and returning variables. How do I declare instance variable? Is a variable declared inside a method local to that method? Would I declare them outside a method? Outside the class? My complete new-ness to object oriented programming is showing, isn't it? Edit: I changed some things, but now I get this error:
it refers to this line:
Do I really need a 'main', or is it just something people do? |
Author: | wtd [ Thu Oct 09, 2008 4:21 pm ] |
Post subject: | RE:ripple carry adder |
http://wiki.compsci.ca/index.php?title=Introduction_To_Java |
Author: | Insectoid [ Thu Oct 09, 2008 6:58 pm ] | ||
Post subject: | RE:ripple carry adder | ||
Okay, I changed some stuff around, but there are some errors still. I suppose it is caused by variables declared in 'main' being used in 'adder'. How do I counter this? Is there a way to make variables universal? Such that any method can use/manipulate them? Code:
|
Author: | [Gandalf] [ Thu Oct 09, 2008 7:10 pm ] |
Post subject: | RE:ripple carry adder |
Yes, keep reading wtd's tutorial, it's all in there and more. Go step by step, when you're done (or at least well into it) none of this should be a problem, and you'll gain understanding far beyond the scope of this assignment. ![]() |
Author: | Insectoid [ Thu Oct 09, 2008 7:19 pm ] |
Post subject: | RE:ripple carry adder |
Oh, this isn't an assignment. I'm doing it for fun. My engineering teacher challenged my to just do the schematic diagram for a half-adder, and having done that, I decided to try to program it! |
Author: | Insectoid [ Thu Oct 09, 2008 7:34 pm ] | ||||
Post subject: | RE:ripple carry adder | ||||
Okay, double posting time! I looked over the tutorial some more, and found out about private variables and stuff, then my editor (eclipse) told me to make the some variables static, so I did, and now have one error,
full code:
|
Author: | Clayton [ Thu Oct 09, 2008 8:04 pm ] |
Post subject: | RE:ripple carry adder |
What's going on with your indentation? Stick with a consistent indentation, otherwise your code is horrible to try and read. Also, on your naming scheme, it is common practice (actually, it's the standard) to use camelCase when naming variables and methods, CamelCase when naming classes, and UPPER_CASE when naming constants. |
Author: | Insectoid [ Thu Oct 09, 2008 8:07 pm ] |
Post subject: | RE:ripple carry adder |
I'll eventually get into the gist of it. I really do enjoy underscores. So everybody will know it's my code! |
Author: | Clayton [ Thu Oct 09, 2008 8:13 pm ] |
Post subject: | RE:ripple carry adder |
There exists a standard for a reason. You'll find it's much easier to get help from a large community such as the one that is behind Java if you stick to those standards. It's good to get into the habit of using them. |
Author: | Insectoid [ Thu Oct 09, 2008 8:21 pm ] |
Post subject: | RE:ripple carry adder |
I suppose the indentation is the result of the lack of Turing's F2. Fine, I'll work on my naming conventions... |
Author: | OneOffDriveByPoster [ Thu Oct 09, 2008 8:25 pm ] |
Post subject: | Re: RE:ripple carry adder |
insectoid @ Thu Oct 09, 2008 8:21 pm wrote: I suppose the indentation is the result of the lack of Turing's F2. Fine, I'll work on my naming conventions... Eclipse has some of the best auto-indenting available. |
Author: | wtd [ Thu Oct 09, 2008 8:46 pm ] |
Post subject: | RE:ripple carry adder |
Yes. I like underscores too, but reading Java code with them used extensively makes me want to gouge out my eyeballs even more than reading Java code usually does. |
Author: | Insectoid [ Fri Oct 10, 2008 10:47 am ] |
Post subject: | Re: RE:ripple carry adder |
OneOffDriveByPoster @ Thu Oct 09, 2008 8:25 pm wrote: insectoid @ Thu Oct 09, 2008 8:21 pm wrote: I suppose the indentation is the result of the lack of Turing's F2. Fine, I'll work on my naming conventions... Eclipse has some of the best auto-indenting available.How? I'm new to eclipse. |
Author: | andrew. [ Wed Oct 15, 2008 2:49 pm ] |
Post subject: | RE:ripple carry adder |
It's CMD + I on Mac. I suppose it would be CTRL + I on Windows. |
Author: | Insectoid [ Wed Oct 15, 2008 3:46 pm ] |
Post subject: | RE:ripple carry adder |
Well, I'm on a mac, so I guess it's CMD+I for me. I got it working now, so I'm just adding in the UI, which requires a decimal-binary converter and many int-to-string commands. Basically, it's a lot of easy stuff that takes a lot of time. Fun. ![]() |