Help With Java

Thread Solved

Join Date: Feb 2008
Posts: 4
Reputation: Helpwithjava is an unknown quantity at this point 
Solved Threads: 0
Helpwithjava Helpwithjava is offline Offline
Newbie Poster

How do i create a High score class using Arrays

 
0
  #1
Feb 29th, 2008
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
Last edited by Helpwithjava; Feb 29th, 2008 at 5:35 pm. Reason: change in title
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,803
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Help With Java

 
0
  #2
Feb 29th, 2008
Originally Posted by Helpwithjava View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,803
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Help With Java

 
0
  #3
Feb 29th, 2008
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:
  1. public void setScoreArrayList(ArrayList<Score> scoreArrayList) {
  2. this.scoreArrayList = scoreArrayList;
  3.  
  4. }
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:
  1. public void setScoreArrayList(ArrayList<Score> scoreList) {
  2. scoreArrayList = scoreList;
  3.  
  4. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 4
Reputation: Helpwithjava is an unknown quantity at this point 
Solved Threads: 0
Helpwithjava Helpwithjava is offline Offline
Newbie Poster

Re: Help With Java

 
0
  #4
Mar 1st, 2008
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;
}

}
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,803
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Help With Java

 
0
  #5
Mar 1st, 2008
You still have some of the same problems as far as having parameters with the same names as the data members:

  1. public void setScoreArrayList(ArrayList<Score> scoreList) {
  2. scorelist = scorelist;
  3.  
  4. }
  5.  
  6.  
  7. public highscore(String name){
  8. this.name = name;
  9. this.scorelist = new ArrayList <Score>(); // this is a Array List
  10. }

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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 4
Reputation: Helpwithjava is an unknown quantity at this point 
Solved Threads: 0
Helpwithjava Helpwithjava is offline Offline
Newbie Poster

Re: Help With Java

 
0
  #6
Mar 2nd, 2008
Thank You for all your help
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC