Ok... I just got this assignment and I\'m not even sure what it is asking Provid
ID: 3621169 • Letter: O
Question
Ok... I just got this assignment and I'm not even sure what it is askingProvide a complete program that simulates the creation and display of Person objects. Provide a Person class that contains:
· Two instance variables named first and last that keep track of a person’s first and last names, respectively.
· A class variable named numOfPeople that keeps track of the total number of instantiated Person objects.
· A 0-parameter constructor that initializes the Person object with the name “John Doe".
· An appropriate 2-parameter constructor.
· Four methods named setFirst, setLast, getNumOfPeople, and printFullName that work in conjunction with this driver method:
public static void main(String[] args)
{
Person person1 = new Person();
person1.printFullName();
Person person2 = new Person("Matt", "Thebo");
person2.printFullName();
person1.setFirst("Paul");
person1.setLast("John");
person1.printFullName();
System.out.println("Total number of people = " +
Person.getNumOfPeople());
} // end main
Explanation / Answer
This is what I came up with. I tried to add comments to explain it a little bit better, and how each part fits in with the instructions. I am a little unsure about how they wanted you to keep track of the numOfPeople, but it counts correctly for the driver method that they wanted you to use. Hope it helps! public class Person{ /*This is where your person class is defined*/ private String first; /*These are your instance variables. Each object created will have their own instance of these variables.*/ private String last; /*They are string data types because they will contain a string of characters.*/ private static int numOfPeople = 0; /*This is your class variable, recognized by the static keyword. It exists only once in this class.*/ public void setFirst(String first) /*Setter method for first name. */ { this.first = first; /*The 'this' keyword tells the program that I am setting the instance variable of first to the method's variable first*/ } public void setLast(String last) /*Setter method for the last name. Set up the same way*/ { this.last = last; } public static int getNumOfPeople() /*This is also static, because it uses your class variable and is something that is not changed per each method instance*/ { numOfPeople++; /*This is how I am keeping track of the number of people. This increments the numOfPeople by 1. I set it as 0 to start at the beginning of the class*/ return numOfPeople; } public void printFullName() { System.out.println(first + " " + last); /*This is what displays their names*/ } public Person () /*This is your Person constructor with no parameters.*/ { first = "John"; last = "Doe"; numOfPeople++; /*I am incrementing the numOfPeople here as well*/ } public Person (String first, String last) /*This is your Person constructor with appropriate parameters*/ { this.first = first; /*Notice the similiarities with the setter methods. The assignment is showing you different ways that you can do basically the same thing.*/ this.last = last; numOfPeople++; /*I am incrementing the numOfPeople here as well*/ } public static void main(String[] args) { Person person1 = new Person(); person1.printFullName(); Person person2 = new Person("Matt", "Thebo"); person2.printFullName(); person1.setFirst("Paul"); person1.setLast("John"); person1.printFullName(); System.out.println("Total number of people = " + Person.getNumOfPeople()); } // end main }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.