Question:
All JAVA experts needed.Help in a program?Sure 10 points?
Avik Sarkar
2013-01-04 07:36:10 UTC
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 3 sub,max,avg
Write a main method create an object and CALL THE ABOVE METHODS


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?
And is it possible to create two objects in JAVA?
I don't want to do the program using 2 classes..And even if the program is correct,the question says to call the member methods from main function,but I have called the constructor earlier in the accept() function.So does my answer goes according to the question?
Can anyone suggest a better way to do the program?

REMEMBER I AM IN CLASS 10 ONLY.SO ANSWER IN THAT WAY ONLY.

The best help will be highly appreciated.
Five answers:
AnalProgrammer
2013-01-04 07:55:05 UTC
Classes in Java should start with a capital letter. Like String.



This is your constructor.

student(String n,int a,int m4,int m5,int m6)



This is how you are calling your constructor.

new student();



You have parameters missing, or you need to create a constructor with no parameters to set the object to default values.



Have fun.
Kaydell
2013-01-04 18:14:43 UTC
Here are the steps that you can do to get your program to compile and run correctly:



1. Declare the accept() method to be static. The problem with the code the way that it is, is that without accept() being static, you'd have to have an object created already, and it's the accept() method that creates the object.



2. Name your class "Student" instead of "student". By convention, class names begin with a capital letter. This helps keep everyone straight as to what a class name is and what a variable name is.



3. Have the accept() method return the Student object that it creates so that main() will have the object to call compute() and display().



4. Change the accept() method so that it doesn't take any parameters.



5. Down in the main() method, remove the line of code where it tries to create a student object. You can't do that there because you don't have the parameters such as name, age, etc to pass into the constructor to create a Student object.



6. Also in main() keep a reference to the Student object that is returned by the accept() method



Student obj1 = accept();



That should be it. If you make these changes, your code should compile and run correctly.
nordland
2016-10-21 04:38:33 UTC
You do favor to grant more advantageous thoughts (or somewhat code) about your software. yet i will supply it a whirl... It received't run infinitely because the variable begins at 2, and the at the same time as is entered at 2, so the consumer under no circumstances extremely receives to regulate the alternative, i.e. var is declared as 2 and the loop says 'at the same time as var is two do some thing' so the loop has evaluated that var is two so the loop is 'accomplished', yet clearly you've declared it before so the loop need no longer be performed. The 'definite' section as well, it extremely is the point you do/do not (?) say this methodology keeps giving the alternative, nicely this can remedy the blunders possibly... //assuming all of us understand what the consumer get admission to is... var userOption = -a million //what ever consumer chosen at the same time as (userOption != 2){ if (userOption==0){ //NO equipment.go out(0); } else{ //definite //do definite... //-> if damage out then: //damage; //-> otherwise //userOption=a million; //to get the hot launch to proceed (yet 'proceed;' or any cost decrease than 2 ought to artwork } } attempt that and note if it extremely works...
2013-01-04 07:54:37 UTC
you are missing a method. you need to have a method for student that is blank .

do something like

void student (){

age = 0;

m1 = 0;

etc

}



make everything start out as blank. that will solve your problem because you are trying to create a student "student obj1=new student();" with no parameters in the parentheses. you just need a method with blank parameters.
Laurence
2013-01-04 07:56:28 UTC
student obj1=new student(); is your problem for the syntax error.



instead call it like this:

student obj1=new student("name",10,1,2,3); // whatever params you need to pass



Yes it is possible to create two objects:

student obj1=new student("name",10,1,2,3); // whatever params you need to pass

student obj2=new student("name",10,1,2,3); // whatever params you need to pass



Your parameterized constructor does not initialize all params.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...