-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathHomeRunDerby.java
More file actions
77 lines (65 loc) · 2.12 KB
/
Copy pathHomeRunDerby.java
File metadata and controls
77 lines (65 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* represents a home run derby that is an
* event where a batter hits a ball thrown
* by the pitcher and tries to hit a home run.
*/
public class HomeRunDerby {
/* Variables */
private Pitcher pitcher;
private Batter batter;
private int pitchNumber;
/* Constructor(s) */
/**
*
* Constructs a new HomeRunDerby object that represents a home run derby
* that is an event where a batter hits a ball thrown by the pitcher and
* tries to hit a home run. A pitcher and batter object must be supplied
* as arguments.
*
* @param Pitcher _pitcher A pitcher object
* @param Batter _batter A batter object
*
*/
public HomeRunDerby(Pitcher _pitcher, Batter _batter) {
this.pitcher = new Pitcher(_pitcher.getName());
this.batter = new Batter(_batter.getName());
this.pitchNumber = 0;
}
/* Methods */
/** void nextPitch()
* increments pitchNumber & moves the pitchers and batters to the next pitch/swing.
*/
public void nextPitch(){
pitchNumber++;
pitcher.nextPitch();
batter.nextSwing();
}
/** string getResults()
gets a swing and a pitch then returns the resulting hit based off those values
*/
public String getResults(){
String pitch = this.pitcher.getPitch();
String swing = this.batter.getSwing();
if (this.pitcher.isGoodPitch() && this.batter.isGoodSwing()){
return pitch + "\n" + swing + "\n" + "It's a hit\n";
}
if (this.pitcher.isGoodPitch() && !this.batter.isGoodSwing()){
return pitch + "\n" + swing + "\n" + "It's a strike\n";
}
if (!this.pitcher.isGoodPitch() && this.batter.isGoodSwing()){
return pitch + "\n" + swing + "\n" + "It's a homerun\n";
}
if (!this.pitcher.isGoodPitch() && !this.batter.isGoodSwing()){
return pitch + "\n" + swing + "\n" + "It's a foul ball\n";
}
}
/** void setPitchNumer()
* sets the pitch number the game is currently on
* @param new pitch number
*/
public void setPitchNumber(int _pitchNumber){
pitchNumber = _pitchNumber;
this.pitcher.setPitchNumber(_pitchNumber);
this.batter.setPitchNumber(_pitchNumber);
}
}