-->

Tuesday, December 17, 2013

How to Use Properties Window to Change Control’s attribute: Java Programming

The controls/objects that a programmer draw on frame/window have some properties associated with them. Each control have its own properties with some common to other controls, which NetBeans enables to change.

The Properties Window provides an easy way to set properties for all objects in a frame/window. To open the Properties Window (if it is not open), choose the Properties command from the Window menu. You may also press the shortcut key for it which is: Control + Shift + 7.

 How to Use Properties Window to Change Control’s attribute: Java Programming

A property can be of any type such as integer, string etc. These can be set by either a simple textbox or a dropdown list. The whole thing is properties window consist of the following elements:

  • Title box Displays the name of the object for which you can set properties.
  • Properties list The left column under Properties Tab, displays all of the properties for the selected object. You can edit and view settings in the right column.

As in previous article we have name a control using inspector window, now the same thing can be done through this properties window in following simple steps.

  • In the Properties window, from the Properties list, select the name of a property.
  • Type in the right column, or click more to type or select the new property setting.

The below image shows the operation of name a control by properties window. The next article will cover up about some components, events and listeners mostly used in java programming.

 How to Use Properties Window to Change Control’s attribute: Java Programming


What are Naming Conventions in NetBeans: Java Programming

Object Naming Conventions, the process of giving a name to the control, basically used to access a control in coding part of programming language. Java NetBeans IDE have its own rules, described in the article, to name a control while programming.

A control's name is one of its most important attributes because you literally refer to a control by its name whenever you want it to do something. Names are so important that every time you put a control on your form, NetBeans IDE automatically gives a name to it. If programmer add a jButton, NetBeans IDE names it jButton1; if you add a jTextField, it automatically named jTextField1.

However, naming controls like this, may be confusing. While naming controls, you need to take care of these things.
  • Must begin with a letter.
  • Must contain only letters, numbers, and the underscore character (_); punctuation characters and spaces are not allowed.
  • Omit the initial letter "J" in object names and you may add its type at the end of it, like ReCalculateCheckBox for an object of class JCheckBox (not ReCalculateCheckBox!). It is just a recommendation, not a rule laid out by Java.

Friendly Names

When naming is a control, the first letter of the friendly name is generally uppercase. This make it easier to read the control’s name, because you can easily differentiate between the friendly name and the control’s abbreviation e.g. ReadonlyCheckBox.  

Name a control

To name a control in NetBeans, double click the control to be named in the Inspector window and type a new name.

What are Naming Conventions in NetBeans: Java Programming

Alternatively, you can right-click on the control’s name in Inspector window or the control itself in design space and select Change Variable Name………. command. The same process can be done through properties pane. Before you actually start working with controls, you should also know how to setup properties for a control and also about event and listeners. Later articles will describe these with examples.

Change name using Properties Window

How to Perform Actions on Window Controls In NetBeans: Java Programming

There are many controls in the palette tab of NetBeans IDE having specific properties of its own. Those controls can easily be added, resize and delete from the frame, while programming in java. This article have some steps to do these tasks in simple way.

To draw a control on Frame/Window in NetBeans


  • Click the desire control’s icon on the Palette. (Say, for example, we want to draw a label. For this we shall first click at its icon on the palette.)
  • Now drag it to desired location in your Frame/Window.
  • Se the control that you selected, now appear on your frame/window.

How to Perform Actions on Window Controls In NetBeans: Java Programming

When you can add controls to your frame, you might find strange behavior of IDE regarding component positioning and sizing. (This is because of Free Design layout manager.) In order to have full control on component's positioning and sizing, you need to know about Layout Managers. The layout of components on a frame (or panel) is controlled by a layout manager, which determines the final placement of each component. Layout Managers will be covered in next articles.

To Remove a Control from the Frame/window in NetBeans


  • Select the control to be deleted, by clicking it. See, the controls appear selected, notice the rectangular boxes at corners and sides.
  • Press Del (or Delete) key. The selected control gets removed.

Another way of removing a control is:

  • Select it
  • Right-click on it (i.e., after selecting click the right mouse-key).
  • A context menu paper. Select delete command from it.

