import java.util.*;
// e.g. to use methods from util library in myProgram
import javax.swing.*;
// e.g. to use methods from swing GUI toolkit in myProgram
4
Fundamental Programming Concepts
Program design involves
data design + algorithm design
Data design
Deciding how all the relevant information will be
represented in the program
i.e. what variables and or objects your program will need
Algorithm design
Creating a sequence of instructions to manipulate the
data in order to fulfil requirements
Primitive Variables
Java has 8 basic or primitive variable types:
byte
short numerical data types
int storing integer values
long
6
Variable Declaration and Assignment
Variables must be declared before they are used
Either declared separately then assigned values
int num1 , num2 ; // declares 2 int variables
num1 = 5; num2 = num1 + 10;
SELECTION
selectively execute instructions based on a decision
In Java decisions can be achieved by
if or if else or switch statements
REPETITION
repeat or loop through a sequence of instructions multiple times
In Java repetition can be achieved by a
for loop or do loop or do while loop
if statements
if (condition) {
/* code here is executed if
* condition is true */
}
10
if else statements
if (condition) {
/* code here is executed if
* condition is true */
}
else {
/* code here is executed if
* condition is false */
}
11
12
switch statement
switch (value) /* here value is a char, but it could
be any integer type, or a String */
{
case 'a':
// do this if value is 'a'
break;
case 'b':
case 'c':
// do this if value is 'b' or 'c'
break;
default:
// do this if value does not match any case
}
for statement
for (int i = 1; i <= n; i++){
/* loops through instructions here
* n times as i counts from 1 to n */
}
14
while statement
while (condition) {
/* executes statements here
* while condition remains true */
}
condition statements
true
false
do-while statement
do {
/* executes statements here
* and continues to execute them
* while condition remains true */
} while (condition);
17
19
Arrays
/* Arrays store multiple values of the same type under
* a single name. Individual values in an array are
* accessed using their index */