CHAPTER 31—Designing a Class (Miles per Gallon)

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to home page   go to next page

creation 4/17/99; revised: 09/12/99, 07/14/02, 01/18/06, 04/01/07

CHAPTER 31—Designing a Class
(Miles per Gallon)

This chapter contains another example of designing a class, and then using objects of that type. The class is named Car, and is a simple miles-per-gallon calculator.

Chapter Topics:

  • How classes are documented
  • Writing a class definition
  • Instance variables
  • Using instance variables in a method
  • Constructors
  • Constructor parameter lists

QUESTION 1:

What three values are you likely to use in calculating miles per gallon for a car?