[Java] Person.java

1 minute read

class Person {
private static int count = 0;
private String firstName;
private String middleName;
private String lastName;
private int age;
private double height;
private double weight;
// Add the instance field “job”
private String job;

// Rewrite the constructor
Person(String firstName, String lastName, int age, double height, double weight,String job) {
Person.count++;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.height = height;
this.weight = weight;
this.job = job;
}

// Rewrite the constructor
Person(String firstName, String middleName, String lastName, int age, double height, double weight,String job) {
this(firstName, lastName, age, height, weight,job);
this.middleName = middleName;
}

public String getMiddleName() {
return this.middleName;
}

// Define a job getter
public String getJob(){
return this.job;
}

public void setMiddleName(String middleName) {
this.middleName = middleName;
}

// define a job setter
public void setJob(String job){
this.job = job;
}

public String fullName() {
if (this.middleName == null) {
return this.firstName + “ “ + this.lastName;
} else {
return this.firstName + “ “ + this.middleName + “ “ + this.lastName;
}
}

public void printData() {
System.out.println (“My name is” + this.fullName () + “”);
System.out.println (“Age” + this.age + “Age”);
System.out.println (“BMI is” + Math.round (this.bmi ()) + “”);
// Please output “Job is ◯◯”
System.out.println (“Job is” + this.job + “);

}

public double bmi() {
return this.weight / this.height / this.height;
}

Tags:

Updated: