0

I'm working on a simple application which returns the student mark based on the particular module. My main issue is with the getModuleMark method, because I need it to return the module mark given the module index.

For the setModuleMark method I have passed both the index module and marks parameters in. I'm just getting a little confused as to what I need to put in the return for module marks.

At the moment when I run the application I get the following output:

Joe Bloggs

Module 0: 50.0

Module 1: 50.0

Module 7: 50.0

See code below:

public class Student {

    public static void main(String[] args) {

     Student student = new Student("Joe", "Bloggs"); 
   
    // Add some marks to the student. 
    student.setModuleMark(0, 10);   
    student.setModuleMark(1, 80);  
    student.setModuleMark(7, 50);
  
   
    // Display the marks.
    System.out.println(student.getForename() + " " +     student.getSurname());
    System.out.println("Module 0: " + student.getModuleMark(0));
    System.out.println("Module 1: " + student.getModuleMark(1));
    System.out.println("Module 7: " + student.getModuleMark(7));
      } 
    
  

    private String forename;
    private String surname;
    private double marks;

    public Student(String forename, String surname) {
      super();
      this.forename = forename;
      this.surname = surname; 
    
      double [] ModuleMark = new double [7]; //Creates array of fixed size 7
    }
  
    /**
     * @return the forename
     */
     public String getForename() { 
        return this.forename;
     }
  
     /**
      * @return the surname
      */
     public String getSurname() {
       return this.surname;
     }
  
     /**
      * @param marks the marks to set
      * @param i 
      */     
     public double getModuleMark (int in) {
       return this.marks;        
     }
  
     public void setModuleMark(int in, double marks) {
     
    this.marks = marks;
  }
}

5 Answers 5

2

There are a lot things which seem to be wrong.

First, the ModuleMark should be declared in the class and not within the constructor.

private double[] ModuleMark; // Declare here

public MyMain(String forename, String surname) {
    this.forename = forename;
    this.surname = surname;
    this.ModuleMark = new double[7]; // Creates array of fixed size 7
}

Next, your getModuleMarks and setModuleMarks method need to be like this

public double getModuleMark(int in) {
    return this.ModuleMark[in]; // return the value present at the given index
}

public void setModuleMark(int in, double marks) {
    this.ModuleMark[in] = marks; // set the marks at the given index in the array.
}

Also, since the ModuleMark is an array of size 7, you can't use the index 7. It would throw an ArrayIndexOutOfBoundsException, because in an array, the maximum possible accessible index is always array.length - 1.

student.setModuleMark(6, 50); // The max possible index
...
System.out.println("Module 7: " + student.getModuleMark(6)); // The max possible index

Note: After these changes, private double marks; would no longer be in use. You can either discard it or have it if you're going to use it for some other purpose in the future.

Sign up to request clarification or add additional context in comments.

Comments

1

public class Student {

public static void main(String[] args) {

 Student student = new Student("Joe", "Bloggs"); 

// Add some marks to the student. 
student.setModuleMark(0, 10);   
student.setModuleMark(1, 80);  
student.setModuleMark(7, 50);


// Display the marks.
System.out.println(student.getForename() + " " +     student.getSurname());
System.out.println("Module 0: " + student.getModuleMark(0));
System.out.println("Module 1: " + student.getModuleMark(1));
System.out.println("Module 7: " + student.getModuleMark(7));
  } 



private String forename;
private String surname;
private double[] marks;

public Student(String forename, String surname) {
  super();
  this.forename = forename;
  this.surname = surname; 

  marks = new double [8]; //Creates array of fixed size 7
}

/**
 * @return the forename
 */
 public String getForename() { 
    return this.forename;
 }

 /**
  * @return the surname
  */
 public String getSurname() {
   return this.surname;
 }

 /**
  * @param marks the marks to set
  * @param i 
  */     
 public double getModuleMark (int in) {
   return this.marks[in];        
 }

 public void setModuleMark(int in, double marks) {

this.marks[in] = marks;

} }

1 Comment

check this one............ First of all you had created the array of 7 elements but you were trying to set and get the 8th element value which will show index out of bound error. Check this one I modified your code.
0

As far as I am concerned, you must firstly add

private double [] ModuleMark

as a Student Class field, and then something like

public double getModuleMark (int in){
   return this.ModuleMark[in];
  }

Comments

0

Since your student can have many modules each having its mark then basically an array is suitable for your use case. So you need to remove double marks and keep only the array double [] ModuleMark.

You could implement your getter and setter like this:

public double getModuleMark (int in) {
    return ModuleMark[in];
}

public void setModuleMark(int in, double mark) {
    ModuleMark[in] = mark;
}

Just beware of ArrayIndexOutOfBoundsException . Better yet I would suggest use an List witch can grow automatically, it has also the ability to access elements using (zero-based) index.

Also please consider java naming conventions for members and classes.

Comments

0

First ModuleMark should be moduleMark by convention. :-)

Second your using getters and Setters should be like this (by convention) for moduleMarks.

 public double[] getModuleMark() {
return moduleMark;
 }
 public void setModuleMark(double[] moduleMark) {
this.moduleMark = moduleMark;
 }

Third -- the scope of module marks might be like this: (defined not just in scope of the student constructor)

   public class Student {
      private double [] moduleMark = new double [7]; 
    ....

Then, if I think your trying to do this:

    // Add some marks to the student.
    student.setSpecificModuleMark(0, 10);
    student.setSpecificModuleMark(1, 80);
    student.setSpecificModuleMark(6, 50);

    // Display the marks.
    System.out.println(student.getForename() + " " + student.getSurname());
    System.out.println("Module 0: " + student.getSpecificModuleMark(0));
    System.out.println("Module 1: " + student.getSpecificModuleMark(1));
    System.out.println("Module 6: " + student.getSpecificModuleMark(6));
   }

private double getSpecificModuleMark(int i) {
    // TODO Auto-generated method stub
    return this.moduleMark[i];
   }

private void setSpecificModuleMark(int i, int j) {
    this.moduleMark[i] = j;

   }

Fourth: note your trying to print out 7 which is index out of bounds.. so use 0--6.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.