DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: StudentList Class

  1. #1
    Join Date
    Dec 2004
    Posts
    46

    StudentList Class

    Hi,

    I have developed this student class which is below. I created a StudentTester class which was very basic in that it asked a student already written in the code to enter his marks for maths, English and science and calculate the average mark.

    Like this:

    Raul Bonano, Please enter your mark for maths
    20
    Raul Bonano, Please enter your mark for english
    20
    Raul Bonano, Please enter your mark for science
    20

    Student no. is 234444
    Student name is Raul Bonano
    Maths mark is 20
    English mark is 20
    Science mark is 20
    The average mark is 20.0

    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;
    }
    }



    I now want to take this implementation of the student class and use an array to develop a StudentList class. The UML diagram StudentList object and a Student object is below:

    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

    Student List

    list: Student[ ]
    Total: int

    StudentList(int)
    add(Student); boolean
    remove(int):boolean
    isEmpty( ):boolean
    isFull( ):Boolean
    getItem(int): Student
    getTotal( ):int

    This is getting me a bit confused and if possible I would like some help on how to create the StudentList class. Once I have done this I want to then develop a StudentListTester class to test the StudentList class.
    The StudentListTester class is something that I will be able to do without much hassle and it will look something like this below:

    Maximum number of items in list? 7
    1.add student to list (this needs to include their name and marks for maths, English and science)
    2.remove student from list
    3.check if list is empty
    4.check if list is full
    5.display list of students
    6.student position? ( explained below)
    7.quit

    enter choice [1-6]:


    6. student position

    where you get a specific student from a specified position in the list.

    e.g.

    list:

    chris
    sam
    tim
    gary
    dave

    pick student : 3
    tim.
    it shows the student in position 3 and his marks for maths English and science


    I would greatly appreciate some help in creating the studentlist class as I am not to sure how to do this. The student class I created may be of some help.

    Thanks

  2. #2
    Join Date
    Jan 2005
    Posts
    38
    Do you know how to use some Data Structures >>> e.g. ArrayList or LinkedList
    If the answer is yes so use them
    if the answer is no
    i can give you the class but in Tuesday because i have som jobs to do it.

    regards,

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    My suggestion

    It seems to me that this is school work and that the assignment requires
    that the student 'rolls his own' list. After all, if ArrayList was to be used
    then there would be no list class to code...

    Code:
    /**
     * A list class that uses an object array.  It is not 'packed' when
     * an object is removed(nulled) so new objects are placed in the first
     * 'vacant' slot that is found.
     * A really nice version should do its own sorting and packing (and growing).
     * NOTE: The list is zero-based; index 0 is the first element in the list,
     * - cause seriously, 1-based array/list processing died w. Fortran....
     */
    public class StudentList {
      static int DEFAULT_LEN=7;
      Student [] studArray=null;
      
      /**
       * Length constructor
       * @param len
       */
      public StudentList(int len) {
        studArray=new Student[len];
      }
      /**
       * Default constructor
       */
      public StudentList() {
        this(DEFAULT_LEN);
      }
      /**
       * Get first vacant slot in arrray
       * @return
       */
      private int getVacantSlot() {
        for (int i=0;i<studArray.length;i++) {
           if (studArray[i]==null) return i;
        }
        return -1;
      }
      /**
       * Add a student
       * @param stud
       * @return
       */
      public boolean add(Student stud) {
        int vacantSlot=getVacantSlot();
        if (vacantSlot==-1) return false;
        studArray[vacantSlot]=stud;
        return true;
      }
      /**
       * Return true only if a student is actually removed
       * The method should throw an exception if the ix is
       * out of bounds but....
       * @param ix
       * @return
       */
      public boolean remove(int ix) {
        if (ix < 0 || ix > studArray.length-1) return false;
        if (studArray[ix]!=null) {
          studArray[ix]=null;
          return true;
        } else {
          return false;
        }
      }
      public boolean isEmpty( ) {
        for (int i=0;i<studArray.length;i++) {
          if (studArray[i]!=null) return false;
        }
        return true;
      }
      /**
       * No vacancy means full
       * @return
       */
      public boolean isFull( ) {
        return getVacantSlot()==-1;
      }
      /**
       * This method returns whatever is foundat studArray[ix]
       * The way this list works it is the callers responsibility
       * to check that the returned value is not null.
       * The method should throw an exception if the ix is
       * out of bounds but....
       * @param ix
       * @return
       */
      public Student getItem(int ix) {
        if (ix < 0 || ix > studArray.length-1) return null;
        return studArray[ix];
      }
      /**
       * Not sure what this method is supposed to do, I have
       * implemented it so it returns the total number of
       * students in the list
       * @return
       */
      public int getTotal() {
        int cnt=0;
        for (int i=0;i<studArray.length;i++) {
          if (studArray[i]!=null) cnt++;
        }
        return cnt;
      }
    
    }
    Last edited by sjalle; 02-25-2005 at 07:45 AM.
    eschew obfuscation

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