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: Setter Methods Query

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    14
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Unhappy Setter Methods Query

    Hi,

    I want to use a setter method to set the value of insurance that is set depending on the value of cost how would you do this?

    A little confused how to get this one to work

    Thanks.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,120
    My Mood
    Mellow
    Thanks
    186
    Thanked 658 Times in 645 Posts

    Default Re: Setter Methods Query

    Show what you've tried, and describe how it could be better. "depending on" suggests the setter would take a parameter (most do) and then execute logic to set the value of insurance per the parameter.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    14
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Setter Methods Query

     /**
        * Set insurance value of the football player
        */
       public void setInsuranceValue() 
       {    
          if (this.cost <= 10) {
             this.value = 5;
          } else if (this.cost >10 && this.cost <30) {
             this.value = 10;
          } else {
             this.value = 15;
          }
       }

    The problem is once I set the cost value the value of value does not change.

  4. #4
    Junior Member
    Join Date
    Jan 2014
    Location
    Saint-Petersburg, Russia
    Posts
    8
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Setter Methods Query

    Quote Originally Posted by jamesh View Post
     /**
        * Set insurance value of the football player
        */
       public void setInsuranceValue() 
       {    
          if (this.cost <= 10) {
             this.value = 5;
          } else if (this.cost >10 && this.cost <30) {
             this.value = 10;
          } else {
             this.value = 15;
          }
       }

    The problem is once I set the cost value the value of value does not change.
    Do you expect value to be recalculated each time you change cost? If so it's wrong. You need to add setInsuranceValue() call to method where you set cost.

  5. The Following User Says Thank You to trod For This Useful Post:

    jamesh (January 23rd, 2014)

  6. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    14
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Setter Methods Query

    Thanks worked straight away!

Similar Threads

  1. Why use a constructor in intialize and not Setter
    By morrism35 in forum Object Oriented Programming
    Replies: 1
    Last Post: December 14th, 2013, 08:26 AM
  2. setter of Type Enum
    By vector_ever in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 9th, 2013, 03:33 PM
  3. Needing some help with array getter and setter methods!!
    By BlackShadow in forum What's Wrong With My Code?
    Replies: 8
    Last Post: April 5th, 2012, 07:11 PM
  4. [SOLVED] Project help...confusion with changing a getter/setter for a driver class
    By coolidge in forum Java Theory & Questions
    Replies: 1
    Last Post: September 30th, 2011, 11:08 PM
  5. update query is firing first then insert query
    By salmondavid88 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 8th, 2011, 10:15 AM