Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

image text in transcribedimage text in transcribed
Sample Output: (In red, entered by the user. In green, I have written for clarification) Name = Nidhi Age = 20 Phone Number = 123-444-3321 Total People = 1//Extra Line, Static variable. Name = Tilak Age = 40 Phone Number = 213-444-2134 Total People = 2 //Extra Line, Static variable. Enter name: Nidh Tilak Enter age: 60 Enter phone number: 123456789 Name = Nidh Tilak Age = 60 Phone Number = 123456789 Total People = 3 //Extra Line, Static variable.--- jGRASP exec: java PersonalInfoDemo19A Enter name: Daniel Hernandez Enter age: 60 Enter phone number: 123 456 7890 Total People = 3 Name = Daniel Age = 20 Phone = 123-444-3321 Name = Hernandez Age = 40 Phone = 213-444-2134 Name = Daniel Hernandez Age = 60 Phone = 123 456 7890 Total People = 3 -jGRASP: operation complete

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions