Needs help in editing

Discussion in 'Java' started by Eric, Jun 23, 2011.

  1. Eric

    Eric Guest

    Hi guys: Can any one please help me how to i do the changes in my
    program according to the following 4 changes.

    1) Create a Date type birth data instance field in the Employee class,
    not a String

    2) Enlarge the constructors for the Employee class and subclasses to
    pass the int month, int day, and int year of the birth date as input
    by the user to PayrollSystemTest to the subclass constructor, and then
    to the Employee class constructor

    3) Input from the user for the 5 specific employees and then comment
    out the hardcoding for the 4 employees in the original code

    4) Report monthly salary amounts and include the November birthday
    bonus

    import java.util.ArrayList;
    import java.util.Date;
    import java.util.Scanner;

    public class Employee {

    String socialSecurity;
    String birthDate;

    public Employee(String socialSecurity, String birthDate){
    this.birthDate = birthDate;
    this.socialSecurity = socialSecurity;
    }

    public String getSocialSecurity(){
    return socialSecurity;
    }

    public String getBirthDate(){
    return birthDate;
    }

    public String toString(){
    return ("Employee: Social security " + socialSecurity
    + " date of birth " + birthDate);
    }

    public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter employees one by one");
    ArrayList<Employee> ar = new ArrayList<Employee>();

    while(true){
    System.out.println(" Employee types available:");
    System.out.println("");
    System.out.println("1-Salaried Employee");
    System.out.println("2-Hourly Employee");
    System.out.println("3-Commission Employee");
    System.out.println("4-Based Salary Commission
    Employee");
    System.out.println("");
    System.out.println("Enter employee type (1-4) .
    Finish list with type 0: ");

    String typeString = keyboard.next();
    // boolean goodInput = true;
    int type = -1;
    try{
    type = Integer.parseInt(typeString);
    } catch(Exception ex){

    }

    if(type <0 || type > 4){
    System.out.println("Invalid type. Please, try
    again. ");
    continue;
    }

    if(type == 0)break;

    System.out.println("Enter social security: ");
    String social = keyboard.next();
    System.out.println("Enter date of birth: ");
    String birth = keyboard.next();

    boolean goodInput = true;
    switch(type){
    case 1:
    System.out.println("Enter salary: ");
    String salString = keyboard.next();
    double salary = -1.0;
    try{
    salary =
    Double.parseDouble(salString);
    } catch(Exception ex){

    }
    if(salary < 0){
    System.out.println("Invalid input. Try
    again");
    goodInput = false;
    break;
    }
    SalariedEmployee emp = new
    SalariedEmployee(social, birth, salary);
    ar.add(emp);
    break;

    case 2:
    System.out.println("Enter hourly wage:
    ");
    String hourlyString = keyboard.next();
    System.out.println("Enter hours worked:
    ");
    String hoursString =
    keyboard.next();
    double hourlyWage = -1.0;
    double hoursWorked = -1.0;
    try{
    hourlyWage =
    Double.parseDouble(hourlyString );
    hoursWorked =
    Double.parseDouble(hoursString );
    } catch(Exception ex){

    }

    if(hourlyWage < 0.0 || hoursWorked < 0.0){
    System.out.println("Invalid input. Try
    again");
    goodInput = false;
    break;
    }
    HourlyEmployee emp1 = new
    HourlyEmployee(social, birth, hourlyWage, hoursWorked);
    ar.add(emp1);
    break;

    case 3:
    System.out.println("Enter
    commission rate: ");
    String commissionString =
    keyboard.next();
    System.out.println("Enter gross sales: ");
    String salesString =
    keyboard.next();
    double commissionRate = -1.0;
    double grossSales = -1.0;
    try{
    commissionRate =
    Double.parseDouble(commissionString );
    grossSales =
    Double.parseDouble(salesString );
    } catch(Exception ex){

    }

    if(commissionRate < 0.0 || grossSales <
    0.0){
    System.out.println("Invalid input. Try
    again");
    goodInput = false;
    break;
    }
    CommissionEmployee emp2 = new
    CommissionEmployee(social, birth, commissionRate, grossSales);
    ar.add(emp2);
    break;

    case 4:
    System.out.println("Enter
    commission rate: ");
    commissionString = keyboard.next();
    System.out.println("Enter gross sales: ");
    salesString =
    keyboard.next();
    System.out.println("Enter salary: ");
    salString = keyboard.next();
    commissionRate = -1.0;
    grossSales = -1.0;
    salary = -1.0;
    try{
    commissionRate =
    Double.parseDouble(commissionString );
    grossSales =
    Double.parseDouble(salesString );
    salary =
    Double.parseDouble(salString );
    } catch(Exception ex){

    }
    if(commissionRate < 0.0 || grossSales
    < 0.0 || salary < 0){
    System.out.println("Invalid input. Try
    again");
    goodInput = false;
    break;
    }

    SalaryBasedCommissionEmployee emp3 = new
    SalaryBasedCommissionEmployee(social, birth, commissionRate,
    grossSales, salary);
    ar.add(emp3);
    }





    }

    for (int i = 0; i < ar.size(); i++) {
    Employee employee = ar.get(i);
    System.out.println(employee);
    }


    }


    }

    class SalariedEmployee extends Employee{
    double salary;

    public SalariedEmployee(String socialSecurity, String
    birthDate, double salary){
    super(socialSecurity, birthDate);
    this.salary = salary;

    }

    public double getSalary(){
    return salary;
    }


    public String toString(){
    return ("Salaried Employee: Social security " +
    socialSecurity + " date of birth " + birthDate + " salary " +
    salary);
    }

    }

    class HourlyEmployee extends Employee {
    double hourlyWage;
    double hoursWorked;

    public HourlyEmployee(String socialSecurity, String
    birthDate, double hourlyWage, double hoursWorked){
    super(socialSecurity, birthDate);
    this.hourlyWage = hourlyWage;
    this.hoursWorked = hoursWorked;

    }

    public String toString(){
    return ("Hourly Employee: Social security " +
    socialSecurity + " date of birth " + birthDate + " hourlyWage
    " + hourlyWage + " hoursWorked " + hoursWorked);
    }


    }

    class CommissionEmployee extends Employee {
    double grossSales;
    double commissionRate;


    public CommissionEmployee(String socialSecurity, String
    birthDate, double grossSales, double commissionRate){
    super(socialSecurity, birthDate);
    this.grossSales = grossSales;
    this.commissionRate = commissionRate;

    }

    public String toString(){
    return ("Commission Employee: Social security " +
    socialSecurity + " date of birth " + birthDate + " Commission
    Rate " + commissionRate + " Gross Sales " + grossSales);
    }


    }

    class SalaryBasedCommissionEmployee extends CommissionEmployee
    {
    double salary;



    public SalaryBasedCommissionEmployee(String
    socialSecurity, String birthDate, double grossSales, double
    commissionRate, double salary){
    super(socialSecurity, birthDate, grossSales,
    commissionRate);
    this.salary = salary;


    }

    public String toString(){
    return ("Salary Based Commission Employee: Social
    security " + socialSecurity + " date of birth " + birthDate +
    " Commission Rate " + commissionRate
    + " Gross Sales " + grossSales + " Salary " +
    salary);
    }


    }
    Eric, Jun 23, 2011
    #1
    1. Advertising

  2. Eric

    Aéris Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Le 23/06/2011 20:25, Eric a écrit :
    > 1) Create a Date type birth data instance field in the Employee class,
    > not a String


    org.joda.time.DateTime from Joda-time library

    > 2) Enlarge the constructors for the Employee class and subclasses to
    > pass the int month, int day, and int year of the birth date as input
    > by the user to PayrollSystemTest to the subclass constructor, and then
    > to the Employee class constructor


    What the usefulness ?
    Datetime can be parsed directly from text like 2011-06-24 (ISO format or
    other) and constructors *must* take Datetime in parameter, not
    month/day/year.
    Ultimately, make a DateFactory or use Joda-time API to convert fields to
    datetime.

    > 3) Input from the user for the 5 specific employees and then comment
    > out the hardcoding for the 4 employees in the original code


    public enum EmployeeType {
    SALARIED, HOURLY, COMMISSION, SALARY_BASED_COMMISSION
    }

    And please :
    — Learn Java, all those questions are very very basic…
    — Don't post so many and so ugly craps code…

    - --
    Aeris
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.11 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

    iQEcBAEBAgAGBQJOA7+yAAoJEK8zQvxDY4P9aIwH/0V6g8aXD2n6LBIYSQdUg97n
    +NY/pWEdOdxaKSX1ScQIlxHWBiX0Eg5/3KNaX5Q8VbS8fFAqYMt6UZRGKREBsAN/
    r5dWxgYvK7/ZHDInKg3q8mEqWI+P+YpH1nB2sNlW0dHf3JjvRqPg3qWwrYC3gwkc
    kQtDqZaJugVfAlz+tHm9N9YPyFTHuT0b3ZBUB9BI1PjdfHHzn1NR0n/FJULSL/SH
    pwwPM+QPzDwQeH9+sv847J4iIjH9nW4VwFp85yh4J9pV0iDw59/8DgCZvTGnIpJp
    dwlmIJFMorMqiVB+uQ+ACaAZ3Mpusk3tddI3/S9iaV3dTvFSKq6Lb1E1h3tCQG0=
    =sQpY
    -----END PGP SIGNATURE-----
    Aéris, Jun 23, 2011
    #2
    1. Advertising

  3. Eric

    Aéris Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Le 24/06/2011 00:35, Aéris a écrit :
    >> 3) Input from the user for the 5 specific employees and then comment
    >> > out the hardcoding for the 4 employees in the original code

    > public enum EmployeeType {
    > SALARIED, HOURLY, COMMISSION, SALARY_BASED_COMMISSION
    > }


    I forgot to say you must use an EmployeeFactory to create employee from
    user input (with reflection for example) to avoid harcoded craps switch
    case.

    - --
    Aeris
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.11 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

    iQEcBAEBAgAGBQJOA8CjAAoJEK8zQvxDY4P9MDkH/2PXWKeE8JJ9kPLlNZoS6pl+
    c2352NA4XpWskE8iOFpT3KBmyQqfv8uclXU7ersbke6vVbmPH7izNWlxgasBJ8eX
    CeNkknTzdR9m/SdapOpDbGKQgPsoUM6bbZcaU1qJRmMmsEMV6gRDxN6lf0lzoJjU
    xSXyJLxMQoQ6UFH8STNEQ5yUh6euQAxnAZtKVlzh31Txhvp3hkyzQpS3rLselGjO
    D4x1SfJUAetmE4zqOY8BAKdjy+SBd2vyPCqA96aTndicVfn73jADKeKaBS+pbf7g
    tRdBFID+BszOFu5AEIRYfF0v6YYSALAyAfm7aGbPSW2Ef/E1ireQckoH8NZp3vs=
    =ZctI
    -----END PGP SIGNATURE-----
    Aéris, Jun 23, 2011
    #3
  4. I think we are being asked to someone's school assignment. Better he
    does it himself.

    On 11-06-23 02:25 PM, Eric wrote:
    > Hi guys: Can any one please help me how to i do the changes in my
    > program according to the following 4 changes.
    >
    > 1) Create a Date type birth data instance field in the Employee class,
    > not a String
    >
    > 2) Enlarge the constructors for the Employee class and subclasses to
    > pass the int month, int day, and int year of the birth date as input
    > by the user to PayrollSystemTest to the subclass constructor, and then
    > to the Employee class constructor
    >
    > 3) Input from the user for the 5 specific employees and then comment
    > out the hardcoding for the 4 employees in the original code
    >
    > 4) Report monthly salary amounts and include the November birthday
    > bonus
    >
    > import java.util.ArrayList;
    > import java.util.Date;
    > import java.util.Scanner;
    >
    > public class Employee {
    >
    > String socialSecurity;
    > String birthDate;
    >
    > public Employee(String socialSecurity, String birthDate){
    > this.birthDate = birthDate;
    > this.socialSecurity = socialSecurity;
    > }
    >
    > public String getSocialSecurity(){
    > return socialSecurity;
    > }
    >
    > public String getBirthDate(){
    > return birthDate;
    > }
    >
    > public String toString(){
    > return ("Employee: Social security " + socialSecurity
    > + " date of birth " + birthDate);
    > }
    >
    > public static void main(String[] args) {
    >
    > Scanner keyboard = new Scanner(System.in);
    > System.out.println("Enter employees one by one");
    > ArrayList<Employee> ar = new ArrayList<Employee>();
    >
    > while(true){
    > System.out.println(" Employee types available:");
    > System.out.println("");
    > System.out.println("1-Salaried Employee");
    > System.out.println("2-Hourly Employee");
    > System.out.println("3-Commission Employee");
    > System.out.println("4-Based Salary Commission
    > Employee");
    > System.out.println("");
    > System.out.println("Enter employee type (1-4) .
    > Finish list with type 0: ");
    >
    > String typeString = keyboard.next();
    > // boolean goodInput = true;
    > int type = -1;
    > try{
    > type = Integer.parseInt(typeString);
    > } catch(Exception ex){
    >
    > }
    >
    > if(type<0 || type> 4){
    > System.out.println("Invalid type. Please, try
    > again. ");
    > continue;
    > }
    >
    > if(type == 0)break;
    >
    > System.out.println("Enter social security: ");
    > String social = keyboard.next();
    > System.out.println("Enter date of birth: ");
    > String birth = keyboard.next();
    >
    > boolean goodInput = true;
    > switch(type){
    > case 1:
    > System.out.println("Enter salary: ");
    > String salString = keyboard.next();
    > double salary = -1.0;
    > try{
    > salary =
    > Double.parseDouble(salString);
    > } catch(Exception ex){
    >
    > }
    > if(salary< 0){
    > System.out.println("Invalid input. Try
    > again");
    > goodInput = false;
    > break;
    > }
    > SalariedEmployee emp = new
    > SalariedEmployee(social, birth, salary);
    > ar.add(emp);
    > break;
    >
    > case 2:
    > System.out.println("Enter hourly wage:
    > ");
    > String hourlyString = keyboard.next();
    > System.out.println("Enter hours worked:
    > ");
    > String hoursString =
    > keyboard.next();
    > double hourlyWage = -1.0;
    > double hoursWorked = -1.0;
    > try{
    > hourlyWage =
    > Double.parseDouble(hourlyString );
    > hoursWorked =
    > Double.parseDouble(hoursString );
    > } catch(Exception ex){
    >
    > }
    >
    > if(hourlyWage< 0.0 || hoursWorked< 0.0){
    > System.out.println("Invalid input. Try
    > again");
    > goodInput = false;
    > break;
    > }
    > HourlyEmployee emp1 = new
    > HourlyEmployee(social, birth, hourlyWage, hoursWorked);
    > ar.add(emp1);
    > break;
    >
    > case 3:
    > System.out.println("Enter
    > commission rate: ");
    > String commissionString =
    > keyboard.next();
    > System.out.println("Enter gross sales: ");
    > String salesString =
    > keyboard.next();
    > double commissionRate = -1.0;
    > double grossSales = -1.0;
    > try{
    > commissionRate =
    > Double.parseDouble(commissionString );
    > grossSales =
    > Double.parseDouble(salesString );
    > } catch(Exception ex){
    >
    > }
    >
    > if(commissionRate< 0.0 || grossSales<
    > 0.0){
    > System.out.println("Invalid input. Try
    > again");
    > goodInput = false;
    > break;
    > }
    > CommissionEmployee emp2 = new
    > CommissionEmployee(social, birth, commissionRate, grossSales);
    > ar.add(emp2);
    > break;
    >
    > case 4:
    > System.out.println("Enter
    > commission rate: ");
    > commissionString = keyboard.next();
    > System.out.println("Enter gross sales: ");
    > salesString =
    > keyboard.next();
    > System.out.println("Enter salary: ");
    > salString = keyboard.next();
    > commissionRate = -1.0;
    > grossSales = -1.0;
    > salary = -1.0;
    > try{
    > commissionRate =
    > Double.parseDouble(commissionString );
    > grossSales =
    > Double.parseDouble(salesString );
    > salary =
    > Double.parseDouble(salString );
    > } catch(Exception ex){
    >
    > }
    > if(commissionRate< 0.0 || grossSales
    > < 0.0 || salary< 0){
    > System.out.println("Invalid input. Try
    > again");
    > goodInput = false;
    > break;
    > }
    >
    > SalaryBasedCommissionEmployee emp3 = new
    > SalaryBasedCommissionEmployee(social, birth, commissionRate,
    > grossSales, salary);
    > ar.add(emp3);
    > }
    >
    >
    >
    >
    >
    > }
    >
    > for (int i = 0; i< ar.size(); i++) {
    > Employee employee = ar.get(i);
    > System.out.println(employee);
    > }
    >
    >
    > }
    >
    >
    > }
    >
    > class SalariedEmployee extends Employee{
    > double salary;
    >
    > public SalariedEmployee(String socialSecurity, String
    > birthDate, double salary){
    > super(socialSecurity, birthDate);
    > this.salary = salary;
    >
    > }
    >
    > public double getSalary(){
    > return salary;
    > }
    >
    >
    > public String toString(){
    > return ("Salaried Employee: Social security " +
    > socialSecurity + " date of birth " + birthDate + " salary " +
    > salary);
    > }
    >
    > }
    >
    > class HourlyEmployee extends Employee {
    > double hourlyWage;
    > double hoursWorked;
    >
    > public HourlyEmployee(String socialSecurity, String
    > birthDate, double hourlyWage, double hoursWorked){
    > super(socialSecurity, birthDate);
    > this.hourlyWage = hourlyWage;
    > this.hoursWorked = hoursWorked;
    >
    > }
    >
    > public String toString(){
    > return ("Hourly Employee: Social security " +
    > socialSecurity + " date of birth " + birthDate + " hourlyWage
    > " + hourlyWage + " hoursWorked " + hoursWorked);
    > }
    >
    >
    > }
    >
    > class CommissionEmployee extends Employee {
    > double grossSales;
    > double commissionRate;
    >
    >
    > public CommissionEmployee(String socialSecurity, String
    > birthDate, double grossSales, double commissionRate){
    > super(socialSecurity, birthDate);
    > this.grossSales = grossSales;
    > this.commissionRate = commissionRate;
    >
    > }
    >
    > public String toString(){
    > return ("Commission Employee: Social security " +
    > socialSecurity + " date of birth " + birthDate + " Commission
    > Rate " + commissionRate + " Gross Sales " + grossSales);
    > }
    >
    >
    > }
    >
    > class SalaryBasedCommissionEmployee extends CommissionEmployee
    > {
    > double salary;
    >
    >
    >
    > public SalaryBasedCommissionEmployee(String
    > socialSecurity, String birthDate, double grossSales, double
    > commissionRate, double salary){
    > super(socialSecurity, birthDate, grossSales,
    > commissionRate);
    > this.salary = salary;
    >
    >
    > }
    >
    > public String toString(){
    > return ("Salary Based Commission Employee: Social
    > security " + socialSecurity + " date of birth " + birthDate +
    > " Commission Rate " + commissionRate
    > + " Gross Sales " + grossSales + " Salary " +
    > salary);
    > }
    >
    >
    > }
    William Colls, Jun 24, 2011
    #4
  5. Eric

    lewbloch Guest

    On Jun 23, 3:39 pm, Aéris <> wrote:
    > I forgot to say you must use an EmployeeFactory to create employee from
    > user input (with reflection for example) to avoid harcoded craps switch
    > case.


    Silly advice to give a newbie.

    Actually, silly advice to give anyone. There's no "must use" in
    factory methods or classes. Reflection is an elephant gun for
    shooting fleas; simple polymorphism suffices in most cases.

    --
    Lwq
    lewbloch, Jun 24, 2011
    #5
  6. Eric

    Aéris Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Le 24/06/2011 17:42, lewbloch a écrit :
    > Reflection is an elephant gun for
    > shooting fleas; simple polymorphism suffices in most cases.


    I totally aggree.

    But with this (craps) code and because constructors with different
    prototype, reflection is unavoidable?

    - --
    Aeris
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.11 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

    iQEcBAEBAgAGBQJOBOVFAAoJEK8zQvxDY4P9Kd8H/17/M093d87q+yvPxZF3AhJa
    JqNGv0IJKzBpLMi0wZpPnIu0zy8yN3G0CXpUIXz0SDN3xjvqmVQwFASVmCzHQorh
    c2wq9X0ZkXs0kxXLRdvzLp3wO6X0T7WsaT6r++W2CFEnougScscwiZ0EyWLE21ow
    1yeZ8PYnsX/UztIjNLv30Ls6TwhCgkqI2jo2yxU7HsSTNh4m+yxCeu14/+92jIAL
    qVb8iSfgv5ja2xDTEwtLl+GX89BW0JCTqrEk3w7aj/XSZTrdTmqQ9OdmgUeTXwfZ
    aJlOk05OVJXhnR/K/QSH45ICbD/Y7i+k6om4FcsAOmVMMsNU72tFsr/VPPCfaK8=
    =E0hj
    -----END PGP SIGNATURE-----
    Aéris, Jun 24, 2011
    #6
  7. Eric

    Lew Guest

    Aéris wrote:
    > lewbloch a écrit :
    >> Reflection is an elephant gun for
    >> shooting fleas; simple polymorphism suffices in most cases.


    > I totally aggree.
    >
    > But with this (craps) code and because constructors with different
    > prototype, reflection is unavoidable?
    >


    Reflection is mostly avoidable. A little light use of 'Class#newInstance()'
    with package-private builders called by a factory method isn't very risky and
    avoids the typical mad craziness of looking up 'Method' or 'Constructor'
    instances. If you're going down that latter route, leave programming to those
    better equipped for it.

    If you think heavy use of reflection will fix crappy code, boy are you ever
    wrong. Shit piled on top of shit only smells worse.

    --
    Lew
    Honi soit qui mal y pense.
    http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
    Lew, Jun 26, 2011
    #7
  8. Eric

    Lew Guest

    William Colls wrote:
    > I think we are being asked to someone's school assignment. Better he does it
    > himself.
    >


    PLEASE do not top-post.

    The OP showed us what he's starting with and asked for _help_, not to "" his
    assignment. (I assume your missing verb is "do".)

    So let's give him some help, if that's OK with you, O Snarky One?

    > On 11-06-23 02:25 PM, Eric wrote:
    >> Hi guys: Can any one please help me how to i [sic] do the changes in my
    >> program according to the following 4 changes.
    >>
    >> 1) Create a Date type birth data instance field in the Employee class,
    >> not a String


    Do you know how to declare a variable to be of a certain type?

    It's done like this:

    SomeType variable;

    Do you know where the 'Date' type is in the standard Java API?
    <http://download.oracle.com/javase/6/docs/api/java/util/package-frame.html>

    Do you know how to declare instance members?
    <http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html>

    >> 2) Enlarge the constructors for the Employee class and subclasses to
    >> pass the int month, int day, and int year of the birth date as input
    >> by the user to PayrollSystemTest to the subclass constructor, and then
    >> to the Employee class constructor


    Do you know how to pass arguments to a constructor? It's very similar to how
    to pass arguments to a method.
    <http://www.oracle.com/technetwork/java/index-jsp-135888.html>
    <http://www.oracle.com/technetwork/java/prog-140388.html#const>
    <http://download.oracle.com/javase/tutorial/java/javaOO/constructors.html>
    <http://download.oracle.com/javase/tutorial/java/javaOO/methods.html>

    >> 3) Input from the user for the 5 specific employees and then comment
    >> out the hardcoding for the 4 employees in the original code


    Do you have your class notes on how to get input?

    >> 4) Report monthly salary amounts and include the November birthday
    >> bonus


    Do you have your class notes on how to present output?

    <http://download.oracle.com/javase/tutorial/essential/io/index.html>

    --
    Lew
    Honi soit qui mal y pense.
    http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
    Lew, Jun 26, 2011
    #8
  9. Eric

    Aéris Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Le 26/06/2011 19:30, Lew a écrit :
    >
    > Reflection is mostly avoidable. A little light use of
    > 'Class#newInstance()' with package-private builders called by a factory
    > method isn't very risky and avoids the typical mad craziness of looking
    > up 'Method' or 'Constructor' instances. If you're going down that
    > latter route, leave programming to those better equipped for it.


    This is what I say…
    On clean code, Class#newInstance + setter avoid reflection.
    But on the craps given code, empty constructor is not available, so
    Class#newInstance is not usable in this case…

    - --
    Aeris
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.11 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

    iQEcBAEBAgAGBQJOB40kAAoJEK8zQvxDY4P9tDcIAMPn2uiUEqevh3Ev26yrh7JK
    SKZsCxfzEtlXmiTjFUItF2XQVZDWChiL+UGpjyrCQhHnyeeL35hRiWWmdfJyxTEw
    IPx5VJ44+7Jk2WAmdGh4fsHDYuYETXKKkfqJnZYZFQPDIElfA6rOBLJgLD+e9Kvj
    PuALhiKa8mqss483NLBVTBYB1303Ro5LoXpkljKl3tIGhI5h3GES8A1QxZJEXHha
    wdrDjtoaAOKa9OfClhZarnEEx07QwxmXE0rAp/OPsUPwtpUgpYgSEB5EopQsNhcA
    C53q+1/B2DNFR7Q2miQrM7uajd4e0C46Fm3obxUSmfzhz6QZm/fhqaIxCgICfrM=
    =CiIk
    -----END PGP SIGNATURE-----
    Aéris, Jun 26, 2011
    #9
  10. Eric

    Lew Guest

    Aéris wrote:
    > Lew a écrit :
    >> Reflection is mostly avoidable. A little light use of
    >> 'Class#newInstance()' with package-private builders called by a factory
    >> method isn't very risky and avoids the typical mad craziness of looking
    >> up 'Method' or 'Constructor' instances. If you're going down that
    >> latter route, leave programming to those better equipped for it.

    >
    > This is what I say…
    > On clean code, Class#newInstance + setter avoid reflection.
    > But on the craps given code, empty constructor is not available, so
    > Class#newInstance is not usable in this case…


    So you suggest lowering the river instead of raising the bridge?

    Again, adding shit to a pile of shit just makes it smell worse. Clean up the
    shit and add clean stuff, i.e., fix the broken design and refactor the code. Duh.

    --
    Lew
    Honi soit qui mal y pense.
    http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
    Lew, Jun 26, 2011
    #10
  11. Eric

    Lew Guest

    Aéris wrote:
    > Lew a écrit :
    >> Reflection is mostly avoidable. A little light use of
    >> 'Class#newInstance()' with package-private builders called by a factory
    >> method isn't very risky and avoids the typical mad craziness of looking
    >> up 'Method' or 'Constructor' instances. If you're going down that
    >> latter route, leave programming to those better equipped for it.

    >
    > This is what I say…
    > On clean code, Class#newInstance + setter avoid reflection.
    > But on the craps given code, empty constructor is not available, so
    > Class#newInstance is not usable in this case…


    I note that you make no effort to evaluate my suggestion against yours for
    their relative merits.

    You can use the existing type that has no no-arg constructor by using a
    builder inside a factory method. No weird reflection needed - just a builder
    that knows how to construct the target object.

    Since the builder is a *new* type, your comment that it doesn't have this or
    that is ridiculous. You create the builder with a no-arg constructor and
    build what you need, returning an instance of the target type to the factory
    method.

    How about you speak to that suggestion, hm?

    --
    Lew
    Honi soit qui mal y pense.
    http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
    Lew, Jun 26, 2011
    #11
  12. Eric

    Roedy Green Guest

    On Thu, 23 Jun 2011 11:25:01 -0700 (PDT), Eric <>
    wrote, quoted or indirectly quoted someone who said :

    >Hi guys: Can any one please help me how to i do the changes in my
    >program according to the following 4 changes.


    see http://mindprod.com/jgloss/homework.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    One of the curses of the computer age is manufacturers now design
    home appliances to die on the very day the warranty expires.
    It is deliberate waste in the service of mindless profit.
    Roedy Green, Jun 28, 2011
    #12
  13. Eric

    Lew Guest

    On 06/23/2011 02:25 PM, Eric wrote:
    > Hi guys: Can any one please help me how to i do the changes in my
    > program according to the following 4 changes.
    >
    > 1) Create a Date type birth data instance field in the Employee class,
    > not a String


    public class Employee
    {
    Date birthData;
    ....

    > 2) Enlarge the constructors for the Employee class and subclasses to
    > pass the int month, int day, and int year of the birth date as input
    > by the user to PayrollSystemTest to the subclass constructor, and then
    > to the Employee class constructor


    They're asking for a lot, aren't they?

    Do you understand the question?

    For example, consider a no-arg constructor:

    public Employee()
    {
    }

    To "enlarge" the constructor is to add another constructor with additional
    arguments. For example, an "enlarged" constructor that takes a 'Date'
    argument looks like this:

    public Employee( Date aDate )
    {
    // do something with the argument
    }

    The term "enlarge a constructor" has no formal meaning in Java. It's an
    informal way to ask for additional constructors.

    Constructors should not ask the user for input. Constructors exist to
    construct the instance, not to perform logic. Note that the problem asks for
    a different type, 'PayrollSystemTest', to accept the user input. It will then
    place the required data into appropriate variables (or one appropriate
    variable) and use those variables (that variable) as arguments to the
    constructor of an 'Employee' subtype.

    So the 'Employee' constructor will not contain logic to accept input from the
    user.

    Nor will any other constructor.

    Since an 'Employee' subtype constructor accepts the arguments, the subtype
    constructor must relay the information to 'Employee' via its call to
    'super(someArguments)'.


    > 3) Input from the user for the 5 specific employees and then comment
    > out the hardcoding for the 4 employees in the original code


    This is an academic exercise. In real code you delete code, not comment it out.

    > 4) Report monthly salary amounts and include the November birthday
    > bonus
    >
    > import java.util.ArrayList;
    > import java.util.Date;
    > import java.util.Scanner;
    >
    > public class Employee {
    >
    > String socialSecurity;
    > String birthDate;


    Usually member variables should be 'private', and in this case, 'final'.

    > public Employee(String socialSecurity, String birthDate){
    > this.birthDate = birthDate;
    > this.socialSecurity = socialSecurity;
    > }


    What if the arguments are 'null'?

    > public String getSocialSecurity(){
    > return socialSecurity;
    > }
    >
    > public String getBirthDate(){
    > return birthDate;
    > }
    >
    > public String toString(){
    > return ("Employee: Social security " + socialSecurity
    > + " date of birth " + birthDate);
    > }
    >
    > public static void main(String[] args) {
    >
    > Scanner keyboard = new Scanner(System.in);
    > System.out.println("Enter employees one by one");
    > ArrayList<Employee> ar = new ArrayList<Employee>();
    >
    > while(true){
    > System.out.println(" Employee types available:");
    > System.out.println("");
    > System.out.println("1-Salaried Employee");
    > System.out.println("2-Hourly Employee");
    > System.out.println("3-Commission Employee");
    > System.out.println("4-Based Salary Commission
    > Employee");
    > System.out.println("");
    > System.out.println("Enter employee type (1-4) .
    > Finish list with type 0: ");
    >
    > String typeString = keyboard.next();
    > // boolean goodInput = true;
    > int type = -1;


    What's with the "-1" crud? Use the string.

    > try{
    > type = Integer.parseInt(typeString);
    > } catch(Exception ex){
    >


    Class, what are we supposed to do with exceptions?

    > }
    >
    > if(type<0 || type> 4){
    > System.out.println("Invalid type. Please, try
    > again. ");
    > continue;
    > }
    >
    > if(type == 0)break;


    Wow.

    > System.out.println("Enter social security: ");
    > String social = keyboard.next();
    > System.out.println("Enter date of birth: ");
    > String birth = keyboard.next();
    >
    > boolean goodInput = true;
    > switch(type){


    Giant Switch Statement Alert!

    Soon you will learn to write subroutines. That will help.

    --
    Lew
    Honi soit qui mal y pense.
    http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
    Lew, Jun 28, 2011
    #13
  14. On 6/26/2011 1:30 PM, Lew wrote:
    > Aéris wrote:
    >> lewbloch a écrit :
    >>> Reflection is an elephant gun for
    >>> shooting fleas; simple polymorphism suffices in most cases.

    >
    >> I totally aggree.
    >>
    >> But with this (craps) code and because constructors with different
    >> prototype, reflection is unavoidable?
    >>

    >
    > Reflection is mostly avoidable. A little light use of
    > 'Class#newInstance()' with package-private builders called by a factory
    > method isn't very risky and avoids the typical mad craziness of looking
    > up 'Method' or 'Constructor' instances. If you're going down that latter
    > route, leave programming to those better equipped for it.
    >
    > If you think heavy use of reflection will fix crappy code, boy are you
    > ever wrong. Shit piled on top of shit only smells worse.


    It depends a little bit about what you are doing.

    I would not want to implement a Java EE 6 server without being
    allowed to use reflection.

    Even some business code can use some reflection even though in
    most cases it is better to hide the reflection via some DI
    framework.

    Reflection is a very useful tool and a very powerful tool. One
    should just limit its use to where it is necesarry.

    A B-52 bomber is also pretty powerful if you want to engage in a war.
    It is not the correct tool for getting rid of the mosquitos in the
    house.

    Arne
    Arne Vajhøj, Jul 22, 2011
    #14
  15. Eric

    lewbloch Guest

    On Jul 22, 3:13 pm, Arne Vajhøj <> wrote:
    > On 6/26/2011 1:30 PM, Lew wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > Aéris wrote:
    > >> lewbloch a écrit :
    > >>> Reflection is an elephant gun for
    > >>> shooting fleas; simple polymorphism suffices in most cases.

    >
    > >> I totally aggree.

    >
    > >> But with this (craps) code and because constructors with different
    > >> prototype, reflection is unavoidable?

    >
    > > Reflection is mostly avoidable. A little light use of
    > > 'Class#newInstance()' with package-private builders called by a factory
    > > method isn't very risky and avoids the typical mad craziness of looking
    > > up 'Method' or 'Constructor' instances. If you're going down that latter
    > > route, leave programming to those better equipped for it.

    >
    > > If you think heavy use of reflection will fix crappy code, boy are you
    > > ever wrong. Shit piled on top of shit only smells worse.

    >
    > It depends a little bit about what you are doing.
    >
    > I would not want to implement a Java EE 6 server without being
    > allowed to use reflection.
    >
    > Even some business code can use some reflection even though in
    > most cases it is better to hide the reflection via some DI
    > framework.
    >
    > Reflection is a very useful tool and a very powerful tool. One
    > should just limit its use to where it is necesarry.
    >
    > A B-52 bomber is also pretty powerful if you want to engage in a war.
    > It is not the correct tool for getting rid of the mosquitos in the
    > house.
    >


    That's why I referred to "heavy use" of reflection and in the context
    of crappy code. I completely agree that reflection is useful when
    needed, but whether you say "B-52 for mosquitoes" or "elephant gun for
    fleas" , the message is the same. Thanks for endorsing my point.

    --
    Lew
    lewbloch, Jul 22, 2011
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darren Spooner

    help editing

    Darren Spooner, Oct 24, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    390
    Darren Spooner
    Oct 24, 2003
  2. Replies:
    43
    Views:
    1,302
    Oliver Wong
    Dec 9, 2005
  3. Replies:
    0
    Views:
    572
  4. Clueless in Seattle

    Novice Needs Help Editing Web Page

    Clueless in Seattle, Apr 15, 2007, in forum: HTML
    Replies:
    4
    Views:
    321
    dorayme
    Apr 16, 2007
  5. Zayd Connor
    Replies:
    8
    Views:
    99
    Zayd Connor
    Apr 6, 2009
Loading...

Share This Page