Reputation:
I am trying to learn Java by myself and I am doing a problem. I am creating a program that gets input from the user to make two Team objects and then calculates the winner. Each team has a name and location, represented as String objects, plus offence and defence capabilities represented as double values, randomly initialized between 0 and 1 using the luck() method.
import java.util.Scanner;
public class Team {
public String name;
public String location;
public double offense;
public double defense;
public String awayName;
public String awayLocation;
public double awayOffense;
public double awayDefense;
/**
* Create a team with specified name and location, and with offense and defense capabilities
* randomly initialized using the luck() method.
*/
public Team(String name, String location) {
this.name = name;
this.location = location;
}
/**
* The luck() method returns a random value between 0 and 1, using Math.random().
*
* @returns a real value in range [0,1]
*/
public double luck() {
return Math.random();
}
/**
* Run a competition against the specified visiting team
*
* @param other the team to play against
* @returns the winner team
*/
Team play(Team visitor) {
home = (this.offense + this.defense + 0.2) * this.luck();
away = (visitor.offense + visitor.defense) * visitor.luck();
return winner;
}
/**
* Run a competition between two teams specified on standard input.
* Print statistics of the winner.
* <p>
* Each team is read in the following format :
* <pre>
* <name>
* <location>
* </pre>
*/
public static void main(String[] args) {
System.out.println("Enter name and location for home team (on separate lines)");
Scanner tn = new Scanner(System.in);
Team team = new Team("Name","Location");
team.name = tn.nextLine();
Scanner tl = new Scanner(System.in);
team.location = tl.nextLine();
Team home = new Team(team.name, team.location);
System.out.println("Enter name and location for away team (on separate lines)");
Scanner atn = new Scanner(System.in);
team.awayName = atn.nextLine();
Scanner atl = new Scanner(System.in);
team.awayLocation = atl.nextLine();
Team away = new Team(team.awayName, team.awayLocation);
System.out.println("Home team is: " + team.name+ " from " + team.location + " rated <home_team_offense> (offense) + <home_team_defense> (defense)");
System.out.println("Away team is: " + team.awayName+ " from " + team.awayLocation + " rated <home_team_offense> (offense) + <home_team_defense> (defense)");
// Create a home and an away team object with the given data.
// Call the home team's play() method with the away team as visitor parameter.
// Print out the winner.
The commented parts tell me what I need to do although I am not sure where to go from here. I am stuck and think that what I have done so far may not even be correct, can anyone give me a push in the right direction? Please help! Thanks!
Upvotes: 0
Views: 158
Reputation: 40438
You've come a good way yourself.
Once you put in the team statistics for offense and defense (why not add them into the constructor arguments?), you can play them against each other like this:
Team winner = home.play(away);
And your play method should look something like this:
Team play(Team visitor) {
double home = (this.offense + this.defense + 0.2) * this.luck();
double away = (visitor.offense + visitor.defense) * visitor.luck();
Team winner = (home > away ? this : visitor);
return winner;
}
You asked how to randomly set the ratings - the constructor would be an ideal place to do this if you want it to only happen once:
public Team(String name, String location) {
this.name = name;
this.location = location;
awayOffense = luck();
awayDefense= luck();
}
Upvotes: 2
Reputation: 1127
I might be wrong, but you don't initialize the offence and defence with a value. It may be better this way:
public Team(String name, String location, double offence, double defence) {
this.name = name;
this.location = location;
this.offence = offence;
this.defence = defence;
}
Upvotes: 0