Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. (TCO 2) Keeping in mind all object-oriented programming best practices, creat

ID: 3558811 • Letter: 2

Question

2. (TCO 2) Keeping in mind all object-oriented programming best practices, create a class for a Chair, with the following specifications: 1. Specify two data members 2. Default Constructor 3. Overloaded Constructor which takes both data member values as input. 4. Generate a unique identification number for each object instantiated from this class. Use a static data member to keep track of the identification number last assigned to an object so that duplications will not occur. 5. Show a statement which instantiates an object of this class using the overloaded constructor. You do not need to provide any accessor/mutator methods or other methods. (Points : 18)

Explanation / Answer

//create a class for a Chair, with the following specifications:
class Chair
{
// 1. Specify two data members
private int unique_no;
private double price;
// 4. Generate a unique identification number for each object instantiated from this class.
// Use a static data member to keep track of the identification number last assigned to an object so
that duplications will not occur.
private static int unique_number = 1;
//2. Default Constructor
Chair()
{
unique_no = unique_number++;
price = 300;
}
//3. Overloaded Constructor which takes both data member values as input.
Chair(double p)
{
unique_no = unique_number++;
price = p;
}
int get_unique_number() { return unique_no; }
}

public class Demo
{
// You do not need to provide any accessor/mutator methods or other methods. (Points : 18)
public static void main(String[] args)
{
// 5. Show a statement which instantiates an object of this class using the overloaded constructor.
Chair C1 = new Chair(450);
System.out.println("Unique number of object C1 is "+ C1.get_unique_number());
}
}