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: Help with beginner mutator method

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with beginner mutator method

    So basically I'm trying to change the value of something by 10, I've written this code but it doesn't appear to work. Any tips?

        public double increaseHours(int x)
        {
            return this.hoursworked + x;
        }

    And used in a test
    employee1.increaseHours(10);

    If necessary I can post the full code...


  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with beginner mutator method

    Well I might as well
    public class Payroll 
    {
        private static double totalpayout;
        private String employeeid;
        private int hourlyrate;
        private int hoursworked;
     
        public Payroll(String id, int hrr, int hrw)
        {
            employeeid=id;
            hourlyrate=hrr;
            hoursworked=hrw;
            totalpayout=hrr * hrw;
        }
        public String getEmployeeid()
        {
            return employeeid;
        }
        public int getRate()
        {
            return hourlyrate;
        }
        public double getHoursworked()
        {
            return hoursworked;
        }
        public double getEmployeepayout()
        {
            return hourlyrate * hoursworked;
        }
        public double getFinal()
        {
            return totalpayout;
        }
        public double increaseHours(int x)
        {
            return this.hoursworked + x;
        }
    }
    import java.util.Date;
    import java.text.DateFormat;
    import java.text.NumberFormat;
     
    class TestPayroll
    {		
    public static void main(String [] arg)
    {
    Date d = new Date();
    DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
    NumberFormat nf = NumberFormat.getCurrencyInstance();
     
    System.out.println("\nPayroll For Week Ending " + df.format(d));
    System.out.println("-------------------------------------");
    Payroll employee1 = new Payroll("444-4444", 30, 25);
    employee1.getEmployeepayout();
    displaySalary(employee1, nf);
     
    Payroll employee2 = new Payroll("555-5555", 20, 50);
    employee2.getEmployeepayout();
    displaySalary(employee2, nf);
     
    System.out.println("\tIncrease " + employee1.getEmployeeid() + " by 10 hours");
    employee1.increaseHours(10);
    displaySalary(employee1, nf);
     
    System.out.println("Total payout amount.. " +  nf.format(employee1.getFinal() + employee2.getFinal()));
    System.out.println("------------ End of report ----------");
    }
    public static void displaySalary(Payroll e, NumberFormat nf)	
    {
    System.out.println("\tEmployee # ......" + e.getEmployeeid());
    System.out.println("\tHours Worked ......" + e.getHoursworked()+ " hours");
    System.out.println("\tHourly Rate ......" + nf.format(e.getRate())+" /hour");
    System.out.println("\tYour salary is ......" + nf.format(e.getEmployeepayout()));
    System.out.println("\t---------------------------------\n");
    }	
    }

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with beginner mutator method

    but it doesn't appear to work.
    How does it not work? Does it compile? Exceptions? Misbehave? It is best to not leave any details out (see the getting help link in my signature)

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with beginner mutator method

    It does not alter the value by +10, it merely displays the information without any change whatsoever

    It compiles and executes without issue

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with beginner mutator method

    Quote Originally Posted by Dysun View Post
    It does not alter the value by +10, it merely displays the information without any change whatsoever

    It compiles and executes without issue
    If you wish to increase the value of the variable hoursworked , then you must set its value...as is your code returns the result of increasing the value, but it does not actually set the value.
    See Assignment, Arithmetic, and Unary Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

Similar Threads

  1. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM
  2. Beginner method call problem
    By Lasda in forum Java Theory & Questions
    Replies: 2
    Last Post: May 10th, 2011, 03:31 PM
  3. Need Help with Accessor and Mutator Methods
    By ankur_032 in forum Object Oriented Programming
    Replies: 1
    Last Post: February 9th, 2011, 10:00 AM
  4. Accesor and Mutator help
    By clevel211 in forum Object Oriented Programming
    Replies: 2
    Last Post: November 5th, 2010, 05:06 PM
  5. What is the matching mutator method?
    By ssmith in forum Object Oriented Programming
    Replies: 1
    Last Post: November 3rd, 2009, 10:03 PM