Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

import java.util.

*;
public class Const1
{float x,y;
float avg;

public Const1()// default constructor


{
x=0.0f;
y=0.f;
avg=0.0f;

}
public Const1(float a,float b)//parameterised constructor
{
x=a;
y=b;
avg=0.0f;

public void printAvg()


{
avg=(x+y)/2;
System.out.println("average of "+x+" and "+y+" is "+avg);
}
public static void main()
{Scanner sc =new Scanner(System.in);
System.out.println("enter first number");
float a=sc.nextFloat();
System.out.println("enter second number");
float b=sc.nextFloat();
Const1 ob1=new Const1();//caliing default constructor to create object ob1
Const1 ob2=new Const1(a,b);//caliing parameterised constructor to create
object ob2
ob1.printAvg();
ob2.printAvg();
}
}

You might also like