You are on page 1of 34

Fundamentals of Software

Development
CT010-3-1
Understanding Programming
Language Constructs

Prepared by: SZK First Prepared on:30 th July 2005 Last Modified on:14th December 2005
Quality checked by: GTK
Copyright 2005 Asia Pacific University College of Technology and Innovation

Topic & Structure of the lesson


Understanding Programming Language Constructs
Variable declarations, definitions, keywords,
constant primitive data types
User-defined data types
Syntax and professional programming practices
Planning and implementing program testing
Testing strategies
Program walkthroughs
Whitebox and blackbox testing
Debugging strategies
Syntax and logic errors
Program tracing
Debugging tools
CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 2 (of 34)

Learning Outcomes
At the end of this topic, you should be able to:
Define and differentiate the various data types in Java
Create , edit, compile and run simple procedural based
Java programs using variables and data types
Use operators in Java programs and subsequently
write expressions that make up a Java program
Plan and implement program testing strategies
Debug programs using appropriate debugging tools

CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 3 (of 34)

Key Terms
If you have mastered this topic, you should be able to use
the following terms correctly in your assignments and
exams:
identifiers
primitive data types
derived data types
program testing strategies
debugging

CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 4 (of 34)

Identifiers/Variables
Variables
int temperature; // The Fahrenheit
temperature
Think of variable like a container for a value :
32
temperature
temperature = 32;

// temperature contains the value 32

The above is an assignment statement and = is the


assignment operator.
CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 5 (of 34)

Identifiers/Variables
To declare > 1 variable :
int fahrTemp, centTemp;
int is the type name
Legal variable name must consists of a letter (upper- or
lowercase) followed by any number (including zero) of letters
& digits.
Illegal variable names : 4.7 !%-Legal variable names :
temperature TEMP23 T $temp_1 T$$1
CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 6 (of 34)

Identifiers/Variables
int Temp;
temp=3;
cause Java to give the error
Undefined variable; temp
To declare a constant value :
final double PI = 3.14159;

CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 7 (of 34)

Scope of Variables
A variable's scope is the block of code within which the
variable is accessible and determines when the variable is
created and destroyed. The location of the variable
declaration within your program establishes its scope and
places it into one of these 4 categories:
Member variable
Local variable
Method parameter
Exception-handler
parameter

CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 8 (of 34)

Keywords
Keywords ~ words that may seems to be legal variable
names but they are not because they are reserved by
the language for special uses.
List of Keywords in Java :
abstract
catch
do
finally
if
interface
outer
return
this
var

CT010-3-1 Introduction to Software Development

boolean
char
double
float
implements
long
package
short
throw
void

break
class
else
for
import
native
private
static
throws
volatile

byte
const
extends
future
inner
new
protected
sure
transient
while

Understanding Programming Language Constructs

case
continue
false
generic
instanceof
null
public
switch
true

cast
default
final
goto
int
operator
rest
synchronized
try

Slide 9 (of 34)

Data types

All variables must have a data type and must be


initialized.
Eg.
int count=3;
or
int count;
count=3;

CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 10 (of 34)

Data types
Java language is rich in its data types.
The variety of data types available allow the
programmer to select the type appropriate to
the needs of the application.
Two major categories of data type :
Primitive (Built in types)
Reference / Object (Derived Types)

CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 11 (of 34)

Data types
Data types in Java

Primitive

Numeric

Derived

Non-numeric

Classes

Arrays

Interface
Integer

Floating point

CT010-3-1 Introduction to Software Development

Character

Understanding Programming Language Constructs

Boolean
Slide 12 (of 34)

Data types primitive data types


Type

Size/Format

Description

byte

8-bit twos complement

Byte-length integer

short

16-bit twos complement

Short integer

int

32-bit twos complement

Integer

long

64-bit twos complement

Long integer

float

32-bit IEEE 754

Single-precision floating point

double

64-bit IEEE 754

Double-precision floating point

char

16-bit Unicode character

A single character

boolean

true or false

A boolean value

integers

Real numbers

others

CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 13 (of 34)

Data types primitive data types


Examples of double values in Java :
3.14159
7.12 9.0 0.5e001 -16.3e+002
The e is called the e-notation in Java;
e separates the number from the exponent.
2.829281 x 108

<=>

2.829281e8

2. 13898121 x 10-15

CT010-3-1 Introduction to Software Development

<=>

2. 13898121e-15

Understanding Programming Language Constructs

Slide 14 (of 34)

Quick Review Question


1. Why do programming languages need to have different
data types?
2. How are data types categorised in Java?
3. How are primary types categorised in Java?
4. How are derived types categorised in Java?
5. What is the size in bytes of the character type?
6. What is the size in bytes, of the double data type?
CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 15 (of 34)

Data types reference data types


Reference types :
Arrays
Classes
Interfaces
The value of a reference type variable, is a reference to the
actual value or set of values represented by the variable.

CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 16 (of 34)

Data types reference data types


Example class references
Classes are the templates for creating objects.
class A
{
variables
constructors
methods
}
A a = new A( )

CT010-3-1 Introduction to Software Development

a is a reference to an object of class A

Understanding Programming Language Constructs

Slide 17 (of 34)

Data types reference data types


Example Array references

int myArray [ ] = new int [4];

