You are on page 1of 11

Chap 1 Fundamental Programming Structure

Chapter 1
Object-Oriented Programming
with JAVA Topics covered:
 Introduction to JAVA
 Primitive data types
Fundamental Programming  Class & Object
Structure  Inheritance
 Polymorphism

Chapter 1 – part 2
1 2

Chap 1 Basic structure of JAVA Chap 1 Basic structure of JAVA

Class structure: Class structure:


public class Hello public class Hello
{ {
// first java program // first java program
public static void main(String args[]) public static void main(String args[])
{ {
System.out.println("Hello Java!"); System.out.println("Hello Java!");
} // end of main method } // end of main method
} // end of class Hello } // end of class Hello

Hello Java!

3 4
Chap 1 Java Elements Chap 1 Java Elements

JAVA Tokens Comment

Purpose
Comment JAVA  Comments are beneficial for the programmer
Elements  help to understand the code
 Optional
Primitive  are useful to understand the operation of
the program
Arithmetic Data Types

5 6

Chap 1 Java Elements Chap 1 Java Elements

+,-,/,%,…
Comment Operators

Reserved
words / int , class , static , …

// single line comment


JAVA keywords

Tokens separators () , {} , ; , …

/* multiple line comment */ literals values assign to an integer,


boolean, characters

Identifiers names of variables, functions,


labels of a class and various
other user defined objects

7 8
Chap 1 Java Elements Chap 1 Java Elements

JAVA Tokens JAVA Tokens


JAVA Identifiers: JAVA Identifiers: Variable
 Consist only letters, digits, underscore characters A memory location which content may change during
and dollar sign $ program execution
 Must begin with letters, underscore or dollar sign Variable
 Case sensitive – uppercase & lower case letters
are different
 Cannot start with number  storing value into variables:
 Cannot be a reserve word 1. Assignment statement
 Identifiers : variable, constant, class or method 2. Initialization
3. Input statement

9 10

Chap 1 Java Elements


Chap 1 Java Elements
Example 1:
class AssignmentOperator {
public static void main(String[] args) { JAVA Tokens
int number1, number2; JAVA Identifiers: Named Constant
//initializing value to number1 A memory location which content is not allowed to
float number3 = 0; change during program execution
System.out.println(“value in number3 is:”+ number3);
Named
// Assigning 5 to number1 Constant static final
number1 = 5;
System.out.println(“value in number1 is:”+ number1);  static can be accessed before any objects of its class
// Assigning value of variable number2 to number1 are created, and without reference to any object
number2 = number1;
System.out.println(“value in number2 is:”+ number2);  final specifies that the value stored is fixed and cannot
} be changed
}

11 12
Chap 1 Java Elements Chap 1 Java Elements

Primitive Data Types Primitive Data Types


Integral data type:
1. Arithmetic types:
1. Integral Data Type Values Size
2. floating-point byte -128 to 127 1 byte
2. Boolean – data type that deal with logical short -32,768 to 32,767 2 bytes
values int -2,147,483,648 to 2,147,483,647 4 bytes
3. Char, the character data type (16-bit
-9,223,372,036,854,775,808
Unicode characters) long to 8 bytes
9,223,372,036,854,775,80

13 14

Chap 1 Java Elements Chap 1 Java Elements

Primitive Data Types Primitive Data Types


Floating-point data type: Boolean data type:

Data Type Values Size Data Type Values Size

float 7 decimal digits 4 bytes Boolean True, False 1 bit

double 16 decimal digits 8 bytes


 Logical values
 Boolean is the type required by the logical
expression and the conditional expressions

15 16
Chap 1 Java Elements Chap 1 Java Elements

Primitive Data Types Class String


char data type:
A sequence characters
Data Type Values Size String is a class
char Unicode character 2 bytes Allow to create objects for holding string
literals
String literal enclosed in double quote “ ”
 Use to stored characters
 Only hold single character at a time
 Characters literals are enclosed in single quote ‘’. Example 2: String name = “Robert”;
String title = “Ice Age” + 2;

17 18

Chap 1 Java Elements Chap 1 Java Elements

string and the operator + Arithmetic Operators

Java allow concatenation operation which  Arithmetic operators are used to perform
allows a string to be appended at the end of mathematical operations
another string 1. Binary operators
Operator + is used to join two strings, or - Operator that has only one operand
string with numeric value 2. Unary operators
- Operator that has two operands
3. Ternary operators
Example 3: “JAVA” + “Programming”; - Operator that has three operands
“Salary = RM” + “3000.00”;
“course mark” + marks;

19 20
Chap 1 Java Elements
Chap 1 Java Elements
Example 5:
Arithmetic Operators class ArithmeticOperator {
public static void main(String[] args) {
Binary Operators double number1 = 12.5, number2 = 3.5, result;
Operator Meaning // Using addition operator
result = number1 + number2;
Addition (also used for string System.out.println("number1 + number2 = " + result);
+
concatenation)
// Using multiplication operator
- Subtraction Operator result = number1 * number2;
System.out.println("number1 * number2 = " + result);
* Multiplication Operator
// Using division operator
/ Division Operator result = number1 / number2;
System.out.println("number1 / number2 = " + result);
% Remainder Operator
// Using remainder operator
result = number1 % number2;
// binary operator System.out.println("number1 % number2 = " + result);
Example 4: }
number1 + number2 = 16.0 }
number1 - number2 = 9.0

21 22

Chap 1 Java Elements


Chap 1 Java Elements

concatenate two or more strings


Example 6: Arithmetic Operators
class ArithmeticOperator {
public static void main(String[] args) {

String start, middle, end, result;


Unary Operators
Operator Meaning
start = "Talk is cheap. ";
middle = "Show me the code. "; Unary plus (not necessary to use since numbers are
end = "- Linus Torvalds"; +
positive without using it)
result = start + middle + end; - Unary minus; inverts the sign of an expression
System.out.println(result);
} ++ Increment operator; increments value by 1
}
-- decrement operator; decrements value by 1
Logical complement operator; inverts the value of a
!
Talk is cheap. Show me the code. – Linus Torvalds boolean

23 24
Chap 1 Java Elements Chap 1 Java Elements
Example 7: class UnaryOperator {
public static void main(String[] args) {
Example 8:
class UnaryOperator {
double number = 5.2, resultNumber;
public static void main(String[] args) {
boolean flag = false;
double number = 5.2;
System.out.println("+number = " + +number);
// number is equal to 5.2 here.
System.out.println(number++);
System.out.println(number);
System.out.println("-number = " + -number);
// number is equal to 5.2 here.
System.out.println(++number);
System.out.println(number);
// ++number is equivalent to number = number + 1
}
System.out.println("number = " + ++number);
}
// number is equal to 6.2 here.

// -- number is equivalent to number = number - 1


System.out.println("number = " + --number);
// number is equal to 5.2 here.

System.out.println("!flag = " + !flag);


// flag is still false.
}
}

25 26

Chap 1 Java Elements


Chap 1 Java Elements

Arithmetic Operators
Arithmetic Operators
Ternary Operators
Ternary Operators
Example 9:
 The conditional operator or ternary operator ?: is class ConditionalOperator {
shorthand for if-then-else statement public static void main(String[] args) {

Syntax: int februaryDays = 29;


String result;

variable = Expression ? expression1 : expression2 result = (februaryDays == 28) ? "Not a leap year" : "Leap year";
System.out.println(result);
}
} }
 If the Expression is true, expression1 is assigned to }