How to Perform Actions on Window Controls In NetBeans: Java Programming

To Move/Resize a control in NetBeans

To move a control you have drawn, click the object in the middle and drag it to the new location. Now release the mouse button.
To resize a control, select it first and then use its sizing handle to resize it. That is, move the mouse pointer over sizing handles. The mouse pointer will change to double-headed arrow (↔). See the fig. Now drag the sizing handle to desired new position and control will be resized.

While adding, laying out and resizing controls on the frame, if you are not happy with the default behavior of rearranging/resizing of the control, then you may done one thing. Before you put any controls on the form, right click your frame in Design View and select Set Layout. From the sub menu that appears, select Absolute Layout.

How to Retrieve Records containing Null Values: SQL Programming

In Sql programming, when programmer want to retrieve records that have null values in their columns. To get such type of records, sql queries must have NULL operator in the where clause.

A NULL value in a column implies that the data value for the column is not available. You might be required to find records that contain null values or records that do not contain NULL values in a particular column. In such a case, you can use the unknown_value_operator in your sql queries.

The syntax of using the unknown_value_operator in the SELECT query is:

SELECT column_list
FROM table_name
WHERE column_name unknown_value_operator

Where

  • Column_list: list of fields to be shown in output.
  • Table_name: from the records are to be retrieved.
  • unknown_value_operator is either the keyword IS NULL or IS NOT NULL.

The following SQL query retrieves only those rows from the EmployeeDepartmentHistory table for which value in the EndDate column is NULL.

SELECT BusinessEntityID, EndDate FROM
HumanResources.EmployeeDepartmentHistory WHERE EndDate IS NULL

There are few records falling in this category, shown in the output.

How to Retrieve Records containing Null Values: SQL Programming

Consider its opposite case, where some records have some date in the EndDate column.

SELECT BusinessEntityID, EndDate FROM
HumanResources.EmployeeDepartmentHistory WHERE EndDate IS NOT NULL

It will shows few records as shown in the output.

How to Retrieve Records containing Null Values: SQL Programming

How to Retrieve Records that Matches a Pattern: SQL Programming

When retrieving data in sql programming, you can view selected rows that match a specific pattern. For example, to create a report that displays all the product names of Adventure Works beginning with the letter P. This task can be done through the LIKE keyword.

The LIKE keyword is used to search a string by using wildcards. Wildcards are special characters, such as * and %. These characters are used to match patterns and some of them are described with example, mostly used by sql server:

  • %    Represents any string of zero or more character(s)
  • _    Represents a single character
  • []    Represents any single character within the special range
  • [^]    Represents any single character not within the specified range

The LIKE keyword matches the given character string with the specified pattern. The pattern can include combination of wildcard characters and regular characters. While performing a pattern match, regular characters must match the characters specified in the character string. However, wildcard characters are matched with fragments of the character string.

The following SQL query retrieves records from the Department table where the values of Name column begin with ‘Pro’. You need to use the ‘%’ wildcard character for this query.

SELECT * FROM HumanResources.Department WHERE Name LIKE 'Pro%'

Output: Shows all the records satisfy the given condition i.e. starting with Pro

How to Retrieve Records that Matches a Pattern: SQL Programming

The following SQL query retrieves the rows from the Department table in which the department name is five characters long and begins with ‘Sale’, whereas the fifth character can be anything. For this, you need to use the '_wildcard character.

SELECT * FROM HumanResources.Department WHERE Name LIKE 'Sale_'

Output: Shows all the records satisfy the given condition i.e. starting with having sale with single character.

How to Retrieve Records that Matches a Pattern: SQL Programming

Monday, December 16, 2013

While Loop with Example in C language

A set of statements may have to be repeatedly executed till a certain condition is reached, in every computer programming. In such situations (C language), we do not know exactly how many times a set of statements have to be repeated. So, naturally we need to have a condition controlled loop like in looping statements. In C language, the condition controlling is done generally by a while statement or while loop. It is also called test and do structure.

Syntax
while (exp)
{
  statement1;
  statement2;
  .
  .
  .
statementn;
}
Where

  • while -  is a reserve word or keyword
  • exp – is the expression which is evaluated to TRUE or FALSE.

