LAGOS JUG WELCOMES HEAD OF JCP HEATHER VANCURA

lagos jug logo designed by frank akogun
“Patrick Curran won’t be coming to Lagos anymore for the tour”, those were the words the Lagos JUG president said and boy did those smiles turn upside down. The Lagos JUG community had prepared for his coming with great expectations. It hurt the excos more than it did the members, we felt it would be a great chance to boost the morale of our members, let them know Oracle/Java depended on them in pushing the Java frontiers in Nigeria and Africa as a whole.

Months after and bam! We are pleased to inform the public that Lagos JUG will be hosting Heather Vancura the new head of jcp (Java Community Process) in Lagos Nigeria.

The event promises to be rewarding, with talks from experienced Java developers in the industry. namely, Stephen Ubogu on “Core software design principles” and Ezewuzie Emmanuel Okafor. Heather Vancura will also be addressing participants at the event.

The Lagos Java User Group has tried hard in encouraging the local community to participate in the development of the Java programming language, be it small or big, I have no doubt that this event will help foster that.

To apply, visit the link, please note that seats are limited and attendance is strictly by invitation.

Caesars Cipher Algorithm (Javascript)

Enter text


Caesar cipher script

[code lang=”javascript”]
var mode = "ceaser";
var shift;
function encrypt(text, shift) {
var result = "";
if (mode == "ceaser"){

//loop through each caharacter in the text
for (var i = 0; i < text.length; i++) {

//get the character code of each letter
var c = text.charCodeAt(i);

// handle uppercase letters
if(c >= 65 && c <= 90) {
result += String.fromCharCode((c – 65 + shift) % 26 + 65);

// handle lowercase letters
}else if(c >= 97 && c <= 122){
result += String.fromCharCode((c – 97 + shift) % 26 + 97);

// its not a letter, let it through
}else {
result += text.charAt(i);
}
}
}
return result;
}

function decrypt(text,shift){
var result = "";
shift = (26 – shift) % 26;
result = encrypt(text,shift);
return result;
}
[/code]

How it works

ES6 classes and prototypes

For those of you who come from a object oriented programming language such as Java, C# and the likes, you tend to think about classes! now i’m sure you would all agree that when it comes to classes, Javascript is uh weird?. Today we’ll be looking at one of the significant upgrades to the Javascript language, the Es6 upgrades to be more precise, along with the new features, Es6 gave Javascript programmers the ability to declare classes (well sort of..). lets dig in!

Creating a class

[code lang=”javascript”]
class Vehicle{
//members and functions here
}
[/code]

We simply declare a class using the “class” keyword followed by the name of the class, in our case it’s called Vehicle.

Adding fields

[code lang=”javascript”]
class Vehicle{
constructor (name, wheels){
this.name= name;
this.wheels = wheels;
}
}
[/code]

Line 2 adds a “constructor”, for those of you who are new to programming, a constructor is a simple block of code (a special block of code to be more precise) that runs every time a class is instantiated. Here we add two fields “name” and “wheels”. Once a object of the class Vehicle is created (instantiated) the constructor runs and accepts two parameters “name” and “wheels” which in turn sets them up using the “this.variableName” syntax. in our case “this.name” and “this.wheels”

Instantiating a class

Trust me this is one of those fancy words you hear in programming, what this simply means is creating objects from predefined classes. in other words, using the class you created.

[code lang=”javascript”]
class Vehicle{
constructor (name, wheels){
this.name= name;
this.wheels = wheels;
}
}

let car = new Vehicle("Toyota", 4);
console.log(car.name); // Toyota
console.log(car.wheels); // 4
[/code]

Line 8 creates a new object of the class Vehicle, when you look at it, a Car is a type of vehicle, makes sense, so its a instance of the class Vehicle. We also pass it the values “Toyota” and “4” which are passed along to the constructor on Line 2. Note that when Line 5 runs, the constructor is called once for every object created (instantiated). Line 6 and 7 prints the properties of the object car

Adding class methods

[code lang=”javascript”]
class Vehicle{
constructor (name, wheels){
this.name= name;
this.wheels = wheels;
}

move(){
console.log(`Vroom! i’m a brand new ${this.name} vehicle`);
}
}

let car = new Vehicle("Toyota", 4);
console.log(car.move()); // Vroom! i’m a brand new Toyota vehicle

[/code]

Line 7 simply adds a new method or a member function called “move”. Notice the use of back ticks in Line 8 “, we skip the usual string delimiter “” and use a new Es6 feature called Template literals, this gives you the ability to interpret variables and expressions within strings. Head over to line 14 and we call the method on a instance of the class.

Extending classes

[code lang=”javascript”]
class Vehicle{
constructor (name, wheels){
this.name= name;
this.wheels = wheels;
}

move(){
console.log(`Vroom! i’m a brand new ${this.name} vehicle`);
}
}

