Java4teachus

Saturday, March 23, 2019

Define Constructor and Default Constructor in java

Constructor in Java

Constructor is one of the distinguish programming functions in Java programming.The purpose of constructor is to "Initialize the object."


Define Constructor and Different Type Of Constructor in Java.jpeg

Definition

A constructor is one of the special member method which it is automatically implicit called by JVM during the object creation and whose role is to place your own values without placing default values.

Advantages of constructor

When we use constructor at the time of writing Java application then we get following advantages.

1) It eliminates in placing default values.
2) It eliminates in calling ordinary methods.

Characters of constructor

 If we want to use a constructor as a part of Java program then we have follow properties.

1) Constructor will be called by the JVM implicitly when the object is created.

2) Constructor name is most similar to class name.
3) Constructor will not have any return type even void also ( if we write any return type then it will be treated as "Ordinary Method".)
4) Constructor should not be static ( because of constructor calling each & every time when an object is created ).
5) Constructor of java never participates in inheritance process (Because every constructor is meant for initializing with their own data member but not data member of another class.
6) 
a)If the access modifier of the constructor is private then an object of an corresponding class can be created in the class context but not in other class context.
b) If the access modifier of the constructor not private then an object of the corresponding class can 
be created both class context and in another class context.

Saturday, March 16, 2019

Control Structure of Java Program

Q .Write a Java program which will accept and print the Number within the given range.
Program:- 

class NumGen
{
int n;
void set(int x)
{
n=x;
}
void generate()
{
if (n<=0)
{
System,out.println(n+"This is invalid input Please Enter Positive Number");
}
else
{
System,out.println("Number within"+n);
for (int i=1; i<=n ; i++)
{
System.out.println(+i);
}
}
}
}
class NumGenDemo
{
public static void main(String[] k)
{
if (k.length!=1)
{
System.out.println("Plz Enter The One number");
}
else
{
int x=Integer.parseInt(k[0]);
NumGen ng=new NumGen();
ng.set(x);
ng.generate();
}
}
}



➧ In the above image we have to compile the java program by using the Javac Tool with the class name called NumGenDemo
    Ex:- Javac NumGemDemo.java
➧ After Compile the java program then execute the program by using the Java Tool and also give the values thought cmd prompt.
    Ex:- Java NumGenDemo -12
➧ Here -12 This is the invalid input please Enter The Positive Number.
➧ Again execute and and give the positive number.

Q .Write a Java program which will accept and print the Number Even Numbers and odd numbers .
Program:-

class EvenOdd
{
int n;
void set(int x)
{
n=x;
}
void findEvenOdd()
{
if (n<=0)
{
System.out.println(n+"Please Enter the Positive Number")
}
else
{
System.out.println("Even Number");
for (int i=2;i<=n ;i++ )
{
System.out.println(i);
}
System.out.println("Even Number");
for (int i=1;i<=n ;i++ )
{
System.out.println(i);
}
}
}
}
class  EvenOddDemo
{
public static void main(String[] k) 
{
if (k.length!=1)
{
System.out.println("Please Enter the One Number");
}
else
{
int x=Integer.pasrInt(k[0]);
EvenOdd e=new EvenOdd();
e.set(x);
e.findEvenOdd();
}
}
}