Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 49
  1. #21
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    just thinking out loud here, but would it be something like

    if (currentMonth == employees.birthDate.month ) ???
    {
    }

  2. #22
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    the more i think into this, i realize the location of the IF statements is incorrect, that would be looping too much code, and wouldn't put the addtional String i need in the right location. i left the scanner and input info, but need help with the If/Else if statements and how to loop this porperly, as well as how to pull the birthdate month appropriately for comparision.

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

    Default

    i guess you have some problems with dates in java. so here is a small program that perhaps contains some statements you can use

    Java Code:
    import java.text.DateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Locale;
    
    
    public class BirthDateExample {
    	private Date birthDate;
    	
    	public BirthDateExample(int day, int month, int year) {
    		// make your checks for day, month and year
    		birthDate = new Date();
    		Calendar cal = Calendar.getInstance();
    		// for manipulating dates use the Calendar class 
    		cal.set(Calendar.DATE, day);
    		cal.set(Calendar.MONTH, month);
    		cal.set(Calendar.YEAR, year);
    		// when the date is set revert it to the Date
    		birthDate = cal.getTime();
    		// format the date before the output
    		DateFormat df = DateFormat.getDateInstance(0, new Locale("us"));
    		System.out.println("The birth date is " + df.format(birthDate));
    	}
    	
    	public static void main(String[] args) {
    		// note that the month in Calendar is zero-based 
    		// the birth date is March 7, 1978
    		BirthDateExample date1 = new BirthDateExample(7, 2, 1978);
    	}
    }

  4. #24
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    thank you for the details. once it click that Date is a package that can be imported, that makes alot more sense, and shows the huge amount of abilities it has.

    i have used the example Date.java code inserted in my program, because that is what my directions specified.

    now my focus is on loop i need to get my program completed.

    can someone point me to logic/rational within this code as to how to loop this properly to add a String "plus $100 Birthday Bonus" to the customer output if the current month input is the month in which their birthday is? i though i had the structure figured out, but it turns out im misunderstanding something....
    current code:

    Java Code:
    // Fig. 10.9: PayrollSystemTest.java
    // Employee hierarchy test program.
    [COLOR="Red"]import java.util.Scanner; //program uses Scanner[/COLOR]
    
    public class PayrollSystemTest 
    {
       public static void main( String[] args ) 
       {
          // create subclass objects
          SalariedEmployee salariedEmployee = 
             new SalariedEmployee( "John", "Smith", "111-11-1111", 12,25,1968, 800.00 );
          HourlyEmployee hourlyEmployee = 
             new HourlyEmployee( "Karen", "Price", "222-22-2222", 11,10,1982, 16.75, 40 );
          CommissionEmployee commissionEmployee = 
             new CommissionEmployee( 
             "Sue", "Jones", "333-33-3333", 9,4,1980, 10000, .06 );
          BasePlusCommissionEmployee basePlusCommissionEmployee = 
             new BasePlusCommissionEmployee( 
             "Bob", "Lewis", "444-44-4444", 6,22,1982, 5000, .04, 300 );
    
          System.out.println( "Employees processed individually:\n" );
          
          System.out.printf( "%s\n%s: $%,.2f\n\n", 
             salariedEmployee, "earned", salariedEmployee.earnings() );
          System.out.printf( "%s\n%s: $%,.2f\n\n",
             hourlyEmployee, "earned", hourlyEmployee.earnings() );
          System.out.printf( "%s\n%s: $%,.2f\n\n",
             commissionEmployee, "earned", commissionEmployee.earnings() );
          System.out.printf( "%s\n%s: $%,.2f\n\n", 
             basePlusCommissionEmployee, 
             "earned", basePlusCommissionEmployee.earnings() );
    
          // create four-element Employee array
          Employee[] employees = new Employee[ 4 ]; 
    
          // initialize array with Employees
          employees[ 0 ] = salariedEmployee;
          employees[ 1 ] = hourlyEmployee;
          employees[ 2 ] = commissionEmployee; 
          employees[ 3 ] = basePlusCommissionEmployee;
          
          [COLOR="Red"]// create scanner to obtain input from user
    		Scanner input = new Scanner ( System.in );
    	
    	  int currentMonth; // current month input by user
    	  
    	  System.out.println ( "Enter the current month ( 1 -12 ):" );
    	  currentMonth = input.nextInt ();[/COLOR]
    	  
    	  System.out.println( "Employees processed polymorphically:\n" );
          
          // generically process each element in array employees
          for ( Employee currentEmployee : employees ) 
          {
             System.out.println( currentEmployee ); // invokes toString
    
             // determine whether element is a BasePlusCommissionEmployee
             if ( currentEmployee instanceof BasePlusCommissionEmployee ) 
             {
                // downcast Employee reference to 
                // BasePlusCommissionEmployee reference
                BasePlusCommissionEmployee employee = 
                   ( BasePlusCommissionEmployee ) currentEmployee;
    
                employee.setBaseSalary( 1.10 * employee.getBaseSalary() );
    
                System.out.printf( 
                   "new base salary with 10%% increase is: $%,.2f\n",
                   employee.getBaseSalary() );
             } // end if
    
             System.out.printf( 
                "earned $%,.2f\n\n", currentEmployee.earnings() );
             
          } // end for
    
          // get type name of each object in employees array
          for ( int j = 0; j < employees.length; j++ )
             System.out.printf( "Employee %d is a %s\n", j, 
                employees[ j ].getClass().getName() ); 
          } // end main
    } // end class PayrollSystemTest
    what i have added so far is in red.

    current output:
    Java Code:
    c:\SimplyJava\PayrollSystem10.8>java PayrollSystemTest
    Date object constructor for date 12/25/1968
    Date object constructor for date 11/10/1982
    Date object constructor for date 9/4/1980
    Date object constructor for date 6/22/1982
    Employees processed individually:
    
    salaried employee: John Smith
    social security number: 111-11-1111
    birth date: 12/25/1968
    weekly salary: $800.00
    earned: $800.00
    
    hourly employee: Karen Price
    social security number: 222-22-2222
    birth date: 11/10/1982
    hourly wage: $16.75; hours worked: 40.00
    earned: $670.00
    
    commission employee: Sue Jones
    social security number: 333-33-3333
    birth date: 9/4/1980
    gross sales: $10,000.00; commission rate: 0.06
    earned: $600.00
    
    base-salaried commission employee: Bob Lewis
    social security number: 444-44-4444
    birth date: 6/22/1982
    gross sales: $5,000.00; commission rate: 0.04; base salary: $300.00
    earned: $500.00
    
    Enter the current month ( 1 -12 ):
    [COLOR="Red"]12[/COLOR]
    Employees processed polymorphically:
    
    salaried employee: John Smith
    social security number: 111-11-1111
    birth date: 12/25/1968
    weekly salary: $800.00
    earned $800.00[COLOR="Red"] plus $100 Birthday Bonus.[/COLOR] //what i need added
    
    plus a $100 birthday Bonus.hourly employee: Karen Price
    social security number: 222-22-2222
    birth date: 11/10/1982
    hourly wage: $16.75; hours worked: 40.00
    earned $670.00
    
    plus a $100 birthday Bonus.commission employee: Sue Jones
    social security number: 333-33-3333
    birth date: 9/4/1980
    gross sales: $10,000.00; commission rate: 0.06
    earned $600.00
    
    plus a $100 birthday Bonus.base-salaried commission employee: Bob Lewis
    social security number: 444-44-4444
    birth date: 6/22/1982
    gross sales: $5,000.00; commission rate: 0.04; base salary: $300.00
    new base salary with 10% increase is: $330.00
    earned $530.00
    
    plus a $100 birthday Bonus.Employee 0 is a SalariedEmployee
    Employee 1 is a HourlyEmployee
    Employee 2 is a CommissionEmployee
    Employee 3 is a BasePlusCommissionEmployee
    
    c:\SimplyJava\PayrollSystem10.8>
    im not sure WHERE here it is that i need to add the loop, and how to read the month properly.

  5. #25
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    But breaking it down into simple methods is the best way to get the logic down.
    Honestly.

    Anyway, employees is an array. Presumably you want a particular entry in that array, which would be an Employee object (which has a getMonth() method).

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

    Default

    if you look at my post you could get the month of the current date by using

    Java Code:
    Calendar cal = Calendar.getInstance();
    		cal.setTime(date);
    		currentMonth = cal.get(Calendar.MONTH);

    so, no user input is needed. by the way, if you want to find you if an employee has its birthday you can use this example

    Java Code:
    	public boolean hasBirthDate(Date birthDate) {
    		// Calendar for the birthdate
    		Calendar calB = Calendar.getInstance();
    		// Calendar for the current date
    		Calendar calC = Calendar.getInstance();
    		// set the birthdate
    		calB.setTime(birthDate);
    		// set the current date
    		calC.setTime(new Date());
    		if (calC.get(Calendar.DATE) == calB.get(Calendar.DATE)
    				&& calC.get(Calendar.MONTH) == calB.get(Calendar.MONTH)) {
    			System.out.println("Yeah, birthday, lets party!");
    			return true;
    		} else {
    			return false;
    		}
    	}

  7. #27
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Except if the instructions for the problem are to take input from the user then that code isn't really usable.

  8. #28
    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 Tolls View Post
    Except if the instructions for the problem are to take input from the user then that code isn't really usable.

    ok, if this is a requirement then let take the input from the user.

    i just made some changes to the code so that the String "plus $100 Birthday Bonus." is printed when the employee has its birthdate. note that i used only the classes SalariedEmployee and HourlyEmployee. the code is attached, hope you got the idea.
    Attached Files Attached Files

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

    Default

    Is it using their Date class and not java.util.Date?

  10. #30
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Is it using their Date class and not java.util.Date?
    no its not.....i appreciate the time that must have taken, but i need to base this off the instructions and code i already have.

    Tolls if you agree that creating this will smaller methods it the best way to complete/learn this, can you walk me through it? what code should i post?

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

    Default

    What I do is write it out, usually as comments.

    So:
    Java Code:
    // Create the various employee objects and as an array.
    
    // Get month from user.
    
    // Find employees with birth dates for that month.
    That's your basic logic.
    For me that would be three method calls.

    Each of those methods would then involve doing things, which I would also write out as above. So my theoretical createEmployeeArray() method would look like:
    Java Code:
    private static Employee[] createEmployeeArray() {
       // Create on of each employee type
       // Add them to an array
       // Loop round the array printing out stuff.
    }
    Most of that there could be done in that method, except I'd take out the print out functionality and make that its own method since that might come in handy.

    So, that's the sort of thing I mean. And it means you can focus on one small piece of code at a time, and it makes it far easier to spot bugs.

  12. #32
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    what you say makes sense...one method at a time each performing its own separate task. Im VERY new to Java still, so i look at it like a huge problem and how to solve it, and opposed to focusing on small tasks and separating them and keeping them clear and simple.

    i had one week of learning about arrays, so im very unknowledgeable with those still, i dont recall ever using one like you have listed above before. here's another question, there is an employees array already in the PayrollSystemTest.java program already, is that different from the new one i should create? or can i use that one somehow?

  13. #33
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Exactly.

    And it's something I see all the time as a contractor going into places where methods (and classes) have grown into monsters because the developers haven't broken things down. It's very easy to fall into that habit.

  14. #34
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Exactly.

    And it's something I see all the time as a contractor going into places where methods (and classes) have grown into monsters because the developers haven't broken things down. It's very easy to fall into that habit.

    PayrollSystemTest.java already contains this array:
    Java Code:
    // create four-element Employee array
          Employee[] employees = new Employee[ 4 ]; 
    
          // initialize array with Employees
          employees[ 0 ] = salariedEmployee;
          employees[ 1 ] = hourlyEmployee;
          employees[ 2 ] = commissionEmployee; 
          employees[ 3 ] = basePlusCommissionEmployee;
    can that be used in this case?

    again im still very early on in the process of understanding arrays, so please pardon my lack of understanding.

  15. #35
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Assuming you have a createEmployeeArray() method (as I show above) then you can simply do:
    Java Code:
    Employee[] employees = createEmployeeArray();
    The method would then contain the code you have that creates the Employee objects and puts them in the array, ending by returning the array. You won't save any lines of coding doing this, but you will make it easier to follow what's going on.

  16. #36
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Assuming you have a createEmployeeArray() method (as I show above) then you can simply do:
    Java Code:
    Employee[] employees = createEmployeeArray();
    The method would then contain the code you have that creates the Employee objects and puts them in the array, ending by returning the array. You won't save any lines of coding doing this, but you will make it easier to follow what's going on.
    wow, i am so lost....

    will doing this change the value of the current array at all? or will it simply give me a method to utilize? i guess i'm....i dont know. im so confused.

  17. #37
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Have you done methods yet?
    It's quite possible I've leapt ahead of where you're at.

  18. #38
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    i have done methods, but ive never seen and array that is a method...

    i have seen this

    public String getName()

    but not a method with an array like that...i don't understand how that would/will work.

  19. #39
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default

    i don't want anyone to just DO IT ALL, but can you modify this with a different font color, starting how/where and kind of explain it as we go, so i can learn this?

    Java Code:
    // Fig. 10.9: PayrollSystemTest.java
    // Employee hierarchy test program.
    import java.util.Scanner; //program uses Scanner
    
    public class PayrollSystemTest 
    {
       public static void main( String[] args ) 
       {
          // create subclass objects
          SalariedEmployee salariedEmployee = 
             new SalariedEmployee( "John", "Smith", "111-11-1111", 12,25,1968, 800.00 );
          HourlyEmployee hourlyEmployee = 
             new HourlyEmployee( "Karen", "Price", "222-22-2222", 11,10,1982, 16.75, 40 );
          CommissionEmployee commissionEmployee = 
             new CommissionEmployee( 
             "Sue", "Jones", "333-33-3333", 9,4,1980, 10000, .06 );
          BasePlusCommissionEmployee basePlusCommissionEmployee = 
             new BasePlusCommissionEmployee( 
             "Bob", "Lewis", "444-44-4444", 6,22,1982, 5000, .04, 300 );
    
          System.out.println( "Employees processed individually:\n" );
          
          System.out.printf( "%s\n%s: $%,.2f\n\n", 
             salariedEmployee, "earned", salariedEmployee.earnings() );
          System.out.printf( "%s\n%s: $%,.2f\n\n",
             hourlyEmployee, "earned", hourlyEmployee.earnings() );
          System.out.printf( "%s\n%s: $%,.2f\n\n",
             commissionEmployee, "earned", commissionEmployee.earnings() );
          System.out.printf( "%s\n%s: $%,.2f\n\n", 
             basePlusCommissionEmployee, 
             "earned", basePlusCommissionEmployee.earnings() );
    
          // create four-element Employee array
          Employee[] employees = new Employee[ 4 ]; 
    
          // initialize array with Employees
          employees[ 0 ] = salariedEmployee;
          employees[ 1 ] = hourlyEmployee;
          employees[ 2 ] = commissionEmployee; 
          employees[ 3 ] = basePlusCommissionEmployee;
          
         [COLOR="Red"] // create scanner to obtain input from user
    		Scanner input = new Scanner ( System.in );
    	
    	  int currentMonth; // current month input by user
    	  
    	  System.out.println ( "Enter the current month ( 1 -12 ):" );
    	  currentMonth = input.nextInt ();[/COLOR]
    	   
    	  System.out.println( "Employees processed polymorphically:\n" );
          
          // generically process each element in array employees
          for ( Employee currentEmployee : employees ) 
          {
             System.out.println( currentEmployee ); // invokes toString
    
             // determine whether element is a BasePlusCommissionEmployee
             if ( currentEmployee instanceof BasePlusCommissionEmployee ) 
             {
                // downcast Employee reference to 
                // BasePlusCommissionEmployee reference
                BasePlusCommissionEmployee employee = 
                   ( BasePlusCommissionEmployee ) currentEmployee;
    
                employee.setBaseSalary( 1.10 * employee.getBaseSalary() );
    
                System.out.printf( 
                   "new base salary with 10%% increase is: $%,.2f\n",
                   employee.getBaseSalary() );
             } // end if
    
             System.out.printf( 
                "earned $%,.2f\n\n", currentEmployee.earnings() );
             
          } // end for
    
          // get type name of each object in employees array
          for ( int j = 0; j < employees.length; j++ )
             System.out.printf( "Employee %d is a %s\n", j, 
                employees[ j ].getClass().getName() ); 
          } // end main
    } // end class PayrollSystemTest
    it seems like the location of the statements needed is as much of a question to me as what the statements need to be.

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

    Default

    OK, methods aside then.

    So this is to do the birthday month bonus then?

    You want to:
    Java Code:
    for each Employee:
    if (month_entered_by_user == employee.getMonth()) {
    print out the extra bit about the $100 bonus.
    }
    That's pseudo code, obviously.
    You've already got the loop.
    You've already got the initial bit of output:
    Java Code:
             System.out.printf( 
                "earned $%,.2f\n\n", currentEmployee.earnings() );
    The question is how to squeeze that extra bit in there.
    If you had a String object called birthdayText, which would be empty, you could do:
    Java Code:
    String birthdayText = "";
    if (month_entered_by_user == employee.getMonth()) {
        birthdayText = "blah blah blah $100";
    }
    System.out.printf( 
                "earned $%,.2f %s\n\n", currentEmployee.earnings(), birthdayText );

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. subclass calling superclass method
    By stackptr89 in forum New To Java
    Replies: 10
    Last Post: 11-22-2010, 08:40 PM
  2. Calling subclassed methods from constructors
    By arefeh in forum New To Java
    Replies: 7
    Last Post: 01-22-2010, 01:22 AM
  3. superclass and subclass
    By mr idiot in forum New To Java
    Replies: 19
    Last Post: 01-03-2009, 08:29 AM
  4. Calling methods from superclass
    By moaxjlou in forum New To Java
    Replies: 7
    Last Post: 12-11-2008, 01:07 AM
  5. SuperClass of an Object
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-06-2007, 03:51 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
  •