Constructor Program in java

Write a program in java to demonstrate the Constructor & Constructor Overloading

 
public class Result 
{
 int sub1,sub2,sub3;
 Result()
 {
  System.out.println("Default.....Constructor.....Call");
  sub1=60;sub2=70;sub3=65;
  System.out.println("Sub-1 :"+sub1);
  System.out.println("Sub-2 :"+sub2);
  System.out.println("Sub-3 :"+sub3);
 }
 Result(int s1,int s2,int s3)
 {
  System.out.println("Parameterazid.....Constructor.....Call");
  sub1=s1;sub2=s2;sub3=s3;
  System.out.println("Sub-1 :"+sub1);
  System.out.println("Sub-2 :"+sub2);
  System.out.println("Sub-3 :"+sub3);
 }
 Result(Result r1)
 {
  System.out.println("Duplicate.....Constructor.....Call");
  sub1=r1.sub1;sub2=r1.sub2;sub3=r1.sub3;
  System.out.println("Sub-1 :"+r1.sub1);
  System.out.println("Sub-2 :"+r1.sub2);
  System.out.println("Sub-3 :"+r1.sub3);
 }
 float per()
 {
  System.out.println("Function.....Call");
  return ((sub1+sub2+sub3)*100)/300;
 }
 public static void main(String[] args)
        {
  Result r=new Result();
  System.out.println("Percentage of 3 Subject is "+r.per());
  System.out.println("");
  
  Result r1=new Result(70,80,90);
  System.out.println("Percentage of 3 Subject is "+r1.per());
  System.out.println("");

  Result r2=new Result(r1);
  System.out.println("Percentage of 3 Subject is "+r2.per());
 }
}

Output

0 comments:

Post a Comment