Specify, design, and implement a class that keeps track of rings stacked on a peg, rather like

Question:

Specify, design, and implement a class that keeps track of rings stacked on a peg, rather like phonograph records on a spindle. An example with five rings is shown here:

Rings stacked on a peg

The peg may hold up to 64 rings, with each ring having its own diameter. Also, there is a rule that requires each ring to be smaller than any ring underneath it. The class’s methods should include: 

(a) A constructor that places n rings on the peg (where n may be as large as 64), and these n rings have diameters from n inches on the bottom to one inch on the top; 

(b) An accessor method that returns the number of rings on the peg; 

(c) An accessor method that returns the diameter of the topmost ring; 

(d) A method that adds a new ring to the top (with the diameter of the ring as a parameter to the method); 

(e) A method that removes the topmost ring;

(f) A method that prints some clever representation of the peg and its rings. Make sure that all methods have appropriate preconditions to guarantee that the rule about ring sizes is enforced. Also spend time designing appropriate private instance variables.

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question
Question Posted: