Jump to content

returning multiple values

- - - - -

  • Please log in to reply
8 replies to this topic

#1
Cruel Hand

Cruel Hand

    Learning Programmer

  • Members
  • PipPipPipPip
  • 109 posts
  • Programming Language:Java
  • Learning:Java, Visual Basic .NET
I have created an Underwear object and I have two methods, getLength() and getWidth(). Instead of those, I was wondering if I could make a class, getSize(), that would return both the length and width?

public class Underwear{

	String color;

	int length, width;

	

	public tuna(String color, int length, int width){

		this.color = color;

		this.length = length;

		this.width = width;

	}

	public void wear(){

		System.out.println("You wear the underwear.");

	}

	public void examine(){

		System.out.println("The underwear is " + color + " and the size is " + length + ", " + width);

	}

	public void hangUp(){

		System.out.println("You hang up the underwear.");

	}

	public String getColor(){

		return color;

	}

	public int getLength(){

		return length;

	}

	public int getWidth(){

		return width;

	}

	

}

I just realized that the examine method kind of nullifies the purpose of the getSize method.

#2
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript
You can only return one value from a method. But what you could do is to return one value which includes more than one value, like an array or a vector or something similar.

#3
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
java.awt.Dimension is a nice class that contains both a width and a height. You could have your method return a Dimension object.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#4
Java_

Java_

    Newbie

  • Members
  • PipPip
  • 13 posts
I believe a method can only return one value, as Vswe has stated. But you could try


public int getSize(){
return width;
return length;
}

#5
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP

Java_ said:

I believe a method can only return one value, as Vswe has stated. But you could try


public int getSize(){
return width;
return length;
}
This is completely wrong.

#6
Cruel Hand

Cruel Hand

    Learning Programmer

  • Members
  • PipPipPipPip
  • 109 posts
  • Programming Language:Java
  • Learning:Java, Visual Basic .NET

Java_ said:

I believe a method can only return one value, as Vswe has stated. But you could try


public int getSize(){
return width;
return length;
}

your code contradicts what you said. Thanks for trying to help anyway

#7
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts

Java_ said:

I believe a method can only return one value, as Vswe has stated. But you could try

public int getSize(){
        return width;
        return length;  <---- Unreachable statement. I don't think this will even compile. 
}

You can return a container object with multiple values.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#8
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Gregwarner's post seems like the best solution. Dimension has already been created for you and holds width and height variables. If you're thinking about expanding this method later to return more than 2 variables, perhaps you should use a List instead.

#9
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
I agree. Use the existing dimension object, or create one yourself. Too bad java doesn't have out parameters.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users