source file name: MarksDemo.java
class Marks
{
int sub1 , sub2;
public Marks(int s1, int s2) // definition constructor
{
sub1=s1;
sub2=s2;
}
}
class Marks1
{
String name;
Marks m;
public Marks1(String nm,int s1, int s2) // definition constructor
{
m=new Marks(s1,s2);
name=nm;
}
public void display()
{
System.out.println("student name "+name+" Marks are: "+m.sub1+" ,"+m.sub2);
}
public void avg()
{
int avg;
avg=(m.sub1+m.sub2)/2;
System.out.println(" Average Marks: "+avg);
}
}
class MarksDemo
{
public static void main(String as[])
{
//Creating objects with constructor
Marks1 ob1=new Marks1("Milind Patil",74,78);
Marks1 ob2=new Marks1("Sanket Shinde",64,70);
ob1.display();
ob1.avg();
ob2.display();
ob2.avg();
}
}
output:
D:\JavaPrograms\Constructors>javac MarksDemo.java
D:\JavaPrograms\Constructors>java MarksDemo
student name Milind Patil Marks are: 74 ,78
Average Marks: 76
student name Sanket Shinde Marks are: 64 ,70
Average Marks: 67
0 Comments
Post a Comment