-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPitcher.java
More file actions
66 lines (62 loc) · 2.08 KB
/
Pitcher.java
File metadata and controls
66 lines (62 loc) · 2.08 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
/**
* Pitcher class simulates a baseball pitcher that can throw pitches.
*
* @author Justin RIley
* @version 1.0
* @since 2023-09-20
*/
public class Pitcher {
private String name;
private int pitchCounter;
/**
* Constructs a new Pitcher object that represents a baseball
* pitcher that can throw baseballs. The pitcher's name must be
* supplied as an argument. The object's pitchCounter is
* initially set to zero.
*
* @param String name Name of the pitcher. Can be one-word or multi-word name.
*/
public Pitcher(String name){
this.name = name;
this.pitchCounter = 0;
}
/**
* String getName() gets the name of the pitcher
* @return String name of pitcher
*/
public String getName(){
return name;
}
/**
* The getPitch method looks at the player's name and the
* current pitch number. If that number letter in the player's
* name is a vowel, then they throw a great pitch. If that
* number letter in the name is a consenent, then they throw a
* bad pitch. For example, "Eli" would throw great pitch, bad
* pitch, then great pitch. Finally, the pitchCounter is
* incremented.
*
* @return String a String that says if they threw a bad pitch or great pitch.
*/
public String getPitch(){
String result = "Oof! " + this.name + " throws a bad pitch";
if(isVowel(this.name, this.pitchCounter)){
result = this.name + " throws a GREAT pitch!";
}
//this.pitchCounter++;
return result;
}
/**
* The static isVowel method takes a String word and an int num.
* If the letter at index location num is a vowel, returns true
* otherwise return false.
* @param String a String word that might contain vowels
* @param int an int num that is the index location to look at
* @return boolean If the letter at index location num is a vowel, returns true. Otherwise, false.
*/
private static boolean isVowel(String word, int num){
String vowels = "aeiou";
String letter = word.substring(num % word.length(),num % word.length() + 1);
return vowels.indexOf(letter.toLowerCase()) >= 0;
}
}