Question
Please help with my java program! Thank you! There is a Player class that implements Comparable and that includes the properties my500s, & my 100s (forget 50's, 20's, 10's, 5's and 1's for this exercise) that refer to the number of $500 bills & $100 bills that a Monopoly player has at any given moment during a game. Write out a complete compare To method so that it compares Player objects and considers a Player object with more money to be greater than a Player object with less money
Explanation / Answer
public class Player implements Comparable { private int my500s = 0; private int my100s = 0; public int nTMoney =0; public Player(int n500,int n100) { this.my100s = n100; this.my500s = n500; nTMoney = my500s*500 + my100s * 100; } public int compareTo(Player objTarget) { return (this.nTMoney -objTarget.nTMoney); } } public class Main { public static void main(String[] args) { Player objPlayer1=newPlayer(3, 2); Player objPlayer2=newPlayer(2, 4); Player objPlayer3=newPlayer(2, 8); if(objPlayer1.compareTo(objPlayer2) >=0) { System.out.println("Player 1 has more money than Player 2"); } else { System.out.println("Player 1 has lesser money than Player 2"); } if(objPlayer1.compareTo(objPlayer3) >= 0) { System.out.println("Player 1 has more money than Player 3"); } else { System.out.println("Player 1 has lesser money than Player 3"); } } }