Writing the code for the student class (below) was fairly straightforward but I am having problems with another class called tester which tests the student class.
Code:
public class Student
{
//The attributes
private String studentNumber;
private String studentName;
private int markForMaths;
private int markForEnglish;
private int markForScience;
//The methods
//The constructor
public Student (String numberIn, String nameIn)
{
studentNumber = numberIn;
studentName = nameIn;
}
//Methods to read the attributes
public String getStudentNumber()
{
return studentNumber;
}
public String getStudentName()
{
return studentName;
}
public void enterMarks(int mathsIn,int engIn, int sciIn)
{
markForMaths = mathsIn;
markForEnglish = engIn;
markForScience = sciIn;
}
public int getMathsMark()
{
return (markForMaths);
}
public int getEnglishMark()
{
return (markForEnglish);
}
public int getScienceMark()
{
return (markForScience);
}
The tester class is the problem, i very simple problem. All i want to change is it to show the name of the student before it asks the student to enter their marks. But it always comes up with errors when i try and do this.
The code is below:
Code:
public class StudentTester
{
public static void main(String []args)
{
int maths,english,science;
Student Marks;
System.out.println("Please enter your mark for maths");
maths = EasyIn.getInt();
System.out.println("Please enter your mark for english");
english = EasyIn.getInt();
System.out.println("Please enter your mark for science");
science = EasyIn.getInt();
Student exam = new Student("234444","David Billabonna");
exam.enterMarks(maths, english, science);
System.out.println("Student no. is " + exam.getStudentNumber());
System.out.println("Student name is " + exam.getStudentName());
System.out.println("Maths mark is " + exam.getMathsMark());
System.out.println("English mark is " + exam.getEnglishMark());
System.out.println("Science mark is " + exam.getScienceMark());
System.out.println("The average mark is " + exam.calculateAverageMark());
System.out.println();
}
}
Any suggestions would be appreciated
Thanks
02-07-2005, 12:19 PM
chimaera
If I understand what you want to do, then you need to instantiate your Student object before the user is asked any questions. Also, you may want to consider renaming your Student instance from "exam" to something else that makes a bit more sense (e.g rename "exam" to "student").
02-08-2005, 05:12 AM
WillisTi
How would i go about doing that?
02-08-2005, 05:35 AM
sjalle1
Code:
public class StudentTester
{
public static void main(String []args)
{
int maths,english,science;
Student Marks; // <----- this guy is just declared but never allocated
Student exam = new Student("234444","David Billabonna"); // <-- moved this guy here
System.out.println(exam.getStudentName()+", Please enter your mark for maths");
maths = EasyIn.getInt();
System.out.println(exam.getStudentName()+", Please enter your mark for english");
english = EasyIn.getInt();
System.out.println(exam.getStudentName()+", Please enter your mark for science");
science = EasyIn.getInt();
exam.enterMarks(maths, english, science);
System.out.println("Student no. is " + exam.getStudentNumber());
System.out.println("Student name is " + exam.getStudentName());
System.out.println("Maths mark is " + exam.getMathsMark());
System.out.println("English mark is " + exam.getEnglishMark());
System.out.println("Science mark is " + exam.getScienceMark());
System.out.println("The average mark is " + exam.calculateAverageMark());
System.out.println();