Question

Hi I really need help with the methods for this lab for a computer science class....

Hi I really need help with the methods for this lab for a computer science class. Thanks

Main (WordTester - the application)

  • is complete and only requires uncommenting to test the Word and Words classes as they are completed. The needed imports, class headings, method headings, and block comments are provided for the remaining classes. Javadocs are also provided for the Word and Words classes.

Word

  • The Word class represents a single word. It is immutable. You have been provided a skeleton for this class. Complete the Word class using the block comments, Javadocs, and the following instructions.

1. A String instance variable named word which will contain the word is declared and initialized in the constructor method for you.

2. The one parameter constructor to initialize the word instance variable is also completed.

3. Complete the getLength method to return the number of characters in the word.

4. Complete the getNumVowels method to return the number of vowels in the word. It should use the VOWELS class constant and String’s length, substring, and indexOf methods.

Do not use any String methods that are not in the AP Subset.

5. Complete the getReverse method to return a Word with the letters of this reversed.

6. Complete the toString method to return the String word.

7. Uncomment the Word test code in WordTester and make sure that Word works correctly.

Extra Credit

● Word – add the following methods to Word:

o public boolean equals(Object other) - This method will compare two Word

objects for equality.

o public Word getSortedUpperCaseWord() - This method will return a new Word with its letters changed to upper case and then sorted. You should create an array of Strings representing the uppercase of each of the letters. Then use either selection sort or insertion sort code to order the array. Then you will have to create a string from the array and then create the Word to return.

● Words – add the following method to Words:

o public void printAnagrams() - This method will print the pairs of anagrams in this. Two words w1 and w2 are anagrams if

w1.getSortedUpperCaseWord().equals(w2.getSortedUpperCaseWord()) is true. This method should only print each pair once.

/**

* Word.java   

* A <code>Word</code> object represents a word.

*/

public class Word

{

    /** The 5 vowels to use for the getNumVowels() method */

   private static final String VOWELS = "AEIOUaeiou";

    /** The word instance variable */

private String word;

    /**

     * Constructs a <code>Word</code> object. No change needed.

     * @param w the string used to initialize the word.

     */

    public Word(String w)

    {

    word = w;

    }

    /**

     * Gets the length of the word.  

     * @return the length (number of characters) of the word.

     */

    public int getLength()

    {

    }

    

    /**

     * Gets the first letter of the word.  

     * @return the fisrt letter of the word

     */

    public char getFirstLetter()

    {

    }

    

    /**

     * Gets the last letter of the word.

     * @return last letter of the word

     */

    public char getLastLetter()

    {   

    }

    

    /**

     * Gets a substring of letters from the word.

     * @param start the starting index

     * @param stop the ending index (inclusive)

     * @return the characters from [start, stop] inclusively

     */

    public String getLetters(int start, int stop)

    {    

     

    }    

    

    /**

     * Finds how many times a letter is in word.  

     * @param letter to find

     * @return count of letter in the word

     */

    public int findLetter(char letter)

    {    

       

    }

   

    /**

     * Gives the string representing the word.  

     * @return the string representing the word.

     */

    public String toString()

    {

         

    }

   

    /**

     * Returns a string that will indicate if word comes before,

     * after or is equal to the parameter.

     * @param stringCompare the string to compare word to

     * @return a string indicating how the two strings compare (before, after, equal)

     */

    public String compareToString(String stringToCompare)

    {    

        

    }    

    /**

     * Gets the reverse word.

     * @return the characters of word reversed as a string

     */

    public String getReverse()

    {

      

    }

    

    /**

     * Determines if word is palindrome. Same word forward and backward

     * @return true if word is palindrome, false otherwise

     */

    public boolean isPalindrome()

    {

      

    }

    

    /**

     * Gets the number of vowels in the word.

     * @return the vowel count in the word

     */

    public int getNumVowels()

    {

   

    }

    

}

MAIN

import java.util.Arrays;

public class Main

{

    public static void main(String[] args)

