The Student Room Group

quick simple java question please!!

aa
(edited 13 years ago)
Reply 1
The constructor will be something like


public Team(String n){

this.name = n;

}



In this case the String with variable name n is passed as a parameter to the constructor. Have you never programmed before?
Reply 2
...
(edited 13 years ago)
looks fine to me.

instead of:
private String name;

you want:
private final String name

the final means that it can't ever be changed.

Actually you may want:
public final String name;

so that you can access it. Normally allowing access to a variable in a class is a bad thing, however as in this case we've made it final, it can't be changed anyway, so it's okay to access it directly.

Hope that helps.
Reply 4
ss
(edited 13 years ago)
Original post by unverified
Oh thanks, you seem to know your stuff!
and a few more things please if you can :smile:

(d) There should be a mutator method for each of the “goals for”, “goals against” and
points fields. Each of these mutator methods has a single parameter whose
value is added to the current value of the appropriate fields. For example,
addGoalsFor(2) should add two to the total number of “goals for”.

would that be,


public void increaseGoalsScored(int scored)
{
scored = scored + this.scored;
}

public void increaseGoalsConceded(int conceded)
{
conceded = conceded + this.conceded;
}


again looks fine to me. I would generally use method names given in a question, when they are given, so addGoalsFor (and then following their theme addGoalsAgainst, and possibly addPoints). Probably no one will care in this case as long as you use understandable names (which you have) because it didn't explicitly tell you what the names should be, but if you do an exam on it, you don't want to give the examiner any excuse to take of marks. And if you are working to a specification you should always use suggested method names. (Though in that case you should be working with an interface anyway).

you don't seem to have mentioned the method for adding points. I'm not sure what game the question is talking about, but from the question, it sounds like the method would be basically identical.

Also make sure your fields for these are private, and you probably want to initialise them if you haven't done that (probably to 0).

hope that helps.
(edited 13 years ago)
Reply 6
ddsa
(edited 13 years ago)
Original post by unverified
Thank you very much, I'm doing alot better than expected with your help.

I hardest thing I have found so far is this bit:

There should be a method to compare this team with another team. This method
will be used when sorting the league table. The method will return ????1 if this team
should be above the other team in the league. The method will return 1 if the
other team should be above this team in the league. The method should return 0
if the two teams are equal. The comparison of two teams should be based on the
following criteria:

i. Compare the number of points that each team has. If this team has higher
points, return -1 . if the other team has higher points, return 1.

ii. If the points are equal, compare the goal difference. If this team has a larger
goal difference, return -1. If the other team has a larger goal difference,
return 1.

iii. If both the points AND the goal difference are equal, compare the “goals for”.
If this team has scored more goals, return -1. If the other team has scored
more goals, return 1.

iv. If the points, goal difference and “goals for” are all equal, return 0;



How do I go about doing this? I have no idea how to compare two variables.
Is it something along these lines:

public void compareTeam()
{ if (this.team.points > team.points) {
return -1;
}
else { return 1;
}
}


sorry if it's very wrong, I'm just starting out and it looks quite wrong lol.
Once again, any help would be very very much appreciated




public void compareTeam() // 1
{ if (this.team.points > team.points) { // 2
return -1;
}
else { return 1;
}
}

1.you want public int compareTeam(Team team), you need to take a team as input to compare to, and you are returning an int, so you fill in the return type as such.

2. you want this.points for your own points not this.team.points (which would only make sense if you kept a field called team, which itself had a field called points). In fact you can drop the this. entirely and just use points, as there is no tighter binding for points than the class.
team.points for the other teams points works if you have called the opponents Team team (as I have in 1). However you may want points to be a private variable, in which case you will need to define a method getPoints, and then use team.getPoints(). (I would certainly advise making all of your non final fields private, at least for now, and provide methods for accessing them. The quicker you get used to this the better.)

Let me know if you don't understand the explanation of any of that. It's important that you understand why things work like they do, if you want to be able to program properly. (And don't worry if it seems tricky, it becomes more natural the more you do it.)
Reply 8
I'm quite confused, I don't really know whats going on.
Have you got MSN or something similar I can speak to you on if you don't mind?
Thank you
don't use MSN much I'm afraid.

I'm happy to answer any questions you have here though, or on PM if you prefer.
Reply 10
fancy helping me through my coursework when your free?
I'm not sure that would be entirely ethical. Also I do have other things to do with my time. However I am willing to help you understand how to program in Java, by answering specific questions that relate to how things work.
Reply 12
Thanks.

The number of points gained by this team, stored as an int. THREE points are
awarded for a win. ONE point is awarded for a draw.

I have created a field:

private int points;

I am struggling as to where I write how much points are awarded for a win and for a draw, will it be a constructor?
Reply 13
Just 2 members: recordWin() and recordDraw() ?
Reply 14
Yeah i've got that sorted thanks :smile:

The next thing i'm having trouble with is:


A class called Group keeps track of the league table for a group of teams.

The Group class has two fields. The name of the group stored as a char and an array
of Team objects representing the teams in the group.

The size of the array will be equal to the number of teams in the group. The array should be created in the constructor.

The constructor for the Group class has TWO parameters.

The name of the group as a char value and the number of teams in the group as an int value.

The values passed to this constructor should be obtained from the Results class.

So far I have:

public class Group
{
private char group;
private int[] teams;

/**
* Constructor for objects of class Group
*/
public Group(char group, int number)
{
// initialise instance variables
teams = new int[4];


Is this correct?

and how do I do this bit 'The values passed to this constructor should be obtained from the Results class.'


Thanks for any help :smile:
Reply 15
Anyone please?
Just completed that was tricky considering youve been learning java for just 6 weeks now lol where you from?

Quick Reply

Latest

Trending

Trending