• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Returning Multiple Values

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I return multiple values from a function? One of my friends said I had to create an object to store the values and then return the object, but I cant really grasp the concept since I dont really know much about java (yet).

Here's an example from the game I'm writing

Potions.java


Main.java
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can either
1. As your friend saids make a class which will hold the returned values
2. or return an array holding the returned values
3. or separate the method in several smaller methods
(there are probably other ways...)

It usually depends on the context. Here, you want to return the number of remaining potions and the player health after drinking the potions. Solution 1 could be appropriate if the player looks something like this :

In that case, you could have a usePotion() method in the Player class, and you wouldn't need the return any value (no parameter either).

Solution 2 would look like :

public int askNumberOfPotionToUse(int availablePotion)

where nbPotion is the int returned by askNumberOfPotionToUse. The returned value would be the player health after drinking.

Personally, I think the health and the potions hold by the player belong to the Player class, so I'd go for solution 1.
 
Brandon Golway
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks for the response, Ill have to think about which method (not meaning a function lol) to use. The game is set up so that there isnt a player class, theres just the int called PlayerHealth. The reason for this is most likely because the game was originally written in python as a final project for one of my college classes and I only had about a week to work on it.

Im taking a java class now and just for the hell of it I decided to convert the program to java since it would be easier to manage.


(skip to my second post for a more concise example)
Heres a simple diagram for my program:

ActionMenu.java contains the main() class with a menu that has two main options (move or check inventory), which has further sub-options below those.

Move just progresses to If statements which call methods in either Monsters.java or Movements.java. There a five of them in Movements.java: findMoney(), findWeapon() which calls a function in Inventory.java, findPotion(), foundNothing(), and GameOver().

Inventory calls ListInventory from Inventory.java which contains 3 methods: ListInventory() which calls ListWeapons() and either NoPotions or UseOrBuy from Potions.java, AddWeapon() and ListWeapons(). Potions.java contains 4 methods: UseOrBuy() which calls either UsePotion() or BuyPotions(), and NoPotions() which also calls BuyPotions().


Edit:
I tried to implement your second way, but it doesnt increase player health or decrease the amount of potions. Im probably missing something since Im doing this during another class

 
Brandon Golway
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heres a simpler example that pretty much does the same thing.

 
Ranch Hand
Posts: 67
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may want to consider the introduction of a player class:



 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brandon wrote:I tried to implement your second way, but it doesnt increase player health


You have to use the returned value of the usePotion method (note that it's more common to give methods a name starting with a small letter) to update the player health.

Read this FAQ about methods parameters in Java. That will explain you why the values are not changed.

Mike wrote:You may want to consider the introduction of a player class:


I agree with Mike. Even though you'd like to port the program as-is, you should try to make your program more OO-ish It will help a lot.
 
Brandon Golway
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the suggestions, I'm going to create a player class since both of you recommended it, its just hard to get used to using objects since all the programming I've ever done is more linear.

Edit:
I tried to implement this and changed stuff around and now I'm getting "Cant make a static reference to a non-static method increasePotions() from type Player"

ActionMenu.java


Movements.java


Player.java
 
Mike Peters
Ranch Hand
Posts: 67
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error you get comes from the call Movements.FindPotion in the ActionMenu. The method FindPotion is not static but you call it in a static way. To get it to compile you must either make the method FindPotion in the Movements class static or create a movements object in the ActionMenu before calling the method (e.g. new Movements().FindPotion()).

I advice you to use static classes and methods only as a last resort. I think object oriented programming is much easier without a lot of static stuff in the code.
 
Brandon Golway
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, its starting to make a little more sense now.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic