Posts

Showing posts from September, 2017

OOP - Objects and Classes

Classes Classes and objects are different terms Classes are an object type. You can create as many objects as you want of a class For example if a class has many fields and one of those is country, then an object would be Australia. In summary Objects are to Classes what Variables  are to Data types. For example  class Pokemon { String name;        //Field String type;         //Field int health;            //Field boolean dodge(){                        //Methods    return Math.random()>0.5; } } In Java each class should be created in its own file and have the extension ".java" For examples classes might be  vehicle.java   or map.java ________________________________________ The main method: This is where the program starts Inside it you can create objects and call methods to run other parts of your code. As defined by : public static void main (String [] args){ //start program here } //public : means you can run this method from any

OOP - IDE

IDE IDE is an integrated development environment that can run any Java code. The combine a bunch of features including highlighting bugs and potential errors. IDE's that exist include: -IntelliJ -NetBeans -Eclipse -Android Studio (Based off IntelliJ)

OOP - Objects: Fields and Methods

What is Object Oriented Programming. Is about programming modelling around objects. We will be using Java for this OOP tutorials. An object is an enhanced data type; that has fields and methods kind of like a primitive type ie int x Objects: -Combine variables together to make your code more meaningful -Code becomes organised and easier to understand -Code becomes more easier to maintain. _________________________________________ For example for a car For Fields: -String make -String model -int year To access or write the car year: We would use: car.year int myCarYear = car.year; Sytem.out.println(car.year); car.year = 2007; Methods: In other words they are running actions that kind of look like calling a function) Methods like any functions can take arguements. void openDoor(int doorNo) If you wanted to open the back door. you could do the following openDoor(5). Assuming the back door is 5. Objects are only really useful when we use fields and met

Java Basics - Errors

Syntax errors (Compile-time error) -Violation of Java's grammatical rules -Will not compile Runtime errors -Happens while programming is running -Will Cause the program to crash Bugs (logical) -Program that just does not do what you expect.

Java Basics - 2D Arrays and Nested Loops

