Case Study Java Classes and Objects
Case Study: Creating a Class
- Team class
UML Diagram
Team |
-wins: int -losses: int |
+Team(w: int, l: int) +getWins(): int +getLosses():int +getWinningPercentage(): double +setWins(w: int): void +setLosses(l: int): void |
UML Diagrams:
- | private |
+ | public |
+Team(w: int, l: int) | constructor |
Constructors
- Constructor - Method used to perform operations on a newly created object
-typically used to initialize instance fields
- Every class needs a constructor. Java creates a 'default constructor' when no constructor is given.
- Default Constructor
Numeric Fields | 0 |
boolean | false |
Reference Variables | null |
Team.java
1 /* 2 * To change this license header, choose License Headers in Project Properties. 3 * To change this template file, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6 package worldseries; 7 8 /** 9 * 10 * @author student 11 */ 12 public class Team 13 { 14 //These are the fields. 15 private int wins; 16 private int losses; 17 private String name; 18 //private double winningPercentage; 19 20 //This is a constructor. 21 public Team(String n, int w, int l){ 22 name = n; 23 wins = w; 24 losses = l; 25 } 26 27 28 public String getName(){ 29 return name; 30 } 31 32 public int getWins(){ 33 return wins; 34 } 35 36 public int getLosses(){ 37 return losses; 38 } 39 40 public double getWinningPercentage(){ 41 double total = wins + losses; 42 return wins / total; 43 } 44 45 public void printStats(){ 46 System.out.println("Name: " + name); 47 System.out.println("Wins: " + wins); 48 System.out.println("Losses: " + losses); 49 System.out.println("WP: " + getWinningPercentage()); 50 51 } 52 53 public void setWins(int w){ 54 wins = w; 55 } 56 57 public void setLosses(int l){ 58 losses = l; 59 } 60 } 61
WorldSeries.java
1 /* 2 * To change this license header, choose License Headers in Project Properties. 3 * To change this template file, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6 package worldseries; 7 8 /** 9 * 10 * @author student 11 */ 12 public class WorldSeries { 13 14 /** 15 * @param args the command line arguments 16 */ 17 public static void main(String[] args) { 18 Team cubs = new Team("Cubs", 92, 70); 19 Team nationals = new Team("Nationals", 97, 65); 20 Team padres = new Team("Indians", 102, 60); 21 22 //Starting statistics 23 cubs.printStats(); 24 25 nationals.printStats(); 26 27 padres.printStats(); 28 29 System.out.println("Now entering the playoffs..."); 30 31 //cubs win, nationals lose, padres lose 32 //cubs win 33 cubs.setWins(cubs.getWins() + 1); 34 35 //nationals lose 36 nationals.setLosses(nationals.getLosses() + 1); 37 38 //padres lose 39 padres.setLosses(padres.getLosses() + 1); 40 41 //Ending statistics 42 cubs.printStats(); 43 44 nationals.printStats(); 45 46 padres.printStats(); 47 48 Team theWinner = winner(cubs, nationals, padres); 49 //Another way to print the winner: 50 System.out.println("The winner is " + theWinner.getName()); 51 52 } 53 54 /** 55 * This method takes in three team objects and prints the name of the team 56 * object with the greatest number of wins and returns that team object 57 * 58 * @param a The first team 59 * @param b The second team 60 * @param c The third team 61 * @return The winning team 62 */ 63 public static Team winner(Team a, Team b, Team c) { 64 if (a.getWins() > b.getWins() && a.getWins() > c.getWins()) { 65 System.out.println(a.getName()); 66 return a; 67 } else if (b.getWins() > a.getWins() && b.getWins() > c.getWins()) { 68 System.out.println(b.getName()); 69 return b; 70 } else { 71 System.out.println(c.getName()); 72 return c; 73 } 74 75 } 76 77 } 78