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
  • 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 =...

  • 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...

  • 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: " +...

  • 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 { //...

  • Complete the class LoopyText. The constructor has been completed and provided to you in Codecheck. public...

    Complete the class LoopyText. The constructor has been completed and provided to you in Codecheck. public LoopyText(String theText) Constructs an object of LoopyText and stores theText inside the object. The parameter theText is a string with zero or more words separated by single spaces. public String getEverySecondCharacter() Returns a string consisting of every other character, starting with the character at index 0. If the string is the empty string return the empty String. public int upperCaseCount() Returns the number of...

  • Implement a class CSVReader that reads a CSV file, and provide methods: int numbOfRows() int numberOfFields(int...

    Implement a class CSVReader that reads a CSV file, and provide methods: int numbOfRows() int numberOfFields(int row) String field(int row, int column) Please use the CSVReader and CSVReaderTester class to complete the code. I have my own CSV files and cannot copy them to here. So if possible, just use a random CSV file. CSVReader.java import java.util.ArrayList; import java.util.Scanner; import java.io.*; /**    Class to read and process the contents of a standard CSV file */ public class CSVReader {...

  • 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...

  • JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time a...

    JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time at your location. Also include a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutes methods. Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you livein California, a new WorldCLock(3) should show the time in NewYork, three time zones ahead. Include an Alarm feature in the Clock class....

  • 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...

  • Need help with this Java. I need help with the "to do" sections. Theres two parts...

    Need help with this Java. I need help with the "to do" sections. Theres two parts to this and I added the photos with the entire question Lab14 Part 1: 1) change the XXX to a number in the list, and YYY to a number // not in the list 2.code a call to linearSearch with the item number (XXX) // that is in the list; store the return in the variable result 3. change both XXX numbers to the...

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