Results 1 to 11 of 11
  1. #1
    Proton is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default Writing a method

    I am writing a method which return an int, a sum of points. The class has got an instance variable which is a SortedMap<Integer, Driver> drivers; where sum of points is stored. First, I have to check that SortedMap keys are between 1 and 4, inclusive, if it is true, get the points and return them.
    So far my code doesn't compile, , compiler says; operator => cannot be applied to Driver, int

    public int getPoints()
    {
    int teamPoints = 0;
    if ((drivers.get("testmember") >= 1) && (drivers.get("testmember") <= 4))

    {
    teamPoints = teamPoints + driver.get("testmember")
    }
    return teamPoints

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    According to your Map your key is an Integer...yet you are passing a String into the get() method.

    And then your value is a Driver, so the result of get() is a Driver, which you then compare with and int.

    Do you, by any chance, have your key and value back to front and it should be Map<Driver, Integer>?

  3. #3
    Proton is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    so how can I pass the Integer number of points to get() argument?

  4. #4
    Proton is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    so how can I pass the Integer number of points to get() argument?
    This is wrong, what I am trying to do is compare the key, which is the driver number, to see if it is between the range 1 and 4, so how can I pass this int value as the argument of get() (drivers.get("testmember")

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    Can you please send the code.
    Ramya:cool:

  6. #6
    Proton is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    this is the method code, I don't know how to reach the SortedMap key
    /**
    * Return the points for the team
    */
    public int getPoints()
    {
    int teamPoints = 0;
    if ((drivers.get("testmember") == 1) || (drivers.get("testmember") ==2))
    //if (this.getDriver(1) != null)
    {
    teamPoints = teamPoints + driver.get().getPoints();
    }

    }

  7. #7
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    I feel like where ever driver is, you should make a getter
    Java Code:
    public int getMember(){
    return testmember;
    }
    
    
    
    public int getPoints()
    {
    int teamPoints = 0;
    if ((drivers.getMember() == 1) || (drivers.getMember() ==2))
    {
    return teamPoints = teamPoints + driver.getMember().getPoints();
    }
    
    }
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  8. #8
    Proton is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    drivers is an instance variable of the class where my method is, this is the begining of the class
    public class F1Team implements Comparable<F1Team>
    {
    /* instance variables */
    private String teamName;
    private SortedMap<Integer, Driver> drivers;

    /**
    * Default constructor for objects of class F1Team
    */
    public F1Team(String newTeamName)
    {
    super();
    this.teamName = newTeamName;
    this.drivers = new TreeMap<Integer, Driver>();
    }

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    So what is the Integer part of the map?

  10. #10
    Proton is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    a single instance variable called drivers
    which will be declared of type SortedMap<Integer, Driver>, where the keys, Integer part of the map,
    will be the driver numbers in the team and the values will be references to
    instances of Driver.
    So what is the Integer part of the map?

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    OK, and you want to get the key from the value?
    I suspect your map is back to front, as I said.

    Yo don't go from value to key, you go from key to value. If you find yourself going the other way then it's quite possible you've gone wrong somewhere.

    Anyway, taking your description at the top you could simply get the list of Map.Entry<Integer, Driver> and iterate over that, checking if the key is between 1 and 4...or whatever it is you're doing?

Similar Threads

  1. I need some help in writing a method
    By loraineEd in forum New To Java
    Replies: 2
    Last Post: 01-02-2010, 04:23 PM
  2. Writing a recursive method :S
    By Thousand in forum New To Java
    Replies: 1
    Last Post: 12-06-2009, 03:13 PM
  3. [SOLVED] Writing an Iterator method in a custom list
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 02-12-2009, 05:22 AM
  4. question about writing a toString method
    By heather.diggs in forum New To Java
    Replies: 2
    Last Post: 11-12-2008, 11:13 PM
  5. Writing the filter method
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 07-03-2007, 03:32 PM

Posting Permissions

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