Chapter1-Review

Download Report

Transcript Chapter1-Review

Chapter 1
Java Programming Review
Introduction
• Java is platform-independent, meaning that you
can write a program once and run it anywhere.
• Java programs can be embedded in HTML
pages and downloaded by Web browsers to
bring live animation and interaction to Web
clients.
• Java source files end with the .java extension.
Every class is compiled into a separate file
bytecode that has the same name as the class
and ends with the .class extension.
Introduction
• To compile a Java source code file, use the
javac command. To run a Java class, use the
java command.
• Every Java program is a set of class definitions.
The keyword class introduces a class definition.
The contents of the class are included in a block.
A block begins with an opening brace “{“ and
ends with the closing “}”. Methods are contained
in a class.
• A Java application must have a main method.
The main method is the entry point where the
application program starts when it is executed.
Problem Solving
• Programming is a process of problem solving
• Problem-solving technique includes analyse the
problem, outline the problem requirements, and
design the steps (called algorithm).
• Three steps problem-solving process in the
programming environment:
– Analyse, outline the problem and its solution
requirements. Design the algorithm to solve the
problem.
– Implement the algorithm uses any programming
language and verify it.
– Maintain or modify the program.
Primitive Data Types and
Operations
• Four integer types (byte, short, int, long) that
represent integers of four different sizes.
• Two floating-point types (float, double) that
represent floating-point numbers of two different
precisions.
• Character type (char) represents a single
character
• All these called primitive data types. When they
are declared, the variables of these types are
created and assigned memory space.
Primitive Data Types and
Operations
• Numeric operations: + (addition), (substraction), * (multiplication), / (division), %
(remainder).
• Increment operator (++), decrement operator (-).
• Casting – convert a value of one type into
another type. Two types: widening and
narrowing type.
• Three types of programming errors: syntax
errors, runtime errors and logic errors.
Selection Statements
•
•
•
•
Boolean types – represents a true or false value.
Boolean operators - &&, &, ||, |, !, and ^.
Relational operators - <, <=, ==, !=, > , >=.
Selection statements are used for building selection
steps into programs. Types: if, if…else, nested if,
switch and conditional expressions.
• if statement – control decisions based on Boolean
expression.
• switch statement – control decisions based on a switch
expression. Keyword break is optional.
• Conditional expression –
booleanExpression ? Expression1 : expression2;
Loop Statements
• while – checks the loop-continuation-condition
first and then execute the loop body.
• do-while – execute the loop body first and then
check the loop-continuation-condition.
• for – execute a loop body a predictable number
of times. Has three parts: initialise a control
variable, loop-continuation-condition, adjust the
control variable.
• Keyword break – immediately ends the
innermost loop and continue – only ends the
current iterations.
Methods
• Method header specifies the modifiers, return
value type, method name and parameters of the
method.
• Example:
public static int max (int num1, int num2) {
…}
• A method may return a value or not (void).
• The arguments that are passed to a method
should have the same number, type, and order
as the parameters in the method definition.
Methods
• When a program calls a method, program
control is transferred to the called method. A
called method returns control to the caller when
its return statement is executed or when its
method-ending closing brace is reached.
• A method can be overloaded – two methods can
have the same name as long as their method
parameter lists differ.
• The scope of a local variable is limited locally to
a method.
Arrays
• Syntax: dataType[ ] arrayRefVar or dataType
arrayRefVar[ ]
• The declaration of an array variable does not
allocate any space in memory for the array. An
array variable contains a reference to an array.
myList
0
1
2
3
4
5
6
• Elements can only be assigned to an array if it
has already been created using new operator:
new dataType[arraysize]
Arrays
• Each element in the array is represented using
the syntax arrayRefVar[index]. An index must be
an integer.
• After an array is created, its size becomes
permanent and can be obtained using
arrayRefVar.length
• Array initialiser – combines declaring an array,
creating an array, and initialising in one
statement:
dataType[ ] arrayRefVar = {value0, value1,
…, valuek);
Arrays
• Passing an array argument to a method is
actually passing the reference of the array.
• Two-dimensional array:
dataType[ ][ ] arrayRefVar or
dataType arrayRefVar[ ][ ]