How to create a parametrized constructor ,with parameters accepting from a accept() function?
For example,
class cons
{
int d;
cons(int a)
{
d=a;
}
void accept(String args[])throws IOException
{
What should I do next?
I want the accept() to accept value from the user, the then to initialize the instance variable with the help of the constructor.So I need to pass the accepted value to the constructor.But how? And I also want to create a main function to create a object and call the function accept().How to do so.I want to use only 1 constructor and not more.
Re: How to create a parametrized constructor ,with parameters accepting from a accept() function?
You can have as many constructor you want in your class. There is no restriction on it.
Re: How to create a parametrized constructor ,with parameters accepting from a accept() function?
Yes I know.but I don't want to create.
Re: How to create a parametrized constructor ,with parameters accepting from a accept() function?
Make accept() static and you can call it with class name directly.
Static functions doesn't require object to call them they can be called using the class name only.
static functions are called like this: className.functionName(functin parameters);
Re: How to create a parametrized constructor ,with parameters accepting from a accept() function?
See here is the question.I have to create the object and call the methods.It is required to call the object by creating object only.
Define a class student described as below:-
Data Members-name,age,m1,m2,m3(marks in 3 sub),maximum,average
Member Methods-
1)A parametrized constructor to initialize the data members
2)To accept the details of a student
3)To compute average and maximum out of 3 marks
4)To display name,age,marks in 3sub,max,avg
Write a main method create an object and call the above method.
Re: How to create a parametrized constructor ,with parameters accepting from a accept() function?
Do you have any code you are having problems with? Please post the code and your questions about the problems you are having.
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Re: How to create a parametrized constructor ,with parameters accepting from a accept() function?
Code java:
import java.io.*;
class student
{
String name;
int age,m1,m2,m3,maxi;
float avg;
student(String n,int a,int m4,int m5,int m6)
{
age=a;
m1=m4;
m2=m5;
m3=m6;
name=n;
maxi=0;
avg=0;
}
void accept(String args[])throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter name");
String a=in.readLine();
System.out.println("Enter age");
int ae=Integer.parseInt(in.readLine());
System.out.println("Enter marks1");
int m7=Integer.parseInt(in.readLine());
System.out.println("Enter marks 2");
int m8=Integer.parseInt(in.readLine());
System.out.println("Enter marhks 3");
int m9=Integer.parseInt(in.readLine());
student obj=new student(a,ae,m7,m8,m9);
}
void compute()
{
int t=m1+m2+m3;
avg=(float)(t/3f);
if(m1>m2&&m1>m3)
maxi=m1;
else if(m2>m1&&m2>m3)
maxi=m2;
else
maxi=m3;
}
void display()
{
System.out.println("Name="+name);
System.out.println("marks 1 ="+m1);
System.out.println("marks 2 ="+m2);
System.out.println("marks 3="+m3);
System.out.println("avg= "+avg);
System.out.println("max="+maxi);
}
public static void main(String args[])throws IOException
{
student obj1=new student();
obj1.accept();
obj1.compute();
obj1.display();
}
}
The above returns a syntax error:cannot find symbol-constructor student
How to correct this error?
--- Update ---
Code java:
import java.io.*;
class student
{
String name;
int age,m1,m2,m3,maxi;
float avg;
student(String n,int a,int m4,int m5,int m6)
{
age=a;
m1=m4;
m2=m5;
m3=m6;
name=n;
maxi=0;
avg=0;
}
void accept(String args[])throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter name");
String a=in.readLine();
System.out.println("Enter age");
int ae=Integer.parseInt(in.readLine());
System.out.println("Enter marks1");
int m7=Integer.parseInt(in.readLine());
System.out.println("Enter marks 2");
int m8=Integer.parseInt(in.readLine());
System.out.println("Enter marhks 3");
int m9=Integer.parseInt(in.readLine());
student obj=new student(a,ae,m7,m8,m9);
}
void compute()
{
int t=m1+m2+m3;
avg=(float)(t/3f);
if(m1>m2&&m1>m3)
maxi=m1;
else if(m2>m1&&m2>m3)
maxi=m2;
else
maxi=m3;
}
void display()
{
System.out.println("Name="+name);
System.out.println("marks 1 ="+m1);
System.out.println("marks 2 ="+m2);
System.out.println("marks 3="+m3);
System.out.println("avg= "+avg);
System.out.println("max="+maxi);
}
public static void main(String args[])throws IOException
{
student obj1=new student();
obj1.accept();
obj1.compute();
obj1.display();
}
}
The above returns a syntax error:cannot find symbol-constructor student
How to correct this error?
Re: How to create a parametrized constructor ,with parameters accepting from a accept() function?
Quote:
a syntax error:cannot find symbol-constructor student
Please copy the full text of the error message and post it here. The error message should show what line the error was on and more details about the problem.
Also please edit the code and properly format it with indentations for nested statements. All statements should not start in the first column.
Re: How to create a parametrized constructor ,with parameters accepting from a accept() function?
look at the constructor you created and then go to the main method and look at the object you instantiated.... does the new object you created match the definition you made for a Student object???