I need help designing a method that finds the percentile of a value given the percentile rank. Here are the exact instructions:

public double getPercentile(int p)
This returns the value of the pth percentile. A value stored in the array is the pth percentile if it is greater than or equal to at least p% of the values and less than or equal to at least (100-p)% of the values. Return the first value in the array that satisfies these conditions. if p is not between 0 and 100 (inclusive), return 0. If the array is empty, return 0. Otherwise, there should be at least one value in the array satisfying the condition.
Hint: Write a private helper method that determines whether a given value is the pth percentile. Call this for each element in the array until you find one that works.

I figured out how to do getRank() on my own but I'm having trouble figuring out how to do it the other way around basically.

package pr02;

public class Percentile {
	private int size;
	private double capacity;
	private double[] array;

	public Percentile(double cap) {
		size = 0;
		capacity = cap;
		array = new double[(int) capacity];

	}

	public boolean add(double value) {
		if (size < capacity) {
			array[size] = value;
			size++;
			return true;
		} else
			return false;
	}
	
	public int getRank(double value){
		double scoresLessThan = 0;
		double rank = 0; 
		for (int i=0; i < size; i++){
			if  (value > array[i])
				scoresLessThan++;
		}
		rank = scoresLessThan/size * 100;
		return (int)rank;
	}
	
	public double getPercentile(int p){
		if (p < 0 || p > 100)
			return 0; 
		if (size <= 0)
			return 0; 
		for (int i=0; i < size; i++){
			
		}
		
	}
	
	

	public int getSize() {
		return size;
	}

	public double getCapacity() {
		return capacity;
	}

	public String toString() {
		String entries = "";
		for (int i = 0; i < array.length; i++) {
			if (i == array.length - 1)
				entries += array[i];
			else
				entries += array[i] + ", ";
		}
		return entries;
	}

}
for (int i=0; i < size; i++){
    if(getRank(array[i])==p)
        return array[i];        
}

return 0;

(You've already written that for loop, I just added the if)

I added the return 0 for shits, just in case it doesnt find anything.

That should do it, assuming I called on getRank correctly.

Holla Back.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.