Computer Science Example
Use of a separate class to read and format data.

import java.io.*;
import java.text.*;

class BodyMassIndex
{
   public static void main (String [] args)
   {
      Person person = new Person ();
      person.getData ();
      person.displayData ();
   } // method main
} // class BodyMassIndex

// Class that displays a person's body mass index.
class Person
{
    private String name;
    private double weight, height, heightFeet, heightInches;
 
    public void getData ()
    {
         System.out.print ("Name: ");
         name = Reader.readString ();
         System.out.print ("Weight: ");
         weight = Reader.readDouble ();
         System.out.print ("Height in Feet: ");
         heightFeet = Reader.readDouble ();
         System.out.print ("Height in Inches: ");
         heightInches = Reader.readDouble ();
         height = 12 * heightFeet + heightInches;
    } // method getData
 
     // Compute the body mass index and display it on the screen.
     public void displayData ()
    {
          double bmi = calculateBMI();
          System.out.println (name + "'s body mass index is " + Reader.decimals (bmi));
     } // method displayData

     // Calculate the body mass index using English units.
     private double calculateBMI ()
     {
          double bmi;
          bmi = (705 * weight) / (height * height);
          return bmi;
     } // calculateBMI
} // class Person
 
class Reader
{
 // Reads a double from the keyboard and returns it to the calling method.
 public static double readDouble ()
 {
      double number;
      BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
      try
      {
           number = Double.parseDouble (stdin.readLine ());
      } catch (IOException e) { number = 0;}
      return number;
     } // readDouble
 
     // Formats a double for string output with two decimal places.
     public static String decimals (double number)
     {
          DecimalFormat decFor = new DecimalFormat ();
          decFor.setMaximumFractionDigits (2);
          decFor.setMinimumFractionDigits (2);
          return decFor.format (number);
     } // decimals

     // Reads a String from the keyboard and returns it to the calling method.
     public static String readString ()
     {
          String string;
          BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
          try
          {
               string = stdin.readLine ();
          } catch (IOException e) { string = "";}
          return string;
     } // readString

     // Reads an integer from the keyboard and returns it to the calling method.
     public static int readInteger ()
     {
          int number;
          BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
          try
          {
               number = Integer.parseInt (stdin.readLine ());
          } catch (IOException e) { number = 0;}
          return number;
     } // readInteger
} // class Reader