199k views
0 votes
Write a print10() method that prints out the first 10 words of the dictionary array.

public class SpellChecker
{
private String[] dictionary = {"the","of","and","a","to","in","is","you","that","it","he","was","for","on","are","as","with","his","they","I","at","be","this","have","from","or","one","had","by","word","but","not","what","all","were","we","when","your","can","said","there","use","an","each","which","she","do","how","their","if","will","up","other","about","out","many","then","them","these","so","some","her","would","make","like","him","into","time","has","look","two","more","write","go","see","number","no","way","could","people","my","than","first","water","been","call","who","oil","its","now","find","long","down","day","did","get","come","made","may","cat","dog","cats","dogs"};

/* 1. Write a print10() method that prints out the first
* 10 words of the dictionary array.
*/

/* 2. Write a spellcheck() method that takes a word as a
* parameter and returns true if it is in the dictionary array.
* Return false if it is not found.
*/

public static void main(String[] args)
{
SpellChecker checker = new SpellChecker();
/* Uncomment to test Part 1
checker.print10();
*/

/* Uncomment to test Part 2
String word = "catz";
if (checker.spellcheck(word) == true)
{
System.out.println(word + " is spelled correctly!");
}
else
{
System.out.println(word + " is misspelled!");
}
*/

// 3. optional and not autograded
// checker.printStartsWith("a");
}
}

User Eric Wich
by
7.4k points

1 Answer

6 votes

Final answer:

The print10() method in the SpellChecker class can be created to iterate through the first 10 elements of the dictionary array and print them out. This is achieved by using a simple for loop within the method.

Step-by-step explanation:

The correct answer is option for writing a method called print10() that prints the first 10 words of the dictionary array in the SpellChecker class is simple. You would define the method within the SpellChecker class, loop through the first 10 elements of the dictionary array, and print each one. Below is the sample code for the print10() method:

public void print10() {
for(int i = 0; i < 10; i++) {
System.out.println(dictionary[i]);
}
}

This method uses a for loop to iterate over the first 10 indices of the dictionary array and prints each word to the console.

The print10() method can be implemented in the SpellChecker class by using a for loop to iterate through the first 10 elements of the dictionary array and print them out. Here is an example implementation:

public void print10() {

for (int i = 0; i < 10; i++) {

System.out.println(dictionary[i]);

}

}

This method will print out the first 10 words in the dictionary array.

User Swordfish
by
8.6k points