Wednesday, December 31, 2014

C PROGRAMMING

C PROGRAMMING

December 2013. The Fall 2013 semester has ended and I received my grades in all four classes. All A's.

Overall, I would say, that I was the top student in my "Introduction to Computer Security" class, because I had the highest grades in both midterm and final exams, and I did not ignore the professor's advice to contribute at least 3 discussions per week in the online class. Some of the students used to follow the professor's advice in the beginning of the class, but as the weeks wore on, most gradually posted very few while some, none at all.

For the next semester, Spring 2014, I was ready to tackle Advanced C++ and earn some money tutoring programming to beginning programming and web development students, including C, which is C++ without classes.

Programs written in C do not have classes. C is rather a procedural language, but is the first to optimize modular programming by using the concept of functions.

C can also represent a class type definition but the fields would not be private and it would not encapsulate its methods. Here is an example of a representation of a class the best that C can do.

This is the definition of the C header file cclass.h that represents a C class.

struct structclass {
   char *str; } astruct;
void setStr(char *c) {
   astruct.str = c;
}
char* getStr(void) {
   return astruct.str;
}

This is the user program that uses the C header file cclass.h that defines the closest to a class a C program can define. Note that it has a set and get methods like classes have.

#include <stdio.h>
#include "cclass.h"

int main(void) {
   setStr("Happy New Year\'s Eve!");
   puts(getStr());
   return (0);
}

Happy New Year!

John Sindayen

Monday, December 29, 2014

JAVASCRIPT

JAVASCRIPT

November 2013. About this time, I am learning a lot about C++, Visual Basic.NET, computer security, and about American History from the Reconstruction Period to the 1960's.
Also, I have received a recorded message from the Tutorial Services about filling in my application as programming tutor. But as I was so busy learning programming and writing my own programs, I decided to start with that tutoring job next semester instead, when I will be taking only a few courses.

I also saw a job advertisement posted in the college campus for JavaScript programmers. So I decided to learn more about it, in my own time.
JavaScript is king of the client-side scripting languages. It has no competition. VBScript used to be competitive as a client-side scripting language, but it was only supported by one web browser, Microsoft's own Internet Explorer.

Learning JavaScript is not just about learning programming in JavaScript. It is also about learning HTML elements, because HTML elements are also used inside a JavaScript program.
The following code shows how JavaScript can make an HTML document dynamic, rather than plain static.

<!DOCTYPE html>
<html>
<head>
<title>JavaScript Greetings</title>
</head>
<body>
<p>Push this button to see the messages.</p>
<button id="clickMe">CLICK ME</button>

<script>
clickMe.onclick = greetings;

function greetings() {
alert("Hello JavaScript!");
alert("Merry Christmas!");
alert("Happy New Year!");
}
</script>

</body>
</html>


Merry Xmas!

John Sindayen

References:
http://programmers.stackexchange.com/questions/44842/why-arent-there-other-client-side-scripting-languages-for-websites
http://www.linuxquestions.org/questions/programming-9/can-vbscript-only-run-in-internet-explorer-244132/

Friday, December 19, 2014

HTML

HTML FUNdamentals

October 2013. Writing HTML code is so much fun because you get instant gratification. If your code is working properly, you can see your code in a Web Browser right away.

I have been actually coding HTML for over a decade. It is easy to learn.

Technically, there is no programming involve in HTML since technically HTML is not a program. However, an HTML code can have a program included into it, for example, a JavaScript program. A CSS code is also like an HTML code. It is also technically not a program.

JavaScript is about the only client side programming language that you can find in an HTML code. No wonder it is so popular! However, you can invoke programs written in other language inside an HTML code, for examples, Java, C++, Visual Basic, Python, Ruby, Perl, and, of course, JavaScript. PHP is another programming language that you can find in an HTML code, but only if the HTML code is stored in a server computer.

The following is the fundamental design of a good HTML code.

<!DOCTYPE html>
<html>
<head>
<title>KNIGHT IN PROGRAMMING: Journey To Programming</title>
<style type='text/css'>
/* CSS code is written here. */
</style>
<script type = "text/javascript">
/* JavaScript code is written here. */
</script>
</head>
<body>
<script type = "text/javascript">
/* JavaScript code is written here. */
</script>
</body>
</html>

Now that you know how to write HTML, you can start writing in the World Wide Web. Who knows, the world can get smarter technically and psychologically and we can make the world of Star Trek come true in reality!

Knowledge is fun!

John Sindayen

References:
http://searchsoftwarequality.techtarget.com/definition/program
http://en.wikipedia.org/wiki/Client-side_scripting
http://programmers.stackexchange.com/questions/129141/is-css-a-programming-language

