Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Person Class public class Person private String name; private int id; private static int personCount = 0; // constructor public Person (String pname) { name

image text in transcribed

image text in transcribed

image text in transcribed

Person Class public class Person private String name; private int id; private static int personCount = 0; // constructor public Person (String pname) { name == = pname; personCount++; id = 100 + personCount; } public String toString() return "name: " + name + " id: " + id (Person count: + personCount + ")"; } // static/class method public static int getCount() { return personCount; Look at class Person on the attached handout. Each person has instance variables for an id and name associated with them. Notice also the private class variable personCount. We will use this class variable to assign a unique id to each person in the order they are "created". Unlike instance variables, which as the name implies come with every instance of a class (i.e. , object), class variables are associated with the class instead. This means, only one copy of this variable is shared among all objects of this class and therefore the value of this variable is the same for all of the objects. To declare a class variable we use the keyword static. Class methods are of course similarly declared. Notice the static method getCount(). Do you see how its value is shared among all possible variables instances) of type person? This is in contrast to the id, which is different for each person. Finally, notice how we can invoke the static method. We can call it with in instance of the class, i.e., an object (e.g., sue.getCount()) - or, and this is different, by just referring to the name of the class, i.e., Person.getCount()). Now, in the space below, trace through and give the output of the StaticTest program shown. Embellishing the Person Class Let us overload our constructor by adding a default (no-args) constructor to Person that defines an object to have the name "N/A and an id of -1. Use the this operator appropriately, and be sure to increment person Count. Add a setter method named reset that can be used to reset the two private instance variables of this class to two values passed in as parameters. This will allow your Person class to now be mutable. Add getter methods getName and getId that can be used to retrieve these two private variables. Finally, let us test our new methods at the end of the StaticTest program. Declare a Person object sample Person that uses the default constructor. Next, reset it to your own name and the last four digits of your social security number using the reset method. Finally, print out your name and id using your getter methods

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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions

Question

create a semiotic sign system to communicate an idea.

Answered: 1 week ago