variable.
 If the Expression is false, expression2 is assigned to
variable.

27 28
Chap 1 Java Elements
Chap 1 Java Elements

Relational Operators Relational Operators


 relational operators determines the relationship between
Example 10:
two operands
 Depending on the relationship, it results to either true or class RelationalOperator {
false public static void main(String[] args) {
 Relational operators are used in decision making and loops
int number1 = 5, number2 = 6;

Operator Description Example if (number1 > number2)


{
== equal to 5 == 3 is evaluated to false System.out.println("number1 is greater than number2.");
}
!= not equal to 5 != 3 is evaluated to true else
{
> greater than 5 > 3 is evaluated to true System.out.println("number2 is greater than number1.");
}
< less than 5 < 3 is evaluated to false }
}
>= greater than or equal to 5 >= 5 is evaluated to true
<= less then or equal to 5 <= 5 is evaluated to true

29 30

Chap 1 Java Elements


Chap 1 Java Elements

logical Operators Example 11:


class LogicalOperator {
 The logical operators || (conditional-OR) and && public static void main(String[] args) {
(conditional-AND) operates on boolean expressions
int number1 = 1, number2 = 2, number3 = 9;
boolean result;

Operator Description Example // At least one expression needs to be true for result to be true
result = (number1 > number2) || (number3 > number1);
conditional-OR; true if // result will be true because (number1 > number2) is true
false || true is evaluated System.out.println(result);
|| either of the boolean
to true
expression is true // All expression must be true from result to be true
result = (number1 > number2) && (number3 > number1);
conditional-AND; true if all // result will be false because (number3 > number1) is false
&& boolean expressions System.out.println(result);
are true }
}

