Posted: 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:
code:
public class full_adder {
public static void main (String [] args) {
int size_array = 1;
boolean []in_A =new boolean [size_array];
boolean []in_B =new boolean [size_array];
in_A [0] = true;
in_A [1] = false;
in_B [0] = false;
in_B [1] = true;
boolean in_C = false;
boolean [] sum = new boolean [size_array];
boolean out_C = false;
int counter = 0;
}
public static boolean adder (boolean a, boolean b, boolean c){
sum [counter] = (a ^ b)^c;
out_C = (a && b)||((a ^ b)&&c);
counter += 1;
return out_C;
if (counter <= size_array) {
Posted: 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]
wtd
Posted: Thu Oct 09, 2008 3:40 pm Post subject: RE:ripple carry adder
Also worth noting:
code:
public static boolean adder (boolean a, boolean b, boolean c){
sum [counter] = (a ^ b)^c;
out_C = (a && b)||((a ^ b)&&c);
counter += 1;
return out_C;
if (counter <= size_array) {
Once you "return out_C" that's the end of execution for the method. The conditional is not evaluated.
Insectoid
Posted: 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:
code:
Syntax error on token "(", ; expected
Syntax error on token ")", ; expected
Syntax error, insert "}" to complete Block
Syntax error, insert "}" to complete ClassBody
at ripple_carry_adder.main(ripple_carry_adder.java:15)
it refers to this line:
code:
public static void main (String args []){
}
Do I really need a 'main', or is it just something people do?
wtd
Posted: Thu Oct 09, 2008 4:21 pm Post subject: RE:ripple carry adder
Posted: 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?
Posted: 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.
Insectoid
Posted: 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!
Sponsor Sponsor
Insectoid
Posted: 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,
code:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at ripple_carry_adder.main(ripple_carry_adder.java:13)
Posted: 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.
Insectoid
Posted: 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!
Clayton
Posted: 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.
Insectoid
Posted: 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...
OneOffDriveByPoster
Posted: 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.
wtd
Posted: 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.