Question

Write the Java code for the class WordCruncher. Include the following members:

**IN JAVA

Assignment 10.1 [95 points]

The WordCruncher class

Write the Java code for the class WordCruncher. Include the following members:

  • A default constructor that sets the instance variable 'word' to the string "default".

  • A parameterized constructor that accepts one String object as a parameter and stores it in the instance variable. The String must consist only of letters: no whitespace, digits, or punctuation. If the String parameter does not consist only of letters, set the instance variable to "default" instead. (This restriction will make your work on some of the following methods much easier.)

  • A method String toString() that returns the instance variable.

  • A method int numLetters() that returns the number of letters in the instance variable.

  • A method int numVowels() that returns the number of vowels in the instance variable.

  • A method boolean beginsWithVowel() that returns true if the first letter of the instance variable is a vowel, and false otherwise.

  • A method String toPigLatin() that returns a String containing the 'pig latin' version of the instance variable. The rules for translating a word to pig latin are:Note that despite the last example, our toPigLatin() method operates on words, not on complete sentences.

    1. If the word begins with a consonant, take the first letter of the word and move it to the end of the word, followed by 'ay'

    2. If the word begins with a vowel, add 'way' to the word. Hint: the method beginsWithVowel() makes this easier.

    3. For example, PIG LATIN IS FUN becomes IGPAY ATINLAY ISWAY UNFAY in pig latin.

  • A method String toGibberish() that returns a String containing the 'gibberish' version of the instance variable. The rules for translating a word into gibberish are:

    1. If the word begins with a consonant, follow the first letter with 'ithag'. So the word 'big' would translate to 'bithagig'.

    2. If the word begins with a vowel, place 'ithag' at the front. So the word 'is' becomes 'ithagis'.

  • A method String reverse() that returns a String that contains the characters of the instance variable, but in reverse.

  • A method int numCharOccurrences(char ch) that returns a count of the number of times the parameter char ch occurs in the instance variable. A character that is a match except for the case (uppercase instead of lowercase or vice versa) should be counted as an occurrence.

The application

Write a class named WordCruncherTest that has only a main method that:

  1. asks the user for a word with the option to enter the word "quit" to quit

  2. creates a WordCruncher object that contains this word (unless the word is "quit") and then:

    1. outputs the number of letters in this object

    2. outputs the number of vowels in this object

    3. output the object string in reverse

    4. outputs the pig latin translation of the string stored in the object

    5. outputs the gibberish translation of the string stored in the object

    6. asks the user to enter one letter, and returns a message indicating how many occurrances of that letter are in the word

The program should continue to do this until the user enters the word "quit"

Submit Your Work

