| | |
Help With Java
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 4
Reputation:
Solved Threads: 0
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
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
Last edited by Helpwithjava; Feb 29th, 2008 at 5:35 pm. Reason: change in title
•
•
Join Date: Jan 2008
Posts: 3,803
Reputation:
Solved Threads: 501
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:
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:
Java Syntax (Toggle Plain Text)
public void setScoreArrayList(ArrayList<Score> scoreArrayList) { this.scoreArrayList = scoreArrayList; }
Java Syntax (Toggle Plain Text)
public void setScoreArrayList(ArrayList<Score> scoreList) { scoreArrayList = scoreList; }
•
•
Join Date: Feb 2008
Posts: 4
Reputation:
Solved Threads: 0
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;
}
}
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;
}
}
•
•
Join Date: Jan 2008
Posts: 3,803
Reputation:
Solved Threads: 501
You still have some of the same problems as far as having parameters with the same names as the data members:
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.
Java Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- FT Junior Java Developer Needed for Major Media firm in NYC (Software Development Job Offers)
- Java/J2EE Senior Software Engineer (Software Development Job Offers)
- Front-end Java Software Engineer for Digital Video/Media Market Leader (Software Development Job Offers)
- Senior Software Engineer (Java) (Web Development Job Offers)
- Java Software Engineer (Software Development Job Offers)
- Java Front-end Developer Engineer for Stealth Media Start-up (Software Development Job Offers)
- Java Developer Required (Software Development Job Offers)
Other Threads in the Java Forum
- Previous Thread: Can you help me do this question?
- Next Thread: JUnit test case problem.
| Thread Tools | Search this Thread |
2dgraphics 3d @param account affinetransform android api apple applet application arc arguments array arrays automation banking binary binarytree bluetooth chatprogramusingobjects class client code color compare component count database derby design detection eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image integer j2me java java.xls javadesktopapplications javaprojects jni jpanel julia keytool keyword linux list macintosh map method methods midlethttpconnection mobile netbeans object os pong problem producer program project projectideas recursion reference replaysolutions ria rim scanner server set size sms sort sourcelabs sql stop string swing terminal threads transforms tree ui unicode validation web windows






