I dont understand the program below. I have mentioned the two errors I have encountered in the code. But I cannot understand the reason
import java.io.*;
class sdata
{
float per;
int m,tm=0,i,n;
sdata(int n)throws Exception
{
DataInputStream dis2=new DataInputStream(System.in);
for(i=1;i<=n;i++)
{
System.out.print("enter marks of subject"+i);
int m=Integer.parseInt(dis2.readLine());
tm=tm+m;
}
per=tm/n;
}
}
class disdata extends sdata
{
//below line gives an error "constructor sdata in class xyz.sdata cannot be applied to given types required:int found:no arguments"
disdata() throws Exception{
System.out.println("TOTAL MARKS OF SUBJECTS:"+tm);
System.out.println("PERCENTAGE OF STUDENT:"+per);
}
}
class sresult
{
public static void main(String args[])throws Exception
{
DataInputStream dis=new DataInputStream(System.in);
int n=Integer.parseInt(dis.readLine());
disdata objj=new disdata();
//the below line creates an error saying "cannot find symbol"
objj.sdata(n);
}
}
sdatais super class ofdisdataand when you are creating a object of disdata with no argument and indisdataconstructor you did not callsdataint constructor so by default it will try to find no argumentsdataconstructor, which is not available, so it gives error.you can do thing call
sdataint constructor fromdisdataconstructor or crate a no argument constructor insdata.