76 lines
		
	
	
	
		
			1.8 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			76 lines
		
	
	
	
		
			1.8 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
| interface Sports {
 | |
|     int getNumberOfGoals();
 | |
|     String dispTeam();
 | |
| }
 | |
| 
 | |
| class Hockey implements Sports {
 | |
| 
 | |
|     private int goals;
 | |
|     private String team;
 | |
| 
 | |
|     public Hockey(int goals, String team) {
 | |
|         this.goals = goals;
 | |
|         this.team = team;
 | |
|     }
 | |
| 
 | |
|     public int getNumberOfGoals() {
 | |
|         return goals;
 | |
|     }
 | |
| 
 | |
|     public String dispTeam() {
 | |
|         return team;
 | |
|     }
 | |
| }
 | |
| 
 | |
| class Football implements Sports {
 | |
| 
 | |
|     private int goals;
 | |
|     private String team;
 | |
| 
 | |
|     public Football(int goals, String team) {
 | |
|         this.goals = goals;
 | |
|         this.team = team;
 | |
|     }
 | |
| 
 | |
|     public int getNumberOfGoals() {
 | |
|         return goals;
 | |
|     }
 | |
| 
 | |
|     public String dispTeam() {
 | |
|         return team;
 | |
|     }
 | |
| }
 | |
| 
 | |
| public class SportsMatch {
 | |
| 
 | |
|     public static void main(String[] args) {
 | |
|         Hockey team1 = new Hockey(3, "India");
 | |
|         Hockey team2 = new Hockey(2, "England");
 | |
| 
 | |
|         Football team3 = new Football(2, "AFC Richmond");
 | |
|         Football team4 = new Football(1, "West Ham United");
 | |
| 
 | |
|         System.out.println("Hockey Match:");
 | |
|         displayWinner(team1, team2);
 | |
| 
 | |
|         System.out.println("\nFootball Match:");
 | |
|         displayWinner(team3, team4);
 | |
|     }
 | |
| 
 | |
|     public static void displayWinner(Sports team1, Sports team2) {
 | |
|         System.out.println(
 | |
|             team1.dispTeam() + " goals: " + team1.getNumberOfGoals()
 | |
|         );
 | |
|         System.out.println(
 | |
|             team2.dispTeam() + " goals: " + team2.getNumberOfGoals()
 | |
|         );
 | |
| 
 | |
|         if (team1.getNumberOfGoals() > team2.getNumberOfGoals()) {
 | |
|             System.out.println("Winner: " + team1.dispTeam());
 | |
|         } else if (team2.getNumberOfGoals() > team1.getNumberOfGoals()) {
 | |
|             System.out.println("Winner: " + team2.dispTeam());
 | |
|         } else {
 | |
|             System.out.println("It's a tie!");
 | |
|         }
 | |
|     }
 | |
| }
 | 