class Plane extends Vehicle{

}

let aircraft = new Plane("F-22A Raptor", 3);
console.log(aircraft.name); // F-22A Raptor
console.log(aircraft.wheels); // 3
[/code]

Extending classes is one of the major reasons why we would ever bother with creating them in the first place. Instead of declaring a whole class all over, why not borrow existing functionalities from an already existing one? Line 12 creates a new class using the “extends” keyword. A plane is a type of vehicle, but it’s in its own league. Creating a separate class for Planes makes perfect sense. Note that we get the same functionalities right out of the box as demonstrated by Line 17 and 18

There’s really no point extending a class if all we are going to use are the same old basic functionality of the old class we are extending from (Vehicle class). Below is an updated code of the Plane class

[code lang=”javascript”]
class Plane extends Vehicle{

constructor(name, wheels, ability) {
super(name,wheels)
this.ability=ability;
}

move(){
console.log(`Swoosh! I’m a super fighter jet ${this.name} with the ability of ${this.ability}`);
}
}
[/code]

Quite a lot is going on here. A Plane compared to a car obviously can do a lot more. We would like to show this and while we are at it, we want a custom message when we call the move method

Line 3 calls the constructor method and passes three parameters “name”, “wheels” and “ability.” Looking at the Plane class, “ability” is the functionality unique to the plane class and basically sums up the term “extending a class”.

As you already know, the job of a constructor is to wire up (calling it bootstrapping also works) the details of a class, but here is where it gets interesting, Line 4 uses a new keyword called “super”. Think of this as a contract, terms and conditions blah blah blah, when extending a class, the moment you call the constructor, you must reference the constructor of the class you are extending, in our case we use the keyword “super”. So what is basically happening on Line 4 is, The Plane class is about wiring up itself, The plane class constructor goes through the parameters passed to it and says, “Hey Vehicle class! I believe the variables “name” and “wheels” belong to you, could you set it up for me? here you go!”. Remember “ability” belongs to the Plane class and not the Vehicle class so it doesn’t bother passing it up to the Vehicle class. Instead it sets it up on Line 5.

When extending a method or member function of a class, you simply use the same method name and change the functionality. In our case, Line 8-9 logs a different message that is unique to the Pane class. There, you all caught up!

Instantiating and calling the various methods of our newly created extended class gives you the expected results as described below on Line 24-28

[code lang=”javascript”]
class Vehicle{
constructor (name, wheels){
this.name= name;
this.wheels = wheels;
}

move(){
console.log(`Vroom! i’m a brand new ${this.name} vehicle`);
}
}

class Plane extends Vehicle{
constructor(name, wheels, ability) {
super(name,wheels)
this.ability=ability;
}

move(){
console.log(`Swoosh! I’m a super fighter jet ${this.name} with the ability of ${this.ability}`);
}
}

let aircraft = new Plane("F-22A Raptor", 3, "flight");
console.log(aircraft.name); // F-22A Raptor
console.log(aircraft.wheels); // 3
console.log(aircraft.ability); // flight
aircraft.move(); // Swoosh! I’m a super fighter jet F-22A Raptor with the ability of flight
[/code]

Static methods

We create static methods when we basically want to use a class for it’s method (utility class). We simply have no interest in instantiating it, we want to call out a functionality simply by using “Class.methodName” instead of “Object.methodName” (I think i lost you there). Lets quickly take a look at a code that explains this

Updated Vehicle class

[code lang=”javascript”]
class Vehicle{
constructor (name, wheels){
this.name= name;
this.wheels = wheels;
}

move(){
console.log(`Vroom! i’m a brand new ${this.name} vehicle`);
}

static dict(){
console.log("A thing used for transporting people or goods");
}
}

let car = new Vehicle("Toyota", 4);
console.log(car.move()); // Vroom! i’m a brand new Toyota vehicle
Vehicle.dict(); // A thing used for transporting people or goods
Vehicle.move(); //Error!! Uncaught TypeError:
[/code]

Line 11 shows the addition of a new method, but notice the use of a new keyword “static”, yup! that’s how you declare a static method. but whats so special about it? glad you asked, head over to Line 19 and boom! there you have it. I called the method of the class directly (Class.methodName == Vehicle.dict()), without instantiating the class or in our case using an already instantiated class object i.e (Object.methodName == car.dict()). Line 20 throws a nasty javascript error, we tried calling a method that has not been declared static, in order to access the move method, I must instantiate the class (create a Vehicle object).

That pretty much sums up ES6 classes for this tutorial, for the sake of reference, below is the completed class code base.

