-->

Thursday, February 13, 2014

Types and Examples of Operators in Java Programming part-1

In Java programming, the operations (specific tasks) are represented by operators and the objects of the operations (s) are referred to as operands.
Java’s rich set of operators comprise of arithmetic, relational, logical and certain other type of operators. Let us discuss these operators in detail.

Arithmetic Operators

To do arithmetic, Java use operators. It provides operators for five basic arithmetic calculations: addition, subtraction, multiplication, division and remainder which are +, -, *, / and % respectively. Each of these operators is a binary operator i.e., it requires two values (operands) to calculate a final answer. A part from these binary operators, Java provides two unary arithmetic operators (that require one operand) also which are unary +, and unary -.

Unary Operators

Operators that act on one operand are referred to us as Unary Operators.

Unary +. The operators unary ‘+’ precedes an operand. The operand (the value on which the operator operates) of the unary + operator must have arithmetic type and the result is the value of the argument.
For example

if a = 5 then +a means 5.
if a = o then +a means 0.
If a = -4 then +a means -4.

Unary-. The operator unary – precedes an operand. The operand of the unary – operator must have arithmetic type and the result is the negation of its operand’s value.
For example

If a = 5 then –a means -5
if a = o then +a means 0. (There is no quantity known as -0)
If a = -4 then +a means -4.

This operators reverse the sing of the operand’s value.

Binary Operators

What is the Scope of Variable in Java Programming?

In Java Programming, an important thing that you must know about variable is their scope. Scope generally refers to the program-region within which a variable is accessible. The board rule is: a variable is accessible with in the set of braces it is declared in e.g.

{
int a ;
:  /* a would be accessible as long as its block (a block is marked with a pair of matching brace) is not closed. Variable a is said to have block scope*/
}

Constants

Often in a program you want to give a name to a constant value. For example you might have fixed tax rate of 0.030 for goods and tax rate of 0.020 for services. These are constants, because their value is not going to change when the program is executed. It is convenient to given these constant a name.

This can be done as follows:

final double TAXRATE = 0.25;
The keyword final makes a variable as constant i.e. whose value cannot be changed during program execution.

Consider the following program that declare and uses two constants.

class calculateTax {
:  {
            final double GOODS_TAX =0.030 ;  
            final double SERVICE_TAX = 0.020 ;
                         …………
         }
     }

The reserved word final tells the compiler that the value will not change in the program. The names of constants follow the same rules as the names for variables. (Programmers sometimes use all capital letters for constants; but that is a matter of personal style, not part of language).      

Once declared constants, their value cannot be modified e.g.; after declaring constant GOODS_TAX, if you issue a statement like:

GOODS_TAX =0.050 ;  //error
It will cause an error, as the value of constants cannot be modified.

Advantage of Constants

They make your program easier to read and check for correctness.
If a constant needs to be changed (for instance a new tax law changes the rates) all you need to do is change the declaration. You don’t have to search through your program for every occurrence of a specific number.

How to use Nested Subqueries in SQL Programming

A subquery can contain one or more subqueries. Subqueries are used when the condition of a query is dependent on the result of another query, which in turn is dependent on the result of another subquery.

Consider an example. You need to view the DepartmentID of an employee whose e-mail address is Sales Agent. To perform this task, you can use the following query:

SELECT DepartmentID FROM HumanResources.EmployeeDepartmentHistory
WHERE BusinessEntityID = /* Level 1 inner query */
(SELECT BusinessEntityID FROM Person.BusinessEntityContact
WHERE ContactTypeID = /* Level 2 inner query */
(SELECT ContactTypeID FROM Person.ContactType WHERE Name= 'Sales Agent')
)

In the preceding example, two queries are nested within another query. The level 2 inner query returns the Contact ID of an employee based on the e-mail address of the employee from the Person table.

The level 1 inner query uses this ContactID to search for the BusinessEntityID of the employee with the given e-mail address. The main query uses the BusinessEntityID returned by level 1 inner query to search for the DepartmentID from the EmployeeDepartmentHistory table.

You can implement subqueries upto 32 levels. However, the number of levels that can be used depends on the memory available on the database server.

How to use Aggregate Functions in SubQuery with Database

While using subqueries, you can also use aggregate functions in the subqueries to generate aggregated values from the inner query. For example, in a manufacturing organization, the management wants to view the sale records of all the items whose sale records are higher than the average sale record of a particular product.

Therefore, the user first needs to obtain the average of a particular product and then find all the records whose sale record exceeds the average value. For this, you can use aggregate functions inside the subquery.

The following example displays the BusinessEntityID of those employees whose vacation hours are greater ta e average vacation hours of employees with title as ‘Marketing Assistant’:

SELECT BusinessEntityID FROM HumanResources.Employee
WHERE VacationHours > (SELECT AVG(VacationHours) FROM HumanResources.Employee
WHERE JobTitle = 'Marketing Assistant')

The output of the subquery that uses the aggregate function is shown in the following figure.

How to use Aggregate Functions in SubQuery with Database

