Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Urgent! - How To Increment Value

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Urgent! - How To Increment Value

    Hi,

    In need of some help as I have been playing around all day trying to get this to increase the value of number by one after each new runner is added. Anybody able to help me get the value of number to increase after each record is added?

    public class Runner 
    {
    /* static variables */
     
     
    /* instance variables */
     
       private int number;       // runner's number
       private String name;      // runner's name
       private String ageGroup;  // standard, junior or senior
       private int time;         // runner's marathon time in minutes
       private int nextNumber;   // next avaliable number
     
     
       /**
        * Default constructor for objects of class Runner.
        */
       public Runner()
       {
          super();
          this.name = "";
          this.ageGroup = "standard";
          this.time = 0;
          this.nextNumber = 1;
     
          this.number = this.nextNumber + 1;
     
     
        }
     
    /* instance methods */
     
        //Only those accessor methods that you will need have been included
     
       /**
        * Returns the receiver's number.
        */
       public int getNumber()
       {
          return this.number;
       }
     
     
       /**
        * Sets the receiver's name.
        */
       public void setName(String aName)
       {
          this.name = aName;
       }
     
     
       /**
        * Returns the receiver's name.
        */
       public String getName()
       {
          return this.name;
       }
     
     
       /**
        * Sets the receiver's ageGroup.
        */
       public void setAgeGroup(String group)
       {
          this.ageGroup = group;
       }
     
       /**
        * Returns the receiver's ageGroup.
        */
       public String getAgeGroup()
       {
          return this.ageGroup;
       }
     
     
       /**
        * Sets the receiver's time.
        */
       public void setTime(int aTime)
       {
          this.time = aTime;
       }
     
     
       /**
        * Returns the receiver's time.
        */
       public int getTime()
       {
          return this.time;
       }
     
    }

    Any help would be great!

    Cheers.
    Last edited by stevenroberts; May 6th, 2014 at 11:31 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Increment Value

    This approach isn't going to work because each instance will have its own copy of number and nextNumber.

    You can either use a static variable that is shared between every instance, or you can pass the number into the constructor.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Increment Value

    Hi Kevin,

    Sorry if this seems like a silly question but how would I do that?

    Cheers,
    Steven

  4. #4
    Junior Member
    Join Date
    May 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs down Urgent - How to increase value

    Hi,

    I am having real problems being able to do this as I have spent two days on this and have not got anywhere with it.

    What I want to do is take the constructor to set the number instance variable of a newly created Runner object to the value of the class variable nextNumber and then increment the value held by nextNumber by 1, so that it will hold the number of the next Runner object to be created.

    [code/Java]
    public class Runner
    {

    /* instance variables */

    private static int number; // runner's number
    private String name; // runner's name
    private String ageGroup; // standard, junior or senior
    private int time; // runner's marathon time in minutes
    public static int nextNumber; // next avaliable number


    /**
    * Default constructor for objects of class Runner.
    */
    public Runner()
    {
    super();
    this.name = "";
    this.ageGroup = "standard";
    this.time = 0;
    this.nextNumber = 1;

    this.number = nextNumber;
    this.nextNumber = number + 1;

    }

    /* instance methods */

    /**
    * Returns the receiver's number.
    */
    public int getNumber()
    {
    return this.number;
    }

    public static int getNextNumber()

    {
    return Runner.nextNumber;
    }

    /**
    * Sets the receiver's name.
    */
    public void setName(String aName)
    {
    this.name = aName;
    }


    /**
    * Returns the receiver's name.
    */
    public String getName()
    {
    return this.name;
    }


    /**
    * Sets the receiver's ageGroup.
    */
    public void setAgeGroup(String group)
    {
    this.ageGroup = group;
    }

    /**
    * Returns the receiver's ageGroup.
    */
    public String getAgeGroup()
    {
    return this.ageGroup;
    }


    /**
    * Sets the receiver's time.
    */
    public void setTime(int aTime)
    {
    this.time = aTime;
    }


    /**
    * Returns the receiver's time.
    */
    public int getTime()
    {
    return this.time;
    }

    }
    [/code]

    Anybody know how to get it to do this lost is an understatement as the moment!

    Thanks.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,820
    My Mood
    Mellow
    Thanks
    177
    Thanked 628 Times in 615 Posts

    Default Re: Urgent! - How To Increment Value

    Since you weren't specific, for the first approach Kevin recommended, review the tutorial Understanding Class Members. For the second, what don't you understand about passing a number to a constructor? Something like:
        // this is a constructor that accepts the parameter value
        public MyClass( int value )
        {
            // assigns MyClass.value to the parameter value
            this.value = value;
     
            // the rest as needed
        }


    --- Update ---

    Please post your code correctly and don't post duplicate topics.

    Duplicate threads merged.

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    KevinWorkman (May 7th, 2014)

Similar Threads

  1. Java String value increment
    By raghu3.ankam in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 24th, 2013, 03:07 AM
  2. Increment a data by 1 after a certain time
    By shimira in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 16th, 2013, 07:14 AM
  3. post increment
    By deependeroracle in forum Java Theory & Questions
    Replies: 1
    Last Post: November 12th, 2012, 10:31 AM
  4. Increment a date
    By colerelm in forum Java Theory & Questions
    Replies: 1
    Last Post: September 30th, 2011, 05:57 AM
  5. [SOLVED] Increment Statements Difference
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: May 10th, 2011, 01:51 PM

Tags for this Thread