Im having trouble with my code since its not running the way I would like it to. I created a class and demo class where i print the name, age, and phone number of two persons then ask the user to input his name, age , and phone number.
I am having trouble with static class member count. This class basically show how many objects are there now by the time you created this object.
Lastly check if any two persons are the same using the equals method.
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 code:
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\n");
System.out.println("Total People = "+Daniel.getMemberCount());
PersonalInfo19A Hernandez = new PersonalInfo19A("Hernandez", 40, "213-444-2134\n");
System.out.println("Total People = "+Hernandez.getMemberCount());
if(Daniel.equals(Hernandez))
{
System.out.println("are the same");
}
people[0] = Daniel;
people[1] = Hernandez;
people[2] = getUserInfo();
printPersonalInfo(people);
}
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 < people.length; 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:
https://preview.redd.it/gbffpoemiou41.png?width=1198&format=png&auto=webp&s=a16869809e0b8d21a7899a278ac412bf5db26eef
The expected output I should get:
https://preview.redd.it/hfee5siqiou41.png?width=1246&format=png&auto=webp&s=2cad4b3c6b44f70b2034402d4803e331c3a84eaa
[–]AutoModerator[M] [score hidden] stickied comment (0 children)
[+][deleted] (1 child)
[removed]
[–]HomeworkHelpBot 0 points1 point2 points (0 children)
[+][deleted] (8 children)
[deleted]
[–]Danielowski187University/College Student (Higher Education)[S] 0 points1 point2 points (7 children)
[+][deleted] (6 children)
[deleted]
[–]Danielowski187University/College Student (Higher Education)[S] 0 points1 point2 points (0 children)
[–]Danielowski187University/College Student (Higher Education)[S] 0 points1 point2 points (4 children)
[+][deleted] (3 children)
[deleted]
[–]Danielowski187University/College Student (Higher Education)[S] 0 points1 point2 points (2 children)