Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Acsse Ifm01b1 2024 L04

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

IFM01B1 : Informatics 1B

Lecture 04: Classes Expanded


Announcements
Announcements
• Exam entrance requirement is that your PCM (Practical Component Mark) is above 50%
• The PCM is the combination of weekly practical assignments and Semester Assessment 2 (the practical test) see
calculation formula in learning.

• If you do not submit a complete digital design (all eight sections present), your weekly practical assignment will
not be marked.

• There are no additional attempts on practical assignments as the solution is discussed during the Saturday
tutorial. You have one week to complete and submit each assignment it is your responsibility to ensure it is
done in time. Don't wait for the last minute to submit.

• You have 7 days (1 weeks) from the practical due date to query your practical mark. Each practical will be
marked within 7 days of the due date. After 1 weeks your practical mark is considered finalized.
Practical Feedback Guidelines
• Your assistant will provide you with feedback during your session only under the following conditions:
1. You have submitted a practical to be marked.
2. You are present during your session to receive feedback (max 8 min/student).
3. Your assistant is in the session to provide feedback.

• If you did not submit a practical, then your assistant can't provide you with feedback.

• If your assistant is not present in the session your practical will be marked before the next practical session.
You can see Mr van der Westhuizen or Mr. Orfao regarding feedback if you want to know where you lost
marks.
Did you
know?
...
Mother of computing

Ada Lovelace (1815-1852)


• English mathematician who has been called "the
first computer programmer" for creating an
algorithm for a computing machine in the mid 1800s
• Her contributions to the field of computer science
were not discovered until the 1950s.
Recap on Classes and Objects
Declaring a Class & Instantiating an Object of a Class

• Classes are declared as follows:

• The keyword Public indicates that the Vehicle class is accessible by anyone
• Instantiation - creating an object
• Object must be declared of the class' type – when you are ready to use the object, use the New keyword

8
Constructors
What is a constructor?
It is a special kinds of public method that used to initialize the object .

A constructor is like a method in that it contain executable code and may be defined with parameter.
this is first method that is run when an instance of type is created

• What is the purpose of a constructor?


• Is to build or create an instance of the object

https://www.dotnetheaven.com/article/constructor-in-vb.net 10
Types of constructors:
• Constructor

• Parameters-less constructor

•Also known as constructor overloading


•It is possible when the methods
• Copy constructor have DIFFERENT parameter patterns

The UML methods for this would be the following:


• Default constructor
<<Constructor>>
+ Vehicles (Tyres : Integer)
+ Vehicles ()
+ Vehicles (objVechiles : Vehicle)

11
Property Methods
Property Methods