Wednesday, December 17, 2014

VISUAL BASIC

VISUAL BASIC

September 2013. The beginning of the Fall semester begun last week.  This time I was taking a full load of classes, namely, Beginning Visual Basic, Beginning C++, Introduction to Computer Security, and American History II.

Visual Basic started life as version 1.0 in 1991.  The last stand alone Visual Basic version is Visual Basic 6.0 released in 1998.

There are several derivatives that followed the Visual Basic programming language during and after its final version release. Visual Basic itself is derived from the archaic BASIC programming language, the most popular language learned by teenage programmers of the last century.

Microsoft replaced Visual Basic 6.0 with its released in 2002 of Visual Basic.NET or VB.NET for short. With this release, Visual Basic becomes part of Visual Studio.NET Framework where developers can program in Visual C++ and Visual C# along with Visual Basic.

I used the Visual Studio 2010 IDE in the college computers when I was taking Beginning Visual Basic. I could have used Visual Studio 2012 Express IDE but I did not have a computer at the time, except for a small 7 inch Sylvania netbook with a Windows CE OS embedded.

To create a Visual Basic program, whether it be a console or form application, you need to create a project first.

In my class, we only created form applications, but here, I will be creating a console application.

Start by opening the Visual Studio 2010 IDE. Click New Project, then Console Application, type the name of the application, then click OK.  A Module template will then appear and you can code your program to this:

Module Module1

   Sub Main()
      Console.WriteLine("Hello World! Merry Christmas " & 2014 & " !")
   End Sub

End Module

Happy Week Before Christmas Day!

John Sindayen

Monday, December 15, 2014

JAVA APPLETS

JAVA APPLETS

August 2013. The Beginning Java course ended last month. And at this time, I fully understood what I was not understanding before the beginning of the course, specifically object's attributes and methods.

For our last assignment in class, we were to write a Java applet.  Writing applets turned out to be one of the best parts of Java programming.

Here is the graphics of the applet I turned in for my assignment:


Here are parts of the comments and code I turned in for this assignment.  The entire applet is written in one class with an inner class to implement the mouse listener.

/*
Author: John Sindayen
Course: CIT130 - Beginning Java
Assignment #5 Homework #1
Due Date: 7/26/2013
*/

//MORE COMMENTS HERE

