Question
I am having trouble with the output I get to match the expected output. I have a class code and demo class that prints information
I am having trouble with the output I get to match the expected output.
I have a class code and demo class that prints information name, age , and phone number. Then it asks the user to input this information and prints it. I am having trouble with Static class membercountin this Exercise and when you are printing the information about the object, your output must show how many objects are there now by the time you created this object.
For example.
Person 1
Total people = 1
Person 2
Total people = 2
The user input/person 3
Total people = 3
My class code:
public class PersonalInfo19A
{
public static int memberCount = 0;
private String name;
private int age;
private String phoneNumber;
public PersonalInfo19A(String n, int a, String pn)
{
name = n;
age = a;
phoneNumber = pn;
memberCount++;
}
public String toString()
{
return name + ", " + age + ", " + phoneNumber;
}
public void setName(String n) {
name = n;
}
public void setAge(int a) {
age = a;
}
public void setPhoneNumber(String pn) {
phoneNumber = pn;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getPhoneNumber() {
return phoneNumber;
}
public int getMemberCount()
{
return memberCount;
}
public boolean equals(Object o)
{
PersonalInfo19A a = (PersonalInfo19A) o;
if(this.name.compareTo(a.name)==0 && this.age==a.age && this.phoneNumber.compareTo(a.phoneNumber)==0){
return true;
}
return false;
}
}
My demo class:
import java.util.Scanner;
public class PersonalInfoDemo19A
{
public static final int size = 3;
public static PersonalInfo19A[] people = new PersonalInfo19A[size];
public static void main(String[] args) {
PersonalInfo19A Daniel = new PersonalInfo19A("Daniel", 20, "123-444-3321 ");
PersonalInfo19A Hernandez = new PersonalInfo19A("Hernandez", 40, "213-444-2134 ");
if(Daniel.equals(Hernandez))
{
System.out.println("are the same");
}
people[0] = Daniel;
people[1] = Hernandez;
people[2] = getUserInfo();
System.out.println("Total People = "+Daniel.getMemberCount());
printPersonalInfo(people);
System.out.println("Total People = "+Hernandez.getMemberCount());
}
public static PersonalInfo19A getUserInfo() {
Scanner input = new Scanner(System.in);
System.out.print("Enter name: ");
String name = input.nextLine();
System.out.print("Enter age: ");
int age = Integer.parseInt(input.nextLine());
System.out.print("Enter phone number: ");
String phoneNumber = input.nextLine();
PersonalInfo19A person = new PersonalInfo19A(name, age, phoneNumber);
return person;
}
public static void printPersonalInfo(PersonalInfo19A[] people) {
for (int i = 0; i
{
System.out.println("Name = "+people[i].getName());
System.out.println("Age = " + people[i].getAge());
System.out.println("Phone = " + people[i].getPhoneNumber());
}
}
}
The output I get:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started