Coding Bootcamp: Constructing and using objects

Aim of the course

Java as an OOP Language

Alan Kay's principles for OOP

Principle 1 - Everything is an object

What is an object

Example of an object

Classes

Creating new objects

Object memory allocation by calling new

    public class BootCamp {
        public static void main(String[] args) {
            BootCamp a, b;
            a = new BootCamp();
            b = new BootCamp();
        }
    }

Object memory allocation by copying

        public class BootCamp {
                public static void main(String[] args) {
                        BootCamp a, b;
                        a = new BootCamp();
                        b = a;
                }
        }

Principle 2 - A program is a bunch of objects communicating with each other

Call method, function

        public class BootCamp {
                public static void main(String[] args) {
                        BootCamp a;
                        a = new BootCamp();
                        a.printSomething();
                }

        public static void printSomething() {
            System.out.println("Hello BootCamp 2016");
        }
        }

Access properties, instance variables, fields

Scope

Scope Examsle: BootCamp Class

    public class BootCamp {
        //Public and private variables
        public int number1, number2;
        private boolean visible;
        private int javaProgramming;
        //Private method
        private void setNumbers(int one, int two) {
            number1 = one;
            number2 = two;
        }
        //Public method
        public void changeNumbers() {
            setNumbers(1,9);
        }
    }

Scope Example: Main

    public class BootCampTest {
        public static void main(String[] args) {
            BootCamp a;
            a = new BootCamp();
            a.changeNumbers();
            a.number1 = 10;
            a.javaProgramming = 1;
            a.setNumber(10,11);
        }   
    }

Scope Example: variable visibility

    public class BootCamp {
        public static void main(String[] args) {
            int x;
            x = 0;
                if (x = 10) {
                int y = 10;
                System.out.println("x and y:" + x + y);
                x = y * 2;
            }
            y=100;
            System.out.println("x is: "+x);
        }
    }

Principle 3 - Each object has its own memory made up of other objects

Principle 4 - Every object has a type

Principle 5 - All objects of particular type can receive the same messages

Problem Space

Solution Space

The call of Constructor (1)

The call of Constructor (2)

Calling Constructor - Example

     public class BootCamp {
        BootCamp() {
        //Do nothing        
        }
        
                public static void main(String[] args) {
                        BootCamp a = new BootCamp();
                }
        }

Class methods and fields

Class methods fields - Example

import java.util.Calendar;

public class FindDay {
  public static String weekDayName(int weekNumber) {
    switch (weekNumber) {
      case Calendar.MONDAY: return "Monday";
      case Calendar.TUESDAY: return "Tuesday";
      case Calendar.WEDNESDAY: return "Wednesday";
      case Calendar.THURSDAY: return "Thursday";
      case Calendar.FRIDAY: return "Friday";
      case Calendar.SATURDAY: return "Saturday";
      case Calendar.SUNDAY: return "Sunday";
      default: return "Invalid day";
  }
}

  public static void main(String args[]) throws Exception {
    FindDay today = new FindDay();
    System.out.println(FindDay.weekDayName(4));
  }
}

Methods for all objects

toString example

    String toString()
    static String toString(int i)
    public class TestingToString {
        public static void main(String[] args) {
            Integer variable1 = 2016;
            int variable = 2016;
            System.out.println(variable1.toString());
            System.out.println(Integer.toString(2016));
            System.out.println(variable.toString()); //Correct?
        }
    }   

Equals example

    public boolean equals(Object obj)
    public class TestingEquals {
        public static void main(String[] args) {
            String str1 = new String("BootCamp 2016");
            String str2 = new String("Learning OOP");

            if (! str1.equals(str2)) {
                System.out.println("They are not");
            } else {
                System.out.println("They are");
            }
        }
    }

Arrays in Java

    int[] array = new int[]{1,4,5,6};
    System.out.println("The size of array is"+ array.length);
    String[] array = new String[]{"Hello BootCampers"," how are you"};
        System.out.println(Arrays.toString(array));

Data types

Primitives vs Non-Primitives

Τype casting (1)


Type casting (2)


double avg = (double) 12/5

int rnd = (int) (29.4/10.0)

Integer xd = (Integer) rnd

Incrementing Examples (1)

Incrementing Examples (2)

Exercise 1

Create a static function, with the name "bootCamp", that receives as an input two int arguments, i.e., number1 and number2, and returns an int result in the end. If "number1" is smaller that "number2" then returns the square root (use Math.sqrt method) of the absolute value (use Math.abs) of the difference among "number1" and "number2". Else return the sum of the two numbers.
Number1 = 25, Number2 = 91
Number1 = 91, Number2 = 25

Exercise 1 result

First: 8, Second: 116

Exercise 2

Create a program that simulates the roll of two dices. The program must execute till both dices return 1 as a result (use Math.random). You can create a class name Dice which has a void roll method. Roll method is rolling the dices till the needed results is aquired and prints the number of rolls needed in order to have two 1s.

Exercise 3

Create an array that can receive 10 elements, as a size, and add in each position an element starting from 1 to 10 statically and print the array. Afterwards, create a method (function) that can shift the array elements right by 10 times. The 10 is given by the user as an input. At the end print the shifted array.

Exercise 3 result


[10, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[9, 10, 1, 2, 3, 4, 5, 6, 7, 8]

[8, 9, 10, 1, 2, 3, 4, 5, 6, 7]

[7, 8, 9, 10, 1, 2, 3, 4, 5, 6]

[6, 7, 8, 9, 10, 1, 2, 3, 4, 5]

[5, 6, 7, 8, 9, 10, 1, 2, 3, 4]

[4, 5, 6, 7, 8, 9, 10, 1, 2, 3]

[3, 4, 5, 6, 7, 8, 9, 10, 1, 2]

[2, 3, 4, 5, 6, 7, 8, 9, 10, 1]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Exercise 4

Convert exercise 1 array and it's elements, from int, to Double. P.S. not double but Double.

Exercise 5

Implement a tic tac toe (triliza) game which is played by two players. You may use one or two dimention array to store the results (the 'X' and 'O'). You can use a while loop to give turns for the 'X' or the 'O' player.
Create a drawFunction that can take as an argument the array and print the current tic tac toe's status before each players turn. Create function that can check rows, columns, and diagonals, which is performed after a player played his symbol, 'X' or 'O', and prints win if there is a winner, otherwise the game still goes on if and if there is available position in the array. If a player placed his symbol in an occupied position print an error message and let him play again.

References


Creative Commons Licence
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.