Question: Write a program named ChopstickDemo.java, which has two classes Chopstick and ChopstickDemo . The Chopstick class has a field name (a String) and a few
Write a program named ChopstickDemo.java, which has two classes Chopstick and ChopstickDemo. The Chopstick class has a field name (a String) and a few other members:
A private constructor that takes a String as the argument and assigns it to name.
An overridden toString() method that returns name.
Two private static variables cs1 and cs2, and they are initialized with names Chopstick1 and Chopstick2.
Two methods access1() and access2(), which return the two static members, respectively.
The ChopstickDemo class only has a main method that prints out the above two static members of Chopstick.
Hint: Theres a similar example in the lecture slides. It was called singleton because only one object was allowed to be created. Here two (and only two) Chopstick objects are allowed to be created.
The program should work like below:
java ChopstickDemo
>Chopstick1
>Chopstick2
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
