SIS2024Y Object-Oriented Programming: Introduction To Java Programming (Part 1)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

SIS2024Y

Object-Oriented Programming

Lecture 2
Introduction to Java programming
(Part 1)

1
Agenda
• Java Programming Style Guidelines
• Comments in Java
• Variables & Constants
• Data Types
– Primitive
– Non-primitive
• Arrays

2
Activity 1
1. Real-world objects contain ___ and ___.
2. A software object's state is stored in ___.
3. A software object's behavior is exposed through ___.
4. Hiding internal data from the outside world, and accessing
it only through publicly exposed methods is known as data
___.
5. ___is the representation of all the essential features of an
object, which means its possible states and behaviours.
6. When we use ___ , we have classes at the top of the
hierarchy and classes at the bottom of the hierarchy (or
those which get the characteristics from some other class).

3
Recap: A simple Java program

4
Java Programming Style Guidelines
1. Names representing types must be nouns and
written in mixed case starting with upper case.
• E.g. Line, AudioSystem, String

2. Variable names must be in mixed case starting


with lower case.
• E.g. line, audioSystem

3. Names representing constants (final variables)


must be all uppercase using underscore to
separate words.
• E.g. MAX_ITERATIONS, COLOR_RED

5
Java Programming Style Guidelines
4. Names representing methods/functions must
be verbs and written in mixed case starting
with lower case.
• E.g. getName(), computeTotalWidth()

5. Abbreviations in names should be avoided.


• E.g. computeAverage(); // NOT: compAvg();

6
Comments in Java
• Comments are preceded by a slash-slash (//)
and ends where the current source code line
ends.
//This is a C++ style comment in Java source code

• Multi-line comments begin with a slash-star


(/*) and ends with a star-slash (*/)
/* this is a multi-line comment in
Java. Comments are useful for describing the meaning of a
section of code
*/ 7
Java Variables & Constants
• Variable names are usually written in all
lowercase letters, with the first letter of each
word after the first word capitalized.
int countStudents = 0;

• Constant names are usually written in all


uppercase letters, with an underscore
separating each word in the name.
static final char TOP_GRADE = 'A';
8
Static Keyword
• The static keyword in java is used mainly for
memory management.

• Static makes memory usage more efficient (i.e. it


saves memory).

• Static can be applied to:


– Variables
– Methods
– Blocks

9
Static Variable Example

10
Static Memory Representation

https://www.javatpoint.com/images/staticvariable.JPG 11
Final Keyword
• The final keyword in java is used to restrict the
user. The java final keyword can be used in many
context:
– Variable
– Method
– Class

• If a variable is declared as final, we cannot change


its value.
– i.e. It will be a constant
12
Final Variable Example

13
Java Data Types
• Based on the data type of a variable, the
operating system allocates memory and
decides what can be stored in the reserved
memory.

• There are two Data Types available in Java:


– Primitive
– Non-Primitive

14
http://www.javatportal.com/image/corejavaintalling/datatype.jpg
15
Primitive Data Types

http://www.geeksforgeeks.org/data-types-in-java/
16
Primitive Data Types
Examples
Can also be written as:
1. boolean done = true; boolean done;
done = false;
2. byte a = 126;
3. char a = 'G';
4. short s = 56;
5. int i=89;
6. long number = 600851475143L
int number = 600851475143;
7. double d = 4.355453532;
8. float f = 4.7333434f;
17
Non-Primitive Data Types
• A variable of a non-primitive type doesn't
contain the value directly; instead, it is a
reference (similar to a pointer) to an object.

• Examples:
– String
– Object
– Array
– Etc…

18
String Data Type
• String Data Type:
String module = new String("Object Oriented Programming");

• Java also provides a more convenient way to initialize


String objects:
String module = "Object Oriented Programming";

• The + operator is defined to perform concatenation for


String objects, and += is defined as a concatenation
short-cut:
String module = "Object" + " Oriented";
module += " Programming";
19
String Data Type
• To find the length of a String:
– s4.length();

• To compare two String:


String s1 = "John";

s1.equals("John"); // CORRECT
s1 == s2; // INCORRECT

20
String Data Type
• A more general compareTo() method is also provided:
– if s1 > s2, it returns positive number
– if s1 < s2, it returns negative number
– if s1 == s2, it returns 0

String s1 = "John";
String s2 = "Jane";
int flag1 = s1.compareTo(s2);

String s3 = "Joan";
int flag3 = s1.compareTo(s3);

String s4 = "John";
int flag5 = s1.compareTo(s4);
21
String Data Type
• Substring:
String s1 = "listen";
String s2 = s1.substring(0, 4); // liste
String s3 = s1.substring(4, 5); // en
String s4 = s3 + s2; //enlist

• We can access individual characters within them:


char ch = s1.charAt(4); //e

• We can find their length:


int itsLength = s4.length(); //5 22
Activity 2
• Write the codes for a
Java program
(Calculator.java) that
initialises 2 numbers
and performs the
following operations:

1) Addition
2) Subtraction Output
3) Multiplication
4) Division

23
Arrays
• Array is a collection of similar types of data.

• It is fixed in size, i.e. we cannot increase the


size of array at run time.
6 th

24
Arrays
• In Java, unlike most other programming languages,
there are three steps to actually filling out an array,
rather one:
– Declare the array.
int [] myIntArray;

– Create space for the array and define the size. To do


this, use the keyword new, followed by the variable
type and size:
myIntArray = new int[500];

– Place data in the array. For arrays of native types, the


array values are all set to 0 initially.
myIntArray[4] = 467;
25
Arrays
• Indexing of arrays starts with 0. I.e. the first element of
an array is: myArray[0]

class ArraySample {

public static void main (String args[]) {

int [] arr = {10,20,30,40,50};

for (int i=0; i<arr.length; i++) {


System.out.println(“arr[“ + i + ”]=” +arr[i]);

}
}
} 26
Activity 3
• Modify the calculator example in Activity 2 such
that the calculation results are now stored in an
array variable. You may assume an appropriate
data type for the array.

• The contents of the array are then displayed on


console.

27
Activity 4
• Now, modify Activity 3 such that another array
variable is used to stored the result type (i.e.
addition, subtraction, multiplication, division).

28
References
• https://www.javatpoint.com/static-keyword-
in-java
• http://geosoft.no/development/javastyle.html
• http://www.javatportal.com/variable-and-
datatype-in-java
• http://www.sitesbay.com/java/java-array

29

You might also like