Student Class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Student Class

Hybrid View

  1. #1
    Join Date
    Dec 2004
    Posts
    46

    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

  2. #2
    Join Date
    Jun 2004
    Posts
    10
    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").

  3. #3
    Join Date
    Dec 2004
    Posts
    46
    How would i go about doing that?

  4. #4
    Join Date
    Dec 2004
    Location
    Norway
    Posts
    14
    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

  5. #5
    Join Date
    Dec 2004
    Posts
    46
    Thankyou

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center