• Variables that are Public can be read or written by any property or method
• Most instance variables are declared as Private
• Variables and methods that are Private are only accessible to other elements in the class that they are
declared
• The use of Private will ensure that information stored in the instance variables are hidden (encapsulation
and abstraction):
• Control over how data is set or returned (data validation)
• Classes more robust (input error won't halt program)

• How to access the information then? (communication between objects impossible at this point)
• Answer: Indirectly through the use of property methods

13
Declaring Property Methods:
• Format of declaration (in the following order):
1. Access modifier
2. Keyword Property
3. The name of property methods with brackets
4. Type

• Visual Studio will automatically generate two accessors, namely: Get and Set which you must
complete (to link up the property with the instance variable)
• The Get accessor returns a value from the object of the class
• The Set accessor assigns a value to the object of the class

14
Readonly property method
If you want the instance variable to be "seen" or accessible to be viewed but not changed you can
set the property method to be read only.

The UML methods for this would be the following:


<<Property>>
+ VINNunmber(): String {readonly}

15
Writeonly property methods
If you want the instance variable that can be "changed" set but never accessible to be viewed.
Basic you can change the value but you can not see the value it is set to write only

The UML methods for this would be the following:


<<Property>>
+ RemoteCode(): String {writeonly}

16
Working with arrays and property methods
When it comes to instance variables the
are arrays the property methods can be
done in two approaches:
1. Give access to only one variable in the
array a time
2. Give access to the whole array in one
go

The approach taken will depend on the


level of access the developer chose to
provide

The UML for the following property


methods will look like this:
<<Property>>
+Sensor(index : Integer) : Double
+Sensor() : Double[0..*]

17
Validation inside the property methods:
Besides the restricting the type of access granted to an instance varaible the property methods can
also include validation to ensure that the instance variables are set to the correct value.

This is only one example of how validation can be done, other approaches will be shown in the
module.

Why would a developer want to valid the values instance variables are set to ???

18
When to use property methods and when to use instance variables in the
class itself?

• When coding inside the class when would you call the instance variable directly or when would you use the
property method?

19
Methods in class
Sub routines and functions:
Within class you will also have methods this can be either:
• Sub routines
• Functions

The one that is created will depend on the purpose of the methods, remember the methods are the
actions of the class.

The UML methods for this would be the following:


<<Methods>>
+ FillTank(value : Double): Double
+ Drive () : void
Working with UML Class Diagrams
What is UML ?

• The Unified Modelling Language (or UML) is a modelling specification maintained by the Object Management Group (or
OMG) for the field of software engineering.

• UML’s aim is to provide a standardised means of visualising the modelling and design of a software based system.

• For more information on UML that you will be exposed during the course of your studies for your degree you can see
the guide designed by Mr Blauw at the https://uml.blauw.me/
Class Diagrams
• This is literally the building blocks of any object oriented programming-based application.
• The design of your classes are vitally important as they will be used in later years to link to
other UML concepts:
• Use cases
• Interaction Sequence Diagrams
• Classes will also be used as a type of blueprint for your database design (Entity Relationship Diagrams).

• Class Diagrams show the classes that you will be implementing in your system.
• Within classes there are the following
• attributes
• operations
• Finally you will also model the relationship (associations) between classes.
https://uml.blauw.me/
Class diagram

• Basic class diagrams are a simple rectangle with 3


Class Name
sections:
1. The name of the class
+ attribute1 : type
2. The attributes of the class – Variables with datatypes
- attribute2 : type
3. The operations of the class – Methods with datatypes
# attribute3 : type
+ Operation1() : type
- Operation2() : type
- Attribute : Datatype
- Operation : Datatype

• Visibility
• Datatypes
• All operations (methods) and attributes (variables) have
• All operation and attributes must have a datatype
to show their level of visibility or access. Basically what
associated with them, these could be:
level of access they are associated with:
• Integer
• Public (+)
• String
• Private (-)
• Double
• Protected (#) ( will be expanded on in a later point)
• Long
• Customed made ones ( will be explained in later lecture)

• In the case of an operation(method) if it does not return a


datatype the keyword “void” is used.

• What methods would you use void for??


If an attribute is an array: If an operation has parameters:
In the case where you have an attribute that is an If your operation has parameters you need to indicate
array you will need to show the number of elements them all along with their datatypes
that can be stored.
Consider the following sub routine
This is done by adding square brackets to the attribute Private sub Place_Results( r as Integer, c as Integer
indicating min and max. t as String)
[min … max] …..
For example: End sub
- Marks[1…10]:Integer It would be represented as follows in the UML
- Assignments [0…*] : Double
* means the max can be any size - Place_Results (r : Integer, c:Integer, t :String) : void
Operation grouping
• In the method area, you can group your operations into Class Name
categories.
• This is purely for aesthetic (looks) and has no impact on
+ attribute1 : type
the actual implementation of the classes. - attribute2 : type
• Each category is surrounded by double angle brackets # attribute3 : type
(“«category»”), and then looks like the example below. <<Catergory1>>
- Operation1() : type
• In Informatics 1B there will be we will be using the - Operation2() : type
following categories: <<Catergory2>>
• <<constructors>> - Operation3() : type
• <<property>>
- Operation4() : type
• <<methods>>

• The categories will be added to the UML as the section is


covered in the lectures.
Complete UML diagram of the Vehicle class
Vehicle
- _Tyres : Integer
- _Name : String
- _VINNumber: String
- _RemoteCode: Integer
- _Sensors[0…*]: Double
<<Constructor>>
+ Vehicles (Tyres : Integer)
+ Vehicles ()
+ Vehicles (objVechiles : Vehicle)
<<Property>>
+ Tyres():Integer
+ Name(): Integer
+ VINNumber():String {readonly}
+ RemoteCode():Integer {writeonly}
+ Sensor(index : Integer) : Double
+ Sensor() : Double[0..*]
<<Methods>>
+ FillTank(value : Double): Double
+ Drive () : void

You might also like