Closed Thread Icon

Preserved Topic: Java Question (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=13052" title="Pages that link to Preserved Topic: Java Question (Page 1 of 1)" rel="nofollow" >Preserved Topic: Java Question <span class="small">(Page 1 of 1)</span>\

 
Rekabwerb
Nervous Wreck (II) Inmate

From: Weddington, NC, US
Insane since: Jan 2004

posted posted 02-10-2004 14:41

class question => A string contains only '0' and '1' characters and spaces. Write a method that takes such a string and makes and returns a "negative" string in which all 0's are replaced with 1's and 1's with 0's. Your methos must rely only on String's methods and not use any iterations or recursion.

I've come up with:
String s1 = "0 1 0 1";
s1 = s2.replace(1, 0).replace(0, 1)

my questions => What's iteration and recursion? and Is what I have close to being right?

I don't know what it means by returning a negative string...

~Sh** happens, wear a diaper

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 02-10-2004 17:37

AFAIK there isn't a "negative" string. I believe this was just the term used for the question.

An interation is a loop where you would parse each character individually ( Iterate through the string )

Recursion is a function that calls itself.

Your answer is close but you are running into a problem. (Besides you had s1 and s2 backwards in the second line)

What would this give you?
String s1 = "0 1 0 1";
s2 = s1.replace(0, 1)

s2 would now equal "1 1 1 1"

So your second replace would just switch it all to zeros. There's a simple fix but I'll let you figure it out.



.:[ Never resist a perfect moment ]:.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 02-10-2004 18:22

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 02-10-2004 19:13

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 02-10-2004 19:20

Nope recursion is only when a function calls itself not multiple calls to the same function.



.:[ Never resist a perfect moment ]:.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 02-11-2004 02:04

How about an example of the two:

recursion, for example you are searching through a binary tree for a specific value.

code:
public Node findXinTree(Node root, int x){
if(root.value = x){
return root;
}
else if(root.value > x){
if(root.getLeftNode() == null) return null;
return findXinTree(root.getLeftNode(),x);
}
else {
if(root.getRightNode() == null) return null;
return findXinTree(root.getRightNode(),x);
}
}



For iteration it is simply something like a list traversal or enumeration.

code:
public int findXinVector(Vector v, int x){
for(int i=0; i < v.size(); i++){
if((int)v.get(i) == x){
return i;
}
}
return -1;
}



If you look through that code it should give you an idea of how it all works.

note: not guaranteed to be syntatically or logically correct.

-Dan-

Rekabwerb
Nervous Wreck (II) Inmate

From: Weddington, NC, US
Insane since: Jan 2004

posted posted 02-11-2004 14:45

thanks for the help, my classmate and I came up with this, but I think it has the same problem as the first thing I posted

public String(String pos, String neg, String temp);
{
String zero = 0
String one = 1
neg = pos.replace(zero, one).replace(one, zero);
}

I read over InI's post again, You're just using the 2's to be like a placeholder so it wont be 1 1 1 1 changed to 0 0 0 0... so I think the above still wouldnt work. I'm starting to catch on though 8)

~Sh** happens, wear a diaper

[This message has been edited by Rekabwerb (edited 02-11-2004).]

synax
Maniac (V) Inmate

From: Cell 666
Insane since: Mar 2002

posted posted 02-11-2004 15:04

Whenever you have to "swap" two elements (like in sorting), chances are you're going to need a temporary placeholder for one of the elements so you don't lose it. This is something to keep in mind for tests and assignments, as (in my case anyhow) it comes up often.

"Nothin' like a pro-stabbin' from a pro." -Weadah

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 02-11-2004 20:55

The reason for the 2's is so your string will follow the following metamorphisis

("0 1 0 1 0 1").replace(1,2) = "0 2 0 2 0 2"

So your next replace will act like

("0 2 0 2 0 2").replace(0,1) = "1 2 1 2 1 2"

Then you will replace the two's back to 0

("1 2 1 2 1 2").replace(2,0) = "1 0 1 0 1 0"

Which would be your answer.

If you simply replace the 0 to 1 and then 1 to 0 you will have your string

0 1 0 1 0 1 turn into 1 1 1 1 1 1 and then 0 0 0 0 0 0 because the operations happen sequentially.
When you do your first replace you will get a string of all the same numbers, your state is not saved unless you make sure to save it in some way or another, in Ini's example the state was saved using the number 2, which would logically correspond to a 1 which has been changed to a 0.


-Dan-

« BackwardsOnwards »

Show Forum Drop Down Menu