Example Program

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:

Yes. No need to memorize that list of methods.

Example Program

Here is an example program that prints a table of powers of two.

import java.io.*;
class PowerTable
{
  public static void main ( String[] args ) 
  {
    // Get filename and create the file
    FileWriter writer = null;
    Scanner scan = new Scanner( System.in );
    String fileName = "";

    System.out.print("Enter Filename-->");  
    try
    {
      fileName = scan.next();
      
      // create the PrintWriter and enable automatic flushing
      out = new PrintWriter( new BufferedWriter( new FileWriter( fileName )), true );
    }
    catch ( IOException iox )
    {
      System.out.println("Error in creating file");
      return;
    }
      
    // Write out the table.
    int value = 1;
    out.println( "Power/tValue"  );
    for ( int pow=0; pow<=20; pow++ )
    {
      out.print  ( pow   );  
      out.print  ( '/t'  );  
      out.println(  value  );  
      value = value*2;
    }
    out.close();
   
  }
}

The character '/t' is the tab character.

QUESTION 17:

Instead of using three statements in a row, would the following have worked as well?

out.println( pow + "/t" + value  );