Learn AP Comp Sci

Problem of the Day

Friday, February 6, 2026


Problem:

The WritingPen class is intended to model a clear ballpoint pen with ink in it. It has a cap that can be removed, and a pen with its cap removed can be used to write words using the ink stored in it. The amount of ink remaining in the pen is visible through the pen's clear body.

A partial description of the class is given here.

public class WritingPen
{
private double inkAmount;
private boolean capOn;

public WritingPen()
{
inkAmount = 2.0; // units are grams
capOn = true;
}

/* code not shown */

}

Which of the following represents some of the methods that might be appropriate for this class?

  1. write(), getPenLength(), capIsOff(), getManufacturer()
  2. getInkAmount(), write(), capIsOn(), removeCap()
  3. capIsOff(), setLanguage(), getInkAmount(), removeCap()
  4. getInkAmount(), removeCap(), getPenPrice(), write()

Show solution:

The correct answer is b. Based on the instance variables shown, the only aspects of the pen that we're considering in this model (this abstraction) are the amount of ink in the pen and whether or not the cap is on. The price, manufacturer, physical dimensions, and language that we're writing in aren't being considered, and therefore, no methods are needed for those qualities.