d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need A Solution For This Java Problem, 100fg Ft
Add Reply New Topic New Poll
Member
Posts: 1,140
Joined: May 30 2009
Gold: 552.00
Oct 15 2016 08:56pm
what this program does is to use the method "filterOnSpeak" to get the objects having String "woof" and "meow"

as you can see the filterOnSpeak method has two for loops and i am asked to implement the filterOnSpeak method using only a single for loop.

100Fg for the first solution.

------------------------------------------------------------------------

public class FilterDriver {

public static void main(String[] args) {

Pet[] pets = {
new Pet("woof"),
new Pet("blub blub"),
new Pet("meow"),
new Pet("hissssss"),
new Pet("woof"),
new Pet("polly want a cracker"),
new Pet("meow"),
new Pet("woof"),
new Pet("woof")
};

Pet[] woofers = filterOnSpeak(pets, "woof");

for (Pet pet : woofers) {
System.out.println(pet);
}

Pet[] meowers = filterOnSpeak(pets, "meow");

for (Pet pet : meowers) {
System.out.println(pet);
}
}

//filterOnSpeak method
private static Pet[] filterOnSpeak(Pet[] pets, String needle) { //needle = input for "woof" and "meow"
int newLength = 0;
for (Pet pet : pets) {
if (pet.speak() == needle) {
newLength += 1;
}

}

// At this point we know how many woofers there are.
// This means that we can now create our woofers array.

Pet[] found = new Pet[newLength];
int nextIndex = 0;

for (Pet pet : pets) {
if (pet.speak() == needle) {
found[nextIndex] = pet;
nextIndex += 1;
}
}

return found;
}

}

----------------------------------------------------------------------------------------

public class Pet {
private String whatISay;

public Pet(String whatToSay) {
whatISay = whatToSay;
}

public String speak() {
return whatISay;
}

public String toString() {
return whatISay;
}
}

This post was edited by nkbartc on Oct 15 2016 09:15pm
Member
Posts: 1,140
Joined: May 30 2009
Gold: 552.00
Oct 15 2016 11:49pm
offering 200FG now. Anyone???
Member
Posts: 1,140
Joined: May 30 2009
Gold: 552.00
Oct 16 2016 01:22am
i used boolean equal method and solved this problem

no one won the reward :bonk:
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 16 2016 11:46am
Oct 15 2016 10:56pm
Oct 16 2016 01:49am
Oct 16 2016 03:22am

Quote
i used boolean equal method and solved this problem

no one won the reward :bonk:


first, i'm glad you were able to solve your issue within a few hours. (seriously. no sarcasm)

second, i dont know where you live, but that's 11pm on a saturday night in US East coast. i think most of the regulars here live in this time zone or close to it. expecting someone to rush to your aid at 2am sunday morning probably isn't realistic. you gotta be patient and wait a couple days.

third, 200 fg isn't significant. i think most of the regulars here already work full time in the field. if your goal is to pay people to do things for you, you'd have to offer something on par to what they're already getting paid to do.

with that said, what is the actual requirement? if you have to use an array (as opposed to an array list or other data structure that supports dynamic sizing), then i'm not clear how you'll avoid the second loop. as you mentioned, you have to know the size first before you can add to it. adding to an arraylist then converting to an array after still uses multiple loops internally. you can create the array with the same length of the original, but then you'll have nulls in there.
Member
Posts: 23,444
Joined: Jun 30 2009
Gold: 167.53
Oct 20 2016 06:39pm
Quote (nkbartc @ Oct 16 2016 03:22am)
i used boolean equal method and solved this problem

no one won the reward :bonk:


good job :)

This post was edited by umeshieee on Oct 20 2016 06:39pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll