In: Computer Science
Develop a program, in which you'll design a class that holds the following personal data: name, address, age, and phone number. As part of the program, you'll then create appropriate accessor and mutator methods. Also, set up three instances of the class. One instance should hold your information, and the other two should hold your friends’ or family members’ information.
Input:
NOTE: The input data do not need to be real data; they need to be entered at the keyboard, when program is run.
Processing: Develop the following:
Output:
Set up a loop and display data within each object:
// PLEASE LIKE THE SOUTION
// FEEL FREE TO DISCUSS IN COMMENT SECTION
// JAVA PROGRAM
import java.util.*;
class PersonalInformation{
// properties
String name;
String address;
int age;
long phoneNumber;
// accessors
public String getName(){
return name;
}
public String getAddress(){
return address;
}
public int getAge(){
return age;
}
public long getPhoneNumber(){
return phoneNumber;
}
// mutators
// accessors
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 setPhoneNumber(long phoneNumber){
this.phoneNumber =
phoneNumber;
}
// main method
public static void main(String arg[]){
// three objects
PersonalInformation array[] = new
PersonalInformation[3];
// initialize
for(int i=0;i<3;i++){
array[i] = new
PersonalInformation();
}
Scanner scan = new Scanner(System.in);
// now ask user for
information
for(int i=0;i<3;i++){
System.out.print("For Object "+(i+1)+"\n");
System.out.print("Enter name ");
String str =
scan.next();
array[i].setName(str);
System.out.print("Enter Address ");
str =
scan.next();
array[i].setAddress(str);
System.out.print("Enter age ");
int a =
scan.nextInt();
array[i].setAge(a);
System.out.print("Enter phone number ");
long x =
scan.nextLong();
array[i].setPhoneNumber(x);
System.out.println();
}
// now print the values
for(int i=0;i<3;i++){
System.out.print("For Object "+(i+1)+"\n");
System.out.print("Name = "+array[i].getName());
System.out.print(" Address = "+array[i].getAddress());
System.out.print(" Age = "+array[i].getAge());
System.out.print(" Phone Number =
"+array[i].getPhoneNumber());
System.out.println();
}
}
}
// SAMPLE OUTPUT