Skip to content

Instantly share code, notes, and snippets.

@noelje
Last active March 9, 2022 07:00
Show Gist options
  • Save noelje/d18736649828a72d488315e388b306e1 to your computer and use it in GitHub Desktop.
Save noelje/d18736649828a72d488315e388b306e1 to your computer and use it in GitHub Desktop.
for tutor to test
package footy;
import java.util.LinkedList;
public class FootyScore {
public static void main(String[] args) {
// a private data structure sufficient to keep track of the team's score;
// chosen DS: linked list
LinkedList<Integer> teamScore = new LinkedList<Integer>();
// a parameterless public method called getPoints
// returns the team's total score in points;
public int getPoints(){
// total points earned
int total = 0;
if((teamScore.size()) > 0){
for (int i = 0; i < teamScore.size(); i++) {
total += teamScore.get(i);
} // end loop
} else {
System.out.println("NO SCORES!");
}
return total;
} // end func
// a parameterless public method called kickGoal
// records the fact that the team has kicked a goal;
public void kickGoal() {
// add 6 points to pointsScored
teamScore.add(6);
}
// a parameterless public method called kickBehind
// records the fact that the team has kicked a behind;
public void kickBehind() {
// add 1 point to pointsScored
teamScore.add(1);
}
// declare a parameterless public method called sayScore
// returns a character string
public String sayScore(){
// no. of goals kicked
int n_goals = 0;
for (int i = 0; i < teamScore.size(); i++){
if(teamScore.contains(6)) {
n_goals++;
}
}
String str_goals = String.valueOf(n_goals);
// no. of behinds kicked
int n_behinds = 0;
for (int i = 0; i < teamScore.size(); i++){
if(teamScore.contains(1)) {
n_behinds++;
}
}
String str_behind = String.valueOf(n_behinds);
int total_points = this.getPoints();
String str_points = String.valueOf(total_points);
return (str_goals + ", " + str_behind + ", " + str_points);
} // end SayScore
// declare a predicate (boolean-valued function) called inFrontOf
// accepts a FootyScore object as its parameter
// returns true if and only if (iff)
// this team's score exceeds that of the team provided as an argument.
public boolean inFrontOf(FootyScore o) {
if(this.getPoints() > o.getPoints()) {
return true;
} else {
return false;
}
}
} // end main
} // end class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment