2

Im trying to write a program which has a method Exam mark This contains objects of pupil class and a string that gives the pupils name. The method should return the pupils exam mark and their name. if there is a pupil called joe and their exam score is 32 then when joe is passes 32 should be printed.

In the student class i have getters, for getexamscore and in a subclass i have the getter getpupilname. the vector elements should be in the pupil class.

If the pupil is not in the class -1 should be returned.

Here is my method and everything must be in only this method:

import java.util.*;
public class vectors
{
   public int lookforMark(Vector <pupil> v, String name)
   {
       int examscoremark=0;
       name="";
       try{

           for(int i=0; i<=v.size(); i++){
               try
               {
                  int element= v.elementAt(i).getexamscore();
                  String element2= v.elementAt(i).getpupilname();


               }
               catch(Exception e)
               {
                   System.out.println("there is an error");
                   return -1;
               }  
            }
        }

Can someone help me on returning the exammark with the pupil name?

Pro-grammer
  • 862
  • 3
  • 15
  • 41

5 Answers5

2

You can create a setMark method on the pupil.

Alternatively, create an object to hold the name and mark and return it.

public class ExamMark {
    private final String name;
    private final int mark;

    public ExamMark(String name, int mark){
        this.name = name;
        this.mark = mark;
    }

    public String getName(){
        return name;
    }

    public int getMark(){
        return mark;
    }
}

Use it like this:

return new ExamMark(v.elementAt(i).getpupilname(), v.elementAt(i).getexamscore());
dogbane
  • 266,786
  • 75
  • 396
  • 414
1

There are two answers to this question:

  1. Use a Pair<E,K> class. There are several available via Google. This is the shortcut.
  2. Create a holding object specific to the information you want to return. This is the cleaner way and should normally be preferred.
nfechner
  • 17,295
  • 7
  • 45
  • 64
1

You specifically said that

In the student class i have getters, for getexamscore and in a subclass i have the getter getpupilname. the vector elements should be in the pupil class.

As a result, you don`t need any extra wrapper classes. Instead of returning an int, return a pupil object like this

public pupil lookforMark(Vector <pupil> v, String name) {
      for(int i = 0; i < v.size(); i++)
         if(v.elementAt(i).getpupilname().equals(name))
            return v.elementAt(i);
      return null;
}

Easy as that. Now the pupil holds the name and also the grade.

Later edit: corrected mistake in for loop.

  • thanks, but you cant do that because there are incompatible types – Pro-grammer Apr 26 '12 at 09:52
  • @F.Dot what do you mean with incompatible types? –  Apr 26 '12 at 09:53
  • when you return return v.elementAt(i); there are incompatible types of string and int – Pro-grammer Apr 26 '12 at 09:56
  • i changed the function signature; it does not return an int anymore, now it is `public pupil lookforMark(...)`. also, v is a Vector holding pupils, so the method elementAt returns a pupil object. there is no incompatibility problem, try it out in your code –  Apr 26 '12 at 10:01
  • Thanks for this, im instructed not to change the method signature. – Pro-grammer Apr 26 '12 at 10:08
  • @F.Dot i highly doubt it is possible to extract more information from that method without changing it –  Apr 26 '12 at 10:11
0

its better to use the List or Map, vector reduce the performance?

create the class result which will be having user and pupil object and initialize the hashmap with key as user and value will be result object.

Map userMap = new HashMap();

public Result getUserMarks(String user){

Result result = userMap.get(user);  

    return result;
}
Uttesh Kumar
  • 290
  • 2
  • 10
0

The best way to return two objects at once is to wrap those two objects in standard java object which can contain two values. This would be an object of type Map.Entry<T1, T2>. To create such object you would use:

new AbstractMap.SimpleEntry<T1, T2>(value1, value2);

So in your case new AbstractMap.SimpleEntry<Integer, String>(mark, name);

ctomek
  • 1,696
  • 16
  • 35