I Would like to create a High Score For a Game in jave Called Jetman. i have created a class in java called Score and Score list to display the score but i do not know how to create a highscore using an Array List

the Score Class is

package jetman;
import java.io.Serializable;


public class Score implements Comparable<Score>,
                              Serializable {

    private String name;//int should have more than enough space - if score overflows 
    private int score; //the user's cheated so they can accept the concequences!!

    /** Creates a new instance of Score */
    public Score(String name, int score) {
        this.name = name;
        this.score = score;
    }
    public Score(){
        this.name = "";
        this.score = -1;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int compareTo(Score o) {
        return score - o.score;
    }

    int getNumRecords() {
        return 0;
    }

}

and the Score List is

package jetman;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;



public class scoreList {
    String filePath = "./score.txt";
    private ArrayList<Score> scoreArrayList = new ArrayList<Score>();



    /** Creates a new instance of scoreList */
    public scoreList() {
        FileWriter writer = null;
        try {
          writer = new FileWriter("output.txt");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
       PrintWriter out = new PrintWriter(writer); 
       out.println(this);

       out.println(10.55);

      //this.add(scoreArrayList);

    }

    public ArrayList<Score> getScoreArrayList() {
        return scoreArrayList;
    }

    public void setScoreArrayList(ArrayList<Score> scoreArrayList) {
        this.scoreArrayList = scoreArrayList;

    }

    public void removeScoreArrayList (Score s) {
        scoreArrayList.remove(s);
        this.scoreArrayList = scoreArrayList;
    }

      private Score add(Score s) {
         scoreArrayList.add(s);
         return s;
      }


}

Thank You

Edited 3 Years Ago by Dani: Formatting fixed

I would like to know how to create a high score system for a java game using an array List. Thankyou

You are going to have to get a lot more specific to get help.

O.K. Good, you've added code. So you want to "create a high score using an array list". So you intend to put all the Scores into an ArrayList and you want to pick the single highest Score FROM that ArrayList? I'm guessing that is what you mean. I would write a function that traverses an ArrayList of type Score and extracts the Score with the maximum score (note capitalization difference). Since your Score class also has "name" in it in addition to "score", you would not be able to compare two Scores directly but would instead have to extract "score" from each Score and return the highest from the Score ArrayList. Also, in code like this:

public void setScoreArrayList(ArrayList<Score> scoreArrayList) {
this.scoreArrayList = scoreArrayList;

}

it is easy to get confused when the parameter's name is the same as the data member's name. It's better to choose a slightly different name for the parameter, like:

public void setScoreArrayList(ArrayList<Score> scoreList) {
scoreArrayList = scoreList;

}

Sorry about this i have writen this code is this correct and how do i add a sort so only the top 5 scores are displayed

The code

/*
 * highscore.java
 *
 * Created on 29 February 2008, 19:54
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package jetman;

import java.util.ArrayList;
import java.util.Vector;
import java.util.Random;

public class highscore extends Vector {
    private String name; // name for the Palyer
    private ArrayList<Score> scorelist;


    // create a new instance
    public highscore(String name){
        this.name = name;
        this.scorelist = new ArrayList <Score>(); // this is a Array List
    }


    public highscore(){
        this.name =" No Name";
        this.scorelist = new ArrayList <Score>();
    }


    public void setScoreArrayList(ArrayList<Score> scoreList) {
        scorelist = scorelist;

    }

    /** Removes the product at index i */
    public void removeScore(int i){     // Removes a score from the list
        scorelist.remove(i);
    }


    // get the name of the player
    public String getName() {
        return name;
    }


    public Score index(int i){
        if(i < scorelist.size() && i >=0)return scorelist.get(i);
        return null;
    }


    /** Returns a textual representation of the Shop object. */
    public String toString() {
        return "name" + name + "Scorelist" + scorelist;
    }

}

Edited 3 Years Ago by Dani: Formatting fixed

You still have some of the same problems as far as having parameters with the same names as the data members:

public void setScoreArrayList(ArrayList<Score> scoreList) {
scorelist = scorelist;

}


public highscore(String name){
this.name = name;
this.scorelist = new ArrayList <Score>(); // this is a Array List
}

setScoreArray looks like it's just a typo. O.K. You have an ArrayList. It looks like each player has an ArrayList of Score, so there'll be one object of type highscore for each player. I don't see anything that involves a vector, just the ArrayList. I also don't see any function that adds a single Score to the ArrayList in this class, which I think you need. I see a removeScore, but no addScore. So I am imagining that you want the Scores to reside in the ArrayList called scorelist. I assume this ArrayList called scorelist contains only one player's Scores. You have a couple of choices. Regardless, I think you need to add an addScore function to your class. You can either only keep track of the top five scores or keep track of all of them, but only later display the top five. You can sort them as you go ar sort them later. I would sort them as you go, so I'd always have a sorted ArrayList. When you insert a new Score into addScore, store it in its correctly sorted spot in the ArrrayList. To do this, I would use an InsertionSort.

Now, if you want to display the top five Scores of all the players total, so one score comes from Joe, two, from Bob, two from Sue, I think that that's a lot harder the way you have your classes set up, but certainly doable. Again, the way you have set up your highscore class leads me to believe that your highscore object comes from a group of scores of one individual player, not a mix of players.

This question has already been answered. Start a new discussion instead.