COMP1409 September2016 Lesson02vA

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

COMP1409:

Introduction to Software
Development I
Lesson 2
Learning Outcomes: Lesson 2
• Comments
• Visibility modifiers (“access modifiers”)
• Methods: return types and parameters
• if statements
• null
• Constructors
• Parameters
• Assignment statement =
• Logical operators
• Relational operators > >= < <= == !=
COMP1409 - Lesson 2 2
Comments
• Comments are ignored by the Java compiler.
• They are used as “notes” for developers.
• There are two categories of comments, and three “styles”.

COMP1409 - Lesson 2 3
JavaDoc Comments
• JavaDoc comments are marked /** with two asterisks */
• These notes get published in documents created by Java.
• JavaDoc-comment everything that is public.

COMP1409 - Lesson 2 4
JavaDoc Comments

COMP1409 - Lesson 2 5
JavaDoc Comments

COMP1409 - Lesson 2 6
Visibility modifiers
• Visibility modifiers are also called access modifiers.
• Instance variables, methods, and constructors can be declared as
public or private (there are also other modifiers, to be discussed
later).
• We will always make our instance variables private.
• We will make our classes and constructors public.
• We will make some methods public, others private.
• private means “accessible only from within this class”.
• public means “accessible from any class”.
COMP1409 - Lesson 2 7
Visibility modifiers

COMP1409 - Lesson 2 8
Methods: return types and parameters
• A method is an instruction, a function, a verb.
• Java methods can take input (parameters) and give output (return).
• An example could be:

COMP1409 - Lesson 2 9
Methods: return types and parameters
• Since this method is public it needs JavaDoc comments on top of it.

COMP1409 - Lesson 2 10
Methods: return types and parameters
• Methods should be named as verbs.
• If a particular method doesn’t need to return anything, its return type
is “void”.
• Example: public void returnNothing(){
}

• Notice that this method also didn’t need any inputs.


• Inputs are called parameters or arguments.

COMP1409 - Lesson 2 11
Methods: return types and parameters
• A method signature includes:
• modifier return-type name(parameters)
• Examples:
public boolean isStudentEnrolled(String studentID)
private void doSomething(double a, int b, char c)
public double getAverage(double num1, double num2, double num3)
private char doSomethingElse()

COMP1409 - Lesson 2 12
Methods: return types and parameters
public boolean isStudentEnrolled(String studentID)
private void doSomething(double a, int b, char c)
public double getAverage(double num1, double num2, double num3)
private char doSomethingElse()

• Call those methods later:


• isStudentEnrolled(“a00123456”)
• doSomething(5.5, 4, ‘c’)
• getAverage(1.0, 2.2, 3.4)
• doSomethingElse()

COMP1409 - Lesson 2 13
Methods: return types and parameters
• Note: a method must return data of the specified return type, or it
will not even compile.

COMP1409 - Lesson 2 14
Methods
• Much more on methods, coming next lesson.

COMP1409 - Lesson 2 15
if statements
• One way for a Java program to make decisions is to use “if
statements”.
• Example:

COMP1409 - Lesson 2 16
if statements
• if statements are very often accompanied by an (optional) else
statement.
• In between these, there
may be zero, one, or many
else if statements also.

COMP1409 - Lesson 2 17
null
• null means “no object” (or “no reference”…more on that later) for
object / reference data types.
• Not relevant for primitive data types.

COMP1409 - Lesson 2 18
Constructors
• A constructor in a class has the exact same name as the class.
• It is called automatically when an object is created.
• Its job is to ensure that the instance variables are set to
proper/normal/valid/sensible initial values.
• A constructor should not be marked private.

COMP1409 - Lesson 2 19
Constructors
• Remember, the
default value for
an int is 0 and for
a String is null.
• We don’t want to
allow the creation
of a Movie object
which was
released before
1895, nor of a
movie whose title is
null.
COMP1409 - Lesson 2 20
Constructor Parameters
• The Movie constructor takes two parameters.
public Movie(int theYearReleased, String theTitle)
• theYearReleased and theTitle are also known as “formal arguments”
• “Actual” arguments would be something like 1998 and “Harry Potter”,
for example:

COMP1409 - Lesson 2 21
Constructor Parameters
• The constructor did its job.
• The “bad” data of 1800 was rejected.

COMP1409 - Lesson 2 22
Assignment statement
• The equals sign = by itself does not mean equals.
• To say “equals” in Java, use two equals signs ==
• One equals sign alone assigns the value on the right side, to the
variable on the left side.

COMP1409 - Lesson 2 23
Logical operators
• There are three logical operators: and, or, not.
• In general these are evaluated left to right, when they are put in
combination with one another.
• and is written as &&
today is Saturday && today is Sunday && today is Monday
• or is written as ||
today is Friday || today is Saturday || today is Sunday
• not is written as !
boolean x = true; x = !x; // x is now false
• Note: not equals is written as !=
COMP1409 - Lesson 2 24
Relational operators

• == note: do not use with Strings or other objects; use .equals()


• != note: do not use with Strings or other objects; use !.equals()
• >=
• <=
•>
•<

COMP1409 - Lesson 2 25

You might also like