In the preceding example, the inner query returns the average vacation hours of all the employees who are titled as Marketing Assistant. The outer query uses the comparison operator ‘>’ to retrieve the employee ID of all those employees who have vacation hours more than the average vacation hours assigned for a Marketing Assistant.

Wednesday, February 12, 2014

Example of Static and instance class member in c# programming

In my previous article, we have already discussed about difference between both static and non static members. Today we will learn example of both. Static member are not accessible by instance of the class. Look
Example of Static and instance class member in c# programming
 In above snap, you can see that compile time error has been occurred, because static member are not accessible by the instance of the class. Static member are only accessible by the class name. Look
Example of Static and instance class member in c# programming
so common variables are composed static members. While, we need object for calling non-static members.
While, we need object for calling non-static members.

How to add Controls Dynamically in ASP.NET

There are times when it is more practical to create a control at run time than at design time. For example, imagine a search results page in which you want to display results in a table. Because you do not know how many items will be returned, you want to dynamically generate one table row for each returned item.

Note Existing controls can often provide the functionality you get from creating controls dynamically. For example, controls such as the Repeater, DataList and RadioButtonList controls can dynamically create rows or other control elements when the page runs.

To programmatically add a control to a page, there must be a container for the new control. For example, if you are creating table rows, the container is the table. If there is no obvious control to act as container, you can use a Placeholder or Panel web server control.

In some instances, you might want to create both static text and controls. To create static text, you can use either a Literal or a Label web server control. You can then add these controls to the container as you would any other control.

Note : Manipulating controls programmatically is often used when creating composite controls.

To Add a Control to a Web Forms Page Programmatically 

Create an instance of the control and its properties

Label firstname = new Label();
firstname.Text = " Enter First Name";

Controls are typically added to the page during the page's load stage.

Add the New Control to the Controls Collection of a Container Already on the Page

Panel1.Controls.Add(firstname);

Note : Because Controls is a collection, you can use the AddAt  method to place the new control at a specific location- for example, in front of other controls. However, this approach can introduce errors into the page.

The following shows the event handler for the SelectedIndexChanged event of a control called DropDownList1. The handler creates as many label controls as the user has selected from the dropdown list. The labels display the word label plus a counter. The container for the controls is a Placeholder Web server control called Placeholder1.

Note : User input in a Web Forms page can include potentially malicious client script. By default, the web forms page validates that user input does not include script or HTML elements.

Complete Code


           <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            font-size: xx-large;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <span class="style1">How many label , you want to create</span><br />
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            Height="25px" onselectedindexchanged="DropDownList1_SelectedIndexChanged" 
            Width="113px">
            <asp:ListItem>Select</asp:ListItem>
            <asp:ListItem>5</asp:ListItem>
            <asp:ListItem>6</asp:ListItem>
            <asp:ListItem>7</asp:ListItem>
        </asp:DropDownList> 
     
    </div>
    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </form>
</body>
</html>

//Code Behind Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int numbers = Convert.ToInt32(DropDownList1.SelectedItem.Text);
        for (int i = 0; i <=numbers ; i++)
        {
            Label l1 = new Label();
            l1.Text = "Label" + i.ToString();
            l1.ID = "Label" + i.ToString();
            PlaceHolder1.Controls.Add(l1);
            PlaceHolder1.Controls.Add(new LiteralControl("<br/>"));

        }
        
        
    }
}

Code generate the following output

How to add Controls Dynamically in ASP.NET

Tuesday, February 11, 2014

How to Perform Text Interaction with GUI in JAVA Programming part-2

setText()

To be continued from the previous article this method is used for sorting text into a GUI component. In Java Programming, this method is mostly used to store or change text in a text based GUI component. The swing components that support setText()  method include: Text field, Text area, Button, Label, Check Box, and Radio Button.

Suppose you want to change the content of field classTF to ‘XI’ through a code statement; for this you can use setText() method. To change text of classTF field to “XI”, you need to write:

classTF.setText(“XI”)

The setText() changes the value of field before the dot (.) with the string in its parentheses. The value to be set has been given as string constant "XI" in the above statement. You can also use a String value variable to specify the new value, as it is being explained below:

String newValstr = “XI”;
classTF.setText(newValstr);

JOptionPane.showMessageDialog()

The last but not least method for text interaction with GUI is showMethodDialog() which is used to display message in a dialog form.

Using this method, programmer can produce a basic dialog displaying a message to the user. The user will see your message only an "ok" button to close the dialog. To use this method, you need to perform it in two steps:

Firstly, in the Source Editor, where you type your code, at top most position type the following link:
Import javax.swing.JOptionPane;

Now display desired message as per following syntax (please notice carefully the case of letters):
JOptionPane.showMessageDiallog(null,” <desired message here>”);

For example, to display a message “Hello there!” you should write
JoptionPane.showMessageDialloglog(null, “Hello there!”) ;

It will display a separate message dialog displaying your message

© Copyright 2013 Computer Programming | All Right Reserved