[code lang=”javascript”]
class Vehicle{
constructor (name, wheels){
this.name= name;
this.wheels = wheels;
}

move(){
console.log(`Vroom! i’m a brand new ${this.name} vehicle`);
}

static dict(){
console.log(‘hello theres’);
}
}

class Plane extends Vehicle{
constructor(name, wheels, ability) {
super(name,wheels)
this.ability=ability;
}

move(){
console.log(`Swoosh! I’m a super fighter jet ${this.name} with the ability of ${this.ability}`);
}
}
[/code]

Now that you understand ES6 classes, what’s next? simple, go brag to your friends about your new found knowledge!. Then you come back running, crying “Dupree, my buddie said he was creating classes long before ES6, how is that even possible!” Not my fault, you failed to look under the hood. always look under the hood! luckily, that’s why i’m here 😀

Javascript Prototypes

Introduction to algorithms using java

[code lang=”java”]
public class Algo {

private int[] arrayList = new int[20];
private int arraySize = 10;

public void arrayGenerator(){
for (int i =0; i<arraySize; i++){
//generate random numbers between 10 and 19
arrayList[i]= (int) (Math.random()*10)+10;
}
}

public void printArray(){
System.out.println("———-");
for (int i=0; i<arraySize; i++){
System.out.print("| " + i +" | ");
System.out.println(arrayList[i] + " |");
System.out.println("———-");
}
}

public static void main(String[] args) {
Algo con = new Algo();
con.arrayGenerator();
con.printArray();

}
}
[/code]

Line 3 and 4 basically creates a new array, with an initial size of 20, we later go ahead and limit the fields to be populated to 10. that would do for this tutorial 🙂

Line 7 declares a method called arrayGenerator, arrayGenerator when called, fills the array with random numbers. and finally line 15 displays the contents of the array calling the method printArray

instantiating our class on line 26, we call the arrayGenerator method before printing the values of the array. below is a screenshot of what you should expect.

Introduction to algorithm using java

Getting an element by it’s index

[code lang=”java”]
public int getValueByIndex(int index){
if(index < arraySize){
return arrayList[index];
}
return 0;
}
[/code]

It’s important you ensure that the search index being passed to the method is within the array size. (remember we set a limit of 10!) passing a search index of 11 upwards would lead to a error. we only return the value at the given index if it is less than the agreed array size if(index < arraySize)… running algo.getValueByIndex(4) should give you 17. 17 is the value whose index is 4. refer to the screenshot above.

Confirm if a given value is present

[code lang=”java”]
public boolean confirmValue(int value){
boolean status = false;
for(int i=0; i<arraySize; i++){
if (arrayList[i] == value){
status = true;
}
}

return status;
}
[/code]

We create a method that returns True or False (denoted by public boolean methodName..). Notice I did not bother checking the size of the incoming argument, any given integer value is perfectly fine. We loop through the array 10 times. For each iteration, we compare the value of each element in our array (arrayList[i]) with that of the given value. this comparison is done in a if statement. unless both values match, the value of status remains false! Once a match is found, we set status equal to True.

run algo.confirmValue(17) //returns True

Delete a value by it’s index

[code lang=”java”]
public void deleteByIndex(int index){
if(index < arraySize){
for(int i = index; i<(arraySize-1); i++){
arrayList[i]= arrayList[i+1];
}

arraySize–;
}

}
[/code]

Following the same ol rules, you want to ensure the index being passed is within the agreed array size (arraySize is 10). Let’s imagine a user passes 4, i becomes 4, we are going to loop through the array for as long as 4 the index is less than 9. here we go

while i=4, i<(arrayList-1) returns true, because 4 is less than 9, then we go into the loop, arrayList[4] which has the value 17 is our target, that's what we want to delete. arrayList[i] = arrayList[i+1] is the same as arrayList[4] = arrayList[4+1], this is how we delete the particular index, we simply overwrite it, we take the 5th element and place its value into the 4th element, the next time we loop again we encounter i++, this increments i by 1, so i becomes 5. arrayList[i] = arrayList[i+1] becomes arrayList[5] = arrayList[5+1] so we take the value of the 6th element (arrayList[5+1]) and place it in the 5th element (arrayList[5]), this way we ensure that other elements get pushed up into the array. get it? and finally we run arrayList--. since we deleted an array, the size no longer becomes 10 but 9, so the new size is arrayList-- (which means 9)

Inserting a value into the array

[code lang=”java”]
public void addValue(int value){
if(arraySize < 20){
arrayList[arraySize]=value;
arraySize ++;
}
}
[/code]

This is quiet easy, when adding an element into the array, we check to see if the arraySize is less than 20, remember private int[] arrayList = new int[20];. the maximum number of elements our array can take is 20. once this condition is met, we proceed by passing the arraySize as the index. this way we are certain that the element appears as the last element in the array. and finally we increase the array size i.e arraySize ++;