Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default Trying to understand

    Hello,

    I am new to the forums and pretty new to Java. I am currently taking a course in Java programming, and I am working on an assignment.

    My problem is this: I can't seem to wrap my head around the specifics. I am making a basic Payroll Program, and I have done all right so far. Now, the assignment is to modify my program so that it uses a class to store and retrieve data. I can't find it anywhere in my reading how to get the class to store and retrieve data.

    Can anyone explain this to me? I don't want anyone to write a solution; I would just like someone to help me understand this concept.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Now, the assignment is to modify my program so that it uses a class to store and retrieve data.

    I'm not too clear what this means.

    What does your payroll program do at the moment? Does it store and retrieve data? Perhaps you could post the exact assignment so that someone can give you a nudge in the right direction or suggest some tutorial type documentation.

  4. #4
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default

    Java Code:
    Create a non-GUI based Java application that calculates weekly pay for an employee. The application should display text that requests the user input the name of the employee, the hourly rate, and the number of hours worked for that week. The application should then print out the name of the employee and the weekly pay amount. In the printout, display the dollar symbol ($) to the left of the weekly pay amount and format the weekly pay amount to display currency.
    I did this.

    Java Code:
    Modify the Payroll Program application so it continues to request employee information until the user enters stop as the employee name. In addition, program the application to check that the hourly rate and number of hours worked are positive numbers. If either the hourly rate or the number of hours worked is not a positive value, the application should prompt the user to enter a positive amount.
    I did this.

    Java Code:
    Modify the Payroll Program application so it continues to request employee information until the user enters stop as the employee name. In addition, program the application to check that the hourly rate and number of hours worked are positive numbers. If either the hourly rate or the number of hours worked is not a positive value, the application should prompt the user to enter a positive amount.
    This is the one with which I am having trouble.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default

    Stupid forum software ...

    kind regards,

    Jos

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by ladykrimson View Post
    [CODE]This is the one with which I am having trouble.

    what kind of troubles? if you are using the System.console look here for more detailed informations.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Just for the record ;)

    Create a non-GUI based Java application that calculates weekly pay for an employee. The application should display text that requests the user input the name of the employee, the hourly rate, and the number of hours worked for that week. The application should then print out the name of the employee and the weekly pay amount. In the printout, display the dollar symbol ($) to the left of the weekly pay amount and format the weekly pay amount to display currency.

    Modify the Payroll Program application so it continues to request employee information until the user enters stop as the employee name. In addition, program the application to check that the hourly rate and number of hours worked are positive numbers. If either the hourly rate or the number of hours worked is not a positive value, the application should prompt the user to enter a positive amount.

    Modify the Payroll Program application so it continues to request employee information until the user enters stop as the employee name. In addition, program the application to check that the hourly rate and number of hours worked are positive numbers. If either the hourly rate or the number of hours worked is not a positive value, the application should prompt the user to enter a positive amount.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Umm...

    I think you posted the second one twice and left out the one that is giving the problem.

  9. #9
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default

    I'm guessing what is required is an Employee object.

    ladykrimson,

    What you need is a class called Employee with variables to hold details such as name, wage, etc. You could then use a series of get and set methods to access the required data.
    The employee object is then created using the new keyword and creates a unique instance of that class. Each instance can be set to contain different values.

    Regards.

  10. #10
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default

    Quote Originally Posted by Ronin View Post
    I'm guessing what is required is an Employee object.

    ladykrimson,

    What you need is a class called Employee with variables to hold details such as name, wage, etc. You could then use a series of get and set methods to access the required data.
    The employee object is then created using the new keyword and creates a unique instance of that class. Each instance can be set to contain different values.

    Regards.
    Thank you so much. That was exactly what was needed. Now, I am having a different issue. Now, I have to call the methods which I have set up. I am having trouble creating a statement to do this. Here is what I have so far (keep in mind, this is incomplete).

    Java Code:
    package payroll.program;
    
    // import predefined Scanner from Java Library
    import java.util.Scanner;
    
    public class PayrollProgram 
    {
        public static void main(String[] args) 
        {    
                // create Scanner to obtain input from command window
                Scanner input = new Scanner(System.in);
                            
                int rate = 0;
                int hours;
                int pay;
                
                //Welcome message
                System.out.println("Welcome to the Payroll Program.");
                System.out.println("Enter the first and last name of the employee or enter - stop - to end the program.");
                String employeeName = input.nextLine(); // read a line of text    
                [B]Employee.setEmployeeName(employeeName);[/B]
                    while (!employeeName.equalsIgnoreCase("stop"))
                    {
                        System.out.printf("Enter the hourly rate for %s/n:", employeeName); // prompt
                        rate = input.nextInt(); //read input from the user
                        System.out.println(); // prints a blank line
                        Employee.setRate(rate);
                    }
                    while (rate < 0)
                    {
                        System.out.println("Please enter a positive number."); //prompt
                        rate = input.nextInt(); //read input from the user
                        System.out.println(); // prints a blank line
                    }
                
                    System.out.printf("Enter the number of hours worked for %s/n:", employeeName); // prompt
                    hours = input.nextInt(); // read input from user
                    System.out.println(); // prints a blank line
                    
                    
                    while (hours < 0)
                    {
                        System.out.println("Please enter a positive number."); //prompt
                        hours = input.nextInt(); // read input from user
                        System.out.println(); // prints a blank line
                    }
                    
                    pay = hours * rate; 
                
                    System.out.printf("The pay for employee, %s is $%d\n",employeeName, pay);     
            }
    }
    
    class Employee
    {
                // employee name, hours, and rate.
                [B]public static String employeeName;[/B]
                public double hours;
                public double rate;
                public double pay;
                
                Employee name = new Employee();
                
                // method to set employee name
                public static void setEmployeeName(String name)
                {
                        employeeName = name;
                }         // end method setEmployeeName
                
                // method to set hours
                public void setHours(double hours)
                {
                        this.hours = hours;
                 }         // end method setHours
                
                // method to set rate
                public void setRate(double rate)
                {
                        this.rate = rate;
                }        // end method setRate
                
                // method to retrieve employee name
                public String  getEmployeeName()
                {
                        return employeeName;
                }        // end method getEmployeeName        
    
                // method to retrieve hours
                public double getHours()
                {
                        return hours;
                }        // end method getHours
                
                // method to retrieve rate
                public double getRate()
                {
                        return rate;
                }        // end method getRate
                
                // method to compute pay
                public double getPay()
                {
                        pay = rate * hours;
                        return pay;
                }        // end method getPay
    }
    I use Eclipse to do my homework. I bolded two lines in the program, because Eclipse suggested that I make the method static. Did I use the wrong statement to call the method?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ladykrimson View Post
    I use Eclipse to do my homework. I bolded two lines in the program, because Eclipse suggested that I make the method static. Did I use the wrong statement to call the method?
    No -- Eclipse does not suggest this but warns you that you are trying to access a variable or method in a static way. The solution is not to make the variable or method static, it's to access it in a non-static way. In fact you should get rid of all statics from your Employee class and instead create an Employee object in your main method and call the objects methods.

  12. #12
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    No -- Eclipse does not suggest this but warns you that you are trying to access a variable or method in a static way. The solution is not to make the variable or method static, it's to access it in a non-static way. In fact you should get rid of all statics from your Employee class and instead create an Employee object in your main method and call the objects methods.
    I can't do that. The assignment says:

    Modify the Payroll Program so that it uses a class to store and retrieve the employee's name, the hourly rate, and the number of hours worked. Use a constructor to initialize the employee information, and a method within that class to calculate the weekly pay.

    I think I got it. I had to add static to all the class methods and take out the "this." and rename the local variable....Whew...
    Last edited by ladykrimson; 10-12-2010 at 07:00 PM.

  13. #13
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default

    Quote Originally Posted by ladykrimson View Post
    I can't do that.
    Actually, you can.
    What you haven't done was to construct the Employee object before accessing the methods.

    1) The first thing to add is an appropriate constructor within the Employee class.
    2) All variables and methods within the Employee class should be returned to non-static.
    3) The line "Employee name = new Employee();" should be removed from the Employee class and placed in the PayrollProgram before any of the methods are called. This code may need altering depending on the constructor called.

    Regards.

  14. #14
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default

    Quote Originally Posted by Ronin View Post
    Actually, you can.
    1) The first thing to add is an appropriate constructor within the Employee class.
    A constructor? What am I constructing within the Employee class?

  15. #15
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default

    A constructor defines how an object is to be initialized. This could be as simple as:
    Java Code:
    public Employee(){}

    or
    Java Code:
    public Employee(String name)
    {
    [INDENT]employeeName = name;[/INDENT]
    }

    Regards.

  16. #16
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default

    OK, how does this look?

    Java Code:
    package payroll.program;
    
    // import predefined Scanner from Java Library
    import java.util.Scanner;
    
    public class PayrollProgram 
    {
        public static void main(String[] args) 
        {    
                // create Scanner to obtain input from command window
                Scanner input = new Scanner(System.in);
                // create new object, Employee
                Employee name = new Employee();
                            
                int rate = 0;
                int hours;
                int pay;
                
                //Welcome message
                System.out.println("Welcome to the Payroll Program.");
                System.out.println("Enter the first and last name of the employee or enter - stop - to end the program.");
                String employeeName = input.nextLine(); // read a line of text    
                Employee.setEmployeeName(employeeName); // calls the method, setEm, from the Employee class
                if (employeeName.equalsIgnoreCase("stop"))
                {
                        System.exit(0);
                 }
                 else
                 {
                            System.out.printf("Enter the hourly rate for %s/n:", employeeName); // prompt
                            rate = input.nextInt(); //read input from the user
                            System.out.println(); // prints a blank line
                            Employee.setRate(rate); // calls the method, setEmployeeName, from the Employee class
                   
                            while (rate < 0)
                            {
                                    System.out.println("Please enter a positive number."); //prompt
                                    rate = input.nextInt(); //read input from the user
                                    System.out.println(); // prints a blank line
                            }
                                
                            System.out.printf("Enter the number of hours worked for %s/n:", employeeName); // prompt
                            hours = input.nextInt(); // read input from user
                            System.out.println(); // prints a blank line
                            Employee.setHours(hours); Employee.setRate(rate); // calls the method, setHours, from the Employee class
                    
                            while (hours < 0)
                            {
                                    System.out.println("Please enter a positive number."); //prompt
                                    hours = input.nextInt(); // read input from user
                                    System.out.println(); // prints a blank line
                            }
                    
                            // Display the name and pay for the employee
                            System.out.printf("The pay for employee, %s is $%d\n",employeeName, getPay);     
                    }
            }
    }
    
    class Employee
    {
                // employee name, hours, and rate.
                public String employeeName;
                public double hours;
                public double rate;
                public double pay;
                
                // employee constructor
                public Employee(String name)
                {
                        employeeName = name;
                }
                
                // method to set employee name
                public void setEmployeeName(String name)
                {
                        employeeName = name;
                }         // end method setEmployeeName
                
                // method to set hours
                public void setHours(double hours)
                {
                        this.hours = hours;
                 }         // end method setHours
                
                // method to set rate
                public void setRate(double rate)
                {
                        this.rate = rate;
                }        // end method setRate
                
                // method to retrieve employee name
                public String  getEmployeeName()
                {
                        return employeeName;
                }        // end method getEmployeeName        
    
                // method to retrieve hours
                public double getHours()
                {
                        return hours;
                }        // end method getHours
                
                // method to retrieve rate
                public double getRate()
                {
                        return rate;
                }        // end method getRate
                
                // method to compute pay
                public double getPay()
                {
                        pay = rate * hours;
                        return pay;
                }        // end method getPay
    }
    And thank you for all your help!

  17. #17
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Quote Originally Posted by ladykrimson View Post
    OK, how does this look?
    ask the compiler :)

    - Employee name = new Employee(); : this constructor does not exist
    - Employee.setEmployeeName(employeeName); and Employee.setHours(hours); these are not static methods, call the methods on your employee object !!!

    - System.out.printf("The pay for employee, %s is $%d\n",employeeName, getPay); : :confused: du you mean getPay() / name.getPay() ? :)

  18. #18
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default

    Better, but not quite there yet.

    Firstly, remove the code
    Java Code:
    Employee name = new Employee();
    because it is not doing anything. I know this contradicts what I said earlier, but bear with me.

    Next, remove
    Java Code:
    Employee.setEmployeeName(employeeName);
    and instead place the code
    Java Code:
    Employee employee = new Employee(employeeName);
    within the else statement right at the start. You are now calling the constructor for Employee you've just put in.

    Lastly, change all referes to "Employee." to "employee.". This allows you to access the properties of the employee object instead of the Employee class. Big difference!
    This also eliminates the need for static methods and variables within the Employee class.

    After this I think you're done... and eRaaaa beat me to it. Oh well!


    eRaaaa,
    Cheers for spotting the getPay, I completely missed it.

    Regards.
    Last edited by Ronin; 10-12-2010 at 10:51 PM. Reason: eRaaaa beat me to it

  19. #19
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default

    Like this?

    Java Code:
    package payroll.program;
    
    // import predefined Scanner from Java Library
    import java.util.Scanner;
    
    public class PayrollProgram 
    {
        public static void main(String[] args) 
        {    
                // create Scanner to obtain input from command window
                Scanner input = new Scanner(System.in);
                          
                int rate = 0;
                int hours;
                int pay;
                
                //Welcome message
                System.out.println("Welcome to the Payroll Program.");
                System.out.println("Enter the first and last name of the employee or enter - stop - to end the program.");
                String employeeName = input.nextLine(); // read a line of text    
                if (employeeName.equalsIgnoreCase("stop"))
                {
                    	System.exit(0);
                 }
                 else
                 {
                	 		employee employee = new employee(employeeName);
                        	System.out.printf("Enter the hourly rate for %s/n:", employeeName); // prompt
                        	rate = input.nextInt(); //read input from the user
                        	System.out.println(); // prints a blank line
                        	employee.setRate(rate); // calls the method, setEmployeeName, from the Employee class
                   
                        	while (rate < 0)
                        	{
                        			System.out.println("Please enter a positive number."); //prompt
                        			rate = input.nextInt(); //read input from the user
                        			System.out.println(); // prints a blank line
                        	}
                                
                        	System.out.printf("Enter the number of hours worked for %s/n:", employeeName); // prompt
                        	hours = input.nextInt(); // read input from user
                        	System.out.println(); // prints a blank line
                        	employee.setHours(hours); // calls the method, setHours, from the Employee class
                    
                        	while (hours < 0)
                        	{
                        			System.out.println("Please enter a positive number."); //prompt
                        			hours = input.nextInt(); // read input from user
                        			System.out.println(); // prints a blank line
                        	}
                    
                        	// Display the name and pay for the employee
                        	System.out.printf("The pay for employee, %s is $%d\n",employeeName, pay);     
                    }
            }
    }
    
    class Employee
    {
                // employee name, hours, and rate.
                public String employeeName;
                public double hours;
                public double rate;
                public double pay;
                
                // employee constructor
                public Employee(String name)
                {
                		employeeName = name;
                }
                
                // method to set employee name
                public void setEmployeeName(String name)
                {
                        employeeName = name;
                }         // end method setEmployeeName
                
                // method to set hours
                public void setHours(double hours)
                {
                        this.hours = hours;
                 }         // end method setHours
                
                // method to set rate
                public void setRate(double rate)
                {
                        this.rate = rate;
                }        // end method setRate
                
                // method to retrieve employee name
                public String  getEmployeeName()
                {
                        return employeeName;
                }        // end method getEmployeeName        
    
                // method to retrieve hours
                public double getHours()
                {
                        return hours;
                }        // end method getHours
                
                // method to retrieve rate
                public double getRate()
                {
                        return rate;
                }        // end method getRate
                
                // method to compute pay
                public double getPay()
                {
                        pay = rate * hours;
                        return pay;
                }        // end method getPay
    }

  20. #20
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default

    Run it and see. It's not a question of whether or not we like it but does it do what you want?

    Regards.

Page 1 of 2 12 LastLast

Similar Threads

  1. Trying to understand the whole 2D thing.
    By xael in forum Java 2D
    Replies: 4
    Last Post: 10-06-2010, 01:41 AM
  2. I don't understand the error ... (help!)
    By XmisterIS in forum New To Java
    Replies: 3
    Last Post: 09-02-2010, 09:33 AM
  3. I don't understand the output,
    By TIMBERings in forum New To Java
    Replies: 1
    Last Post: 12-10-2009, 08:03 AM
  4. understand the code
    By prof.deedee in forum New To Java
    Replies: 8
    Last Post: 11-11-2009, 02:43 AM
  5. I don´t understand
    By Manikyr in forum New To Java
    Replies: 6
    Last Post: 02-22-2009, 11:22 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
  •