Unit 5 - Classes
Notes
-
Parts of classes
- Definition
- Signature
- Method Body
-
camelCase - start with lowercase, then every word is uppercase to separate word
-
return void - returns nothing, code just executes
-
static --> do not require an object to access the methods in the class
- non-static --> requires an object to acces methods in class
public class WordMatch
{
private String _secret;
public WordMatch(String secret)
{
_secret = secret;
}
public void scoreGuess(String guess)
{
int counter = 0;
// iteration for first substring index
for (int loop = 0; loop < _secret.length(); loop++)
{
// loop for second substring index
for (int loop2 = loop + 1; loop2 < _secret.length() + 1; loop2++)
{
// if statement
if (guess.equals(_secret.substring(loop, loop2)))
{
counter++;
}
}
}
// Returning a point value
int points = counter * guess.length() * guess.length();
System.out.println("" + guess + " = " + points);
// return points;
}
}
WordMatch game = new WordMatch("mississippi");
game.scoreGuess("i");
game.scoreGuess("iss");
game.scoreGuess("issipp");
game.scoreGuess("mississippi");
System.out.printf("\n");
WordMatch game2 = new WordMatch("aaaabb");
game.scoreGuess("a");
game.scoreGuess("aa");
game.scoreGuess("aaa");
game.scoreGuess("aabb");
game.scoreGuess("c");