/**
* Chapter 3, exercise 2, page 123
*
* Write a declaration for the BallpointPen class
* as desribed in section 3.2. Leave the body
* of the methods empty.
*
***/
public class BallpointPen {
// variables (data)
private boolean pointIsExposed;
private double inkRemaining;
BallpointPen(){
}
// toggle value of pointIsExposed.
public void pressButton() {
pointIsExposed = ! pointIsExposed;
}
// reduce inkRemaining
public void write(int secSpentWriting){
inkRemaining = inkRemaining - secSpentWriting;
}
// reset inkRemaining to original value
public void replaceCartridge(){
inkRemaining = 1000.0;
}
// return value of inkRemaining
public double checkInkRemaining(){
return inkRemaining;
}
}
// =================================================
/**
* Chapter 5, exercise 5, page 123
*
* Write a declaration for the BallpointPen class
* as desribed in section 3.2. Leave the body
* of the methods empty.
*
***/
/*
(a) press the button
(b) write for 200 sec
(c) replace cartridge
(d)
*/
BallpointPen jimsPen = new BallpointPen();
jimsPen.pressButton();
jimsPen.write(200); // write for 200 seconds.
jimsPen.replaceCartridge();
double inkLeft = jimsPen.checkInkRemaining();
syntax highlighted by Code2HTML, v. 0.9.1