9

There was a question in my book that said this (there are no answers):

Suppose we have a class Beta declared with the header:

class Beta extends Alpha

so Beta is a subclass of Alpha, and in class Alpha there is a method with header:

public int value(Gamma gam) throws ValueException

Write a static method called addValues which takes an object which could be of type ArrayList<Alpha> or ArrayList<Beta>, and also an object of type Gamma. A call to the method addValues must return the sum obtained by adding the result of a call of value on each of the objects in the ArrayList argument, with the Gamma argument as the argument to each call of value. Any call of value which causes an exception of type ValueException to be thrown should be ignored in calculating the sum.

My Attempt:

public static int addValues(ArrayList<? extends Alpha> arr, Gamma gam) {
    int sum = 0;
    for (int i = 0; i < arr.size(); i++) {
        try {
            sum += arr.get(i) + gam;
        } catch (Exception e) {
            i++;
        }
    }
    return sum;
}

Although I know for starters that the line sum += arr.get(i) + gam is going to give me an error, because they are not straight forward ints that can be added. The book provides no more information on the question so what I have written here is everything required for the question.

1
  • The question says ValueException, not Exception. Commented Apr 20, 2015 at 12:15

2 Answers 2

10

You are supposed to call the value method in order to get the values you are supposed to be adding.

Beside that, you can use the enhanced for loop to make your code cleaner.

public static int addValues(ArrayList<? extends Alpha> arr, Gamma gam) 
{
    int sum = 0;
    for (Alpha alpha : arr) {
        try {
            sum += alpha.value(gam);
        } catch (ValueException e) {
        }
    }
    return sum;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ahhh yes, I should've understood the question better! I didn't notice this, thank you! (Will mark correct once timer has passed).
You should also only catch a ValueException, per the problem statement. If you get an OutOfMemoryException or something, then you wouldn't want to keep summing.
1

Use wild card with bounded type as Alpha as you want to access the value then use arr.

public static int addValues(ArrayList<? extends Alpha> arr, Gamma gam) 
{
    int sum = 0;
    for (Alpha alpha : arr) {
        try {
            sum += alpha.value(gam);
        } catch (Exception e) {
        }
    }
    return sum;
}

Regarding the second question of overriding the equal function

public boolean equals(Gamma g) {
        if(g!= null && g instanceof Alpha && value(g) == value(this.myGam)){
            return true;
        }else{
            return false;
        }
    }

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.