Anda di halaman 1dari 11

CMM001 Introduction to Object Oriented Programming

CMM023 Object Oriented Programming for Oil and Gas

Summary of Java Syntax and


Programming Elements

Dr David Lonie Room N432


1
d.p.lonie@rgu.ac.uk

Procedural programming in Java

Here we will summarise the basic concepts and syntax involved


in procedural programming in the Java language as described in
Lectures 1-5.

These concepts are:


storing data in variables and arrays,
creating statements,
decision structures,
repetition structures
These ideas are common to programming in any language, the Syntax will
vary between languages, but the concepts are universal.
2
Java Applications
A java application (i.e. a java program that runs) must, at a
minimum, consists of a single class containing a single method
called main()

public class myProgram{

public static void main (String[] args){


// code for program goes here
}

Calling Existing packages


If the program uses any methods from existing Java libraries or packages,
these can be imported at the start of the code

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

public class myProgram {


public static void main (String[] args) {
// code for program goes here
}
}

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

float numerical data types


double storing floating point values

char storing single text characters


boolean storing logical (true or false) values

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;

Or declared and assigned in one statement


int num3 = 26; // declares and assigns

// boolean variables are true or false


boolean answer = true, done = false ;

// doubles can store fractions and scientific notation


double pi = 3.14, y = 2.3E10;
7

Characters and Strings in Java


The char data type stores single characters
char letter = 'y', ch = '3' ;

We can also declare String objects


String str1 = "Robert Gordon University";

Note: assigning a value to a char variables uses single quotes


' '
assigning a value to a String object uses double quotes
" "
8
Fundamental Control Structures
Program logic consist of THREE fundamental control structures:
SEQUENCE
a single instruction or sequence of instructions

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 */
}

condition must be an boolean expression,


i.e. something which is, or evaluates, to true or false

10
if else statements
if (condition) {
/* code here is executed if
* condition is true */
}
else {
/* code here is executed if
* condition is false */
}

condition must be an boolean expression, i.e. something


which is, or evaluates, to true or false

11

nested if else statements


Multi-way decisions can be made by making if else decisions
within if else decisions e.g.
if (condition1)
{
// code here is executed if condition1 is true
}
else if (condition2)
{
// code here is executed if condition2 is true
}
else if (condition3)
{
// code here is executed if condition3 is true
}
else
{
// otherwise code here is executed
}

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
}

value can be an int, a char or a String [Java 7 and higher]


A switch statement can have as many cases as you want
13

for statement
for (int i = 1; i <= n; i++){
/* loops through instructions here
* n times as i counts from 1 to n */
}

for (int i = 1; i < n; i++){


/* loops through instructions here
* (n-1) times as i counts from 1 to n-1 */
}

14
while statement

while (condition) {
/* executes statements here
* while condition remains true */
}

condition statements
true

false

condition must be an boolean expression, i.e. something


which is, or evaluates, to true or false
15

do-while statement
do {
/* executes statements here
* and continues to execute them
* while condition remains true */
} while (condition);

condition must be an boolean expression, i.e. something


which is, or evaluates, to true or false
16
Output
/* output to the command window can be done with print,
println or printf methods */
System.out.print("prints this message");
System.out.println("new line and prints this message");

/* output in a popup window can be done with


showMessageDialog method */
JOptionPane.showMessageDialog(
null, " prints this message ");

To use JOptionPane must import javax.swing.* ;

17

Input using showInputDialog


// input from a popup window with showInputDialog method
String response = JOptionPane.showInputDialog(
"Show this message and get user input as string");

Note: to use JOptionPane must import javax.swing.* ;

// To convert the string to a character


char c = response.charAt(0);

// To convert the string to a integer


int n = Integer.parseInt(response);

// To convert the string to a Double


double x = Double.parseDouble(response);
18
Input using Scanner
// input from command window with scanner object
Scanner sc = new Scanner(System.in);
System.out.print("Tell user to enter something:");

int n = sc.nextInt(); // gets integer input


double x = sc.nextDouble(); // gets double input
String str = sc.next(); // gets string input
char ch = sc.next().charAt(0); // gets char input

Note: to use Scanner object must import java.util.*;

19

Arrays
/* Arrays store multiple values of the same type under
* a single name. Individual values in an array are
* accessed using their index */

int[] data1 = new int[10];


// creates an array of 10 int, with default values 0

String[] data2 = new String[]{"one", "two", "three"};


// creates an array of 3 Strings, with specified values

double[] data3 = new double[4];


data3[0] = 2.1; data3[1] = 0.2;
data3[2] = 5.6; data3[3] = -1.3;
/* creates an array of 4 doubles, and assigns values to
* individual elements */
20
Arrays
/* Arrays and loops are often used together */

// e.g. to print out values in an array:


for (int i=0; i<data.length; i++){
System.out.println("element " + i + " is " + data[i]);
}

// e.g. to input several values from single user input:


String input = JOptionPane.showInputDialog("Enter data:");
Scanner text = new Scanner(input);
int numData = 0; double[] data = new double[10] ;
// creates an array of 3 Strings, with specified values
while(text.hasNextDouble()){
data[numData++] = text.nextDouble();
}
21

CMM001 Java programs so far


The programs which we have written so far consist of
a single class
with no object data
a single(static) method, called main, used to contain local
variables and an algorithm
public static void main (String[] args)

Next step is to embrace Javas Object Oriented nature by


learning how to
use existing classes and libraries
create Classes, then
using Objects and Methods from those classes in applications
22

Anda mungkin juga menyukai