Knowledge7

The Linux and Mobile Solution Provider

  • About
  • Training
  • Services
  • Clients
  • In the news
  • Blog
  • Contact Us
You are here: Home / Blog

Types and Variables

This topic is part of our Mastering Computer Programming training

Welcome to our Mastering Computer Programming training!

Let us start by answering the following questions:

  • What is a computer?
  • What is computer science (also known as Informatique in countries such as France or Germany)?
  • What is a program?
  • What is a programming language?
  • Why Python?

The next step is to work through a series of exercises which will increase our understanding of computer programming using Python. For that, we will use the book How to Think Like a Computer Scientist / Learning with Python written by Jeffrey Elkner, Allen B. Downey, and Chris Meyers and which can be read online.

Try to find solutions to the exercises found at the end of Chapter 1 in the book, The Way of the Program. Pay special attention to the difference between evaluating an expression (e.g. ‘This is a test…’) as opposed to printing the evaluation of an expression (e.g. print ‘This is a test…’)

Variables, expressions and statements

Now, let’s tackle Chapter 2 of the book: Variables, expressions and statements. In Python, values are typed (i.e. integer, string, float, …) and, consequently, variables which contain values are also typed.

Let’s work out the exercises at the end of this Chapter. Pay special attention to the fact that an assignment has no value and, consequently, cannot be used in an expression. Furthermore, it is important to understand the difference between input() and raw_input().

This topic is part of our Mastering Computer Programming training

Our forthcoming training courses

  • No training courses are scheduled.

Download our Euro 2012 TV Guide Android application

We are proud to announce the launching of our Euro 2012 TV Guide Android application available for free in the Google Play Store.

Euro 2012 is starting in a few days in Ukraine and Poland. Use this application to know on which TV channels each match is being shown. Choose among the following countries:

  • Mauritius
  • France
  • United Kingdom
  • United States

You can browse day by day. Click on the flag of a country will open a browser with the latest information on each team participating in the World Cup (courtesy of Wikipedia.)

Changelog

Name    Date            Comment
======= =============== ====================
1.0     2012-06-06      First release
1.1     2012-06-08      Fixed some colours
                        and the Portugal flag
1.2     2012-06-15      Fixed some errors
                        concerning matches
1.3     2012-06-20      Added quarter finals
1.4     2012-06-25      Semi finals updates

Download Euro 2012 TV Guide and have fun!

Our forthcoming training courses

  • No training courses are scheduled.

Android Application Security

Android Applications Security

View more presentations from KnowledgeSeven

Today, I talked about Android Application Security during a workshop organised by the National Computer Board. I had the pleasure to explain to the audience how the permission model works in Android, how Android monitors all apps during runtime and how to use advanced tools such as LBE Privacy Guard on rooted Android devices to add an additional layer of security to Android.

I am happy to say that some people told me they liked my presentation a lot and that’s why I’m sharing it to all of you.

Feel free to ask me questions if you have any.

Our forthcoming training courses

  • No training courses are scheduled.

Using a Linux cluster to make an application 8 times quicker

Introduction

This post is about how we managed to solve the interesting problem of randomly generating more than 12 million unique codes of 8 non-ambiguous characters.  This is an interesting problem as the most obvious solutions would take days to generate the numbers. Our solution was to write a very efficient program in C and make it run on a cluster of 11 computers each having a dual-core processor and 2Gb of RAM. Even so, the application took 18 hours to run but, in the end, we got our 12 million unique codes.

This post was written by Avinash Meetoo, Managing-Director, and Noorani Bakerally, Research and Development Specialist of Knowledge7.

The problem

