Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 160
  • Last Modified:

Is this the correct syntax for creating classes, objects, and methods in Java and should birthdays be created as strings or dates?

Is this the correct syntax for creating a class, objects, and methods in Java


---For Defining a Class named Person and amethod named age

Class Person
     string Fname
     string Lname
     string Birthday

  int age ( )
    return Date – getJulian(Birthday)

-- For Defining 2 Objects named Employee and Student

Person Employee = new Person ( );  int e_age;
Person Student = new Person ( );  int s_age;

--- For assigning attributes values

Employee.Fname = ‘Sam’
Employee.Lname = ‘Brown’
Employee.Birthday = ’07/13/73’
e_age = Employee. age ( )
0
dlabraham
Asked:
dlabraham
  • 2
2 Solutions
 
objectsCommented:
public class Person
{
     String Fname
     String Lname
     String Birthday

  int age ( )
  {
     int age = ...; // add your clac here
     return age;
  }
}
 
-- For Defining 2 Objects named Employee and Student

Person Employee = new Person ( );  
int e_age;
Person Student = new Person ( );  
int s_age;

--- For assigning attributes values

Employee.Fname = "Sam";
Employee.Lname = "Brown";
Employee.Birthday = ’07/13/73";
e_age = Employee. age ( );

0
 
CEHJCommented:
Your attributes should be private and accessed using get/set methods:


public class Person {

      private String firstName;
      private String lastName;

      public void setFirstName(String firstName) {
        this.firstName = firstName;
      }

      public String getFirstName() {
        return firstName;
      }

      public void setLastName(String lastName) {
        this.lastName = lastName;
      }

      public String getLastName() {
        return lastName;
      }
      
      // more, including constructor(s)
}

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now