2021. 4. 5. 21:54ㆍJava programming
Write a class with the name Person. The class needs three fields with the names firstName, lastName of type String and age of type int.
Write the following methods:
*Method named getFirstName without any parameters, it needs to return the value of the firstName field.
*Method named getLastName without any parameters, it needs to return the value of the lastName field.
*Method named getAge without any parameters, it needs to return the value of the age field.
*Method named setFirstName with one parameter of type String, it needs to set the value of the firstName field.
*Method named setLastName with one parameter of type String, it needs to set the value of the lastName field.
*Method named setAge with one parameter of type int, it needs to set the value of the age field. If the parameter is less than 0 or greater than 100, it needs to set the age field value to 0.
*Method named isTeen without any parameters, it needs to return true if the value of the age field is greater than 12 and less than 20, otherwise, return false.
*Method named getFullName without any parameters, it needs to return the full name of the person.
*In case both firstName and lastName fields are empty, Strings return an empty String.
*In case lastName is an empty String, return firstName.
*In case firstName is an empty String, return lastName.
To check if s String is empty, use the method isEmpty from the String class. For example, firstName.isEmpty() returns true if the String is empty or in other words, when the String does not contain any characters.
public class Person {
private String firstName;
private String lastName;
private int age = 0;
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public int getAge(){
return age;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public void setAge(int age){
if(age < 0 || age > 100){
age = 0;
}
this.age = age;
}
public boolean isTeen(){
if(age > 12 && age < 20){
return true;
}
else {
return false;
}
}
public String getFullName(){
if(firstName.equals("") && lastName.equals("")){
return "";
}
else if (lastName.equals("")){
return firstName;
}
else if (firstName.equals("")){
return lastName;
}
else {
return firstName + " " + lastName;
}
}
}
The fields (instance variables) in the class Person should be in private form as access modifer.
Like I said in the previous post, 'this.' is used for distinguishing between instance variables and local variables in case two names both of variables are the same.
A variable with 'this.' ahead of a variable name is an instance variable whereas a variable without 'this.' is a local variable.
'Java programming' 카테고리의 다른 글
Coding Exercise 32: Point (0) | 2021.04.07 |
---|---|
Coding Exercise 31: Wall Area (0) | 2021.04.06 |
Coding Exercise 29: Sum Calculator (0) | 2021.04.04 |
Coding Exercise 28: Paint Job (0) | 2021.04.04 |
Coding Exercise 27: Input Calculator (0) | 2021.04.03 |