Blog
What is a constructor in java?
29/03/2013 07:16Generally constructors are those functions which have same function name as class name.They are also called as special types of fuinctions used to initialize the objects.However there are some restrictions in constructors which are not there in an ordiary functions.They are as follows
- Only values can be assigned in constructors.Neither we can not perform any mathematical operations nor use any kind of print,for etc statements
- In one of my program I had defined 4 methods of defining function.One of that method was using "return" keyword but in constructors we cant use any return keyword
On the bsis of this we can differentiate between constructors and functions
Constructors | Functions |
|
|
To print whether the number is perfect or not
18/03/2013 08:27
public class perfectnumbers
{
public static void main(String args[])
{
int a=6;
int sum=0;
for(int i=1;i<a;i++)
{
if(a%i==0)
{
sum=sum+i;
}
}
if(sum==a)
System.out.println("the number is perfect");
else
System.out.println("the number is imperfect");
}
}
To print the sum of first 20 natural numbers
18/03/2013 08:26
public class adding20naturalnumbers
{
public static void main(String args[])
{
int sum=0;
for(int i=1;i<=20;i++)
{
sum=sum+i;
System.out.println(sum);
}
}
}
To print squares of 10 numbers from one to ten
01/01/2002 08:24
public class squaresofnumbers
{
public static void main(String args[])
{
for(int i=1;i<=10;i++)
System.out.println(i*i);
}
}
To check whether the number is prime or composite
18/01/2013 08:24
public class primecomposite
{
public static void main(String args[])
{
int n=56;
int c=0;
for(int a=1;a<=n;a++)
{
if(n%a==0)
c++;
}
if(c==2)
System.out.println("the number is prime");
else
System.out.println("the number is composite");
}
}
Calculate discount on the basis of sales
01/01/2002 08:22
public class calculatingdiscountsonsales
{
public static void main(String args[])
{
int sales=2000;
double discount=0;
if(sales>15000)
{
discount=0.3;
double prize=sales*discount;
System.out.println(prize);
System.out.println("discount is 30 percent");
}
if(sales>10000&&sales<15000)
{
discount=0.2;
double prize=sales*discount;
System.out.println(prize);
System.out.println("discount is 20 percent");
}
if(sales>1000&&sales<10000)
{
discount=0.1;
double prize=sales*discount;
System.out.println(prize);
System.out.println("discount is 10 percent");
}
}
}
Print whether the number is +ve or -ve
01/01/2002 08:22
public class postivenegative
{
public static void main(String args[])
{
int a=-90;
if(a>0)
System.out.println("the number is +ve");
if(a<0)
System.out.println("the number is -ve");
else
System.out.println("the number is 0");
}
}
Print which number is largest
01/01/2002 08:19
public class printinglargeorsmallnumbers
{
public static void main(String args[])
{
int n1=95;
int n2=95;
{
if(n1>n2)
System.out.println("1st is largest");
if(n2>n1)
System.out.println("2nd is largest");
if(n2==n1)
System.out.println("both are same");
}
}
}
Print whether the number is odd or even
01/01/2002 08:17
public class oddeven
{
public static void main(String args[])
{
int a=53;
if(a%2==0)
System.out.println("the number is even");
else
System.out.println("the number is odd");
}
}
Generate table of any number
01/01/2002 08:16
public class generatingtables
{
public static void main(String args[])
{
int x=95;
for (int a=1;a<=10;a=a+1)
{
int y=x*a;
System.out.println(y);
}
}
}