You are on page 1of 15

COMP1409:

Introduction to Software
Development I
Lesson 5
2 Learning Outcomes: Lesson 5

 Arithmetic operators
 Incrementing and decrementing
 Overloading constructors and methods
 Switch statement
 Calling internal methods
COMP1409 - Lesson 5
3
Arithmetic Operators

+ addition
- subtraction
* multiplication
/ division
% modulus or remainder after division
++ increment
-- decrement
COMP1409 - Lesson 5
4 Arithmetic Operators with assignment
operator

+= add then assign


-= subtract then assign
*= multiply then assign // don’t
/= divide then assign // do
%= remainder then assign // this

COMP1409 - Lesson 5
5
Example

int number = 1;
int value = 3;

number = number + value;


can also be expressed: number += value;

number = number - value;


can also be expressed: number -= value;
COMP1409 - Lesson 5
6
Incrementing and Decrementing
int number = 1;

Increment: number = number + 1;


can also be expressed: number++;
or ++number;

Decrement: number = number - 1;


can also be expressed: number--;
or --number;

COMP1409 - Lesson 5
System.out.println(7/4.0); // 1.75
System.out.println(7/4); // 1

int x = 5;
x++; // 6
x = x + 1; // 7
x+=1; // 8

x = x + 12; // 20
x+=12; // 32

x--; // 31
x*=2; // 62
System.out.print(x);

int y = 30 % 7; // 2; 30 / 7 = 4 remainder 2
int z = 30 / 7; // 4

int a = 30 * 5; // 150
8
Overloading
 Both constructors and methods can be overloaded
 Overloading allows a class to have two or more methods having same name, if their
signatures are different
 Three ways in which a signature can be different:
1. Different number of parameters
2. Different type of parameters
and/or
3. Different order of parameters

COMP1409 - Lesson 5
9
Overloading
public Book()
public Book(String title)
public Book(int numPages)
public Book(String title, int numPages)
public Book(int numPages, String title)

COMP1409 - Lesson 5
10
The Switch Statement
 Alternative to “if” statements
 Allows a variable to be tested for equality against a list of discrete values
 Each value is called a case
 The variable used can only be int, byte, short, char, Strings and enums
 Caution: Statements will execute until a break statement is reached
 Can have an optional default case, which can only appear at the end of the entire switch

COMP1409 - Lesson 5
11
The Switch/Case Statement
class Switch{
public static void test(String direction){
switch(direction){
case "north":
System.out.println("going up");
break; // if you remove this break, “north” prints “going up going down”
case "south":
System.out.println("going down");
break;
case "west":
System.out.println("going left");
break;
case "east":
System.out.println("going right");
break;
default:
System.out.println("error");
break;
}
}
}

COMP1409 - Lesson 5
12
The Switch Statement
*note the variable selection
is of type int

COMP1409 - Lesson 5
13
The Switch Statement (“if” version)
*note the variable selection
if(selection == 1){ is of type int
// do something here
}else if(selection == 2){
// do something here
}else if(selection == 3){
// do something here
}else{
// do something here when all other // cases fail
}

COMP1409 - Lesson 5
14
The Switch Statement
// prints the range of grades that gives that letter (e.g. if the parameter is ‘A’ or ‘a’ (case insensitive!) then the method prints “80 to 100”, etc…. A is
80 to 100; B is 60 to 79; C is 50 to 59; F is 0 to 49; other grades just print “invalid letter grade”
public void printGradeRange(char letterGrade){
switch(letterGrade){
case(‘A’):
case(‘a’):
System.out.println(“80 to 100”);
break;
case(‘B’):
case(‘b’):
System.out.println(“60 to 79”);
break;
case(‘C’):
case(‘c’):
System.out.println(“50 to 59”);
break;
case(‘F’):
case(‘f’):
System.out.println(“0 to 49”);
break;
default:
System.out.println(“invalid letter grade”);
Break;
COMP1409 - Lesson 5
}
15
Calling own methods
public BankAccount(int thePIN, double theBalanceUSD){
if(isPinValid(thePIN)){
PIN = thePIN;
}
}

private boolean isPinValid(int aPIN){


if((aPIN < 0) || (aPIN > 9999)){
return false;
}else{
return true;
}
}
COMP1409 - Lesson 5

You might also like