Question
PLEASE HELP me on my INTRO TO JAVA Homework assignemnt In this assignment, you are using a class that is provided to you. This class
PLEASE HELP me on my INTRO TO JAVA Homework assignemnt
In this assignment, you are using a class that is provided to you. This class is for storing sentences and performing some behaviors on them.
- setSentence: receives a String and returns nothing
- getSentence: receives nothing and returns a String
- setSeperator: receives a String and returns nothing
- printTokens: receives an int and returns nothing
- countWords: receives nothing and returns an int
- returnWord: receives an int and returns a String
Create two objects of type Sentence. Use the accessors (set methods) and mutators (get methods) and other methods of the object and perform the below activities:
- Create one object of type Sentence, use the setSentence method which receives a String as its parameter, and pass the below sentence as the input parameter to the method (Note that you should put a string literal should be between double quotes):
- A method might return a value of its return type, and you can store that value in a variable of the appropriate type.
- For the same object, perform the below tasks:
- Call the setSeperator method passing to it a "t" as the input parameter.
- Call the printTokens method, passing to it 4 as the input parameter.
- Use the countWords and returnWord (passing 2 as its input parameter) methods to construct and print the below sentence:
This sentence has [output from countWords] and its third word is [output from returnWord(2)].
import java.util.Scanner; import java.util.StringTokenizer;
class Sentence{ private String sentence = ""; private StringTokenizer st; private String seperator; void setSentence(String sentence) { st = new StringTokenizer(sentence, " "); if(st.countTokens() < 2) { System.out.println("This is not a sentence! It has less than 2 words!"); System.out.println("Input parameter is NOT stored."); } else{ this.sentence = sentence; System.out.println("Input parameter is stored."); } } String getSentence() { return this.sentence; } void setSeperator(String seperator) { this.seperator = seperator; } String getSeperator() { return seperator; } void printTokens(int tokensToPrint) { System.out.println("Sentence: \"" + this.sentence + "\""); System.out.println("Seperator: \"" + this.seperator + "\""); st = new StringTokenizer(this.sentence, this.seperator); int i = tokensToPrint; int j = 0; while(i > 0 & st.hasMoreTokens()) { System.out.println("Token " + j++ + ": " +st.nextToken()); i--; } } int countWords() { st = new StringTokenizer(this.sentence, " "); return st.countTokens(); } String returnWord(int wordIndex) { st = new StringTokenizer(this.sentence, " "); int i = 0; while( st.hasMoreTokens()) { if(i == wordIndex) { return "\"" + st.nextToken() + "\""; } st.nextToken(); i++; } System.out.println("Error! The requested index was higher than sentence length!"); return ""; } int countOccurrences(String wordToCount) { int count = 0 ; st = new StringTokenizer(this.sentence, " "); while(st.hasMoreTokens()) { String token = st.nextToken(); if(token.contains(wordToCount)) { count++; } } return count; } } public class Main {
public static void main(String[] args) { // Do not change anything above this line // Write your code here. // Do not change anything below this line
I got stuck around here.
Sentence s1 = new Sentence(); s1.setSentence(" }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started