- The bubble sort algorithm works by swapping adjacent pairs in the list until all adjacent pairs are sorted in order, at which the entire list is sorted
- by making passes through the array, each pass moves from left to right
- the first pass compares element 1 and element 2 and swaps them if they are out of order, then compares element 2 and element 3 and swaps them if they are out of order, and so on
- a group of programming statements that are given a name
- two types of methods
- ```function-type methods``` - calculates and returns a value
- ```procedure-type methods``` - executes some commands and has a void return type
## Why Methods
- allows for code to be reused throughout a program
- more efficient and neatly organized programs
- allow for easy modification later on
### Examples:
```java
public static int square(int number){
return number * number;
}
public static void printHelloWorld(){
System.out.println("Hello World!");
}
```
- int - return type
- square - method name
- int number - formal parameter
## Built in methods
|Method|Description|
|:-----|:----------|
|```Math.abs(x)```| returns the absolute value of the paramter x|
|```Math.random()```|returns a pseudorandom value uniformally distributed between 0 and 1|
|```Math.round(x)```|returns the value of x rounded according to the usual arithmetic rules|
|```Math.ceil(x)```|returns the value of x rounded up to the nearest integer|
|```Math.floor(x)```|returns the value of x rounded down to the nearest integer|
|```Math.max(x, y)```|returns the greatest of values x and y|
|```Math.min(x, y)```|returns the smallest of values x and y|
|```Math.sqrt(x)```|returns the value of the square root of x|
|```Math.pow(x, y)```|returns the value if x raised to the power of y|
|```Math.PI```|returns the double value of the mathemetical π|
|```.charAt(x)```|returns the character at index x|
|```.substring(st, ed)```|returns the string beginning at st and ending at ed(exclusive)|
|```.toLower()```|returns the lower-cased version of the string|
|```.toUpper()```|returns the upper-cased version of the string|
|```.split(regex)```|splits the string at index(s) where the ```regex``` appears and returns an array of strings|
|```isCharacter(x)```|checks if x is an character|
## More on Methods
- ```signature``` - method name and parameter
- if method is to be used outside the class in which it is defined, it must be declared as a public in the class
- call or invoke a method to use it
- ```formal parameter``` - the names of the parameter accepted in the signature
- ```actual parameter``` - the values passed into a method
- if no parameters, empty set of parentheses are used
- ```java
c.readInt();
```
## Return Type Methods
- public static ```return type``` ```method name```(parameters)
- Example:
```java
public static int square(int num1){
// do stuff here
}
```
## Void Type Methods
- public static void ```method name```(parameters)
### Example:
```java
public static void drawPicture(){
// do stuff here
}
public static void draw4Circles(int x1, int y1, int x2, int y2){
// do stuff here
}
```
### Java language - Strongly typed
- meaning you are not allowed to assign a value to a variable that is consistent with its declare type
- **```Scope of variable```** - the part of the program over which the variable can be accessed or referenced
- referes to the ```accessibility``` of a variable
- variables cannot be accessed before they are declared
- Variables can be declared in several different places
- class bodies (referred to as global or class level variables)
- as parameters to methods(in method signature)
- in a method body
- in a statement block (like a loop or a while loop)
```java
public class VariableExample{
static Scanner myScanner = new Scanner(System.in);
public static void main(String[]args){
// main method
}
}
```
- Description of Example
- variable myScanner is declared outside of the main method in the class level
- the variable is considered to be a global variable that can be accessed anywhere in the class
- for now, global variables should have the keyword ```static``` preceding the declaration
- In methods
- a method may declare local variable in the body of the method for use onlyin that method
- variable may also be declared in the parameter list - only can be accessed in the method
```java
public static int thirdPower(double number){
int cube; // local variable
cube = number * number * number;
return cube;
}
public static double thirdPower(double number){
double cube;
cube = number * number * number;
return cube;
}
```
- variable cube in thirdpower is local to that method
- local variables cannot be accessed from outside of the method
- In blocks of code
- variables defined in a block are only accessible from within the block
- the scope of the variable is the block in which it is defined
```java
for(int x = 0; x <5;x++){
System.out.println(x);
}
```
- the variable x can only be accessed in the for loop
## Method void return type
- return type void means that a method will not return a value
- the method can still have parameter when the return type is void
## Naming conventions
- method names should indicate an action
- verbs make good methods names
- methods names should begin with a lowercase letter and then an uppercase letter should begin with each word within the name
- method names may not contain spaces
## Pass By Value
```java
public static void drawBar(int length){
for(int i = 0; i <length;i++){
System.out.print("* ");
}
System.out.println();
}
```
- means that when a method is called, a ```copy``` of the value of each argument is passed to the method
- this copy can be changed inside the method, however such a change will have no effect on the actual argument
- copies of the actual parameter values from main are sent to the methods, where they become ```foramal parameters```. When the method is finished, the copies are discarded. The actual ```paramter``` values remain unchanged. Notice that nothing is returned in the above method and how values ```a``` and ```b``` are not changed in the main method when passed in the method below
```java
public static void main(String[]args){
int a = 0, b = 10;
System.out.println("The starting value of a and b are: " + a + " and " + b);
change(a, b);
System.out.println("The values of a and b are: " a + " and " + b);
}
public static void change(int a, int b){
a = 999;
b = 21;
}
```
## Pass by reference
- occurs when an object is passed to a method, its memory address location (its ```reference```) is used
- ```Arrays``` behave like objects, their memory location is passed to the method
- that means that when an array is manipulated in the method that we are actually ```changing``` the array
- be cautious when sending an array in the method as it will change the data in the original array
```java
public static void main(String[]args){
int array[] = new int[4];
a[0] = 1000;
a[1] = 2000;
a[2] = 3000;
a[3] = 4000;
System.out.print("The values of the array are: ");
for(int i = 0; i <a.length;i++){
System.out.print(a[i] + " ");
}
System.out.println();
System.out.println();
change(array);
System.out.print("The values of the array are: ");
|Analysis|Also called ```problem solving```, its the process of ```breaking problems down into smaller more manageable parts```|
|Design|In this stage, an ```algorithm``` which is a set of instructions defined to solve the problem|
|Implementaion|Also referred to as ```coding```. Where you express the algorithm in a ```programming language```|
|Testing|Invlovles the running of the program will a full range of data. Valid data: ```the data the user should be inputting.``` Invalid data: ```incorrect or unexpected data```|
|Maintenance|```Maintaining a program over time so that any changes are needed are incorporated```|
- **```valid data```**: the data the user should be inputting
- **```invalid data```**: ```incorrect``` or ```unexpected``` data
- **```algorithm```**: a set of ```insturction``` defined to ```solve``` the problem
- **```Well designed program```**: a program that ```reliably``` solves the problem it was created to solve
- **```Waterfall Model```**: A flow chart, each phase must be completed before the next one