Java Chapter 1 - Programming

1.1 Programming Styles

Procedural programming is a style of programming where instructions are executed one after another in sequence.  BASIC, COBOL, and FORTRAN are well know procedural programming languages.  Often, GOTO statements were used to move to other parts of the program.

Structured programming enhanced procedural programming by providing a logical top-down structure for programs.  The need for GOTO statements was eliminated.  Program flow was improved by FOR and WHILE loops.  Modules/Functions were created to contain common tasks and could be reused in other programs.  Languages that encouraged structured programming include Ada, Pascal, and C.  Below is an example C program containing a reusable function to convert Fahrenheit to Celsius.

Module/Function Example

#include <iostream.h>
float Convert(float);

main()
{
   float F,C;
   cout << "Enter temperature in Fahrenheit: ";
   cin >> F;

   C = Convert(F);

   cout << "It is " << C << " degrees Celsius.\n";
}


float Convert (float F)
{
   float C = (5.0/9.0) * (F - 32.0);
   return C;
}

Object-oriented programming (OOP) is a style of programming organized around objects rather than actions. Objects or classes are defined that may mimic real-world objects.  These objects have methods (functions) and data (attributes).  C++, Java, and C# are example languages that allow for OOP.

A simple example of an object is a pair of dice.  Below shows the data and methods for a pair of dice.

Pair of Dice Object

Data (Attributes) Methods (Functions)
The numbers being shown
e.g. 5 and 3
Roll the dice

Another object definition could be a checking account customer at a bank as shown below.

Bank Customer Object

Data (Attributes) Methods (Functions)
Personal Info
 - Name
 - Address
 - Phone #
 - E-mail address
 - Password / PIN
Account Info
 - Account Balance
 - Transactions
Deposit
 - Online transfer
 - Cash/check deposit
Withdraw
 - Online transfer
 - Use debit card
 - Write check

Below is an example OOP program.

OOP Example

DeckofCards Lucky;             // define object Lucky to be a deck of cards
Player Kirk;                   // define object Kirk to be a player

Lucky.Shuffle();               // shuffle the deck of cards
Lucky.Cut();                   // cut the deck or cards

Kirk.Cards = Lucky.Deal(5);    // deal 5 cards to Kirk
Kirk.ShowCards();              // display the cards
Kirk.PokerHand();              // display poker hand
 

Program Output

.-------. .-------. .-------. .-------. .-------.
|/-\ /-\| |   *   | |   _   | |/-\ /-\| |  / \  |
|\  -  /| | *   * | | _( )_ | |\  -  /| | /   \ |
| \   / | |*     *| |(_ . _)| | \   / | | \   / |
|  \_/  | |  *I*  | |   I   | |  \_/  | |  \ /  |
|  1 0  | |  1 0  | |  1 0  | |   K   | |   K   |
'-------' '-------' '-------' '-------' '-------'
FULL HOUSE

Inheritance refers to creating classes that inherit attributes and methods from other classes.  For example, a bank may have a class for a customer that includes all the personal information for the person.  If this customer opens a savings account, then they inherit the properties for the savings account class.  If they receive a loan then they inherit the properties for the loan class.

1.2 Programming Terms

A syntax error is when you have typing errors or use the programming language incorrectly.  The compiler will usually find syntax errors.

A run-time error occurs when a program compiles o.k., but crashes during execution.

A logic error occurs when a program compiles and runs o.k., but doesn't produce accurate results.

An identifier is a name of a variable, class, or method.

Whitespace consists of nonprinting characters - spaces, tabs, carriage returns.  Whitespace is ignored by most programming languages.

An SDK (Software Development Kit) provides a set of tools to create an application using a programming language.  A closely related term, IDE (Integrated Development Environment), is a software application that provides tools for a programmer such as a source code editor, compiler, forms designer, and debugger.

1.3 Java Programming

A computer's or CPU's native language is machine language.  Most programming languages require the source code to be compiled into machine language before they run.  Some languages such as BASIC and JavaScript are interpreted and not compiled.  Interpreted languages are usually less efficient than compiled languages.

Java source code is compiled halfway to Java bytecode.  This code is then executed by the Java Virtual Machine (JVM) installed on a computer.  The JVM can be installed on many different types of computers making the Java language platform-independent.

Java programs embedded in a web page are called applets.  Stand-alone Java programs are called applications.  Applications may be console (text-only) or windowed containing a graphical user interface.