We had been asked by a very well-known Mauritian company to generate 12,050,000 unique codes. The codes had to be unique (in the sense that it is impossible for a code to appear twice (or more) in the list and each code had to be 8 non-ambiguous characters long (no 0 or o for example).

This is an interesting problem from a technical point of view as the possible solutions all need a lot of processing power. It is also an interesting problem from a business point of view because, well, many draws and games require a lot of unique codes with only a few considered as being winning codes. Of course, it is essential that the codes be non-ambiguous (so that people can read them, write them and/or transmit them without mistakes). It is also essential that the codes be random and be selected from a very large set of possible codes (in order to decrease frauds).

Possible slow solutions

The most obvious solution is to generate a random code and, before adding it to a list, check whether the code does not already exist in the list. The complexity of this algorithm is clearly O(N2) and, even on a quick computer, would have taken years to run.

A variation is to use an algorithm which generates a random code and systematically adds it to a list. After some time, the list will have quite a lot of codes but with possible repetitions. The idea is to generate more than 12,050,000 (to account) for the repetitions and eliminate them in order to only keep the 12,050,000 unique codes needed. This algorithm is better as it runs in O(N) but, still, it would have taken more than one week to run according to our calculations.

Our quicker solution

We decided to implement a concurrent program using the MPI (Message Passing Interface) library and written in C (for maximum speed). The concurrent program was made to run in parallel on a cluster of 11 Linux computers:

  • The first node (rank = 0) is a master node which has as responsibility to save the list of all generated codes on disk.
  • Each remaining node (rank = 1 to 10) would then generate unique codes using an algorithm devised by Jon Bentley in the classic book, Programming Pearls. The ten nodes were programmed to start generating codes from different initial points so as not to bias the results.

Strictly speaking (and this had an important impact on the speed of the algorithm), we decided to make the nodes generate long integers. The encoding was code à posteriori using a set of 32 non-ambiguous characters. The 12,050,000 codes were then statistically analysed and we found out that they were entirely random and without any repetitions.

Because of the Linux cluster, we manage to bring the run time to 18 hours instead of more than the seven days the previous version required. This allowed us to run the parallel program more than once at Knowledge7 and, each time, we got 12,050,000 entirely random codes hence validating our algorithms and architecture used.

Followups

As we are very satisfied with the performance of the program, we intend to use it again if ever we get similar demands. We also were very happy with the fact that the Linux cluster worked perfectly from day 1. It is even possible that we run a Linux Cluster Installation and Programming training in the future.

Would you be interested?

Our forthcoming training courses

  • No training courses are scheduled.

Generics and Concurrency in Java

This topic is part of our Object-Oriented Programming in Java training

Java is a powerful programming language and, in order to become a productive programmer, one of the best books to read is Effective Java by Joshua Bloch. In it, one can learn about how to use the object-oriented capabilities of the language to build software with quality attributes such as maintability and flexibility.

Generics

Since version 5, Java also provides constructs for generic programming which we are going to explore now.

Generics allow programmers to construct generic data structures. Java already provides generic lists and maps which we have been using in the previous applications we wrote. They are called generic simply because they allow for any kind of objects without any constraints.

Generic Memorizer

Write a Java class which implements a generic memorizer class that remembers the last five values it was asked to memorize. A test program should instantiate an Integer as well as a String memorizer, make them memorize ten values (of course, only the last five values are going to be memorized) and display their contents on screen.

RandomElement

Write a Java class called RandomElement which contains one method called takeOne which return a random element from any collection passed as a parameter. A test program should then create a collection of a specific kind and check whether takeOne works as expected. (takeOne needs to be a generic method).

Concurrency

Java provides various constructs and libraries which allow the programmer to write concurrent programs. The basic capabilities include Threads and Runnables.

MultithreadedCounter

Write a concurrent Java program which creates two distinct threads that respectively increments and decrements a volatile integer variable initialised to 0, waits for 10 seconds then stops the two threads and displays the value stored in the variable.

Modify the program to create 50 incrementer threads and 50 decrementer threads. What do you notice?

Modify the program to use an Executor instead. Is it better? What is the difference between the shutdown and shutdownNow methods?

ProducersAndConsumers

Write a Java program that creates 10 producers of random numbers between 0 and 999 and 10 consumers. Use a LinkedBlockingQueue with a maximum queue size of 10000 to store the numbers being produced and being consumed. Make sure that the producers and consumers are stopped after 10 seconds.

Future Fibonacci

Write a Java program that slowly calculates the Fibonacci of various numbers concurrently. The program should pause for 5 seconds while the calculations are being done and display all completed results. Then the program should pause for enough time for the remaining calculations to be done and display the remaining results. The program should used Futures.

This topic is part of our Object-Oriented Programming in Java training

Our forthcoming training courses

  • No training courses are scheduled.

Design Patterns in Java

This topic is part of our Object-Oriented Programming in Java training

Once someone understand sound object-oriented analysis, design and programming principles, typically by reading a good book on Object-Orientation (such as Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development by Craig Larman) and programming real applications a lot, he/she can then embark upon mastering design patterns.

A Design Pattern is a solution which experienced software developers have been using over the years when they have to solve a given type of problem. Useful design patterns include:

  • creational patterns like Abstract factory and Singleton,
  • structural patterns like Adapter (or Façade) and Composite and
  • behavioral patterns like Observer and Strategy

One good way to learn about Design Patterns is to write real software applications which, at their core, use patterns in order to solve real problems.

First exercise: SecurityManager

In a lot of applications, there exist classes which need to have only one instance (one object created at runtime). Examples are database connections, security managers to verify passwords, credit card processors, etc.

Write a Java class called SecurityManager which only have one instance throughout the application. Use the Singleton design pattern.

Second exercise: IOFacade

Frequently, we need to hide a set of classes with a complex interface behind a simpler class. This happens, for example, when reusing code written by another team of programmers.

Write a Java class called IOFacade which simplify the use of the Java I/O facilities by making exceptions less intrusive. Use the Façade design pattern. (This pattern is somewhat related to the Adapter as used in InputStream -> InputStreamReader -> BufferedReader.)

Third exercise: Observers

In a lot of operating systems and online services, observers register their interest for a specific event and, when this event happens, the subject of the event notifies all the observers. This allows all the observers to know that the event has occurred without having to resort to polling (which is wasteful of resources).

Write a Java program allows Observers to be added to Subjects. When the state of the Subject changes, all Observers should be immediately notified. Use the Observer design pattern.

Fourth exercise: Solar System

Finally, quite a lot of applications e.g. desktop environments such as Gnome or Windows, vectorial drawing apps, word processors, ERP systems, etc. need to model hierarchies e.g. graphical widgets, shapes, words and people / products.

As a tribute to Dennis Ritchie, write a Java program that models the whole of the Solar System including the star, planets, natural satellites and artificial satellites. Use the Composite design pattern. The class diagram for the Solar System application is:

This topic is part of our Object-Oriented Programming in Java training

Our forthcoming training courses

  • No training courses are scheduled.

Looking for a half-length half-height PCIe network card in Mauritius

Please help us find a PCI-Express (PCIe) Gigabit network card in half-length half-height form factor in Mauritius. Pictured above is such a network card, a HP NC112T, and it would be great for us to be able to buy something similar quickly.

Thanks for helping us.

Our forthcoming training courses

  • No training courses are scheduled.

Object-Oriented Programming using Java

This topic is part of our Object-Oriented Programming in Java training

Developing software using object-oriented principles require a programmer to have a sound understanding of a number of important concepts:

  • Object-Oriented Analysis: what is the problem to be solved?
  • Object-Oriented Design: how are we going to solve the problem
  • Object-Oriented Programming: actually implementing the solution using an Object-Oriented Programming Language

One good way to learn Object-Oriented Analysis and Design is to read Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development by Craig Larman. In it, the author explores ways to use the Unified Modeling Language (UML) and Design Patterns to create object-oriented software which are resilient to change and which have a sound architecture.

First exercise: Phone-Book

The first exercise is to Write a Java program which allows the user to manipulate a phone-book. The program should allow for the creation of new name/number pairs, searches and deletions (three use-cases which belong to the use-case diagram).

The software to be written needs to find solution for each of the three use-cases, needs to be object-oriented and each class written needs to be unit tested using the JUnit framework.

The steps to follow are:

  • Draw the use-case diagram
  • For each use-case, draw a sequence diagram
  • From the sequence diagrams, derive a class diagram
  • For each class identified, implement it in Java
  • Immediately test each class using JUnit
  • Write a test suite
  • Write the main application providing a simple text interface for the application

Second exercise: Simple E-Commerce Application

Write a Java application which allows the user to add products to a shopping cart. The total cost (taking in account shipping rates should always be displayed). The application should ideally have a graphical interface.

The different products are books and CDs (Interestingly, both have now been replaced by their e-variants: e-books and music streaming using services such as Pandora and Spotify!). Any product has a name, a price and a shipping rate (which is dependent on the type of product). A  book ships for Rs 200.00 and a CD ships for Rs 100.00.

The shopping cart should allow the user to add products to it and should know how to calculate its total price (which includes the prices and shipping rates of each product it contains).

To evaluate whether the shopping cart works correctly or not, it is important to build a main application. In the previous exercise, we built a text interface. For this e-commerce application, we are going to build a simple graphical user interface for it (as shown on the left).

The key elements are a set of buttons which when clicked on add the corresponding product to the initially empty shopping cart. The total price of the shopping cart is always shown on the status line (at the bottom of the application). We are going to use the Swing library to build the graphical interface. Swing is relatively straightforward if its essential concepts are understood, namely the JFrame, the JTextField, the JButton and, very importantly its method, addActionListener).

