Collegeboard Unit 2 - Using Objects
A notebook to document learning of objects
- 2.1: What are objects?
- 2.2: Initializing/Constructing
- 2.3: Methods
- 2.4 Methods with parameters
- 2.7 String Methods + comparing
- HOMEWORK
- Google Form
2.1: What are objects?
Objects are a reference type, similar to something like a String class. In fact, a String is an object. However, Objects contain primitive data types as well as reference data types. When you reference an object, you are referencing the point at which it is located.
Objects are defined by a class, which describes what attributes the object has and what things it can do with methods.
- House metaphor: blueprint is the class, each house is an object
An object is not a type of a class, but it is a specific instance.
2.3: Methods
Methods are code that do something, sometimes taking parameters. THey can change data, print text, etc.
There are also void and non void methods. Void methods don't return anything, but change data or print things. The void keyword is used.
Methods can be static or non static.
Static methods are general to the class, and do not have to be in any specific object. It basically changes something general. To make a method static, the static keyword is added.
String a = "hello";
String b = "world";
String result = a + " " + b;
System.out.println(result);
Mixing types in concatenation is also possible, but often parentheses are needed to do calculations if using multiple primitives. Otherwise, it will take each number as a string and just append it.
String message = "12" + 4 + 3; // print out 1243
System.out.println(message);
String message = "12" + (4 + 3); // print out 127 with the parenthesis
System.out.println(message);
String a = "blah";
System.out.println(a.equals("bleh")); // false
System.out.println(a.equals("blah")); // true
compareTo returns an integer, comparing the string relative to the string within the parentheses. It returns a negative if the original string comes before the one inside, or positive if its afetr.
String a = "blah";
System.out.println(a.compareTo("bleh")); // negative
System.out.println(a.compareTo("blah")); // 0
System.out.println(a.compareTo("bl")); // positive
comparing numbers The normal operations in if statements still apply here, like:
- <
- >
- <=
-
=
- !=
- ==
Using these will return true or false depending on if it works.
Math.random();
public class WordMatch
{
/** The secret string. */
private String secret;
/** Constructs a WordMatch object with the given secret string of lowercase letters. */
public WordMatch(String word)
{
/* implementation not shown */
this.secret = word;
}
/** Returns a score for guess, as described in part (a).
* Precondition: 0 < guess.length() <= secret.length()
*/
// START OF SOLUTION A
public int scoreGuess(String guess) {
// Tracks number of times the substring appears
int occurrences = 0;
// secret.length() - guess.length() to avoid index overflow with substring later on
for (int i = 0; i <= secret.length() - guess.length(); i++) {
// Checks if the substring is equal to the guess
if (secret.substring(i, i + guess.length()).contains(guess)) {
occurrences++;
// Moves forward by guess.length() - 1 to move forward in the string (and avoid repeats)
// -1 is included because i++
i += guess.length() - 1;
}
}
return occurrences * guess.length() * guess.length();
}
// END OF SOLUTION A
// START OF SOLUTION B
public String findBetterGuess(String guess1, String guess2)
{ /* to be implemented in part (b) */
if (scoreGuess(guess1) > scoreGuess(guess2)) {
return guess1;
} else if (scoreGuess(guess2) > scoreGuess(guess1)) {
return guess2;
} else if (guess1.compareTo(guess2) > 0) {
return guess1;
} else {
return guess2;
}
}
// END OF SOLUTION B
public static void main(String[] args) {
WordMatch testA = new WordMatch("mississippi");
System.out.println(testA.scoreGuess("issippi"));
System.out.println(testA.scoreGuess("mississippi"));
WordMatch testB = new WordMatch("concatenation");
System.out.println(testB.findBetterGuess("ten" , "nation"));
System.out.println(testB.findBetterGuess("con", "cat"));
}
}
WordMatch.main(null);
public int scoreGuess(String guess) {
// Tracks number of times the substring appears
int occurrences = 0;
// secret.length() - guess.length() to avoid index overflow with substring later on
for (int i = 0; i <= secret.length() - guess.length(); i++) {
// Checks if the substring is equal to the guess
if (secret.substring(i, i + guess.length()).contains(guess)) {
occurrences++;
// Moves forward by guess.length() - 1 to move forward in the string (and avoid repeats)
// -1 is included because i++
i += guess.length() - 1;
}
}
return occurrences * guess.length() * guess.length();
}
The condition secret.substring(i, i + guess.length()).contains(guess)
checks if the substring the length of the guess contains the guess itself. If it does, it adds one to the occurences, and moves the pointer to avoid repeats.
Part B
findBetterGuess, returns better guess of two strings. If they are equal, alphabetically greater guess is returned
public String findBetterGuess(String guess1, String guess2)
{ /* to be implemented in part (b) */
if (scoreGuess(guess1) > scoreGuess(guess2)) {
return guess1;
} else if (scoreGuess(guess2) > scoreGuess(guess1)) {
return guess2;
} else if (guess1.compareTo(guess2) > 0) {
return guess1;
} else {
return guess2;
}
}
The first two if statements are purely for returning the higher score guess. However, if they are equal, then the compareTo metod finds the alphabetically greater guess.