Pages

Sunday, June 10, 2012

If conditions


In this lesson we are going to learn how to use the If coniditions. The If condition is used to decide where the execution of the code must go. Inside the If condition the is a logical condition which decide if the code will be executed inside the If condition or if it goes out. Then, the If condition can have an Else-If to catch the next execution, or it can have a final Else to execute the default code.

Example:
int a = 3, b = 4;
if (a == b){ //the following code will not be executed
System.out.println("a is equal to b");
}
else if (a > b){ // the following code will be executed
System.out.println("a is greater than b");
}
else
System.out.println("a is less than b");
/* the code before will not be executed. By the way, this code is considered inside the else, although not having braces. In those cases, only the next sentence will be executed */

Maths


In this lesson we are going to learn the basic arithmetic operations we can do in Java.

Symbol Operation
+ Adition
- Subtraction
* Multiplication
/ Division
% Remainder

Examples:
a = 5 + 3; //a = 8
b = a - 3; //b = 5
c = b * 2; //c = 10
d = c / 2; //d = 5
e = d % 2; //e = 1

Unary operations:

Symbol Operation
++ Increments a value by one
-- Decrements a value by one

For those one, the variable can be before or following the symbol, but the operation will be different. Here is an example:
int a = 0;
a++; // a = 1
++a; // a = 2
System.out.println(a++); // print 3
System.out.println(++a); // print 4 but a = 3. The instruction prints without changing the value of the variable.

Another form of this use is the following:
int a = 0;
a = +1; //it increments the value of the variable "a".

Logical Operations:

Symbol Operation
== Equal to
!= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
&& Conditional AND
|| Conditional OR
! Inverts the value of a boolean

Examples:
int a = 5, b = 3;
return (a == b); //return false
return (a != b); //return true
return (a > b); //return true
return (a >= b); //return true
return (a < b); //return false
return (a <= b); //return false
return ((a == b) && (a != b)); //return false
return ((a == b) || (a != b)); //return true
return !(a == b); //return true

Bitwise and bit shift operations:

Symbol Operations
~ Unary bitwise complement
<< Signed left shift
>> Signed right shift
>>> Unsigned right shift
& Bitwise AND
^ Bitwise exclusive OR
| Bitwise inclusive OR

Examples:
int a = 2; // binary value: a = 00000000 00000000 00000000 00000010
a = ~a; // a = -3, binary value: a = 11111111 11111111 11111111 11111101
a = a << 2; // a = -12, binary value: a = 11111111 11111111 11111111 11110111
a = a >> 2; // a = -3 binary value: a = 11111111 11111111 11111111 11111101
a = a >>> 2; // a = 1073741823 binary value: a = 01111111 11111111 11111111 11111111
a = a & 2; // a = 2 binary value: a = 00000000 00000000 00000000 00000010
a = a ^ 2; // a = 0 binary value: a = 00000000 00000000 00000000 00000000
a = a | 2; // a = 2 binary value: a = 00000000 00000000 00000000 00000010

Enum classes


The enum classes are used to enumerated multiple secuential values with its names. Those values are statics, and can be accessed publicly. 

Example:
public enum Week{
MONDAY, //numeric value = 0
TUESDAY, //numeric value = 1
WEDNESDAY, //numeric value = 2
THURSDAY, //numeric value = 3
FRIDAY, //numeric value = 4
SATURDAY, //numeric value = 5
SUNDAY //numeric value = 6
}

Apart of this, the enum class can have variables and methods as other classes. When there are some variables in the enum class, those variables will be linked to the enum values. Thus, the enum values can be setted as you want. Here is another example:

Code:
public enum Bottle{
 LITTLE (250), MEDIUM (500), BIG (1500);
 private int ml;
 public Bottle (int size){
  this.ml = size;
 }
 public int getSize(){
  return this.ml;
 }
}
public class Pack{
 private int nBottles;
 private Bottle bottleSize;
 public Pack (int nBottles, Bottle bottleSize){
  this.nBottles = nBottles;
  this.bottleSize = bottleSize;
 }
 public int getNBottles(){ return this.nBottles; }
 public Bottle getBottleSize() { return this.bottleSize; }
 public static void main(String[] args){
  Pack p = new Pack(6, Bottle.BIG);
  System.out.println("This is a pack of " + p.getNBottles() + " bottles.");
  System.out.println("Each " + p.getBottleSize() + " bottle has " + p.getBottleSize().getSize() + " ml.");
 }
}
Output:
This is a pack of 6 bottles.
Each  BIG bottle has 1500 ml.

