Introduction to Java: A First Look at Classes

A First Look at Classes


Team

    • wins: int
    • losses: int

+ Team(w: int, l: losses)

+ getWins(): int

+getLosses(): int

+ setWins(w: int)

+ set Losses(l: int)

+ getWinningPercentage(): double


C:\Users\alexj\Documents\NetBeansProjects\Projs\MarchMadness\src\marchmadness\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 marchmadness;
 7 
 8 /**
 9  *
10  * @author alexj
11  */
12 public class Team {
13     
14     //instance fields
15     private int wins;
16     private int losses;
17     private String name;
18     
19     //instance methods
20     
21     public Team(int w, int l, String n){
22         wins = w;
23         losses = l;
24         name = n;
25     }
26     
27     //no-arg constructor
28     public Team(){
29         wins = 0;
30         losses = 0;
31     }
32     
33     public int getWins(){
34         return wins;
35     }
36     
37     public int getLosses(){
38         return losses;
39     }
40     
41     public double getWinningPercentage(){
42         
43         return (double) wins / (wins + losses);    
44     }
45     
46     public void setWins(int w){
47         wins = w;
48     }
49     
50     public void setLosses(int l){
51         losses = l;
52     }
53     
54     public String toString(){
55         return name;
56     }
57     
58 }
59 

C:\Users\alexj\Documents\NetBeansProjects\Projs\MarchMadness\src\marchmadness\MarchMadness.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 marchmadness;
 7 
 8 /**
 9  *
10  * @author alexj
11  */
12 public class MarchMadness {
13 
14     /**
15      * @param args the command line arguments
16      */
17     public static void main(String[] args) {
18         /*
19         Jawhawks vs. Penn (Jayhawks)
20         Jawhawks vs. Seton Hall (Jayhawks)
21         */
22         
23         //instantiate a jayhawks object 
24         //at beginning of tournament
25         
26         Team jayhawks = new Team(0, 0, "Jayhawks"); //constructor #2 (no-arg)
27         Team penn = new Team(0, 0, "Penn"); //constructor #2 (no-arg)
28         Team setonHall = new Team(0, 0, "Seton Hall"); //constructor #2 (no-arg)
29         
30         //Round 1: Jayhawks beat Penn
31         jayhawks.setWins(1);
32         penn.setLosses(1);
33         setonHall.setWins(1);
34         
35         //Round 2: Jayhawks beat Seton Hall
36         jayhawks.setWins(2);
37         setonHall.setLosses(1);
38         
39         System.out.println("Current Stats:");
40         System.out.println("Jayhawks:");
41         //wins
42         System.out.printf("Wins: %d \n", 
43                 jayhawks.getWins());
44         //losses
45         System.out.printf("Losses: %d \n", 
46                 jayhawks.getLosses());
47         //winningPercentage
48         System.out.printf("Winning Percentage: %.2f \n", 
49                 jayhawks.getWinningPercentage());
50        
51         //call the winner method
52         Team result = winner(jayhawks, setonHall, penn);
53         System.out.println("The winning team is " + result.toString());
54     }
55     
56      /**
57          * This method accepts three team objects and returns
58          * the team object with the greatest number of wins
59          * @param a The first team
60          * @param b The second team
61          * @param c The Third team
62          * @return the team with the most wins
63          */
64         public static Team winner(Team a, Team b, Team c){
65             Team winningTeam = new Team();
66             if (a.getWins() > b.getWins() && a.getWins() > c.getWins()){
67                 winningTeam = a;
68             }
69             else if (b.getWins() > a.getWins() && b.getWins() > c.getWins()){
70                 winningTeam = b;
71             }
72             else
73                winningTeam = c;
74             
75             return winningTeam;
76         }
77     
78     
79 }
80 

Return to top