CIS260 – Lecture Notes – April 12th – Modeling on 2D arrays

Simulating a golf score card.

ScoreCard Class

package cis260.matos;

publicclassScoreCard {

// class variables

publicstaticintTOTAL_HOLES = 3;

publicstaticint[] yards = { 390, 378, 471, 393, 138,

568, 184, 443, 353 };

publicstaticint[] par = { 4, 4, 5, 4, 3, 5, 3, 4, 4 };

privateint [][] tabStrokes;

// constructor(s)

publicScoreCard()

{

tabStrokes = newint[4] [TOTAL_HOLES];

}

// mutators (get/set)

publicvoidsetScore(int player, int hole, int strokes)

{

tabStrokes[player] [hole] = strokes;

}

publicintgetScore (int player, int hole)

{

returntabStrokes[player][hole];

}

// user-defined methods

publicinttotalScore(int player)

{

int result = 0;

for (int hole=0; hole < TOTAL_HOLES; hole++)

{

result += tabStrokes[player][hole];

}

return result;

}

publicinttotalPar()

{

int result=0;

for (int hole = 0; hole < TOTAL_HOLES; hole++)

{

result += par[hole];

}

return result;

}

publicvoidshowScoreCard()

{

for (int hole=0; hole < TOTAL_HOLES; hole++)

{

System.out.println("Hole: " + hole

+ " Yards: " + yards[hole]

+ "Par: " + par[hole] );

for(int player = 0; player < 4; player++)

{

System.out.println("Player " + player + "\t"

+ tabStrokes[player][hole]);

}

}

// summarize individual scores

System.out.println("");

for (int player=0; player < 4; player++)

{

System.out.println("Total for player " + player

+ "\t" + this.totalScore(player)

+ " / " + this.totalPar() );

}

}

}

ScoreCard Driver

package cis260.matos;

publicclassScoreCardDriver {

/**

* testing the ScoreCard class

*/

publicstaticvoid main(String[] args) {

ScoreCard sc = newScoreCard();

//TODOScoreCard sc = new ScoreCard("Steve", "Ben", "Sharen", "Tom");

sc.setScore(0, 0, 4);

sc.setScore(1, 0, 5);

sc.setScore(2, 0, 6);

sc.setScore(3, 0, 3);

sc.setScore(0, 1, 5);

sc.setScore(1, 1, 7);

sc.setScore(2, 1, 4);

sc.setScore(3, 1, 4);

sc.setScore(0, 2, 3);

sc.setScore(1, 2, 3);

sc.setScore(2, 2, 6);

sc.setScore(3, 2, 5);

sc.showScoreCard();

}

}

Console

Hole: 0 Yards: 390Par: 4

Player 04

Player 15

Player 26

Player 33

Hole: 1 Yards: 378Par: 4

Player 05

Player 17

Player 24

Player 34

Hole: 2 Yards: 471Par: 5

Player 03

Player 13

Player 26

Player 35

Total for player 012 / 13

Total for player 115 / 13

Total for player 216 / 13

Total for player 312 / 13