Example Interface references

CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 18 (of 34)

Common errors & debugging


Syntax error: a violation of the Java grammar
rules detected during program translation. Some
common causes of syntax errors:
Incorrect data types (in operations, assignments, returned
values, etc.)
Incorrect use of quotation marks (not in pairs)
Errors in use of comments (dangling comments)

Run-time error: an attempt to perform an invalid


operation detected during program execution:
No object file error: the name of a class does not match the
name of the file containing it (sometimes due to case
sensitivity)
Class path errors: the compiler doesnt find the source files
Data entry errors: user enters the wrong type of data
CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 19 (of 34)

Common errors & debugging


More Run-time errors:
Arithmetic overflow: attempt to store in a
variable a value that is too large and does
not fit in memory allocated for variable
Attempt to divide by zero

Logic errors: caused by a program that

follows an incorrect algorithm (no syntax or


run-time error, but the program output is
incorrect). Test the program thoroughly,
compare its output with the expected results.

CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 20 (of 34)

Some common syntax errors

CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 21 (of 34)

The Debugger
Debugger = program to run your program,
interrupt it, and inspect variables
Three key commands:
Set Breakpoint
Single Step
Inspect Variable

Two variations of Single Step


Step Over = don't enter method call
Step Inside = enter method call

CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 22 (of 34)

Debugging
IDE Debugger tool helps programmers debug their
programs:
control program execution: step through the code line-byline, or run to a given point
set breakpoints in the program
monitor data values with watches and inspectors, etc.

A simple technique for debugging is to insert


diagnostic output statements in methods for
displaying intermediate results.
For example, display messages in the console
window as follows:
System.out.println ("intermediate result: " +
value);

CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 23 (of 34)

Debugging a Program
Debugging is the major activity performed
by programmers during the testing phase
Testing determines if there is an error,
debugging determines the cause of it
Debugging is like detective work
Inspect carefully the information displayed by
your program
Insert additional diagnostic output statements
in the method to determine more information
CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 24 (of 34)

Using a Debugger
Debuggers often are included with IDEs
A debugger can execute your program
incrementally rather than all at once
Single-step execution executes in increments as
small as one program statement
Breakpoints are used to traverse large portions
of code before stopping
The actual mechanics of using a debugger
depend on the IDE that you are using
CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 25 (of 34)

Example: The Debugger Stopping


at a Breakpoint

CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 26 (of 34)

Program Trace
Output statements in your program for diagnostic purposes
eg. if (status == SINGLE)
{
System.out.println("status is SINGLE");
...
}
...
Stack trace tells you the contents of the call stack
eg. Throwable t = new Throwable();
t.printStackTrace(System.out);
Looks like exception report:
java.lang.Throwable
at TaxReturn.getTax(TaxReturn.java:26)
at TaxReturnTest.main(TaxReturnTest.java:30)
Drawback of trace messages: Need to remove them when testing is
complete, stick them back in when another error is found
CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 27 (of 34)

Why do Testing?
Normally testing is done by
The programmer
Other members of the software team who did not code the
module being tested
Final users of the software product

Do not rely on programmers for testing as they are often


blind to their own oversights
Companies also have quality assurance organizations
that verify that the testing process is performed correctly
In extreme programming, programmers work in pairs
where one writes the code and the other writes the tests

CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 28 (of 34)

Preparations for Testing


A test plan should be developed early in the
design phase
Aspects of a test plan include deciding how the
software will be tested, when the tests will occur,
who will do the testing, and what test data will be
used
If the test plan is developed early, testing can
take place concurrently with the design and
coding
A good programmer practices defensive
programming and includes code to detect
unexpected or invalid data
CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 29 (of 34)

Developing the Test Data


Test data should be specified during the analysis
and design phases for the different levels of
testing: unit, integration, and system
In black-box testing, we are concerned with the
relationship between the unit inputs and outputs
There should be test data to check for all expected
inputs as well as unanticipated data

In white-box testing, we are concerned with


exercising alternative paths through the code
Test data should ensure that all if statement
conditions will evaluate to both true and false
CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 30 (of 34)

Testing Coverage Question


What is Black-box testing?
What is White-box testing?
Why testing ->Reason:
Easy to overlook error branches during
testing
Make sure to execute each branch in at
least one test case

CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 31 (of 34)

Quick Review Question


We are going to work together to write some more
Java programs that we can test in the lab.
1. Write a Java program to display the lines:
This is the first line.
This is the second line.
2. Write a Java program to
Assign the value 45.35 to the float variable
price, 10 to the integer variable units and
calculate and display the total value of price *
units

CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 32 (of 34)

Summary of Main Teaching Points


Understanding Programming Language Constructs
Variable declarations, definitions, keywords,
constant primitive data types
User-defined data types
Syntax and professional programming practices
Planning and implementing program testing
Testing strategies
Program walkthroughs
Whitebox and blackbox testing
Debugging strategies
Syntax and logic errors
Program tracing
Debugging tools
CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 33 (of 34)

Next Lesson
Structured Programming Control Structures
Operators and expressions
Conditional constructs
if..else constructs
nested if..else constructs
switch..case
break and continue statements

CT010-3-1 Introduction to Software Development

Understanding Programming Language Constructs

Slide 34 (of 34)

You might also like