-
Student Class
Hi,
I have designed a basic student class diagram (below) in order to write the code for this class.
Student
studentNumber: String
studentName: String
markForMaths:int
markForEnglish:int
markForScience:int
Student(String, String)
getNumber(): String
getName(): String
enterMarks(int, int, int)
getMathsMark():int
getEnglishMark():int
getScienceMark():int
calculateAverageMark():double
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);
}
public double calculateAverageMark()
{
return (markForMaths + markForEnglish + markForScience)/3.0;
}
}
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
-
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").
-
How would i go about doing that?
-
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();
}
}
Java Wreck
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks