Page 1 of 2 12 LastLast
Results 1 to 20 of 33
  1. #1
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default 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.

    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
    	}
    	}
    
    }
    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?

    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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  3. #3
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Sure. So to be consistent, this is my latest version of the code itself and following is the compiler message.

    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;
    	}
    	}
    
    }
    The compiler message is:
    ----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.

  4. #4
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Thank you for your help!

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  6. #6
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by pbrockway2 View Post
    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.
    Oh okay. So is it possible to combine a bunch of things as one thing, then return it?

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  9. #9
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    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.

  10. #10
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Yes, but do you return a single value, or could I return numerous values and print them all from that single return statement?

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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);
    		}
    	}
    }

  13. #13
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Thank you. I'll try it out and get back to you either way. Once again, thank you for taking the time to help.

  14. #14
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    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:

    Java Code:
    public class Roller {
    	private int total;
    	
    	public Roller(int total) {
    		this.total = total;
    	}
    I understand the first public class Roller. I understand the variable, though I am not sure why it is private.

    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?

  15. #15
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    270
    Rep Power
    4

    Default

    I actually want to tell it to print count, a, b, c, d, e, and f,
    consider to use int[]

  16. #16
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by milovan View Post
    consider to use int[]
    as in an array?

  17. #17
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    270
    Rep Power
    4

    Default

    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];
    }

  18. #18
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by milovan View Post
    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];
    }
    Oh sweet. This might work. Just a few questions. Is this technically an array? Also, Do the values retain their type? Will the remain int? Thanks.

    pbrockway2, I would still like to know the answers if you don't mind. Thank you!

  19. #19
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    270
    Rep Power
    4

    Default

    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...

  20. #20
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by bigsonny View Post
    1) What does this do:

    Java Code:
    public class Roller {
    	private int total;
    	
    	public Roller(int total) {
    		this.total = total;
    	}
    I understand the first public class Roller. I understand the variable, though I am not sure why it is private.

    However, I am completely lose when I see the public Roller (int total). Is there another roller class? Is this an instance?

    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.)

    4) In the DiceCombo class, there is a string formatting, is this what I should have done to return mine?
    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.

    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).

Page 1 of 2 12 LastLast

Similar Threads

  1. Problem with my return method
    By braddy in forum New To Java
    Replies: 4
    Last Post: 10-19-2010, 06:34 AM
  2. Not able to return the method value
    By dmakshay2002 in forum Advanced Java
    Replies: 11
    Last Post: 05-28-2010, 02:07 PM
  3. Method won't return value
    By footyvino in forum New To Java
    Replies: 2
    Last Post: 03-26-2010, 10:49 AM
  4. Method return type problem
    By McChill in forum New To Java
    Replies: 7
    Last Post: 05-05-2009, 09:21 PM
  5. Return value of method
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 08:23 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •