Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Tolls

Thread: Accessor and mutator methods help

  1. #1
    ajb5876 is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Accessor and mutator methods help

    I need to add some accessor methods so that the private variables in my EmployeePayment can be access by an EmployeePaymentTest class in order to draw out the report at the end. I have searched online to find examples/tutorials on how to do this but since I am new to JAVA I do not full understand how to implement this. Can anyone help me out?

    Much appreciated,
    Austin

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Accessor and mutator methods help

    What have you tried? Where is your SSCCE?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    ajb5876 is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Accessor and mutator methods help

    Java Code:
    package employeepayment;
    
    
    import java.text.NumberFormat;
    import java.util.Scanner;
    
    /**
     * @author Austin Benner
     */
    public class EmployeePayment {
              
        private double weeklyManagerPay;
        private int numManager = 0;
        private int numHourlyWorker = 0;
        private int numCommWorker = 0;
        private int numPieceWorker = 0;
    
        private void setManagerPay(double amount) {
            weeklyManagerPay = amount;
        }
        
        private double calcManagerPay() {
            numManager++;
            return weeklyManagerPay;
        }
        
        private double calcHourlyWorkerPay(double hourlySalary, int totalHours) {
            
            numHourlyWorker++;
                    
            if(totalHours <= 40) {
                return totalHours * hourlySalary;
            } else {
                return (40 * hourlySalary) + ((totalHours - 40) * (hourlySalary * 1.5));
            }
            
        }
        
        private double calcCommWorkerPay(double grossWeeklySales) {
            numCommWorker++;
            return (grossWeeklySales * .075) + 350;
        }
        
        private double calcPieceWorkerPay(int totalPieces, double wagePerPiece) {
            numPieceWorker++;
            return totalPieces * wagePerPiece;
        }
        
        //Formats a double as a currency string
        private static String formatCurrency(double amount) {
            return NumberFormat.getCurrencyInstance().format(amount);
        }
    
       
       class EmployeePaymentTEST {
            public void main(String[] args){
          
            Scanner sc = new Scanner (System.in);
            int paycode = 0;
            
            EmployeePayment calculator = new EmployeePayment();
            calculator.setManagerPay(625.00);
    
            //Loop until paycode of -1 is entered.
            while(paycode != -1) {
                
                System.out.print("Enter paycode (-1 to end): ");
                paycode = sc.nextInt();            
            
                switch(paycode) {
                    case 1:  
                        System.out.println("Manager worker selected.");
                        System.out.print("Manager worker's pay is ");
                        System.out.println(formatCurrency(calculator.calcManagerPay()));
                        break;
                    case 2:
                        System.out.println("Hourly worker selected.");
                        System.out.print("Enter the hourly salary: ");
                        double hourlySalary = sc.nextDouble();
                        System.out.print("Enter the total hours worked: ");
                        int totalHours = sc.nextInt();
                        System.out.print("Hourly worker's pay is ");
                        System.out.println(formatCurrency(calculator.calcHourlyWorkerPay(hourlySalary, totalHours)));
                        break;
                    case 3:
                        System.out.println("Commission worker selected");
                        System.out.print("Enter gross weekly sales: ");
                        double grossWeeklySales = sc.nextDouble();
                        System.out.print("Commission worker's pay is ");
                        System.out.println(formatCurrency(calculator.calcCommWorkerPay(grossWeeklySales)));
                        break;
                    case 4:
                        System.out.println("Piece worker selected.");
                        System.out.print("Enter the number of pieces: ");
                        int totalPieces = sc.nextInt();
                        System.out.print("Enter the wage per piece: ");
                        double wagePerPiece = sc.nextDouble();
                        System.out.print("Piece worker's pay is ");
                        System.out.println(formatCurrency(calculator.calcPieceWorkerPay(totalPieces, wagePerPiece)));
                        break;
                }
                
                System.out.println();
            
            }
       }
        }
           //Output report
            System.out.println("Total number of managers paid\t\t:" + calculator.numManager);
            System.out.println("Total number of hourly workers paid\t:" + calculator.numHourlyWorker);
            System.out.println("Total number of commission workers paid\t:" + calculator.numCommWorker);
            System.out.println("Total number of piece workers paid\t:" + calculator.numPieceWorker);
       
        }

    Here is what I have, I currently have both classes created and EmployeePaymentTest has the main within it. Its actually throwing a code at the bottom for the output Java that I have created.

    run:
    java.lang.ClassFormatError: Method "<error>" in class employeepayment/EmployeePayment has illegal signature "(Ljava/lang/Object;)Ljava/lang/System$out$println;"
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java :791)
    at java.security.SecureClassLoader.defineClass(Secure ClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader .java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader. java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:4 23)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 56)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Launc herHelper.java:482)
    Exception in thread "main" Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)


    Hope this gives you more of an idea of whats going on/ needs to be done,
    Austin

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

    Default Re: Accessor and mutator methods help

    EmployeePayment has no non-private methods there.
    What's the point of that?
    I also wouldn't put your test code inside the class its testing.
    Separate them.

    Did this code compile?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    ajb5876 is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Accessor and mutator methods help

    So the responsibilities of the EmployeePayment class should be

    Calculate the workers pay
    Keep track of how many of each type of worker
    Expose methods to access this information

    And the responsibilities of the EmployeePaymentTest class should be

    Prompt the user for information
    Pass this information to the EmployeePayment class via the appropriate methods
    Display the output from the EmployeePayment class methods to the console
    Format and display the report on the screen

    This is what I am trying to achieve,
    -Austin

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

    Default Re: Accessor and mutator methods help

    OK.
    And how does that change what I said?
    The test class should not be in with the other one.
    The other one should not be all private methods.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    ajb5876 is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Accessor and mutator methods help

    Im not sure I'm following you...

  8. #8
    ajb5876 is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Accessor and mutator methods help

    Classes are now separated correctly I think. How do I go about getting rid of my Scanner classes and adding in Accessors/Mutators to call back to EmployeePayment?

    Am I in the right direction?


    Java Code:
    package employeepayment;
    
    import java.text.NumberFormat;
    //import java.util.Scanner;
    
    /**
     * @author Austin Benner
     */
    public class EmployeePayment {
        
        public double weeklyManagerPay;
        public int numManager;
           public int getnumManager() {
           return numManager;
    }
        public int numHourlyWorker = 0;
            public int getnumHourlyWorker() {
            return numHourlyWorker;
    }
        public int numCommWorker = 0;
            public int getnumCommWorker() {
            return numCommWorker;
    }
        public int numPieceWorker = 0;
            public int getnumPieceWorker() {
            return numPieceWorker;
    }
    
        private void setManagerPay(double amount) {
            weeklyManagerPay = amount;
        }
        
        private double calcManagerPay() {
            numManager++;
            return weeklyManagerPay;
        }
        
        private double calcHourlyWorkerPay(double hourlySalary, int totalHours) {
            
            numHourlyWorker++;
                    
            if(totalHours <= 40) {
                return totalHours * hourlySalary;
            } else {
                return (40 * hourlySalary) + ((totalHours - 40) * (hourlySalary * 1.5));
            }
            
        }
        
        private double calcCommWorkerPay(double grossWeeklySales) {
            numCommWorker++;
            return (grossWeeklySales * .075) + 350;
        }
        
        private double calcPieceWorkerPay(int totalPieces, double wagePerPiece) {
            numPieceWorker++;
            return totalPieces * wagePerPiece;
        }
        
        //Formats a double as a currency string
        private static String formatCurrency(double amount) {
            return NumberFormat.getCurrencyInstance().format(amount);
        }
        {  
            EmployeePayment calculator = new EmployeePayment();
            calculator.setManagerPay(625.00);
        }
    }
        
    
            class EmployeePaymentTest {      
        private void main(String[] args) {
         
    //        Scanner sc = new Scanner (System.in);
            int paycode = 0
        
            //Loop until paycode of -1 is entered.
            while(paycode != -1) {
                
                System.out.print("Enter paycode (-1 to end): ");
                paycode = sc.nextInt();            
            
                switch(paycode) {
                    case 1:  
                        System.out.println("Manager worker selected.");
                        System.out.print("Manager worker's pay is ");
                        System.out.println(formatCurrency(calculator.calcManagerPay()));
                        break;
                    case 2:
                        System.out.println("Hourly worker selected.");
                        System.out.print("Enter the hourly salary: ");
                        double hourlySalary = sc.nextDouble();
                        System.out.print("Enter the total hours worked: ");
                        int totalHours = sc.nextInt();
                        System.out.print("Hourly worker's pay is ");
                        System.out.println(formatCurrency(calculator.calcHourlyWorkerPay(hourlySalary, totalHours)));
                        break;
                    case 3:
                        System.out.println("Commission worker selected");
                        System.out.print("Enter gross weekly sales: ");
                        double grossWeeklySales = sc.nextDouble();
                        System.out.print("Commission worker's pay is ");
                        System.out.println(formatCurrency(calculator.calcCommWorkerPay(grossWeeklySales)));
                        break;
                    case 4:
                        System.out.println("Piece worker selected.");
                        System.out.print("Enter the number of pieces: ");
                        int totalPieces = sc.nextInt();
                        System.out.print("Enter the wage per piece: ");
                        double wagePerPiece = sc.nextDouble();
                        System.out.print("Piece worker's pay is ");
                        System.out.println(formatCurrency(calculator.calcPieceWorkerPay(totalPieces, wagePerPiece)));
                        break;
                }
                
                System.out.println();
                {        
                        //Output report
            System.out.println("Total number of managers paid\t\t:" + calculator.getnumManager);
            System.out.println("Total number of hourly workers paid\t:" + calculator.getnumHourlyWorker);
            System.out.println("Total number of commission workers paid\t:" + calculator.getnumCommWorker);
            System.out.println("Total number of piece workers paid\t:" + calculator.getnumPieceWorker);
            
        }   
            
            }
    Last edited by ajb5876; 02-15-2013 at 01:47 AM.

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

    Default Re: Accessor and mutator methods help

    You haven't separated them.
    They really should be in two separate files.
    Having them together just confuses things.

    Anyway, you have accessors for your attributes for your EmployeePayment class (though the attributes should be private).
    You just need mutators.

    And then, of course, you need to explain what your problem is.
    If it doesn't compile then explain what errors you get.
    If it doesn't run properly then show us the exceptions (with stack trace) and highlight the line on whcih they occur.
    If it runs, but you don't get the results you expect then show us what you get and explain why that is wrong.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    ajb5876 is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Accessor and mutator methods help

    Alright I have them In two different files, but neither of them will compile. Both say that they cannot find <main class> Along with that it is throwing all sorts of errors on the left side of the window. I think they are all dealing with the my calculator/number format.

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package employeepayment;
    
    import java.text.NumberFormat;
    import java.util.Scanner;
    
    /**
     *
     * @author Austin J Benner
     */
          class EmployeePaymentTest {      
        private void main(String[] args) {
         
            Scanner sc = new Scanner (System.in);
            int paycode = 0;
        }
    //Formats a double as a currency string
        private static String formatCurrency(int amount) {
            return NumberFormat.getCurrencyInstance().format(amount);
        }
        {  
            EmployeePayment calculator = new EmployeePayment();
            calculator.setManagerPay(625.00);
        }         
                    
        
            //Loop until paycode of -1 is entered.
            while(paycode != -1) {
                
                System.out.print("Enter paycode (-1 to end): ");
                paycode = sc.nextInt();            
            
                switch(paycode) {
                    case 1:  
                        System.out.println("Manager worker selected.");
                        System.out.print("Manager worker's pay is ");
                        System.out.println(formatCurrency(calculator.calcManagerPay()));
                        break;
                    case 2:
                        System.out.println("Hourly worker selected.");
                        System.out.print("Enter the hourly salary: ");
                        double hourlySalary = sc.nextDouble();
                        System.out.print("Enter the total hours worked: ");
                        int totalHours = sc.nextInt();
                        System.out.print("Hourly worker's pay is ");
                        System.out.println(formatCurrency(calculator.calcHourlyWorkerPay(hourlySalary, totalHours)));
                        break;
                    case 3:
                        System.out.println("Commission worker selected");
                        System.out.print("Enter gross weekly sales: ");
                        double grossWeeklySales = sc.nextDouble();
                        System.out.print("Commission worker's pay is ");
                        System.out.println(formatCurrency(calculator.calcCommWorkerPay(grossWeeklySales)));
                        break;
                    case 4:
                        System.out.println("Piece worker selected.");
                        System.out.print("Enter the number of pieces: ");
                        int totalPieces = sc.nextInt();
                        System.out.print("Enter the wage per piece: ");
                        double wagePerPiece = sc.nextDouble();
                        System.out.print("Piece worker's pay is ");
                        System.out.println(formatCurrency(calculator.calcPieceWorkerPay(totalPieces, wagePerPiece)));
                        break;
                }
                
                System.out.println();
                {        
                        //Output report
            System.out.println("Total number of managers paid\t\t:" + calculator.getnumManager);
            System.out.println("Total number of hourly workers paid\t:" + calculator.getnumHourlyWorker);
            System.out.println("Total number of commission workers paid\t:" + calculator.getnumCommWorker);
            System.out.println("Total number of piece workers paid\t:" + calculator.getnumPieceWorker);
            
        }   
            
            }
            }

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

    Default Re: Accessor and mutator methods help

    Right, does your EmployeePayment class compile?
    If not then post it here and post the errors you are getting.

    If we get that in a sensible position, since it is used by the other one, then that's half the battle.
    ajb5876 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    ajb5876 is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default Re: Accessor and mutator methods help

    That does not compile either. Main Class cannot be found. It also says on line 10 that it is an illegal start of expression.

    -Austin


    Java Code:
    package employeepayment;
    
    /**
     * @author Austin Benner
     */
    public class EmployeePayment {
        
       public void main(String[] args){
        
        public double weeklyManagerPay;
           public double getweeklyManagerPay() {
           return weeklyManagerPay;
    }
        public int numManager;
           public int getnumManager() {
           return numManager;
    }
        public int numHourlyWorker = 0;
            public int getnumHourlyWorker() {
            return numHourlyWorker;
    }
        public int numCommWorker = 0;
            public int getnumCommWorker() {
            return numCommWorker;
    }
        public int numPieceWorker = 0;
            public int getnumPieceWorker() {
            return numPieceWorker;
    }
    
        public void setManagerPay(int amount) {
            weeklyManagerPay = amount;
        }
        
        public double calcManagerPay() {
            numManager++;
            return weeklyManagerPay;
        }
        
        public double calcHourlyWorkerPay(double hourlySalary, int totalHours) {
            
            numHourlyWorker++;
                    
            if(totalHours <= 40) {
                return totalHours * hourlySalary;
            } else {
                return (40 * hourlySalary) + ((totalHours - 40) * (hourlySalary * 1.5));
            }
            
        }
        
        public double calcCommWorkerPay(double grossWeeklySales) {
            numCommWorker++;
            return (grossWeeklySales * .075) + 350;
        }
        
        public double calcPieceWorkerPay(int totalPieces, double wagePerPiece) {
            numPieceWorker++;
            return totalPieces * wagePerPiece;
        }
        
    } 
    
    }
    Last edited by ajb5876; 02-15-2013 at 05:56 PM.

Similar Threads

  1. Mutator methods
    By toterpunkt in forum New To Java
    Replies: 8
    Last Post: 02-24-2011, 08:17 AM
  2. Accessor & Mutator methods understanding..
    By 6thDAY in forum New To Java
    Replies: 3
    Last Post: 08-14-2010, 07:27 PM
  3. Accessor/Mutator Question
    By noble in forum New To Java
    Replies: 4
    Last Post: 02-02-2010, 04:21 AM
  4. passing vs accessor
    By gcampton in forum New To Java
    Replies: 15
    Last Post: 01-08-2010, 03:21 AM
  5. Replies: 6
    Last Post: 12-02-2008, 11:15 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
  •