31 32
Chap 1 Java Elements
Chap 1 JAVA Class & Object

Example 12:
Defining a class
public class MyClass {
Syntax: int x = 5;
}
access_modifier class ClassName
{
// variables or fields
// methods
}  In JAVA, class should always start with an uppercase
first letter
 The name of the java file should match the class name

MyClass.java

33 34

Chap 1 JAVA Class & Object Chap 1 JAVA Class & Object

Defining a class Defining a class


Class members declaration: Class members declaration:
Class members: Syntax:
1. Attributes/fields Class members : attributes / fields
2. Methods/functions
access_modifier data_type variableName;

public class MyClass {


// variables or fields Class members : methods/functions
// methods Class members
} access_modifier return_type functionName(){}

35 36
Chap 1 JAVA Class & Object Chap 1 JAVA Class & Object

Defining a class
Defining a class
Answer for Example 13
Example 13
Define a class called Circle which contains radius as
a double type data and method called getArea() which public class Circle{
private double radius;
returns an area of a circle. The following formula is used
to calculate an area of a circle; public double getArea(){
area = 3.14 x radius2 return 3.14 * radius * radius;
}
}

37 38

Chap 1 JAVA Class & Object


Chap 1 JAVA Class & Object

Example 14:
Creating an object public class Circle{
private double radius;
In JAVA, object of a class is created by using new
keyword public double getArea(){
return 3.14 * radius * radius;
Syntax: }

public static void main(String args[]){


//Creating an object or instance
ClassName objectName = new ClassName(); Circle bulat = new Circle();
}
}

39 40
Chap 1 JAVA Class & Object
Chap 1 JAVA Class & Object
Example 15:
public class Circle{
private double radius;
Accessing class members
public double getArea(){
• In JAVA, Accessing class members (call methods and return 3.14 * radius * radius;
access instance variables) achieved by using . operator }

Syntax: public static void main(String args[]){


//Creating an object or instance
Circle bulat = new Circle();
.
objectName variableName;
//Accessing class members
.
objectName MethodNames(); System.out.println(bulat.radius);
System.out.println(bulat.getArea());
}
}

41 42

Chap 1 JAVA Class & Object


Chap 1 JAVA Class & Object
Example 16:
//Defining a Student class.
class Student{ Exercise
//defining variables or fields
int id;
String name;
Define a class called Student which contains
studentName and studentId data members and
//creating main method inside the Student class methods called setName() to store students name
public static void main(String args[]){
and setId() to store student matrix number. Create
//Creating an object or instance an object from this class and display the student
Student s1 = new Student(); name and student id on the computer screen.
//Printing values of the object
System.out.println(s1.id);
System.out.println(s1.name);
}
}

43 44

You might also like