1

So I have and interface that is implemented by a class called Vehicle that accepts a double value as the parameter:

public class Vehicle implements Efficiency
{
  //instance variable
  private double efficiency;

  public Vehicle(double x)
  {
  //parameter initializes the instance variable 
  x = efficiency; 
}

This is the interface:

public interface Efficiency 
{
  double getEfficiency(); 
}

I need to create a method called getFirstBelowT() that accepts an Efficiency array and a double as parameters. The method is supposed to return an Efficiency object that is the first object in the array that is less than the double value in the parameter. How can I compare the elements in the Efficiency array to the double value? This is what I have so far:

//a method that returns an Efficiency object that is the first
//object in the array with a efficiency below the double parameter

public static Efficiency getFirstBelowT(Efficiency[] x, double y)
{ 
    //loop through each value in the array
    for(Efficiency z: x)
    {
        //if the value at the index is less than the double
        if(z < y)
        {   //returns the first value that is less than y and stops looping
            // through the array.
            return z; 
            break; 
        }
    }
    //returns null if none of the element values are less than the double                
    return null;
}
1
  • 2
    "parameter initializes the instance variable " No: efficiency = x would initialize the member variable, you are overwriting the parameter value with zero. Commented Oct 25, 2015 at 23:58

1 Answer 1

1

You're basically there:

  1. You'd need to call the getEfficiency method in the conditional:

    if(z.getEfficiency() < y)
    
  2. Get rid of the break after the return: it's unreachable code.

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

1 Comment

Ya, I had forgotten to do that. I got it to work. Thank you very much.

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.