java - Please help stuck on this UML diagram -
uml: -name: string -playerid: int -scores: int[] -bestscore: int -numberofplayers: int << constructors>> +player() +player(name: string) << set methods>> +setname(name: string):void +setscores(score: int[]):void << methods >> +getplayerid():int +getname():string +getscores():int[] +getbestscore(): int +calculateaverage(): double << helper methods >> -calculatebestscore(): void
- the constructors increment static variable numberofplayers , assign value of numberofplayers playerid.
- the calculateaverage() public method determines average score player data stored in scores array.
- the getbestscore() method calls calculatebestscore() private helper method determines highest score achieved far player.
any ideas how should set numberofplayers assign value playerid ? , how calculateaverage , calculatebestscore methods ?
my code @ moment is:
//class declaration of player class public class player { /*--------------- data fields -------------------------------------- attributes of class */ private string name; private int playerid; private int bestscore; private int numberofplayers; private int scores; /* -------------- constructor -------------------------------------- */ public player(string name) { this.name = name; this.numberofplayers = numberofplayers + 1; this.playerid = this.numberofplayers; } //create set method setname public void setname(string name) { this.name = name; } //create set method setscores public void setscore(int score) { scores = score; } //create method getplayerid public int getplayerid() { return playerid; } //create method getname public string getname() { return name; } //create method getscores public int getscores() { return scores; } //create method getbestscore public int getbestscore() { return bestscore; } //create method calcualteaverage public double calculateaverage() { } }
the constructors increment static variable numberofplayers , assign value of numberofplayers playerid
give variable static
identifier, , initialize it:
private static int numberofplayers = 0;
every time create new player class, increment 1:
public person() { numberofplayers++; }
you'll need method expose value of numberofplayers, , make static
player class can call it:
public static int getnumplayers() { return numberofplayers; }
test it's valid approach such:
person = new person(); person b = new person(); person c = new person(); system.out.println(c.getnumplayers()); //will 3 based on above
regarding calculation of scores, getscores
, setscore
methods problematic. first, getscores
implies must return more 1 score. second, setscore
has me wondering how many times must 1 player, , store values grow in collection of scores? feel ambitious me guess @ different approaches may satisfy need. if school assignment, check instructor. i'll share 1 approach uses arraylist
data structure:
private arraylist<integer> scores = new arraylist<integer>(); public person() { } public void setscore(int score) { scores.add(score); }
to best score - how maximum value list/arraylist
to average score - calculating average of array list?
Comments
Post a Comment