This topic is part of our Object-Oriented Programming in Java training

Our forthcoming training courses

  • No training courses are scheduled.

Top 7 reasons to learn Computer Programming

Our next Mastering Computer Programming (C++) training will run during two weeks from Monday to Friday from 9:00 to 12:00 (2-13 December) in our premises in Quatre-Bornes. Students will be offered a 20% discount upon presentation of their student card.

Get Free Information

Corporate clients: We can arrange for dedicated sessions for your batch of employees. For your specific needs, please call us on 5834-9001 or email us at .

Individual clients: Leave your contact details and we will let you know when the course is scheduled.

First reason: Work in an IT company

Computer programmers are in high demand in all tech companies all over the world. Tech companies include Google, Apple, Microsoft, Facebook, etc. but also the myriad of smaller startups. In Mauritius, since the creation of the Cybercity, the Government wants that IT become one of the pillars of our economy and this explains why so many IT companies like Accenture, Ceridian, TNT Express, etc. are present in Mauritius. All of them are recruiting right now, looking for the best computer programmers and are ready to give very interesting salaries to them.

Second reason: Work in any company where IT is essential

Most people use computers when they work. And those who possess computer programming skills have an added advantage over those who don’t. For example, a financial analyst who knows how to program Excel can go much further in his understanding of financial data and, hence, produce more accurate reports. Another example is a biologist who uses her knowledge of programming to better develop new molecules and medicines. This allows people who understand programming to rapidly advance in their company’s hierarchy.

