Lecture1
Lecture1
—
Software Design & Java Basics
Further Programming
1
RMIT Classification: Trusted
Learning Outcomes
Describe the three broad phases of Software Development: Analysis, Design and
Implementation
Describe the basic structure of an OO system and how to initially identify classes, methods,
attributes and relationships
Explain the purpose of Encapsulation and Information Hiding
Describe Coupling and Cohesion and Explain the Coupling/Cohesion trade-off
Introduce Java basic components and syntax
22
RMIT Classification: Trusted
—
Software Design
3
RMIT Classification: Trusted
Object-Oriented Development
In general, development consists of three broad activities
• These can be further subdivided but conceptually activities generally fall into one of these
broader categories
• Analysis (OOA)
• Understanding and defining the problem
• Design (OOD)
• A high-level conceptual solution expressed using diagrams, pseudocode, etc.
• Programming (OOP)
• A detailed solution implemented in a programming language such as Java, C#, C++, etc.
• The Java Language Specification is the definitive reference for Java coding conventions Code
Conventions for the Java Programming Language
44
RMIT Classification: Trusted
55
RMIT Classification: Trusted
66
RMIT Classification: Trusted
77
RMIT Classification: Trusted
Discovering Attributes
To discover attributes, ask question such as: “I am an
account. What should I know? What do I need to remember?
What properties do I have? What states can I be in?”
• My account number?
• My account balance?
• My owner?
• My bank?
For each attribute, you need to decide on its name, data type,
visibility (public, protected, private or default (package), also
any modifiers i.e., should it be a constant (final) or variable,
and have instance or class scope (static).
88
RMIT Classification: Trusted
99
RMIT Classification: Trusted
Each class encapsulates its own data (attributes) and methods according to its own specific
semantics.
10
10
RMIT Classification: Trusted
11
11
RMIT Classification: Trusted
12
12
RMIT Classification: Trusted
13
13
RMIT Classification: Trusted
Coupling
Classes should be properly encapsulated. This refers to each
class holding all the data/functionality it requires to fulfil its
purpose, without being unduly dependent on others
The dependence of some classes on others in this context is
called coupling. This reliance should be minimised in your class
designs wherever possible
Excessive coupling between classes can create problems
when items are modified – with so many dependencies, changing
one will imply that many others may also need to be changed
Coupling can be objectively measured with automated code
analysis tools
14
14
RMIT Classification: Trusted
Cohesion
When a class is designed, each of its elements should have a logical
and well-defined role in the class being able to fulfil its purpose. This
tight relation of internal components is known as cohesion
A highly cohesive object will not have items that are only loosely
related to its role – the object will define one concept or perform one
task well
Cohesion is a good thing – it reflects a design that is very clear and
pure to its intended purpose
Cohesion is harder to measure automatically since it requires semantic
understanding
Coupling and Cohesion is a tradeoff – increasing cohesion (good) can
increase coupling (bad)
15
15
RMIT Classification: Trusted
Can range from low detail (classes and relationships only) to high detail
(e.g., full method signatures, visibility etc.)
Class diagrams are part of a modelling language (UML)
Class diagrams can be generated from code and skeleton code can be
generated from diagrams
18
18
RMIT Classification: Trusted
19
19
RMIT Classification: Trusted
20
20
RMIT Classification: Trusted
Javadoc Comments
21
21
RMIT Classification: Trusted
Javadoc Annotations
Tag & Parameter Usage Applies to
@author John Smith Describes an author. Class, Interface, Enum
Method description
Source: https://en.wikipedia.org/wiki/Javadoc
23
23
RMIT Classification: Trusted
Attribute description
Reference to others
24
24
RMIT Classification: Trusted
—
Introduction to Java
Further Programming
25
RMITRMIT
Classification: Trusted
Classification: Trusted
What is Java?
• High-level programming language invented in 1995 by
James Gosling and his colleagues at Sun Microsystem
(later acquired by Oracle)
• Java source files (.java) are compiled into Java bytecode
(.class) which can be executed by the Java Virtual Machine
(JVM)
• Works on anything that has a JVM implementation
(computers, supercomputers, smartphones, game
consoles, etc.)
3
26
26
RMITRMIT
Classification: Trusted
Classification: Trusted
Why Java?
• How does a typical program run?
27
27
RMITRMIT
Classification: Trusted
Classification: Trusted
Why Java?
• With Java, we can "write once, run anywhere"
Java compiler
Bytecode
Source files
files
(.java)
(.class)
28
28
RMITRMIT
Classification: Trusted
Classification: Trusted
Why Java?
• A bit slower than C/C++ but faster than other languages
• Many high-quality built-in and third-party libraries
• Object-oriented
• Multi-threaded
• and more …
29
29
RMITRMIT
Classification: Trusted
Classification: Trusted
30
30
RMITRMIT
Classification: Trusted
Classification: Trusted
31
31
RMITRMIT
Classification: Trusted
Classification: Trusted
32
32
RMITRMIT
Classification: Trusted
Classification: Trusted
10
33
33
RMITRMIT
Classification: Trusted
Classification: Trusted
11
34
34
RMITRMIT
Classification: Trusted
Classification: Trusted
Operators
Level Category Operators Associativity
1 Postfix (), [], ., ++, -- Left to right
2 Unary +, -, !, ~, ++, --, (type) Right to left
3 Multiplication *, /, % Left to right
4 Addition +, - Left to right
5 Comparison <, <=, >, >= Left to right
6 Equality ==, != Left to right
7 Assignment =, +=, -=, *=, /=, %= Right to left
8 Logical &&, ||, ! Left to right
12
35
35
RMITRMIT
Classification: Trusted
Classification: Trusted
36
36
RMITRMIT
Classification: Trusted
Classification: Trusted
Statements
• A statement specifies an action in a program
• There are 4 types of statements:
o Declaration: to declare variables and constants
int count;
o Assignment: to assign a value to a variable or constant
count = 0;
o Method invocation: to call a method
System.out.println("Hello World!");
o Control: to change the execution order of statements
15
37
37
RMITRMIT
Classification: Trusted
Classification: Trusted
Comments
• // This is a single-line comment
• /* This
is
a multi-line comment
*/
• /** This is a javadoc style comment
that can be gathered by java tools
to generate documentation
*/
13
38
38
RMITRMIT
Classification: Trusted
Classification: Trusted
Basic I/O
• Output to console
System.out.println("A string to be displayed");
System.out.print("Another string to be displayed");
16
39
39
RMITRMIT
Classification: Trusted
Classification: Trusted
Basic I/O
• Frequently used format specifiers
Format Specifier Output Example
%c a character 'a'
17
40
40
RMIT Classification:
RMIT Trusted
Classification: Trusted
Basic I/O
• Input from console
Scanner scan = new Scanner(System.in);
System.out.print("Please enter something: ");
String word = scan.next(); // scan one token (one word)
String line = scan.nextLine(); // scan one line
18
41
41
RMITRMIT
Classification: Trusted
Classification: Trusted
Basic I/O
Scanner method Description
String next(String pattern) Scans and returns the next token as a String if it matches the pattern
42
42
RMITRMIT
Classification: Trusted
Classification: Trusted
20
43
43
RMIT Classification: Trusted
RMIT Classification: Trusted
44
44
RMITRMIT
Classification: Trusted
Classification: Trusted
22
45
45
RMITRMIT
Classification: Trusted
Classification: Trusted
23
46
46
RMITRMIT
Classification: Trusted
Classification: Trusted
24
47
47
RMITRMIT
Classification: Trusted
Classification: Trusted
25
48
48
RMITRMIT
Classification: Trusted
Classification: Trusted
26
49
49
RMITRMIT
Classification: Trusted
Classification: Trusted
27
50
50
RMITRMIT
Classification: Trusted
Classification: Trusted
Strings
• A string is simply an array of characters
• Java provides the String class (object data type) and several methods to allow
working with strings conveniently
• Create strings
String greeting = new String("Hello");
String greeting = "Hello";
• String length
System.out.println(greeting.length());
• String comparison
System.out.println(greeting.equals("hello"));
3
51
51
RMITRMIT
Classification: Trusted
Classification: Trusted
Strings
• String concatenation
String firstName = "John"; String lastName = "Doe";
String fullName = firstName + " " + lastName;
System.out.println(fullName);
• Substring
String txt = "RMIT University Vietnam";
System.out.println(txt.substring(5, 8));
• Searching in a string
System.out.println(txt.indexOf("Viet"));
System.out.println(txt.indexOf("Hello"));
4
52
52
RMITRMIT
Classification: Trusted
Classification: Trusted
Strings
Method Description
int length() Returns the number of character in this string
boolean isEmpty() Returns true if, and only if, length() is 0
boolean equals(String str) Returns true if this string is equal to str
boolean equalsIgnoreCase(String str) Returns true if this string is equal to str, ignoring case differences
int compareTo(String str) Compares two strings lexicographically
int compareToIgnoreCase(String str) Compares two strings lexicographically, ignore case differences
char charAt(int index) Returns the character at the given index
int codePointAt(int index) Returns the Unicode code point of the character at the given index
String concat(String str) Returns a new string that concatenates this string with str
String toUpperCase() Returns a new string with all letters in uppercase
String toLowerCase() Returns a new string with all letters in lowercase
String substring(int beginIndex, int endIndex) Returns a new string that is a substring of this string
5
53
53
RMITRMIT
Classification: Trusted
Classification: Trusted
Strings
Method Description
boolean startsWith(String str) Returns true if this string starts with str
boolean endsWith(String str) Returns true if this string ends with str
boolean contains(String str) Returns true if this string contains str
int indexOf(String str) Returns the index of the first occurrence of str in this string ,
or -1 if there is no such occurrence
int lastIndexOf(String str) Returns the index of the last occurrence of str in this string ,
or -1 if there is no such occurrence
String replace(char oldChar, char newChar) Returns a new string resulting from replacing all occurrences
of oldChar in this string by newChar
String replaceAll(String regex, String replacement) Replaces each substring in this string that matches the
given regex with the given replacement
String[] split(String regex) Splits this string around matches of the given regex
boolean matches(String regex) Returns true if this string matches the given regex
6
54
54
RMITRMIT
Classification: Trusted
Classification: Trusted
Strings
• Conversion between strings and numbers
String intString = "123";
int intValue = Integer.parseInt(intString);
String doubleString = "3.14159";
double doubleValue = Double.parseDouble(doubleString);
55
55
RMITRMIT
Classification: Trusted
Classification: Trusted
Strings
• In Java, strings are immutable – they can’t be changed after created
• The following code creates a new string and assigns it back to the
original variable:
String greeting = "Hello";
greeting += " World";
• More details can be found in the String API
56
56
RMITRMIT
Classification: Trusted
Classification: Trusted
Regular Expressions
• A regular expression (regex) is a string that describes a pattern in a
sequence of characters
• Regex can be used for matching, replacing, and splitting strings
String s1 = "Java is fun";
String s2 = "Java is cool";
String regex = "Java.*";
System.out.println(s1.matches(regex));
System.out.println(s2.matches(regex));
57
57
RMITRMIT
Classification: Trusted
Classification: Trusted
Regular Expressions
Regular expression Matches
x a specified character x
. any single character
(ab|cd) ab or cd
[abc] a, b, or c
[^abc] any character except a, b, or c
[a-z] any character from a through z
[^a-z] any character except a through z
[a-e[m-p]] a through e or m through p
[a-e&&[c-p]] intersection of a-e with c-p
\d a digit, same as [0-9]
\D a non-digit
10
58
58
RMITRMIT
Classification: Trusted
Classification: Trusted
Regular Expression
Regular expression Matches
\w a word character, same as [a-zA-Z0-9_]
\W a non-word character, same as [^a-zA-Z0-9_]
\s a whitespace character
\S a non-whitespace character
p* zero or more occurrences of pattern p
p+ one or more occurrences of pattern p
p? zero or one occurrence of pattern p
p{n} exactly n occurrences of pattern p
p{n, } at least n occurrences of pattern p
p{n, m} between n and m occurrences of pattern p (inclusive)
^ beginning of a line
$ end of a line 11
59
59
RMITRMIT
Classification: Trusted
Classification: Trusted
Quiz
• What is the regular expression of an even integer?
Answer: (\d)*[02468]
12
60
60
RMITRMIT
Classification: Trusted
Classification: Trusted
Arrays
• An array is a fixed-size order collection of items of the same data type
• Create arrays
int[] nums; // declare an array
nums = new int[4]; // specify array length(size)
String[] cars = new String[4]; // combine in one statement
• Create and initialize arrays
int[] nums = {5, 10, 15, 20};
String[] cars = {"Honda", "Toyota", "Ford", "BMW"};
13
61
61
RMITRMIT
Classification: Trusted
Classification: Trusted
Arrays
• Access and update an item
String[] cars = {"Honda", "Toyota", "Ford", "BMW"};
System.out.println(cars[2]);
cars[2] = "Kia";
System.out.println(cars[2]);
• Loop through an array using a for statement and array indexes
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}
14
62
62
RMITRMIT
Classification: Trusted
Classification: Trusted
Arrays
• Loop through an array using a for-each statement
String[] cars = {"Honda", "Toyota", "Ford", "BMW"};
for (String car : cars) {
System.out.println(car);
}
15
63
63
RMITRMIT
Classification: Trusted
Classification: Trusted
Multidimensional Arrays
• Create a multidimensional array
int[][] chessCells = new int[8][8];
• Create and initialize a multidimensional array
int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
• Access multidimensional array items
for (int i = 0; i < myNumbers.length; i++) {
for (int j = 0; j < myNumbers[i].length; j++) {
System.out.print(myNumbers[i][j] + " ");
}
System.out.println();
}
16
64
64
RMIT Classification: Trusted
—
Thank you
COSC2440 teaching team
65