Interface classes


The interface classes are more like a template of a class. The interface show how must be the subclass, which methods must implements, those methods must not be implemented here but in the subclasses Those classes are used to organize a serie of future classes which will act all together. It is like the guidelines all the subclasses must follow. Here is an example:
public interface CompanyInterface{
public void buySupplies();
public void paySalaries();
}
public class Supermarket implements CompanyInterface{
public void buySupplies(){
System.out.println("Buying fruits...");
}
pubilc void paySalaries(){
System.out.println("Paying cashiers...");
}
}
public class Garage implements CompanyInterface{
public void buySupplies(){
System.out.println("Buying tyres...");
}
pubilc void paySalaries(){
System.out.println("Paying mechanics...");
}
}

Abstract classes


An abstract class can be defined as the beginnig of a class. It implements abstract methods that can be used and modified in subclasses which extends the abstract one. The abstract methods do not have a body, the body must be implemented in the subclasses. An example can be the following: a first abstract class is Shape, the second one y Square which extends the first one. Then the Square class can use the methods and variables of the Shape class. Not all the abstract methods must be implemented. Here is an example:
public abstract class Shape{
public abstract void setName(String name);
public abstract void setNumberOfFaces(int faces);
}
public class Square extends Shape{
private String name;
public void setName(String name){
this.name = name;
}
}
public class Cube extends Shape{
private String name;
private int faces;
public void setName(String name){
this.name = name;
}
public void setNumberOfFaces(int faces){
this.faces = faces;
}
}

Methods


In this lesson we are going to study the methods. The methods becomes from the classes. They can change some propeties of the classes or the can obtain some value or functionalities of a class. Every method can have one or more arguments passed by the user, and can return a value or not. The methods can be public, private or protected. A public method can be called from a main programm where the class is constructed. A private method can only be called in the owner class. And the protected methods are method that can be called internally and from any subclass method. The subclasses will be discussed in another lesson. The variables can also be public, private or protected. Both methods and variables can be defined without specifying the visibility, here they will take the default visibility, private.

Structure of a method:

[visibility] [data returned] [name] ([data type of argument 1] [name of argument 1], [data type of argument 2] [name of argument 2]...){
[...]
}

Example:
public int getInteger(int argument1){
//something
return 1;
}

The constructor are methods too, but they are called only when the class is instancied. We can differenciate it from other methods because the constructor must have the same name as the class and it do not return anything. One class can have more than one constructor but they must be different by anyone of them from the arguments they have.

Here is an example:
public class ObjectA{
//variables
private String v1;
private int v2;
//constructor 1
public ObjectA(){
this.v1 = "Hello";
this.v2 = 0;
}
//constructor 2
public ObjectA(String arg1, int arg2){
this.v1 = arg1;
this.v2 = arg2;
}
//public method
public int getV2Incremented(){
this.v2 = this.increment(this.v2);
return this.v2;
}
//private method
private int increment(int value){
return value++;
}
}

Classes


Here we are going to explain the definition of a class. A class is an object that could be used in a main programm. This class has usually properties and methods. The properties can be modified by the user with the gets and sets methods. And the methods are used to get some results related to the class. This can be better explained with an example:
public class ObjectA {
//private properties
int property1;
int property2;
//constructor
public ObjectA(){
this.property1 = 0;
this.property2 = 0;
}
//gets and sets methods
public int getProperty1(){
return this.property1;
}
public void setProperty1(int property1){
this.property1 = property1;
}
public int getProperty2(){
return this.property2;
}
public void setProperty2(int property2){
this.property2 = property2;
}
//another method
public void increment(){
this.property1++;
this.property2++;
}
}

Now I am going to explain the example. First there is a class called ObjectA, which has to properties (property1 and property2). The properties can be accessed and modified by the get and set methods. Thus there is anohter method "increment" which increment the value of the variables property1 and property2 in one value. And finally, there is the constructor. The constructor is needed to create a local copy of the class ObjectA. In the constructor we can initialize the variables and call private methods if it is necessary.

There are multiple types of classes: enum, abstract, interface or class. All must be public if they are alone in the file. If anyone is inside another, it can be public or private. The main class of the file must be named as the file.