Working of the while loop: 

The following sequences are carried out after executing the statements that appear just before the while loop

  • If the expression exp is evaluated to FALSE at the time of entry into the loop, the control comes out of the loop without executing the body of the loop. Later, the statements after the while loop are executed.
  • If the expression exp is evaluated to TRUE, the body of the loop is executed. After executing the body of the loop, control goes back to the beginning of the while loop and exp is again checked for TRUE or FALSE.
  • Thus, the body of the while loop is repeatedly executed as long as exp is evaluated TRUE. Once the expression exp is evaluated FALSE, the control comes out of the while loop and the statements which after the while loops are executed.

As the expression exp is evaluated to TRUE or FALSE in the beginning of the while loop, the loop is also called entry controlled or top testing loop.

If a relational expression is evaluated to TRUE, the result is 1. If a relational expression is evaluated to FALSE, the result is 0. If an arithmetic/logical/bitwise or any other expression is evaluated to non-zero value (for example 10, 999, etc) then the condition is TRUE. After evaluation, if the result is zero (0), then the condition is FALSE.

Some special forms of while loop are shown in table below:

While Loop with Example in C: Computer Programming

Algorithm: to reverse a given number

Step1:    [Input the number]
    Read : N
Step2:    [Initialize reverse number rev to 0]
    Rev = 0
Step3:    [Separate and reverse the number]
    While N !=0
        digit = N%10
        N = N/10
        Rev = rev*10+digit
    [End of while]
Step4:    [Output the reversed number]
    Write: rev
Step5:    Exit

Let N is 123, which is the give number to be reversed. In step1, N will be 12. In step2, N will be 1 and finally in step3, N is zero after separating a digit each time. So, the above three statements should be executed as long as N is not zero. Also, in step1, note that initial value of reverse is 0 and at the end of final step, reverse is 321.

Step by step Analysis
Step1:   
    Obtain 1st digit     =    123%10     = 3
    Reverse         =    0*10+3      = 3
    Separate 1st digit    =    123/10      = 12
Step2:   
    Obtain 2nd digit     =    12%10         = 2
    Reverse         =    3*10+2      = 32
    Separate 1st digit    =    12/10          = 1
Step3:   
    Obtain 3rd digit     =    1%10         = 0
    Reverse         =    32*10+1      = 321
    Separate 3rd digit    =    1/10          = 0
While Loop with Example in C: Computer Programming

C Program
main()
{
int n,revnum=0,digit;
printf("Enter any integer number here:\n");
scanf("%d",&n);
while(n!=0)
{
digit=n%10;
revnum=revnum*10+digit;
   n=n/10;
}
printf("The reverse number of %d is %d",n,revnum);
}

While Loop with Example in C: Computer Programming
The output is exactly the reverse no of input by the user.

Do-While loop in C

Computer Programming : Nullable types, Null coalescing operator in c# Programming

In Computer Programming, C# Data types are divided into two broad categories such as Value types and Reference type. In Value types, some inbuilt types such as int, float, double, structs, enum etc. These all are non-nullable types, it means these types don't hold null value. In other hand Reference types such as string, Interface, Class, delegates, arrays etc. can hold null value.
By default value types are non nullable. To make them nullable use, programmer have to use ?
  • int a=0   a is non nullable, so a cannot be set to null, a=null will generate compiler error
  • int ?b=0  b is nullable int, so b=null is legal

Operator Need

If programmer want to assign null value to non-nullable data types then there are some operators explained with an example. 

Lets take an simple example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a;
            int? b = 100;
            a = b ?? 0;
            System.Console.WriteLine(a);
            Console.ReadKey();
        }
    }
}
Computer Programming : Nullable types, Null coalescing operator in c# Programming

Output show that if b hold some value like 100 then Null coalescing operator assign value to variable (a). If b hold null value then Null coalescing operator assign default value, which is zero to variable a. 

Replace the declaration of variable b with the following line:
            int? b = Null;
This will outputs zero (default value).
© Copyright 2013 Computer Programming | All Right Reserved