-->

Monday, March 3, 2014

Increment and Decrement Operators in Java Programming

Earlier article was about the operators in java with some of the binary operators, example of each. This article is about plus (+) operator and increment/decrement operator in brief.

Operator + with strings

Programmer have used the operator ‘+’ with numbers. When you use + with numbers, the result is also a number. However, if you use operator + with strings, it concatenates them for example:

(5 + 6) results in to 11.
(“5” + “6”) results in to “56”.
(“17” + “A, V. Vihar”) result in to “17 A, V. Vihar”
(“abc” + “123”) results in to “abc 123”
(“” + 5 + “xyz”) results in to “5xyz”
(“” + 5) results in to “5”

(In above two expressions Java would internally convert 5 in to “5” first and then concatenate with “xyz” and “” respectively.)

Increment/Decrement Operators (++, --)

Java includes two useful operators not generally found in other computer languages (expect C and C++). These are the increment and decrement operators, ++ and --. The operators ++ adds 1 to its operand, and – subtracts one.

In other words,
a = a + 1;        same as   ++a ; or a++;
And
a = a – 1          same as --a ; or a --;

However, both the increment and decrement comes into two varieties: they may either precede of=r follow the operand. The prefix version comes before the operand (as in ++a or --a) and the postfix version comes after the operand (as in a++ or a--). The two version have the same effect upon the operand, but they differ when they take place in an expression, that would be discussed in later articles.

About the Default Create Action Methods in MVC Controller

Earlier article was about the default index action of the controller, which in other words used to list the records from the database created in MVC application. Except this list (Index) action, all other actions are of two types i.e. Get and Post, having particular motive in the programming.

According to name, Get method is used to get a particular model and return to the view that will show that model. The below code will return the blank model of Student class (Controller). User will enter particular information in the textboxes shown on the form and click on submit button.

// GET: /Student/Create
public ActionResult Create()
{
return View();
}

After clicking on the submit button, compiler goes to the post method of the same action i.e. Post Create action, as written below:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Student student)
{
if (ModelState.IsValid)
{
db.Students.Add(student);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(student);
}

The first two line of this code are attributes used to tell the compiler about this method i.e. this is post method and validates fields on submit method. The parameter is of type student means the view is of Student type (having all the fields of Student class) as shown some lines of the create view:

@model MvcApplication1.Models.Student
@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>
……………….
……………….
……………….

This view is strongly typed view because of the first line and have all the fields of student class (only name is shown here, remaining are something like this).

So, turn back on our previous discussion that was about the post Create action. This action will then check the model is valid or not (Validation). If valid then it will add this new student in the student table and then save the changes to the database. If not it will return to the same page with the errors generated.

The last line will return to the same action i.e. Create with the model passed as parameter and of course validation errors, you can check this to not enter some fields in the form and submit the data. The next will be to edit the student.

C program to read and display a polynomial using Linked List

C program to read and display a polynomial using Linked List:
#inculde<stdio.h>
#inculde<stdlib.h>
#inculde<conio.h>
struct node
{
int coeft;
int degree;
struct node *link;
};
typedef struct node poly;
main()
{
poly *root,*temp,*new;
int hdegree, coeft;
root=NULL;
clrscr();
printf(“Enter the highest degree of polynomial:”)
scanf(“%d”,&hdegree);
while(hdegree>=0)
{
printf(“Enter coefficient of variable with degree %d”,hdegree);
scanf(“%d”,&coeft);
if(coeft!=0)
{
new=(poly*)malloc(sizeof(poly));
if(new= =NULL)
{
printf(“Memory allocation error…”); exit(0);
}
new->coeft=coeft;
new->degree=hdegree;
new->link=NULL;
if(root= =NULL)
{
root=new;
temp=root;
}
else
{
temp->link=new;
temp=new;
}
}
hdegree--;
}
clrscr();
printf(“\n The Polynomial is:\n\n”);
temp=root;
while(temp!=NULL)
{
if(temp->coeft>0)
 printf(“+%dx%d”,temp->coeft,temp->degree);
else
  printf(“%dx%d”,temp->coeft,temp->degree);
temp=temp->link;
}
 getch();
}

Subtraction assignment operator in c# programming

This is type of binary operator, which means it works with more than one operand. In c#, this can be understood from given code
int a=10,b=5;
a -=b;
Console.WriteLine(a);
In this preceding code, we get 5 as a result. It means subtract operand b from a. This types of expression is equivalent to a=a-b. Lets take an simple example with output

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 10;
            int b = 20;
            b -= a;
            Console.WriteLine(b);
         
            Console.ReadKey();

        }
    }
}

Code generate the following output

Subtraction assignment operator in c# programming

Date of weeks in C# programming

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>
<!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>Total weeks</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Total Weeks" 
            onclick="Button1_Click" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

// Code behind class

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

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       //initialize the datetime class instance 
        DateTime currentdate = new DateTime();

        Label1.Text = "current date : " + currentdate.ToLongDateString() +"<br/>";

        Label1.Text += "Week Date ";
        Label1.Text += currentdate.DayOfWeek.ToString()+"<br/>";


        int dayOfWeek = (int)currentdate.DayOfWeek;

        Label1.Text += "Day of week ";
        Label1.Text += dayOfWeek; 
    }
}

Code generate the following output

Date of weeks in C# programming

Sunday, March 2, 2014

Polynomial representation using Linked List for Data Structure in 'C'

Polynomial representation using Linked List

The linked list can be used to represent a polynomial of any degree. Simply the information field is changed according to the number of variables used in the polynomial. If a single variable is used in the polynomial the information field of the node contains two parts: one for coefficient of variable and the other for degree of variable. Let us consider an example to represent a polynomial using linked list as follows:
Polynomial:      3x3-4x2+2x-9
Linked List:

In the above linked list, the external pointer ‘ROOT’ point to the first node of the linked list. The first node of the linked list contains the information about the variable with the highest degree. The first node points to the next node with next lowest degree of the variable.
Representation of a polynomial using the linked list is beneficial when the operations on the polynomial like addition and subtractions are performed. The resulting polynomial can also be traversed very easily to display the polynomial.







The above two linked lists represent the polynomials,3x3-4x2+2x-9 and 5x3-2x2+6x+3 respectively. If both the polynomials are added then the resulting linked will be:




The linked list pointer ROOT gives the representation for polynomial, 8x3-6x2+8x-6.

C Program to read and display a polynomial using linked list .

Operators in expression, C# programming

There are various types of operators, we use in expression. Where we use multiple arithmetic operator known as expression. Your arithmetic expression is
 d=a+b*c;
In this expression, we used three operator, such as assignment operator, arithmetic positive, and  arithmetic multiplication operator. Also we used four operands, such as a,b,c and d operands. So we use variety of operator for designing expression for user query. Some of operator we use, like
Mathematical operators are
+, -  :  Unary positive, negative
+ : Addition
- : Subtraction
* : Multiplication
/ : Division
% : reminder

If you use more than one operator in any expression, evaluate expression according to operator precedence also evaluate expression left to right according to precedence rule.
Now, just take an simple precedence of mathematical operator are:

Unary + and - (I)

* and /  (II)

+ and - (III)

In above mentioned expression ( d=a+b*c ), first calculate multiplication between operand b and c, after that result of the multiplication will be added with operand a. If you want to override the default precedence, use parentheses around the portion of the expression that is to be evaluated first. In some other expression, where we use string datatype then we use + operator for string concatenation. So we can say that '+' operator is used in addition as well as string concatenation.
© Copyright 2013 Computer Programming | All Right Reserved