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?

7
  • 3
    why not return a pupil object instead of an int? Commented Apr 26, 2012 at 9:22
  • Consider using ArrayList instead of Vector, unless you use it in a concurrent environment which I believe you don't. Commented Apr 26, 2012 at 9:23
  • 1
    Use a wrapper class that encapsulates an exam mark with Pupil name. Why not call it something like "ExamGrade" - as this implicitly suggests identifying data as well as a mark. Commented Apr 26, 2012 at 9:28
  • 1
    why return the name? isn't it given as a parameter? Commented Apr 26, 2012 at 9:29
  • @alegen how would i return the object? Commented Apr 26, 2012 at 9:33

5 Answers 5

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());
Sign up to request clarification or add additional context in comments.

4 Comments

i cant change the method also i have done this in the pupil class as mentioned.
@f-dot You can't return both the exam mark and the pupil name, if your method signature returns an int. You will have to change it.
how would i return the object then, since i cant change the return type of the method.
you can't. You would have to change the method from public int lookforMark to public ExamMark lookforMark.
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.

Comments

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.

6 Comments

thanks, but you cant do that because there are incompatible types
@F.Dot what do you mean with incompatible types?
when you return return v.elementAt(i); there are incompatible types of string and int
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
Thanks for this, im instructed not to change the method signature.
|
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;
}

Comments

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

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.