Reputation: 3
as the title says. I have two classes, one that makes a "player", and another that makes a "team", which includes an arraylist of players. one of my methods in the team class is howmanyallstars, where I try to see how many allstars there are on the team, but I'm getting an error on my boolean.
public class Player {
private String nameofplayer;
private int currentposition;
private double baverage;
private boolean allstar;
public Player (String name, int position, double battingaverage, boolean isallstar) {
nameofplayer = name;
currentposition = position;
baverage = battingaverage;
allstar = isallstar;
}
then I have:
public class Team {
private String nameteam;
private static ArrayList roster;
private double priceofgame;
private double budget;
private int numberofgames;
public Team (String teamname, ArrayList teamroster, double price, double teambudget, int games) {
nameteam = teamname;
roster = teamroster;
priceofgame = price;
budget = teambudget;
numberofgames = games;
}
public static int numberOfAllstars(Team name) {
int rostersize = roster.size();
int numofallstars = 0;
for (int counter = 0; counter <= rostersize; counter++) {
if (roster.get(counter).allstar == true) numofallstars++;
}
return numofallstars;
}
as I said, the .allstar is being flagged, and I don't know how to fix it. Can someone please help?
Upvotes: 0
Views: 131
Reputation: 500
One problem is you declared roster as a generic ArrayList without any type information.
roster.get(counter) will return an Object instead of a Player.
You can solve this two ways:
Declare roster as an ArrayList of Player objects instead of as a generic ArrayList
private static ArrayList<Player> roster;
Cast roster.get(counter) to an object of type Player
if (((Player) roster.get(counter)).allstar == true) {
numofallstars++;
}
Another problem is that you have declared allstar as a private variable in the Player class. Private variables can only be accessed by the class in which they are declared.
The best solution to this is not to change the visibility of the variable from private to public. Instead, you should create an accessor method to retrieve the value of the variable from outside of the class.
Here is an example:
private boolean allstar;
//...
public boolean isAllstar() {
return allstar;
}
A third problem is see is that your method, numberOfAllstars, will throw an exception because the for loop's terminating condition is incorrect.
Just like Arrays, Lists of size n will be accessed with indices starting at 0 and ending at n - 1. Because your for loop terminates when counter <= rosterSize, your next line will try to access the List at an index equal to its size. This index is invalid for the List, so an exception will be thrown.
Here is the correct way to write the loop:
for (int counter = 0; counter < rosterSize; counter++)
Upvotes: 1
Reputation: 2467
1- Define allstar in your class as public, or use a public get/private set property:
private bool _allstar = false;
public string allstar
{
get
{
return name;
}
private set
{
name = _allstar;
}
}
2- It is better to define roster list as an ArrayList<Player>
.
3- You can use linq to make your function a one liner:
public static int numberOfAllstars(Team name) {
return roster.Where (e=>e.allstar).Count();
}
Upvotes: 0
Reputation: 178263
First, your roster
is of the raw form of ArrayList
, so the get
method will return an Object
, not a Player
. Use generics to specify "of what" on your ArrayList
:
private static ArrayList<Player> roster;
and in your Team
constructor:
public Team (String teamname, ArrayList<Player> teamroster, double price,
double teambudget, int games) {
Next, allstar
is a private
field in Player
. Create a public
getter method and call it instead of referencing the field directly.
Next, for some reason your roster
variable and numberOfAllstars
method are static
. There's no reason for that. Just make everything non-static
here. Also, there's no reason for the name
parameter on numberOfAllStars
; it can be removed.
Additionally, once you do that, you will run off the end of the array with this for
loop, because indexes range from 0
to size() - 1
.
for (int counter = 0; counter <= rostersize; counter++) {
Try
for (int counter = 0; counter < rostersize; counter++) {
And, the comparison to true
is unnecessary; the condition is already a boolean
. Try
if (roster.get(counter).isAllstar())
(once you've made the getter method isAllstar()
.
Upvotes: 2
Reputation: 93842
The problem is that the allStar field is private. So you can't access it in your Team class. You need to create a getter to access the allstar value for your player class.
public class Player {
public boolean isAllStar(){
return this.allStar;
}
}
Then you'll be able to call it in your loop :
public static int numberOfAllstars(Team name) {
int numofallstars = 0;
for (int counter = 0; counter < roster.size(); counter++) {
if (roster.get(counter).isAllStar()) numofallstars++;
}
return numofallstars;
}
Note that arrays are 0 base indexed so if should be counter < roster.size()
. Also don't use raw types, use generics.
ArrayList<Player> roster;
Upvotes: 1
Reputation: 2607
private boolean allstar;
is private, so you can't access it outside of Player
class.
Make geter for it in Player class:
public boolean getAllstar() {
return allstar;
}
and then, in Team class:
if (roster.get(counter).getAllstar()) numofallstars++;
Upvotes: 1