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