Professional Documents
Culture Documents
Visual Programming Pt1
Visual Programming Pt1
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
Hello Java!
3 4
Chap 1 Java Elements Chap 1 Java Elements
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
+,-,/,%,…
Comment Operators
Reserved
words / int , class , static , …
Tokens separators () , {} , ; , …
7 8
Chap 1 Java Elements Chap 1 Java Elements
9 10
11 12
Chap 1 Java Elements Chap 1 Java Elements
13 14
15 16
Chap 1 Java Elements Chap 1 Java Elements
17 18
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
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.
25 26
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) {
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
29 30
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
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
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: }
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 }
41 42
43 44