2D Arrays int dimensions[] [] where the first box is along the y axis and then the x axis. In programming 'i' represents the column and 'j' represents the row For example: grid[i][j] grid[column][row] ________________________________________ Nested Loops for(int i = 0; i<3; i++){    //this is the outer loop     for(int j=0; j<3; j++){  //this is the inner loop           System.out.println("What, where, who, when!");      } } The above message repeats 3 times x 3 times. Giving the printing the line 9 times over.

Java Basics - Arrays 2

Arrays and Loops We can pass arrays through functions. For example public double calculateAverage(double [] temperatures){ If we look up and index beyond the size of the array we will get the following error: "ArrayIndexOutOfBoundsException" Therefore it is important to check that the array is not out of bounds. __________________________________________________________________ How to search for the minimum number in the array: Lets make the following program: /* *Searches an array of speed for the fastest(smallest value) *@ param speed array of speeds *@return double the fastest speed found */ public double search(double [] search){ int size = search.length; double min=speed[0]; for(int i = 1; i<=size; i++){    if(search[i]<min) min = search[i]; } return min; } ________________ How to reverse an array of strings example public void printInReverse(String[] stringArray) { for (int i = stringArray.length - 1; i>=0; i--){ Syst

Java Basics - Arrays 1

Arrays  Arrays allow us to store many variables or strings in one place. For example month[0] = "January"; month[1] = "February"; month[2] = "March"; We can add numbers as well int number[0] =1; int number[1] = 5; int sum = number[0] and number [1]; //sum = 6; How do we declare an array int [] numbers {12,-2,4,6,-9,0,2}; double[] numbers {12.896,-2,4,6,-9.2,0,2};

Java Basics - Loops 1

While Loops While loops are continuous loops that repeat as long as a condition stays true. For Example: while(x<5){ //execute some code here x++: } _______________________________ For Loops for(int i=1; i<5; i++) { //write code here } Section 1: Intialize loop counter at value; Section 2:  The condition to continue looping Section 3: What to to the loop counter every cycle Now. Lets run through the following for loop below for(int i=1; i<=5; i++) { //run code here } i Is i<=5? Step into loop again? 1 1<=5 (true) Yes 2 2<=5 (true) Yes 3 3<=5 (true) Yes 4 4<=5 (true) Yes 5 5<=5 (true) Yes 6 6<=5 (false) No Note we only increment at the end of the loop. for(int i=1; i<=5; i++) { //run code here //increment i by 1 here at the

Java Basic - Function 3

 Java Documentation Java documentation lets us know about functions that java has built in and already recognizes. It tells us about function descriptions, variable types and how to use them. Java is owned  by Oracle and they contain all the documentation online. It is the holy bible that all java developers use. Lets create out own Java documentation below. The usual basic documentation is as follow: /** *General description of a function * *@param a first input parameter, named a *@param b second paramter, named b * *@return description of return value */ Now for a more real example eg dimension(double width, double depth, double height); /** *This volume function calculates the volume of a given rectangle by multiplying its sides * *@param width is the width dimension  (double) *@param depth is the depth dimension  (double) *@param height is the height dimension (double) * *@return  volume  is the dimension (as a double) */ There is automatic softwar

Java Basics - Functions 2

Parameters and Arguements. Parameters are variables in the parentheses of our function defintion, that we can be used inside a function. For example: public void greeting( String location ){         //greet a specific location         System,out.println("Hello, " + location ); } _________________________________ Arguments  are specific values passed into our function call. For example:  greeting( "Australia" ); ___________________________________ How to list multiple parameters in a function: For example:  public void boxVolume (int height, int width, int depth) { //Code goes here } _____________________________ _______ Return Values In the following example:     public void functionName(). The void (return type) means  not returning any data    outside the function   we can only complete an action. In the following example:   public boolean functionName(). The boolean means  we can return a boolean value outside the function definiti

Java Basics - Functions 1

Function Function allow us to group together block of code and allow us to do a bunch of tasks and reuse it Calling a function is equivalent to executing the code in the definition. Example of the println function. public void println(String x){     //check that the String exists    if (x== null){            x= "null";    }    try {    ensureOpen();    //write String to screen     textOut.write(x);    }catch(IOException e) {          trouble = true;    }    newline();    } Function call of the above example --> System.out.println("Print this:); Functions allow the following: -Easy to reuse code -Organize and group code -More maintainable code. _____________________________________________________________ The word public  is an access modifier and anyone can use public functions. The word void is a return type that does not return data and only performs actions eg printing text. In the case above the println is the function name.

Java Basics - Control Flow and Conditionals 3

Switch Statement Can be used instead of using "if->else if->else" statements int someValue = 444; String someText; switch(someValue){    case 123: someText = "write this text for 123.";      break;    case 444: someText = "write this text for 444.";  //This piece of code will execute.      break;    case 178: someText = "write this text for 123.";      break;     default : someText = "nothing here"; } System.out.println(someText); break --> breaks out of each case default -->default case executes when the other cases are not true. Tip: when you have several identical cases, you can list them together like this, or you could just list "default" and let it catch any case not covered above. For example case 8: case 9: default: schedule = "Free!"; break;

Java Basics - Control Flow and Conditionals 2

Logical Operators There are three main logical operators: AND  && For example: 3<5 && 2> 15  results in false AND needs both of its conditions to be true to evaluate to true. OR        || For example:3< 5 || 2>15 results to true OR only needs one of its conditions to be true to evaluate to true. NOT     ! For example:!(3<5) results to false The NOT turns a boolean value into its opposite value The order of operations is: 1. Parentheses () 2. NOT ! 3. AND && 4. OR || More on Logical Operators AND evaluates before OR.  So false&&true||true  evaluates to true  while false&&(true||true) evaluates to false _______________________________________________ Nested IF Statements This means that an if statement is within an if statement. if(condition1 ==true){        if(condition2 == true) {} //this is the nested if statement. }

Java Basics - Control Flow and Conditionals 1

IF Statement  Using Boolean Varibles in If statements. The below if statement will execute only when true. ie if(1){} boolean isCold = true; if(isCold) { //execute code } Variable scope is the block of code where a variable can be used and referred to. ____________________ Else Statement boolean isCold = true; if(isCold) { //execute code } else { //execute code } _________________________ Else if  Statement An else if comes after an if statement boolean example1 = ?; boolean example 2 = ?; if(example1) { //execute code } else if(example2){ //execute code } else { //execute code } _______________________________ Boolean expressions This is where we compare numbers or variables to true or false boolean b1 = 3<5; //this will evaluate to true. boolean b2 = 5<3; //this will evaluate to false. Side Notes:  ">" and "< " always come before "=" in expressions. For example  x>=10 or x<=7. Th

Java Basics - Variable and Data Types Part 2

Variable Types (numbers) Integer: int exampleVariable       //store (-) and (+) numbers that are small Long:    long exampleVariable   //store (-) and (+) numbers that are big Double: double exampleVariable //store fractions with decimal places Variable Types (text) String:          String = String fullText = "example"; Character:    char answer = 'b' or  char answer = '3'; Variable Types (decisions) Boolean: boolean fact = true;  //Can only have two values, true or false. Variable Arithmetic Addition: int x = 2+3    //=5 Subtraction: int y=5-2   //=3 Multiplication:  int days = 7*4 //= 28 Division:        double div =5/2 //=2                        double div = 5/2.0; //=2.5                          (Note: the 2.0 forces it to store the fractional part instead of throwing it away) Combining arithmetic variables. double paid = 10; double change = 3.25; double tip = (paid-change)*0.2 // Note here th

Java Basics - Variable and Data Types Part 1

Declaring and initializing a variables: int box; //declare variable type and name. box = 5;  //initialize variable Updating and setting avariable: box = box -3; //read from right to left //the box now is equal to the integer of 2, because  5-3=2 Displaying the above variable to the outside world: System.out.println(box); You can also declare and initialize in one statement: ie int price = 0; ________________________________________________ Declaring a String Variable: String driver; //declare driver = "Hamish" // Initialize Getting the length of the string int letter = driver.length(); Displaying the above variable to the outside world: System.out.println(letter); //Notethe out put would be 6. Changing to all upper case: driver = driver.toUpperCase(); //Now the string is "HAMISH" String Concatenation String firstName; firstName = "John"; String secondName; secondName = "Bloke"; String fullName = firstN

The Journey Begins

Image
Where do I start…. This is the beginning of my journey into a pure software career. I hope you enjoy this blog and it teaches you something. About me: After finishing a Mechatronics Degree, I found myself in manufacturing starting in R&D using Arduino, Computer vision, C++, C# to make Mechatronics product prototypes. For the last 2 years at another company I was making physical products from scratch and putting them into mass production in the many of thousands. These products won design awards but I found the job frustrating more than satisfying and I found a life of repetition forming that I didn't want for my future. I always enjoyed the coding side of my career and the variety of challenges it brings. You will find tutorials and how I went about projects on this blog site.  You can follow me at: - StackOverFlow - GitHub  (for my project/(s) code) - LinkedIn On the side I enjoy the following  -Adventure sports -Travelling -Solving Rubik cube