Posts

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};