    {

    // Test Word

   

    Word one = new Word("chicken");

    System.out.println("Word one: " + one);

    System.out.println("Number of chars: " + one.getLength());

    System.out.println("Number of vowels: " + one.getNumVowels());

    System.out.println("Reverse: " + one.getReverse());

    System.out.println("\n\n")

    // Test Words

   

    Words test1 = new Words(new String[] {

    "one", "two", "three", "four", "five", "six", "seven",

    "alligator"});

    System.out.println("Words test1: " + test1);

    System.out.println("Number of words with 2 vowels: " +

    test1.countWordsWithNumVowels(2));

    System.out.println("Number of words with 5 chars: " +

    test1.countWordsWithNumChars(5));

    test1.removeWordsWithNumChars(3);

    System.out.println("\nWords test1 after removing words " +

    "with 3 chars:\n" + test1);

    System.out.println("\nReversed words:\n" +

    Arrays.toString(test1.getReversedWords()));

    System.out.println("\n\n");

}


0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi Student,

I have completed few of the methods but few of the methods are to be written by you yourself. If you face any issue in that later, let me know, I will provide you the answers in comments.

Below is the code for the same and the output-

import java.util.Arrays;

public class Main

{

public static void main(String[] args)

{

// Test Word

Word one = new Word("chicken");

System.out.println("Word one: " + one);

System.out.println("Number of chars: " + one.getLength());

System.out.println("Number of vowels: " + one.getNumVowels());

System.out.println("Reverse: " + one.getReverse());

System.out.println("\n\n");

// Test Words

Words test1 = new Words(new String[] {

"one", "two", "three", "four", "five", "six", "seven",

"alligator"});

System.out.println("Words test1: " + test1);

/* System.out.println("Number of words with 2 vowels: " +

test1.countWordsWithNumVowels(2));

System.out.println("Number of words with 5 chars: " +

test1.countWordsWithNumChars(5));

test1.removeWordsWithNumChars(3);

System.out.println("\nWords test1 after removing words " +

"with 3 chars:\n" + test1);

System.out.println("\nReversed words:\n" +

Arrays.toString(test1.getReversedWords()));*/

System.out.println("\n\n");
}
}

public class Word

{

/** The 5 vowels to use for the getNumVowels() method */

private static final String VOWELS = "AEIOUaeiou";

/** The word instance variable */

private String word;

/**

* Constructs a <code>Word</code> object. No change needed.

* @param w the string used to initialize the word.

*/

public Word(String w)

{

word = w;

}

/**

* Gets the length of the word.

* @return the length (number of characters) of the word.

*/

public int getLength()

{
       return this.word.length();

}

  

/**

* Gets the first letter of the word.

* @return the fisrt letter of the word

*/

public char getFirstLetter()

{
   return this.word.charAt(0);
}

  

/**

* Gets the last letter of the word.

* @return last letter of the word

*/

public char getLastLetter()
{   

   return this.word.charAt(this.word.length()-1);
}

  

/**

* Gets a substring of letters from the word.

* @param start the starting index

* @param stop the ending index (inclusive)

* @return the characters from [start, stop] inclusively

*/

public String getLetters(int start, int stop)

{

return word.substring(start, stop);

}

  

/**

* Finds how many times a letter is in word.

* @param letter to find

* @return count of letter in the word

*/

public int findLetter(char letter)

{

return word.split(letter+"").length-1;

}

/**

* Gives the string representing the word.

* @return the string representing the word.

*/

public String toString()

{
   return word.toString();
}

/**

* Returns a string that will indicate if word comes before,

* after or is equal to the parameter.

* @param stringCompare the string to compare word to

* @return a string indicating how the two strings compare (before, after, equal)

*/

   public String compareToString(String stringToCompare)

   {
       if (word.compareTo(stringToCompare) == 0) {
           return "equal";
       }
       if (word.compareTo(stringToCompare) > 0) {
           return "after";
       }
       if (word.compareTo(stringToCompare) < 0) {
           return "before";
       }
       else return null;
   }

/**

* Gets the reverse word.

* @return the characters of word reversed as a string

*/

public String getReverse()

{

StringBuilder sb = new StringBuilder(word);
return sb.reverse().toString();

}

  

/**

* Determines if word is palindrome. Same word forward and backward

* @return true if word is palindrome, false otherwise

*/

   public boolean isPalindrome()

   {
       StringBuilder sb = new StringBuilder(word);
       String rev = sb.reverse().toString();

       return (word.equals(rev) ? true : false);

   }
  

/**

* Gets the number of vowels in the word.

* @return the vowel count in the word

*/

public int getNumVowels()

{

//Run a double for loop to count this-> do it yourself
   return 0;
}

  

}


public class Words {

   public Words(String[] strings) {
       // TODO Auto-generated constructor stub
   }

}

Output-

Word one: chicken
Number of chars: 7
Number of vowels: 0
Reverse: nekcihc

Words test1: Words@7852e922

Regards,

Ankit

Add a comment
Know the answer?
Add Answer to:
Hi I really need help with the methods for this lab for a computer science class....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Objectives Problem solving using arrays and ArrayLists. Abstraction. Overview The diagram below illustrates a banner constructed...

