Lecture Notes (CSIT111 – Programming Fundamentals)
Lecture #1 (2/3/22) → Subject Overview:
How Computers Work:
• Hardware → System Software → Application Software → User
How Computer Programs Work
• Input → Central Processing Unit (CPU) → Output
- Memory allows CPU to function/work
• Computers only understand binary code
Programming Languages:
• C → Java → Python
- These languages convert to machine code (binary) for computer
• Compiled:
- To machine code: Assembly, C, C++
- To bytecode: Java
• Interpreted/Scripting (without compilation):
- Python, JavaScript, PHP, Perl, shell
Java:
• General-purpose language suitable for a wide range of applications
- Supports the most advanced software development concepts
• Basic structural unit of a Java program is a ‘class’
- Instances of classes placed in memory become objects
• Uses ‘Java Class Library’ to construct a ‘Java Application’
- Class Loader → Bytecode Verifier → Virtual Memory → Execution Engine
• JDK → write + compile program
• JRE → execute + run program (a subset of JDK)
Java Programming Process:
1. Write a program:
- Use any text editor to write it (such as Notepad or Textmate)
- Save text into a .java file (file name must be exact same as the class name)
2. Compile a program:
- Open the Apple Terminal
- Navigate to the directory where the java program is kept
- Type the command to compile: javac [Link]
- If no bug → you will find [Link] file in the same directory
- If bug → the complier will report them (need to fix them + recompile)
3. Execute a program:
- Run the program by typing: java filename
- The program outputs will appear immediately
• [Link](“…”) → displays text on new lines
• [Link](“…”) → displays text on same line
Lecture #2 (9/5/22) → Object Oriented Programming:
Imperative Programming Concept:
• When a program is executed, it is a sequence of invisible electrical signals which
represent binary microprocessor instructions
- We cannot reproduce original program code from binary instructions
• But, we can reuse modules/parts with well-defined interface components
- To increase productivity (rather than starting from scratch)
Object Oriented Design (OOD):
• ‘Object Oriented Design’ methodology introduces the concept of objects
- These become major software building blocks
• Involves their properties, behaviours and interactions (external) in program
functionality, rather than commands and microprocessor (internal)
- Message → Object → Result
• Functional View:
- Data processing methods → data to be processed
• OOD View (split system’s data/behaviour):
- Object 1 → Main Object → Object 2 → Object 3 → Object 4
Evolution of OOD:
• Simula (1967) → basis around debugging and support for hardware platforms
• Smalltalk (1980) → debugger, code editor, class library
• C++ (1983) → very efficient
• Java (1995) → focus on the internet and independent applications
• C# (2000) → originated from Java
What Can an Object Do?
• Data Provider → collect, store + provide data
• Service Provider → provide services + computing
• Constructor → generate other objects + maintain relationship between them
• Coordinator → analyse events + delegate tasks to others
• Controller → make decisions
Internal Structure of Objects:
• Each object should contain data and methods (to process data)
- To interact with other objects → they include interface methods
• Physically: objects consist of binary data and microprocessor instructions which
occupy a reserved block of memory (actual layout is system dependent)
• Logically: objects are declared in your program
Class vs Object:
• Class → an abstraction defined by a programmer (NOT in computer memory)
- Does not contain actual values (logical entity) → inexecutable
• Object → an instance of a class generated (IS placed in computer memory)
- All properties must have values (physical entity) → executable
Classes:
• Can be interpreted as blueprints/prototypes for objects
- Cannot use classes directly (can only use objects based on their classes)
• For Example: a class ‘Polygon’ can be used to create shapes as objects
- Properties → used to generate different objects (e.g. sides, colour)
- Behaviour → things which require action (e.g. how area is calculated)
• Conceptual Class (start):
- From your mind (can only be understood by yourself)
- A rough description of items/concepts
• Readable Class (through):
- A formal description of classes (data structure, functions, relationships)
- Can be understood by anyone with background knowledge
- UML Class Diagram
• Executable Class (target):
- Programs
- Can be used by everyone
• ‘Attributes → describe the data contained within an object of a class (usually –)
- Name
- Type
• ‘Methods’ → ways an object processes data + interacts with others (usually +)
- Prefix + name
- Formal parameters
- Return value type
Object Intrinsic Features:
1. Identity
- Each object has a unique identifier that is used to recognise it
o Specified by programmer or assigned by a constructor
- Allows for correct interaction between objects
2. State
- A set of actual values stored in an object (e.g. for properties, attributes)
o Attributes that cannot be changed → ‘constants’
3. Behaviour
- A set of supported actions/reactions according to roles (what object does)
o In OOD, these are called ‘methods’
- Some methods can change object state → ‘mutators’
- Some methods can only provide value of an attribute → ‘accessors’
Fundamental OOD Concepts:
• Encapsulation:
- Where critical object attributes/methods are protected inside the object to
prevent unauthorised access to them to make sure that:
o Objects cannot be alternated/corrupted
o Hidden components can be modified without affecting object
- Public → can use for interaction with other objects (allow attributes to do)
- Private → hidden inside objects and cannot be accessed from outside
o Methods + attributes which are irrelevant to object interaction should
be specified as private to prevent damage/changes
Unified Modelling Language (UML):
• A consolidation of research on object-oriented modelling, used to:
- Make sketches to communicate key points of program architecture
- Provide a formal/detailed specification of a software system
- Generate code directly from UML diagrams if a special tool is used
• Class Diagrams:
- Contain classes and the relations between them
- Specify class attributes to describe any state of the object from a class
- Specify methods to implement a required object behaviour
- Specify object state
• Class Symbol:
- The most basic is a box with the class name (used for non-important ones)
o More can be added on later (e.g. attributes, methods)
• Object Symbol:
- Contains object name, class name, and specific values for each attribute
instantiation
Relationships Between Classes:
• Association: indicates that one class has/uses another class (“has” or “uses”)
• Aggregation: one class has references to objects of other class (“has-a-relationship”)
• Composition: components live inside the container (“contains” or “owns”)
- Deletion of the container destroys the component objects
• Multiplicity: used when describing the extent/level of association
- Exact Number → 1 or 2,4,6,8 (simply write it)
- Range of Numbers → 1..10
- Unspecified → * or ‘n’
• NOTE: static → common attribute across different things (can be changed)
• NOTE: constant → an attribute that cannot be changed (does not have method)
Lecture #3 (16/3/22) → Anatomy of a Java Program:
Program #1 → Basics of Java:
• CLASS:
1. Use the class keyword (spelt with all lowercase letters)
2. Specify its name (identifier)
- Can only include letters, digits, _ and $ (no spaces!!)
- Each word begins with a capital letter
- Should be meaningful
3. Provide a pair of curly braces { } to specify the class body
• METHOD:
- static <method> main (…) is a starting point on every Java program
o Where the program execution starts
- Keywords which must be used include:
o public → an access specifier that indicates that main (…) is visible
o static → indicates that this method is placed in shared memory and
becomes available right after the program starts
o void → indicates that this method does not return any information
o String[] args → takes the command line arguments to the program
• DISPLAY A MESSAGE:
- Instructs the JVM to display a message included in double quotes “ ”
o An ‘executable statement’ → (must be terminated with a semicolon ;)
- Executes a pre-defined Java method print (…) that belongs to the object out
o The object out → is a static object that belongs to the class System
▪ The System is one of the Java API classes
▪ The dot (.) means “has”
- So, system has out that has print (…)
• COMMENTS:
- It is common to insert comments to improve the readability of programs
o /* … */ → can span several lines
o // … → one-line comments
Program #2 → Add Two Numbers:
• println (…) → displays the result and moves to next line
- Another method that belongs to the object out
Program #3 → Calculate the Average:
• Can only be achieved through introducing ‘variables’
- As the previous example only instantly
displays data but does not store it