Third reason: Become an IT entrepreneur and change the world

What do Bill Gates (Microsoft), Steve Jobs (Apple), Linus Torvalds (Linux), Sergei Brin and Larry Page (Google) & Mark Zuckerberg (Facebook) have in common? They all have a deep understanding of computer hardware and computer programming. They also all created their own small companies which became massive after some time and changed our world. In the Silicon Valley, entrepreneurs are creating new IT companies everyday and, in Mauritius, this is also happening. Why not be like them and create your own IT company?

Fourth reason: Understand our digital world

Our world is now 100% digital. We are surrounded by computers, smartphones and tablets. All those devices run software applications created by computer programmers and, in order to use them productively, whether at home or at work, one has to understand the logic of computers. Computer Programming teaches how computers work and how to better leverage the power of modern computers, for example, to manage your finance, run your own company or plan your various activities.

Fifth reason: Computer programming is interesting

Building software allow you to model anything that crosses your mind! You can build your own digital world with complex multimedia effects, build your own financial simulation system, build complex websites with large communities of enthusiastic people, create your own entertainment software, etc. In other words, computer programming allow you to express what you really want and the only limit is your own imagination.

Sixth reason: Computer programming is fun

Modern computer games are programmable. People can create their own characters and weapons and animate them. This allows gamers to go much further when they are competent computer programmers. It is also true that programmable electronic devices like the Arduino and the Raspberry Pi are creating a lot of buzz right now. With those devices and with a bit of imagination and skills, anyone can create his own electronic masterpieces be it a novel alarm, an intelligent robot or a home automation system.

Seventh reason: Because it’s possible in Mauritius right now!

Knowledge7 offers a quality Mastering Computer Programming training course to all those beginners and students who want to learn and master computer programming. Our training will run during two weeks from Monday to Friday from 9.00 to 12.00 (2-13 December) in our premises in Quatre-Bornes. Students will be offered a 20% discount upon presentation of their student card.

We provide participants with all required training material as well as all the software which are going to be used free of charge.

Contact Liliane on 5834-9001 now for expert advice! Places are limited! Feel free to share.

Our forthcoming training courses

  • No training courses are scheduled.

Android Apps Security

The National Computer Board is organising a workshop on Mobile Hacking & Application Security on Monday 30 April at Ébène and Avinash Meetoo, Managing-Director of Knowledge7, has been invited to be one of the panelists.

Avinash’s presentation is going to be on Android Apps Security and will cover topics such as Android app signing, the permission model, how the Android runtime enforces security and how to monitor security on actual smartphones and devices.

Anything important you believe he has missed and should be included in the talk?

Our forthcoming training courses

  • No training courses are scheduled.
« Previous Page
Next Page »

Looking for something?

Want to know more?

Get our newsletter

Discover the latest news, tips and tricks on Linux, the Web and Mobile technologies every week for FREE

This work is licensed by Knowledge7 under an Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) license.