    Objectives Problem solving using arrays and ArrayLists. Abstraction. Overview The diagram below illustrates a banner constructed from block-letters of size 7. Each block-letter is composed of 7 horizontal line-segments of width 7 (spaces included): SOLID                        as in block-letters F, I, U, M, A, S and the blurb RThere are six distinct line-segment types: TRIPLE                      as in block-letter M DOUBLE                   as in block-letters U, M, A LEFT_DOT                as in block-letters F, S CENTER_DOT          as in block-letter...

  • Given code: /** * Provides some methods to manipulate text * */ public class LoopyText {...

    Given code: /** * Provides some methods to manipulate text * */ public class LoopyText { private String text;    /** * Creates a LoopyText object with the given text * @param theText the text for this LoopyText */ public LoopyText(String theText) { text = theText; }    //Your methods here } Given tester code: /** * Tests the methods of LoopyText. * @author Kathleen O'Brien */ public class LoopyTextTester { public static void main(String[] args) { LoopyText loopy =...

  • Trying to figure out these methods for comp sci class and I really don't understand. Any...

    Trying to figure out these methods for comp sci class and I really don't understand. Any help would be really appreciated.       /**    *    */    public static int [] setNums(String vals)    {    //this is a helper method to convert a string to an array for searching purposes        //declare and instantiate an array with length as the number of chars in vals               //lopp through each char in vals   ...

  • Please help me. package a1; public class Methods {    /*    * Instructions for students:...

    Please help me. package a1; public class Methods {    /*    * Instructions for students: Use the main method only to make calls to the    * other methods and to print some testing results. The correct operation of    * your methods should not depend in any way on the code in main.    *    * Do not do any printing within the method bodies, except the main method.    *    * Leave your testing code...

  • hey dear i just need help with update my code i have the hangman program i...

    hey dear i just need help with update my code i have the hangman program i just want to draw the body of hang man when the player lose every attempt program is going to draw the body of hang man until the player lose all his/her all attempts and hangman be hanged and show in the display you can write the program in java language: this is the code bellow: import java.util.Random; import java.util.Scanner; public class Hangmann { //...

  • This is the code I have written for a BingoBall program, I'd appreciate it if someone...

    This is the code I have written for a BingoBall program, I'd appreciate it if someone could help me fix this public class BingoBall { private char letter; private int number; // NOTE TO STUDENT // We challenge you to use this constant array as a lookup table to convert a number // value to its appropriate letter. // HINT: It can be done with with a single expression that converts the number // to an index position in the...

  • Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** *...

    Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** * Program starts with this method. * * @param args A String to be printed backwards */ public static void main(String[] args) { if (args.length == 0) { System.out.println("ERROR: Enter a String on commandline."); } else { String word = args[0]; String backwards = iterativeBack(word); // A (return address) System.out.println("Iterative solution: " + backwards); backwards = recursiveBack(word); // B (return address) System.out.println("\n\nRecursive solution: " +...

  • I need help with this. I need to create the hangman game using char arrays. I've...

    I need help with this. I need to create the hangman game using char arrays. I've been provided with the three following classes to complete it. I have no idea where to start. HELP!! 1. /** * This class contains all of the logic of the hangman game. * Carefully review the comments to see where you must insert * code. * */ public class HangmanGame { private final Integer MAX_GUESSES = 8; private static HangmanLexicon lexicon = new HangmanLexicon();...

  • Complete the following code: You are asked to write some functionalities for a spelling checker i...

    Complete the following code: You are asked to write some functionalities for a spelling checker inside Tree.java that has at least the following methods: /*Adds a word to a spelling checker’s collection of correctly spelled words*/ void add(String word) /*Returns true if the given word is spelled correctly*/ boolean check(String word) /*Returns back all words in the tree in alphabetical order*/ public String getDictionaryInAlphabeticalOrder() Store the collection of correctly spelled words in a 26-ary tree. The number 26 indicates that...

  • Help with program /* Lab 7, Part E: Created by Celine Latulipe public class Lab7Parte {...

    Help with program /* Lab 7, Part E: Created by Celine Latulipe public class Lab7Parte { public static void main(String [] args) // TODO: put your code here to generate a random number between 0 and 20 // and then print out that many asterisks on a single line. { بہا } We were unable to transcribe this image/* Lab 7, Part F: Created by Celine Latulipe public class Lab 7PartF { public static void main(String [] args) { //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT