brainfrz

NameReader

Jan 24th, 2016
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. /**********************************************************
  2.  * Program Name   : Program Template - Name Reader
  3.  * Author         : Terry Weiss
  4.  * Date           : January 22, 2016
  5.  * Course/Section : CSC264 - 001
  6.  * Program Description: This program will accept a first and last name and then
  7.  *     center the name in the middle of the screen.
  8.  *
  9.  * Methods:
  10.  * -------
  11.  * Main - Reads the names and displays them
  12.  **********************************************************/
  13.  
  14.  
  15. public class NameReader
  16. {
  17.    /**********************************************************
  18.     * Method Name    : NameReader - Main
  19.     * Author         : Terry Weiss
  20.     * Date           : January 22, 2016
  21.     * Course/Section : CSC264 - 001
  22.     * Program Description: This method will read a name entered by the user. It
  23.     *     then creates a new String with the name centered for console output,
  24.     *     and displays it
  25.     *
  26.     * BEGIN main
  27.     *    Prompt for first name
  28.     *    Prompt for last name
  29.     *    Clear screen
  30.     *    Calculate leading space to center title for 80 columns
  31.     *    Display centered title
  32.     *    Create String with name padded to be centered
  33.     *    Output name
  34.     * END Read Name
  35.     **********************************************************/
  36.      public static void main (String [] args)
  37.     {
  38.         //local constants
  39.         final int COLUMNS = 80; //Number of columns for console output
  40.  
  41.         //local variables
  42.         String firstName, lastName;    //User's first and last name
  43.         int padding;                   //Number of leading spaces to center name
  44.  
  45.         Library64 myLib = new Library64();
  46.  
  47.         /********************   Start main method  *****************/
  48.  
  49.         //Prompt for first name
  50.         System.out.print("Enter first name: ");
  51.         firstName = Keyboard.readString();
  52.        
  53.         //Prompt for last name
  54.         System.out.print("Enter last name:  ");
  55.         lastName = Keyboard.readString();
  56.        
  57.         //Clear screen
  58.         myLib.clrscr();
  59.        
  60.         //Calculate leading space to center title for 80 columns
  61.         //Length of full name is first name's length + 1 space + last name's length
  62.         padding = (COLUMNS - firstName.length() - 1 - lastName.length() ) / 2;
  63.        
  64.         //Display centered title
  65.         System.out.println("                                  Name Reader\n\n");
  66.        
  67.         //Display full name
  68.         System.out.println(Util.setLeft(padding, firstName + " " + lastName));
  69.     } //end main method
  70.  } //end Template264
Advertisement
Add Comment
Please, Sign In to add comment