Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem with calling a method in another class

Posted on 2009-04-29
10
Medium Priority
?
161 Views
Last Modified: 2013-11-23
Hi everyone,
I´m trying to create a little game in my university class. One of the classes I have called "Hero" and I get a message when I´m trying to complice. "Cannot find symbol - method getWeight".  But I know that I have a method called getWeight in my Item Class. What is the problem?

(In this method I want the Hero to pick up the Item if it is not to heavy).
 
public boolean pickUpItem (String description, Item item)
    {
        if (mItems.getWeight() < (mStrength.getStrength() * 1.5))
           return true;
            
            else 
                return false;
                System.out.printl ("You can´t pick up the item, it´s too heavy for you!");

Open in new window

0
Comment
Question by:Lisa79
  • 5
  • 5
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24267221
whats the item class look like?

0
 

Author Comment

by:Lisa79
ID: 24267242
objects>

Hi again! :)
Here is the code..
public class Item
{
    private String mDescription;
    private int mWeight;
    
    public Item (String description, int weight)
    {
        mDescription = description;
        mWeight = weight;
    }
    
    public int getWeight()
    {
        return mWeight;
    }
    
    public String toString()
    {
        return ("a rusty old armor with dark stains on it");
    }
 
   
}

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 24267251
>         if (mItems.getWeight() < (mStrength.getStrength() * 1.5))

your Item var is named item, not mItems

        if (item.getWeight() < (mStrength.getStrength() * 1.5))

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Lisa79
ID: 24267282
I have tried that one to... then i get the message "int cannot be dereferenced"?
0
 
LVL 92

Expert Comment

by:objects
ID: 24267300
at what line? u sure its not a new error at a different place

What type is mStrength?

0
 

Author Comment

by:Lisa79
ID: 24267348
at >         if (item.getWeight() < (mStrength.getStrength() * 1.5))

mStrength > int.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 1000 total points
ID: 24267357
if its an int then it should be:

if  (item.getWeight() < (mStrength * 1.5))

0
 

Author Comment

by:Lisa79
ID: 24267377
it works! :) thank you! Could you explain to me why it has to be just mStrength?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 1000 total points
ID: 24267399
because that variable is an int (which is a primitive type and not a class) so you can access it directly. You only call methods on objects. primitive (int, float, double, char, byte) do not have methods
0
 

Accepted Solution

by:
Lisa79 earned 0 total points
ID: 24267419
ahaa... thanks! You are the best. :) Have a nice weekend.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question