ClassName objectName = new ClassName();
ClassName
refers to the name of the class you want to instantiate.objectName
is the reference variable that points to the newly created object.new ClassName()
allocates memory and initializes the object.0
, null
, etc.).public class MyClass {
private int value;
// Parameterized constructor
public MyClass(int initialValue) {
value = initialValue;
}
}
public class WordPair {
/** Constructs a WordPair object. */
public WordPair(String first, String second)
{
/* implementation not shown */
}
/** Returns the first string of this WordPair object. */
public String getFirst()
{
/* implementation not shown */
} /** Returns the second string of this WordPair object. */
public String getSecond()
{
/* implementation not shown */
}
}
public class WordPairList {
/** The list of word pairs, initialized by the constructor. */
private ArrayList<WordPair> allPairs;
/** Constructs a WordPairList object as described in part (a). * Precondition: words.length >= 2 */
public WordPairList(String[] words)
{
/* to be implemented in part (a) */
}
/** Returns the number of matches as described in part (b). */
public int numMatches() {
/* to be implemented in part (b) */
}
}
public class WordPair {
private String First;
private String Second;
/** Constructs a WordPair object. */
public WordPair(String first, String second)
{
this.First = first;
this.Second = second;
}
/** Returns the first string of this WordPair object. */
public String getFirst()
{
return this.First;
} /** Returns the second string of this WordPair object. */
public String getSecond()
{
return this.Second;
}
}
public class WordPairList {
/** The list of word pairs, initialized by the constructor. */
private ArrayList<WordPair> allPairs;
/** Constructs a WordPairList object as described in part (a). * Precondition: words.length >= 2 */
public WordPairList(String[] words)
{
this.allPairs = new ArrayList<WordPair>(); //Some people miss this for some reason. You need an array list so that way you can add more pairs needed in the future.
for (int i = 0; i<words.length;i++){ //Goes through each word
for(int j = i+1;j<words.length;j++){ //This is the nested for loop; The i+1 makes it so that j doesn't overlap itself, otherwise, it would think that it was pairing with itself; Many people miss this surprisingly
this.allPairs.add(new WordPair(words[i],words[j]));
}
}
}
/** Returns the number of matches as described in part (b). */
public int numMatches() {
int result = 0;
for (WordPair wp:allPairs){ //this for iterates through each word pair in the allPairs arraylist
if(wp.getFirst() == wp.getSecond()){ //This makes sure that the words are equal to each other to create a valid pair.
result += 1; //This increments for every pair
}
}
return result;
}
}
//Main Function
//Example 1:
String[] wordNums = {"one", "two", "three"};
WordPairList exampleOne = new WordPairList(wordNums);
//Example 2
String[] phrase = {"the", "more", "the", "merrier"};
WordPairList exampleTwo = new WordPairList(phrase);
//Example 3:
String[] moreWords = {"the", "red", "fox", "the", "red"};
WordPairList exampleThree = new WordPairList(moreWords);
System.out.println("Here are the results for example 2 and 3");
System.out.println(exampleOne.numMatches());
System.out.println(exampleTwo.numMatches());
System.out.println(exampleThree.numMatches());
Here are the results for example 2 and 3
0
1
2
Below is an example of an overloaded constructor. It’s identical to the code above, except now when creating a WordPairList object if we don’t pass through any words, tester words will be given and used.
public class WordPairList {
/** The list of word pairs, initialized by the constructor. */
private ArrayList<WordPair> allPairs;
/** Constructs a WordPairList object as described in part (a). * Precondition: words.length >= 2 */
public WordPairList(String[] words)
{
this.allPairs = new ArrayList<WordPair>();
for (int i = 0; i<words.length;i++){
for(int j = i+1;j<words.length;j++){
this.allPairs.add(new WordPair(words[i],words[j]));
}
}
}
public WordPairList()
{
String[] testerWords = {"test", "testing", "best", "test", "hello"};
this.allPairs = new ArrayList<WordPair>();
for (int i = 0; i<testerWords.length;i++){
for(int j = i+1;j<testerWords.length;j++){
this.allPairs.add(new WordPair(testerWords[i],testerWords[j]));
}
}
}
/** Returns the number of matches as described in part (b). */
public int numMatches() {
int result = 0;
for (WordPair wp:allPairs){ //this for iterates through each word pair in the allPairs arraylist
if(wp.getFirst() == wp.getSecond()){ //This makes sure that the words are equal to each other to create a valid pair.
result += 1; //This increments for every pair
}
}
return result;
}
}
String[] words = {"the", "more", "the", "merrier"};
WordPairList exampleOne = new WordPairList(words);
WordPairList exampleTwo = new WordPairList();
System.out.println("Here are the results for the examples");
System.out.println(exampleOne.numMatches());
System.out.println(exampleTwo.numMatches());
Here are the results for the examples
1
1
GridWorld Case Study is a program that was previously used to supplement the AP CSA curriculum and was on the AP Exam from 2008 to 2014. While it is no longer tested, it is still a useful program to help prepare for the AP Exam as it provides a graphical environment that shows objects inhabiting and interacting with each other in a grid. For those interested, you can start here.