Class Definition

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 previous page   go to home page   go to next page

Answer:

Of course!

Class Definition

Here is the miles per gallon program, now with a skeleton for the Car class. To keep the program short, user interaction has been left out.

class Car
{
  // instance variables


  // constructor


  // methods

}

class MilesPerGallon
{
  public static void main( String[] args ) 
  {
    Car car = new Car( 32456, 32810, 10.6 );
    System.out.println( "Miles per gallon is " + car.calculateMPG() );
  }
}

The source file must be named MilesPerGallon.java after the name of the class that contains main().

QUESTION 5:

Decide what variables should go in the data section. Look back to the Car class for the class to see what you need.