Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Not getting the suitable output!

$
0
0
i am writing a program that stores high scores in non decreasing order. I have written the code with no error but I am not getting the required output. Hope someone can help me out with it.

/**
* Class for storing high scores in an array in non-decreasing order.
*/

public class Scores1 {
public static final int MAX_ENTRIES = 5; // number of high scores we keep
protected int numEntries; // number of actual entries
protected GameEntry[] entries; // array of game entries (names & scores)

/** Default constructor */
public Scores1() {
entries = new GameEntry[MAX_ENTRIES];
numEntries = 0;
}

/** You will insert methods for updating the set of high scores here.
* Be sure that you understand the code. */

/** Add GameEntry method */

public void add(GameEntry u)
{
int newScore = u.getScore();

if(numEntries == MAX_ENTRIES)
if(newScore <= entries[numEntries - 1].getScore())
return;
else
{
numEntries++;
for(int i = numEntries -1;(i>=1)&&(newScore >entries[i - 1].getScore());i-- )
{
entries[i]= entries[i-1];
entries[i] = u;
}
}
}
/** Remove GameEntry method */
public GameEntry remove(int i) throws IndexOutOfBoundsException
{
if(i<0 || i>=numEntries)
{
throw new IndexOutOfBoundsException("Invalid Index i ");
}
GameEntry temp = entries[i];
for(int j = i;j<numEntries -1;j++)
{
entries[j] = entries[j+1];
entries[numEntries -1] = null;
numEntries--;

}
return temp;
}
/** Returns a string representation of the high scores list */

public String toString() {
String s = "[";
for (int i = 0; i < numEntries; i++)
{
if (i > 0){ s += ", ";} // separate entries by commas
s += entries[i];
}
return s + "]";
}

public static void main(String[] args)
{
Scores1 ss = new Scores1();
ss.add(new GameEntry("Gotti",99));
ss.add(new GameEntry("Sunny",98));
ss.add(new GameEntry("Lensky",97));
ss.add(new GameEntry("Lucky",100));
ss.add(new GameEntry("Castallo",101));
ss.add(new GameEntry("Seigal",150));
ss.add(new GameEntry("Paul",148));
ss.add(new GameEntry("Gambino",160));
ss.add(new GameEntry("Frank",170));
ss.add(new GameEntry("Johnson",190));
ss.add(new GameEntry("Castallo",102));
System.out.println(ss);
ss.add(new GameEntry("columbo",2));
System.out.println(ss);
ss.add(new GameEntry("Bannano",200));
System.out.println(ss);
//ss.remove(0);
System.out.println(ss);
}

} 




For GameEntry:
/*
* A Class to represent a game; storing the name of the player
* and the score achieved.
*/
public class GameEntry {
protected String name; // name of the person earning this score
protected int score; // the score value

/** Constructor to create a game entry */
public GameEntry(String n, int s) {
name = n;
score = s;
}

/** Retrieves the name field */
public String getName() { return name; }

/** Retrieves the score field */
public int getScore() { return score; }

/** Returns a string representation of this entry */
public String toString() {
return "(" + name + ", " + score + ")";
}
}



I am getting an output of only:

[]
[]
[]

Hope someone can find why I am not getting the desired output.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>