import javax.swing.*; // Needed to extend JApplet.
import java.awt.*; // Needed to draw graphics.
import java.awt.event.*; //Needed to listen to mouse events.
// Begin class definition of applet.
// Extend JApplet class.
public class House extends JApplet
{
  // Declare global boolean variables.
  boolean openRightWindow, openDoor, openLeftWindow;
  // Applet method to set up the applet.
  public void init()
  {
    // Initialize global variables.
    openLeftWindow = true;
    openRightWindow = true;
    openDoor = true;
    // Build the content pane with white background.
    getContentPane().setBackground(Color.WHITE);
    // Add mouse event listener.
    addMouseListener(new MyMouseListener());
  }
  // Method that builds the contents of the content pane.
  public void paint(Graphics g)
  {
    // Invoke the superclass paint method.
    super.paint(g);
    // Set color to black.
    g.setColor(Color.BLACK);

//MORE CODES AND COMMENTS

Drawing graphics in the computer is so much fun and when you're writing their code, that's where the excitement steps in.

Happy drawing!

John Sindayen

Friday, December 12, 2014

PYTHON

PYTHON

July 2013. Second month of the 8 weeks course in Beginning Java.  About this time, I am getting used to creating classes for my objects.  Some of the online students, however, were not understanding the concepts of classes and objects.

One student asked every one of our classmates about helping her understand why there were two classes in the program, via email.  I responded to her email request.

A few weeks later, she wrote me she had also asked our professor for help.  But that didn't help her either.  She ended up withdrawing from the fast-paced summer course.

Java requires strict programming rules, and writing a Java program requires a programmer to define one or more classes.  There are some programming language, however, that have no set standard for programming.  One of them is Python.

During my last week, this week, of tutoring in the Networking Lab, a student showed me his Python program.  In it, he had a series of definition of function followed by a call to that same function.  At the end of his program he had a call to the main function.  His program worked, but I told him his program was the first program I’ve seen with such a weird style of programming.

Here is one way of programming Python using good programming practices.

#MAIN FUNCTION: opens file and call functions to write and display file.
def main():
    f = open('testfile2014.txt', 'a')
    writeFile(f)
    displayFile(f)

#WRITEFILE FUNCTION: writes a text into file.
def writeFile(f):
    f.write('\n')
    f.write(input("Type a text to enter in file:\n"))
    f.close()

#DISPLAYFILE FUNCTION: reads a file and display it on screen.
def displayFile(f):
    with open('testfile2014.txt', 'r') as f:
        read_data = f.read()
    f.closed
    print(read_data)

#MAIN PROGRAM
main()

Happy Twelve Days Before Christmas!

John Sindayen

Tuesday, December 9, 2014

JAVA CLASSES

JAVA CLASSES

June 2013. First day of my online class in Java. I bought the Java textbook and it cost a lot of money, compared to the other books of other subjects.

I realized Computer Science books are more expensive than other subjects because publishers are expecting students to pay more since when Computer Science students graduate they will earn higher salaries compared to those who majored in other subjects.

My Java book cost me I think about $170 from the college bookstore.  The book was "Starting Out with Java: Early Objects."  And the book of the other programming related class that I was taking, Project Management, also cost more than $100, but I used a $50 textbook voucher the college has given me for working 8 hours in the college library.

Here’s some segments of the program I submitted to my professor in my Beginning Java course. The following code defines a class’s instance variables, a constructor, mutator, and accessor methods. I have used ellipsis to indicate some parts of the original code that are not shown here.

class Book
{
   // Declare class fields with private access.
   private String title, author, publisher;
   private int numPages;
   // Class default constructor accepting no argument.
   public Book()
   {
      // Initialize class fields of default constructor.
      title = "Project Java";
      author = "John Sindayen";
      publisher = "Random House";
      numPages = 300;
   }
   /* ... */
   // Mutator method to assign user input to title field.
   public void setTitle()
   {
      Scanner keyboard = new Scanner(System.in);
      title = keyboard.nextLine ();
   }
   /* ... */
   // Accessor method to return value of title field.
   public String getTitle()
   {
      return title;
   }
   /* ... */
}

Welcome to Java, class!

John Sindayen

Reference:
http://www.pearsonhighered.com/product?ISBN=0132164760

Friday, December 5, 2014

C++

C++

May 2013.  About this time I met a university C++ programming student in the university library I have been visiting.  I found out from him that the kid's game Minecraft was written in Java, when he mentioned it to me.

He was a Computer Science major and he told me he wanted to be an expert in C++ because it was the best language in the world.  That got me to thinking I should learn C++ also after learning Java. And that's how I chose C++ to be my second language, rather than the other option of C or Visual Basic.

Here's a C++ program I wrote 8 months after that meeting.  I was working as a programming tutor in the college I was attending after my professor asked me to tutor.  He actually asked me to be a tutor after taking his Beginning Java class, but I was too busy with my own full-time classes, to get around to applying for the position.

I used this C++ program to tutor a Beginning C++ student who wanted to know what's really happening inside a C++ program.

/*
Program that prints bill receipt for customer with
amount of purchase
amount of tax
amount of total bill.
Output: print total bill.
*/

#include <iostream>
#include <iomanip>

using namespace std;

//Start program.
int main()
{
    //Declare variables.
    double purchaseAmount;
    double tax;
    double totalBill;

    //Ask user.
    cout << "Enter amount of purchase: $";

    //Input statement.
    cin >> purchaseAmount;

    //Calculate tax.
    tax = purchaseAmount * 0.20;

    //Calculate total bill.
    totalBill = purchaseAmount + tax;

    //Output statement.
    cout << "\nTotal Bill: " << "$"
            << setprecision(2) << fixed << totalBill;

    return 0;
}
//End program.

Happy computing!

John Sindayen

Wednesday, December 3, 2014

JAVA ArrayList

JAVA ARRAYLIST

April 2013.  In preparation for going back to school under the auspices of the veteran's VRAP program, I perused more computer books.  I studied, in particular, a small paperback computer dictionary, from the front cover to the back cover.  I read every computer terminologies that were in the dictionary and their definitions.  Not all the sentences, but at least the first sentence.  I did this because I did not really know what computer knowledge was expected from a programming student.

I also read encyclopedias on Computer Science because it may help me further understand learning computer programming.  As the old adage goes, "Chance favors the prepared mind".

While we're on the subject of dictionaries, here's a sample program for writing dictionaries.

/*
   Sample program illustrating the use of ArrayList class.
   Program for a dictionary of three terms and definitions.
*/

import java.util.*;

public class ArrayListSample
{
  public static void main(String[] args)
  {
    ArrayList<String> term = new ArrayList<String>();
    ArrayList<String> definition = new ArrayList<String>();
    Scanner scanner = new Scanner(System.in);
    for(int i = 0; i < 3; i++)
    {
      System.out.print("Enter term: ");
      term.add(scanner.nextLine());
      System.out.print("Enter definition: ");
      definition.add(scanner.nextLine());
    }
    scanner.close();
    System.out.println("\tLIST OF TERMS & DEFINITIONS");
    for(int i = 0; i < 3; i++)
    {
      System.out.print(term.get(i) + ": ");
      System.out.println(definition.get(i));          
    }
  }
}

Here is a sample compilation and execution of this code:

C:\>javac ArrayListSample.java

C:\>java ArrayListSample
Enter term: computer
Enter definition: device programmer uses.
Enter term: programmer
Enter definition: person that writes game software, etc.
Enter term: C
Enter definition: programming language and letter of alphabet.
        LIST OF TERMS & DEFINITIONS
computer: device programmer uses.
programmer: person that writes game software, etc.
C: programming language and letter of alphabet.

This program uses parallel arrays while implementing the ArrayList class.  It only has three elements, but you can have as many elements as you want providing your computer has enough memory for all these elements.

If you plan to have a million elements in your ArrayList, or even close to ten thousand elements, depending on memory requirements, you should opt for a better technology to improve your system and software performance.  A program that implements linked list written in C or C++ can make your software go faster.  And if looking for a term, implement a binary search algorithm for faster searching.

Happy computing!

John Sindayen

Sunday, November 30, 2014

JAVA

JAVA

March 2013.  About this time, I set my sights into bigger targets.  I set my sights into using college programming books in the university library.  What programming books did I read?  Java, of course, because it was what I was going to study when I start going back to school.

Why did I decided on learning Java, anyway, instead of the other languages?  I think the reason for that was because it was the programming language with lots of available books in the public library.  And it was the language of the first programming book that I used to learn from, the AP Computer Science book, in the public library.

So I perused the university Java books for weeks, whenever I find the time to go to the university.  I was learning more about programming in Java, until I get to almost three-quarters of the pages of one Java book.

I was having a hard time understanding what is happening in the code that was using swing components, especially the statements that were using more than one dot operators.  Of course, it would help if I was coding the book code in a Java IDE.  But I didn’t have a computer and the website that I was using to compile Java programs online does not allow using GUI or applet codes. Here’s an example of that complex code:

import java.awt.*;
import javax.swing.*;

public class Myclass
{
   public static void main(String[] args)
   {
      JFrame frame = new JFrame();
      frame.getContentPane().add(new JPanel(new FlowLayout(FlowLayout.LEFT)));
      frame.getContentPane().add(new JPanel(new FlowLayout(FlowLayout.CENTER)));
      frame.setSize(500, 500);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}

The above is a complete program that displays a Java GUI window. The window has nothing on it except the close button, the minimize button, and the maximize button.

For the code to display the GUI window, the code started by importing the AWT and Swing packages from the Java Standard Library.  The window is implemented by the class definition.  The class main method first creates the window object, adds panels to it, and modifies it further. The window is then made visible to the user.

Understanding the OOP paradigm is the key to programming in Java.  Java is the first language that is purely object oriented.  It is purely object oriented because you cannot make a Java program unless you make a class.  And classes are the stuff that objects are made of.

The dot operators used in the statements are actually member operators. Member operators accessed the class fields and the class methods that objects have encapsulated.  There is actually another notation for a class member operator, but that is used only in C++.

Happy Thanksgiving!

John Sindayen

Sunday, November 23, 2014

PROGRAMMER

PROGRAMMER

February 2013. About this time, I run my first program. It was a Java program, of course, because it was the only programming language I was trying to learn at this time, in preparation for my going back to school.

I was using the public library computers. Since the public library does not allow downloading anything into their computers, I used an online compiler to compile my programs.

I actually compiled a lot of Java programs, because I was just learning to be a programmer. I was using the compiler to know if I really know what I have just learned about Java from reading the public library books.

I wrote the programs all by myself without the aid of the Java books I was learning from, because there was not enough room to put them in the library's Computer Room. There were 20 computers, and everyone of them is almost always being used in that small room.

One of my earliest Java program is still accessible in the online compiler website. I have written my name on the program as shown here:

/**
     This is a documentation of my first program.
     It will display the copyright of the program.
*/

class cdocumentation {
     public static void main(String args[]) {
          System.out.print("\tThis program is written by John Sindayen.\n");
          int vyear = 2013;
          System.out.println("\t\t\t\bCopyright " + vyear);
          }}
/*
This is the end of the program.
*/

During this early period, I have already shown some good programming practices. Here are some:
a. Documentation
b. Use of indentations and spaces

Some good programming practices that needed to be applied in this early program are these:
a. Uppercase first letter for a Java class name.
b. Conventionally, array brackets should be placed beside the String data type.
c. Ending braces should be in different lines to make it more readable.

Finally, here are some of the prolific programmers who has invented their own programming language, or otherwise known for something famous.

John Backus created ForTran, short for Formula Translation, a scientific programming language.
Alan Cooper created Visual Basic programming language.
Brad Cox created Objective-C object oriented programming language.
Brendan Eich created JavaScript web scripting language.
James Gosling created Java, the first pure object oriented programming language.
Grace Hopper created COBOL, short for Common Business Oriented Language, a business programming language. She became a U.S. Navy admiral.
Toru Iwatani created the Pac-Man game.
Rasmus Lerdorf created PHP client-server based web scripting language.
Ada Lovelace is the first known programmer.  She programmed Charles Babbage's mechanical computer. She is otherwise known as the Countess of Lovelace.
Yukihiro Matsumoto created Ruby object oriented programming language.
Markus Persson created the Minecraft game.
Dennis Ritchie created C, the ancestor of many modern programming languages.
Guido van Rossum created Python, an all object type programming language.
Bjarne Stroustrup created C++, the first language to implement object oriented programming.
Linus Torvalds created Linux open source operating system.
Niklaus Wirth created Pascal procedural programming language.

Welcome to the club!

John Sindayen

References:
http://en.wikipedia.org/wiki/List_of_programmers
http://stackoverflow.com/questions/26776008/does-python-data-type-are-all-object-based-on-c-class

Thursday, November 20, 2014

COMPUTER

KNIGHT IN PROGRAMMING : JOURNEY TO PROGRAMMING

COMPUTER

This blog is about my journey to programming, in various language cultures and platform environments. Here we go!

January 2013. I saw my first Java program, in a public library book. It was one of those high school programming book. It was titled "Be Prepared for the AP Computer Science Exam in Java."

I thought the book was for me since it's written for high school students. I read every pages, starting from the softbound cover until I got to a program solving for a Fibonacci series using recursion, some 200 pages later.

There were two function calls of the same recursive function in the return statement. I couldn't figure out why the output is such after doing several walk-through of the resulting output.* The two calls to the recursive function was throwing me out of the loop. So back to square one! Start from scratch!

I then started to read books that actually teaches Java to a beginner. The AP book was actually not a book for learning Java. It was a book for reviewing what you know of Java, by reminding you of what you need to know before doing its exams at the end of each sections and chapters, all of which were to write the output of a code segment or to test your programming concept.

Why do I have to learn Java? I was planning to enroll in the VRAP program available to U.S. Armed Forces veterans. And I have always been interested with computers. So I planned to major in Computer Programming. It was either that or Culinary Arts, but I wasn't interested in making a career of cooking. I'd rather cook codes than cook cookies! ;-)

Using the beginner books, I learned how to code the traditional first program for programmers, Hello World!, in Java.

Python is actually the easiest first programming language for a beginner because it only takes one line of code to program Hello World. This:

print(“Hello World!”)

Save that single statement from IDLE IDE, name the file, and every time you run the program from IDLE, the Python interactive mode responds with >>>Hello World!

Anyway, I think learning about binary code, is actually the first thing one should learn if one is to be a professional computer programmer.  So here is a binary code instruction that a specific computer architecture is able to execute:

10001100011010000000000001000100

Binary code is very much computer architecture dependent. You need to design your computer to accept your machine language in order for your computer to execute your code. This particular code is specific to MIPS computer architecture using 32 bits long instructions. This particular instruction tells the computer to load a value into register 8 by referencing the address stored in register 3.

This sounds like a lot of technical stuff. It is! But learning to be a good computer programmer takes a lot of learning. It takes at least 10,000 hours of coding and learning before you can consider yourself an expert. It’ll probably be much easier to earn your wings in the Air Force than to be an expert computer programmer. ;-)

But always starting from the beginning helps! Knowing your PC, CPU, ALU, CU, I/O storage devices, RAM, ROM, bits, bytes, and words before learning your programming syntaxes and semantics will go a long way into understanding computers.

Happy computing!

John Sindayen

*After learning from the beginning books, I was able to reproduce the same output as the correct answer in my walk-through of the output.

References:
http://en.wikipedia.org/wiki/Machine_code
http://www.amazon.com/Prepared-Computer-Science-Exam-Java/dp/0965485358
http://crunchify.com/write-java-program-to-print-fibonacci-series-upto-n-number/