Results 1 to 20 of 33
Thread: Problem with method & return
- 03-19-2011, 05:10 AM #1
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Problem with method & return
Hello,
I have this problem. I am fairly new, well born again, to Java and I'm working on this little code, but I ran into this problem.
so I created a class to simply run a loop, where it counts until a value is reached.
So In my constructor, I pass 6 variables and then I set them equal to 6 variables that I had defined within the class, so far so good.
Then I create a method and here lies the problem.
The variables are all of the integer type, but I want to return the printed statement. When I compile it, naturally, I get an error about the return type not being compatible "incompatible types". Is there a way around this?Java Code://Creates a class called Combination where the possible combinations will be returned. public class Combination { //Declares value of variables in the method public int a = 0; public int b = 0; public int c = 0; public int d = 0; public int e = 0; public int f = 0; public int g = 0; public int h = 0; public int count = 1; //constructor public Combination (int Wmb1, int Wmb2, int Wmb3, int Wmb4, int Wmb5, int Mb, int WmbLimit, int MbLimit) { a = Wmb1; b = Wmb2; c = Wmb3; d = Wmb4; e = Wmb5; f = Mb; g = WmbLimit; h = MbLimit; } public getCombo() { System.out.println ("The number of possible combinations are: "); while (f<h){ System.out.println (count+"."+a+" "+b+" "+c+" "+d+" "+e+" "+f); count++;//increments the count, which is equal to number of combinations f++;//Increments f return;//Help, this is where I'm stuck } } }
Should I print it in the main class? Can I return more than one variable? Can I use more than one return statement within a method?
Thank you,
bigsonny
- 03-19-2011, 05:19 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Could you post the actual compiler message? If there are more than one, post them all.
I ask because the getCombo() method declaration has problems before the return statement.
- 03-19-2011, 05:25 AM #3
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Sure. So to be consistent, this is my latest version of the code itself and following is the compiler message.
The compiler message is:Java Code://Creates a class called Combination where the possible combinations will be returned. public class Combination { //Declares value of variables in the method public int a = 0; public int b = 0; public int c = 0; public int d = 0; public int e = 0; public int f = 0; public int g = 0; public int h = 0; public int count = 1; //constructor public Combination (int Wmb1, int Wmb2, int Wmb3, int Wmb4, int Wmb5, int Mb, int WmbLimit, int MbLimit) { a = Wmb1; b = Wmb2; c = Wmb3; d = Wmb4; e = Wmb5; f = Mb; //Passes values of White ball limit and Mega ball Limit to the variables in the methods declaration g = WmbLimit; h = MbLimit; } public int getCombo() { System.out.println ("The number of possible combinations are: "); while (f<h){ System.out.println (count+"."+a+" "+b+" "+c+" "+d+" "+e+" "+f); count++;//increments the count, which is equal to number of combinations f++;//Increments f return; } } }
----jGRASP exec: javac -g Combination.java
Combination.java:39: missing return value
return;
^
1 error
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
- 03-19-2011, 05:25 AM #4
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Thank you for your help!
- 03-19-2011, 05:27 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
But to answer you questions at the end: you return exactly one thing per method (of the type declared by that method) and returning it will be the very last thing the method does.
- 03-19-2011, 05:30 AM #6
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
- 03-19-2011, 05:31 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
OK, so (in your revised code!) getCombo() is supposed to return an int. Is it supposed to return the number of combinations? In that case you have a variable, count, which, according to the comment holds the value you want to return.
So the return statement should be
Java Code:return count;
As I said before returning is the last thing your method will do. So put the return statement after the while loop. Only then has count done all its incrementing.
- 03-19-2011, 05:33 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
is it possible to combine a bunch of things as one thing, then return it?
Yes it is. You write a class to represent the bunch and you return an instance of that class.
- 03-19-2011, 05:36 AM #9
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Well it's not supposed to return count alone.
I actually want to tell it to print count, a, b, c, d, e, and f, as long as f is smaller than a given limit. So the idea would be for it to actually return all of the different combinations.
- 03-19-2011, 05:37 AM #10
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Yes, but do you return a single value, or could I return numerous values and print them all from that single return statement?
- 03-19-2011, 06:53 AM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
I sounds to me that some other class should be constructing the combinations, counting them and printing out what it wants. The Combination class should just model what you expect a combination to be - creating a bunch of combinations is not really typical behaviour of a combination.
- 03-19-2011, 07:08 AM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Perhaps it's easier to describe with code. In what follows a DiceCombo illustrates the very limited nature of what a combination is. Another class, Roller, is responsible for generating combinations with a given (rather trivial) property. Roller's getCombos() method returns just a single thing (a list == bunch of combinations) and it prints nothing. Yet another method - in this case main(), but it could be anything - uses what getCombos() returns to print stuff.
Java Code:/** * A class representing the values of three distinguished dice. */ public class DiceCombo { private int redDie; private int greenDie; private int blueDie; /** * Creates a combo with given values. */ public DiceCombo(int redDie, int greenDie, int blueDie) { this.redDie = redDie; this.greenDie = greenDie; this.blueDie = blueDie; } /** Returns the total of the dice. */ public int getTotal() { return redDie + greenDie + blueDie; } @Override public String toString() { return String.format("DiceCombo[%d,%d,%d]", redDie, greenDie, blueDie); } } import java.util.ArrayList; import java.util.List; /** * A class representing a bunch of DiceCombo instances that satisfy some condition - in * this case a trivial one: that the sum equals some given value. */ public class Roller { private int total; public Roller(int total) { this.total = total; } /** * Returns the bunch of DiceCombos that satisfy this roller's total condition. */ public List<DiceCombo> getCombos() { // construct the bunch that will be returned List<DiceCombo> ret = new ArrayList<DiceCombo>(); // figure out the combos that have the desired total for(int red = 1; red < 7; red++) { for(int green = 1; green < 7; green++) { for(int blue = 1; blue < 7; blue++) { DiceCombo combo = new DiceCombo(red, green, blue); if(combo.getTotal() == total) { ret.add(combo); } } } } // return the bunch return ret; } public static void main(String[] args) { int testTarget = 11; Roller test = new Roller(testTarget); List<DiceCombo> combos = test.getCombos(); System.out.printf( "There are %d combos found whose total is %d.%n", combos.size(), testTarget); System.out.println("They are:"); for(DiceCombo combo :combos) { System.out.println(combo); } } }
- 03-19-2011, 02:43 PM #13
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Thank you. I'll try it out and get back to you either way. Once again, thank you for taking the time to help.
- 03-19-2011, 07:01 PM #14
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Oh okay, I think I get it.
I had the same thing. I had my main class, similar to the roller class above and I had my combo class, similar to the DiceCombo.
I am dissecting the roller class and I have a few questions please:
1) What does this do:
I understand the first public class Roller. I understand the variable, though I am not sure why it is private.Java Code:public class Roller { private int total; public Roller(int total) { this.total = total; }
However, I am completely lose when I see the public Roller (int total). Is there another roller class? Is this an instance?
2) Why is that necessary?
3) I don't see the point of the DiceCombo class, if all it does is return the values and the string format? Those are only a few lines of code, they don't seem to improve the main class. Am I failing to appreciate something?
4) In the DiceCombo class, there is a string formatting, is this what I should have done to return mine?
- 03-19-2011, 07:37 PM #15
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
consider to use int[]I actually want to tell it to print count, a, b, c, d, e, and f,
- 03-19-2011, 07:43 PM #16
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
- 03-19-2011, 07:57 PM #17
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Java Code:public int[] getCombo() { while (f<h){ System.out.println (count+"."+a+" "+b+" "+c+" "+d+" "+e+" "+f); count++; f++; } [B]int [] i = {count, a, b, c, d, e, f};[/B] // if you want to return values after [I]while[/I] loop return [B]i[/B]; }
- 03-19-2011, 08:15 PM #18
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
- 03-19-2011, 09:01 PM #19
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Yes it is Array.
You can use it like:
Java Code:int [] newArray = new Combination(/* ints for constructor*/).getCombo(); int number = newArray[0]; // will return first value in the array, in your case, value of [I]count[/I] variable...
- 03-19-2011, 09:52 PM #20
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
The total in the Roller class represents the total that the roller will "select" for. Ie only dice combos with this total will be included in the combos that the roller generates.
There is an instance of Roller, but it doesn't appear until the main() method - and it could in practice be in some other class altogether. That instance is generating the combos with a total of 11.
2) Why is that necessary?
It's not necessary to split the problem into two classes. But, it seemed to me, you had two different sorts of things: combinations, and combination generating processes. My thinking - such as it was - was a kneejerk reaction along the lines of "2 types of thing: two classes".
3) I don't see the point of the DiceCombo class, if all it does is return the values and the string format? Those are only a few lines of code, they don't seem to improve the main class. Am I failing to appreciate something?
Simple is good! A combination is a simple sort of thing. The behaviour is limited to returning the information that is needed to decide on inclusion in a bunch of combos (which can be counted etc), and some String information that can be used to textually describe the combination.
(My motivation here was your question where you seemed to be doing those two things: creating a bunch of combos, and wanting to print stuff about them.)
As I said above your original question referred to the need to print the combinations at some point. This meant I was going to have the DiceCombo class have a method returning a String to be printed. toString() just seemed like a good idea, but there is nothing special about that. The important point is that I converted your demand for a method that would (in part) print a whole lot of combos into a method that would just return a String.4) In the DiceCombo class, there is a string formatting, is this what I should have done to return mine?
String.format() is just a personal preference.
------------------------
By the way, I haven't tried to figure out what your code is doing or you describe it. So there is something a bit general about my points. I just see a method doing multiple things (calculating, printing, returning) and think there should be multiple methods. Likewise I see combinations and the generation of possible combinations subject to a constraint, and I think there should be multiple classes (combinations and generators).
Similar Threads
-
Problem with my return method
By braddy in forum New To JavaReplies: 4Last Post: 10-19-2010, 06:34 AM -
Not able to return the method value
By dmakshay2002 in forum Advanced JavaReplies: 11Last Post: 05-28-2010, 02:07 PM -
Method won't return value
By footyvino in forum New To JavaReplies: 2Last Post: 03-26-2010, 10:49 AM -
Method return type problem
By McChill in forum New To JavaReplies: 7Last Post: 05-05-2009, 09:21 PM -
Return value of method
By cachi in forum New To JavaReplies: 1Last Post: 08-01-2007, 08:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks