This discussion is archived
7 Replies Latest reply: Mar 1, 2013 9:44 AM by 939520 RSS

Java Classes

987741 Newbie
Currently Being Moderated
Hi,

I am new to Java, so excuse if this question is real basic:

I have 3 questions.

Write a Java class called Person which describes a person with these properties:
- First Name
- Last Name
- Age

Write a Java class which describes a company employee which uses the Person class above and adds two extra properties:
- Position
- Join Date

Write a Java class called Company which contains
- One or more employees
- Business type


I know how to do the first one:

public class Person {

String fName;
String lName;
int age;
}

But I am very trouble with the last two, please can someone help

Thank you
  • 1. Re: Java Classes
    Kayaman Guru
    Currently Being Moderated
    984738 wrote:
    But I am very trouble with the last two, please can someone help
    Do you know how to extend a class? That's the key for the second question.

    For the third question, you are probably meant to use an array of employees.

    Good luck.
  • 2. Re: Java Classes
    987741 Newbie
    Currently Being Moderated
    public class describe extends Person {

    String Position;
    int jDate;
    }

    Is this how the second question is done?
  • 3. Re: Java Classes
    Kayaman Guru
    Currently Being Moderated
    984738 wrote:
    public class describe extends Person {

    String Position;
    int jDate;
    }

    Is this how the second question is done?
    Well, I certainly wouldn't call a class "describe". Would you say "my company has many describes working there"? Of course not.

    But yes, extends is the keyword for creating a subclass.
  • 4. Re: Java Classes
    994177 Newbie
    Currently Being Moderated
    public class Person {
         
         String firstName;
         String lastName;
         int age;
         
         public Person() {
              super();
         }

         public Person(String firstName, String lastName, int age) {
              this.firstName = firstName;
              this.lastName = lastName;
              this.age = age;
         }
         
         public String getFirstName() {
              return firstName;
         }
         public void setFirstName(String firstName) {
              this.firstName = firstName;
         }
         public String getLastName() {
              return lastName;
         }
         public void setLastName(String lastName) {
              this.lastName = lastName;
         }
         public int getAge() {
              return age;
         }
         public void setAge(int age) {
              this.age = age;
         }
         
    }

    ------------------------------------------------------------------------------------------
    import java.util.Date;

    public class Employee extends Person {
         
         String position;
         Date joiningDate;

         public Employee() {
              super();
         }

         public Employee(String position, Date joiningDate) {
              this.position = position;
              this.joiningDate = joiningDate;
         }

         public String getPosition() {
              return position;
         }
         public void setPosition(String position) {
              this.position = position;
         }
         public Date getJoiningDate() {
              return joiningDate;
         }
         public void setJoiningDate(Date joiningDate) {
              this.joiningDate = joiningDate;
         }
         
    }
    ---------------------------------------------------------------------------------

    import java.util.List;

    public class Company {
         
         List<Employee> employees;
         String businessType;
         
         public Company() {
              super();
         }

         public List<Employee> getEmployees() {
              return employees;
         }

         public void setEmployees(List<Employee> employees) {
              this.employees = employees;
         }
         public String getBusinessType() {
              return businessType;
         }
         public void setBusinessType(String businessType) {
              this.businessType = businessType;
         }
         
    }
  • 5. Re: Java Classes
    994177 Newbie
    Currently Being Moderated
    public class Person {
         
         String firstName;
         String lastName;
         int age;
         
         public Person() {
              super();
         }

         public Person(String firstName, String lastName, int age) {
              this.firstName = firstName;
              this.lastName = lastName;
              this.age = age;
         }
         
         public String getFirstName() {
              return firstName;
         }
         public void setFirstName(String firstName) {
              this.firstName = firstName;
         }
         public String getLastName() {
              return lastName;
         }
         public void setLastName(String lastName) {
              this.lastName = lastName;
         }
         public int getAge() {
              return age;
         }
         public void setAge(int age) {
              this.age = age;
         }
         
    }

    ------------------------------------------------------------------------------------------
    import java.util.Date;

    public class Employee extends Person {
         
         String position;
         Date joiningDate;

         public Employee() {
              super();
         }

         public Employee(String position, Date joiningDate) {
              this.position = position;
              this.joiningDate = joiningDate;
         }

         public String getPosition() {
              return position;
         }
         public void setPosition(String position) {
              this.position = position;
         }
         public Date getJoiningDate() {
              return joiningDate;
         }
         public void setJoiningDate(Date joiningDate) {
              this.joiningDate = joiningDate;
         }
         
    }
    ---------------------------------------------------------------------------------

    import java.util.List;

    public class Company {
         
         List<Employee> employees;
         String businessType;
         
         public Company() {
              super();
         }

         public List<Employee> getEmployees() {
              return employees;
         }

         public void setEmployees(List<Employee> employees) {
              this.employees = employees;
         }
         public String getBusinessType() {
              return businessType;
         }
         public void setBusinessType(String businessType) {
              this.businessType = businessType;
         }
         
    }
  • 6. Re: Java Classes
    994177 Newbie
    Currently Being Moderated
    public class Person {
         
         String firstName;
         String lastName;
         int age;
         
         public Person() {
              super();
         }

         public Person(String firstName, String lastName, int age) {
              this.firstName = firstName;
              this.lastName = lastName;
              this.age = age;
         }
         
         public String getFirstName() {
              return firstName;
         }
         public void setFirstName(String firstName) {
              this.firstName = firstName;
         }
         public String getLastName() {
              return lastName;
         }
         public void setLastName(String lastName) {
              this.lastName = lastName;
         }
         public int getAge() {
              return age;
         }
         public void setAge(int age) {
              this.age = age;
         }
         
    }

    ------------------------------------------------------------------------------------------

    import java.util.Date;

    public class Employee extends Person {
         
         String position;
         Date joiningDate;

         public Employee() {
              super();
         }

         public Employee(String position, Date joiningDate) {
              this.position = position;
              this.joiningDate = joiningDate;
         }

         public String getPosition() {
              return position;
         }
         public void setPosition(String position) {
              this.position = position;
         }
         public Date getJoiningDate() {
              return joiningDate;
         }
         public void setJoiningDate(Date joiningDate) {
              this.joiningDate = joiningDate;
         }
         
    }

    ------------------------------------------------------------------------------------------

    import java.util.List;

    public class Company {
         
         List<Employee> employees;
         String businessType;
         
         public Company() {
              super();
         }

         public List<Employee> getEmployees() {
              return employees;
         }

         public void setEmployees(List<Employee> employees) {
              this.employees = employees;
         }
         public String getBusinessType() {
              return businessType;
         }
         public void setBusinessType(String businessType) {
              this.businessType = businessType;
         }
         
    }
  • 7. Re: Java Classes
    939520 Explorer
    Currently Being Moderated
    I think it may be a good idea to avoid giving complete answers to homework questions. The student needs to reason through the assignment on his own and exercise is gray matter.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points