Write a class that holds the following personal data: name, address, age, and ph
ID: 3884005 • Letter: W
Question
Write a class that holds the following personal data: name, address, age, and phone number. Store each datum in a field of an appropriate type. (Hint: What is the best type for a variable that stores a phone number? Perhaps counterintuitively, is not a numeric type. Why not?) Write getters and setters for each of your class’ fields. Demonstrate your class by writing an application program that creates three instances of it. One instance should hold your information and the other two should hold the information of two of your friends or family members.
Explanation / Answer
Please find my implementation.
Please let me know in case of any issue.
##########
public class Personal {
// instance fields
private String name;
private String address;
private int age;
private String phone;
// getters
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public int getAge() {
return age;
}
public String getPhone() {
return phone;
}
// setters
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
public void setAge(int age) {
this.age = age;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
##########
public class PersonTest {
public static void main(String[] args) {
// creating object of Person class
Personal myInfo = new Personal();
myInfo.setName("Pravesh Kuamr");
myInfo.setAge(24);
myInfo.setAddress("Bangalore");
myInfo.setPhone("123456789");
System.out.println("My Info: "+myInfo.getName()+", "+myInfo.getAge()+", "+myInfo.getAddress()+
", "+myInfo.getPhone());
}
}
/*
Sample run:
My Info: Pravesh Kuamr, 24, Bangalore, 123456789
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.