Using Constructors & Member Function to add two numbers: Java

In this Java video tutorial we have covered both ways of adding numbers: using constructors as well as using methods.

Constructors:

Constructor name and the class name must be same.
No return type.
There are 3 types of constructors:
1. Default constructors.
2. Parameterized constructors.
3. Copy Constructors.

In this video tutorials we have shown the use of Parameterized constructors.

calc(int a, int b)
{
no1 = a;  
no2 = b;  
}

Adding and displaying the result:

public void sum()
{
System.out.println("Sum of "+no1+" and "+no2+" is "+(no1+no2));
}

Strings are written inside double quotes and the variables written outside the double quotes will display the value stored in them.
+ (plus) is concatenation operation.
(no1+no2) adds the value present in no1 and no2 and displays the result of addition.

Video Tutorial


[youtube https://www.youtube.com/watch?v=gUXq1psurdk]

YouTube Link: https://www.youtube.com/watch?v=gUXq1psurdk [Watch the Video In Full Screen.]



Object Creation:

/* calc c; // object declaration. It will have null in it.
   c = new calc(20,10); // Construction call
*/ 
   calc c = new calc(30, 10);

Full Java code: Using Constructors & Member Function to add two numbers(add.java)

import java.lang.*;
 
class calc
{
int no1, no2;
 
calc(int a, int b)
{
no1 = a;  // 30
no2 = b;  // 10
}
 
/*
public void assign(int a, int b)
{
no1 = a;  // 30
                no2 = b;  // 10
}
*/ 
public void sum()
{
System.out.println("Sum of "+no1+" and "+no2+" is "+(no1+no2));
}
}
 
class add
{
public static void main(String args[])
{
/* calc c; // object declaration. It will have null in it.
c = new calc(20,10); // Construction call
*/ 
calc c = new calc(30, 10);
//c.assign(30,10);
c.sum();
}
}

Member functions are called with the help of object.
Syntax:
object.memberfuntion();

PHP Comments, String Concatenation, Addition of Numbers & Hello World

Today lets see these 4 basic things:
1. The classic “Hello World” program.
2. Comment system in PHP.
3. Concatenation of Strings.
4. Addition of Numbers.



Classic “Hello World” program
Program to output simple string

1
2
3
<?php
          echo "Hello World";
?>

Comments:

1
// This is single line comment
1
# This is also a single line comment
1
2
3
/* This is how multi-line comments
    are written.
*/

Anything following a double forward slash or a hash( # ) symbol will be ignored by your parser. Comments are very important and are often ignored by programmers. Comments come handy when we come back to the codes and want to make some changes to the coding. So its always a good practice to write comments wherever appropriate.

Concatenation of Strings
Concatination is joining of two or more strings end to end. Ex: if we concatenate Micro and soft, it will become Microsoft.
In PHP, we use .[DOT] operator for concatenation. Below is an example:

1
2
3
4
<?php 
         echo "Micro" . "soft";
         echo "<br />Oracle, " . "Sun Microsystems";
?>

The output for the above php code is:

1
2
Microsoft
Oracle, Sun Microsystems

Addition of Numbers
We can use variables for addition of numbers or we can simply make use of echo or print for addition of numbers.

1
2
3
<?php
         echo 10+20;
?>

This gives 30 as the output.

1
2
3
<?php
         echo "5"+10;
?>

This gives 15 as output. The string “5” will be implicitly converted into number and will be added to the actual number 10.