You should have two source code files. Name the files according to the class names given above. Execute the program and copy/paste the output into the bottom of the WordCruncherTest.java file, making it into a comment. Use the Assignment Submission link to submit the source file(s). When you submit your assignment there will be a text field in which you can add a note to me (called a "comment", but don't confuse it with a Java comment). In this "comments" section of the submission page let me know whether the program(s) work as required.

Keep in mind that if your code does not compile you will receive a 0.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class WordCruncher {

    private String word;

    // default constructor
    public WordCruncher() {
        word = "default";
    }

    // overloaded constructor
    public WordCruncher(String word) {
        this.word = word == null ? "default" : word;
        // The String must consist only of letters: no whitespace, digits, or punctuation.
        // If the String         // parameter does not consist only of letters, set the
        // instance variable to "default" instead.
        for (char letter : word.toCharArray()) {
            if (!Character.isLetter(letter)) {
                word = "default";
                return;
            }
        }
    }

    @Override
    public String toString() {
        return word;
    }

//A method int numLetters() that returns the number of letters in the instance variable.
    public int numLetters() {
        return word.length();
    }

    //A method int numVowels() that returns the number of vowels in the instance variable.
    public int numVowels() {
        int vowels = 0;
        for (char letter : word.toLowerCase().toCharArray()) {
            vowels += letter == 'a' || letter == 'e' ||
                    letter == 'i' || letter == 'o' ||
                    letter == 'u' ? 1 : 0;
        }
        return vowels;
    }
//A method boolean beginsWithVowel() that returns true if the first
// letter of the instance variable is a vowel, and false otherwise.
    public boolean beginsWithVowel() {
        char letter = word.toLowerCase().charAt(0);
        return letter == 'a' || letter == 'e' ||
                letter == 'i' || letter == 'o' ||
                letter == 'u';
    }

    //A method String toPigLatin() that returns a String containing the 'pig latin'
    public String toPigLatin() {
        return beginsWithVowel() ?
                word.toLowerCase() + "way" :
                word.substring(1).toLowerCase() + word.substring(0, 1).toLowerCase() + "ay";

    }
//A method String toGibberish() that returns a String containing the 'gibberish'
    public String toGibberish() {

        return beginsWithVowel() ?
                "ithagis" + word.toLowerCase() :
                word.substring(0, 1).toLowerCase() + "ithag" + word.substring(1).toLowerCase();
    }

    // method String reverse() that returns a String
    public String reverse() {
        String reverse = new String();
        for (int i = 0; i < numLetters(); i++) {
            reverse = word.charAt(i) + reverse;
        }
        return reverse;
    }
//A method int numCharOccurrences(char ch) that returns a count of the number of times
    public int numCharOccurences(char ch) {

        int times = 0;
        for (char letter : word.toLowerCase().toCharArray()) {
            times += letter == ch ? 1 : 0;
        }
        for (char letter : word.toUpperCase().toCharArray()) {
            times += letter == ch ? 1 : 0;
        }
        return times;
    }
}

=======================================================================================



import java.util.Scanner;


public class WordCrucherTest {

    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {

        while (true) {

            System.out.print("Enter a word or type \"quit\" to quit: ");
            String word = scanner.nextLine();
            if(word.equalsIgnoreCase("quit")){
                break;
            }else{

                WordCruncher crucher = new WordCruncher(word);
                System.out.println("Number of letters : "+crucher.numLetters());
                System.out.println("Number of vowels: "+crucher.numVowels());
                System.out.println("Reverse : "+crucher.reverse());
                System.out.println("Pig Latin version: "+crucher.toPigLatin());
                System.out.println("Gibberish version: "+crucher.toGibberish());
                System.out.print("Enter a letter: ");
                char letter = scanner.nextLine().charAt(0);
                System.out.println("This letter occured: "+crucher.numCharOccurences(letter)+" times.");
            }

        }
    }
}

=======================================================================================

thanks !!

Add a comment
Know the answer?
Add Answer to:
Write the Java code for the class WordCruncher. Include the following members:
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
  • In java: A fun language: f you recently ate at Chipotle, you may have received a...

    In java: A fun language: f you recently ate at Chipotle, you may have received a brown paper bag with gibberish letters written on it: “AKINGMAY AWAY IGPAY EALDAY ABOUTWAY IGPAY ATINLAY”. In fact this message is written in Piglatin, a simple but fictitious language. Piglatin converts a word in English using the following two rules: If the letter begins with a vowel, then “WAY” is appended to the word. If the letter begins with a consonant (a letter other...

  • In C Sixth: Pig Latin (10 Points) For this part of the assignment, you will need...

    In C Sixth: Pig Latin (10 Points) For this part of the assignment, you will need to write a program that reads an input string representing a sentence, and convert it into pig latin. We'll be using two simple rules of pig latin: 1. If the word begins with a consonant then take all the letters up until the first vowel and put them at the end and then add "ay" at the end. 2. If the word begins with...

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

  • Correct the syntax errors to allow the code to pass all of the provided doctests. You...

    Correct the syntax errors to allow the code to pass all of the provided doctests. You may have heard of "Pig Latin," a set of rules for modifying regular English to render it unintelligible to those who do not know the rules. This problem asks you to fix a function that will convert an English word to Pig Latin The rules for converting a word to Pig Latin are as follows: 1. If the word starts with a vowel, add...

  • Write a class StringsAndThings. The class has one instance variable of type String and a constructor...

    Write a class StringsAndThings. The class has one instance variable of type String and a constructor with a parameter to initialize the instance variable. The parameter could contain any characters, including letters, digits, spaces, special characters (+, -, $, @,...), and punctuation marks. Write methods: countNonLetters - that will count how many of the characters in the string are not letters. You can use Character's isLetter method. moreVowels - which returns true if the String parameter has more vowels than...

  • JAVA QUESTION 16 Write and submit the java source for the following class specification: The name...

    JAVA QUESTION 16 Write and submit the java source for the following class specification: The name of the class is Question_16 Class Question_16 has 3 instance variables: name of type String, age of type int and height of type double. Class Question_16 has the following methods: print - outputs the data values stored in the instance variables with the appropriate label setName - method to set the name setAge - method to set the age setHeight - method to set...

  • In Java, write a program that prompts the user to input a sequence of characters and...

    In Java, write a program that prompts the user to input a sequence of characters and outputs the number of vowels. You will need to write a method to return a value called isAVowel which will return true if a given character is a vowel and returns false if the character is not a vowel. A second method, called isAConstant, should return true if a given character is a constant and return false if the character is not a constant....

  • Java programming: please help with the following assignment: Thank you. Create a class called GiftExchange that...

    Java programming: please help with the following assignment: Thank you. Create a class called GiftExchange that simulates drawing a gift at random out of a box. The class is a generic class with a parameter of type T that represents a gift and where T can be a type of any class. The class must include the following An ArrayList instance variable that holds all the gifts,The ArrayList is referred to as theBox. A default constructors that creates the box....

  • please write in Java and include the two classes Design a class named Fan (Fan.java) to...

    please write in Java and include the two classes Design a class named Fan (Fan.java) to represent a fan. The class contains: An int field named speed that specifies the speed of the fan. A boolean field named fanStatus that specifies whether the fan is on (default false). A double field named radius that specifies the radius of the fan (default 5). A string field named color that specifies the color of the fan (default blue). A no-arg (default) constructor...

  • Java Write a class named CheckingAccount containing: An instance variable named balance of type double, initialized...

    Java Write a class named CheckingAccount containing: An instance variable named balance of type double, initialized to 0. A method named deposit that accepts a parameter of type double. The value of the balance instance variable is increased by the value of the parameter. A method named withdraw that accepts a parameter of type double. The value of the balance instance variable is decreased by the value of the parameter. A method named getBalance that accepts no parameters, returns 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
ADVERTISEMENT