Can you please produce the Java code that solves this non-assessed homework problem? I'm very new to Java and Object-Oriented Programming and I have no idea how to construct the logic for this code.
PROBLEM:
"In this exercise, you will build a program that determines the validity of a select few simple English sentences, according to rules detailed below. A sentence will be represented as an array of words, which you will create as a static nested class `Word`.
`Word` is a good candidate for a static nested class, because every sentence (except the empty sentence) contains at least one word. Furthermore, the Word class does not have much use apart from checking the validity of sentences.
Create a class `Sentence` with an instance variable `public Word[] words`.
Furthermore:
* The class should have a constructor `Sentence(int size)`, where
`size` determines the length of the `sentence` field of a given
Sentence.
* The class should have an instance method `public boolean
isValid()` that determines the validity of a sentence according to
the rules detailed below.
* You may add as many more methods as you require.
Also create a public static nested class `Word`, with instance variables `String word` and `Type type`.
Within this class, you must create:
* A public enum type `Type` that contains `NOUN`, `ADJECTIVE`, and
`VERB`.
* The constructor Word(String value, Type type).
RULES FOR SENTENCE VALIDITY:
1. A sentence must have a length of at least 1.
2. A NOUN must be followed by a VERB (unless it is at the end of a
sentence).
3. An ADJECTIVE must be followed only by another ADJECTIVE or a
NOUN.
4. A sentence must have one and only one VERB.
5. A sentence must end in a NOUN or in a VERB."
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Sentence.java
public class Sentence {
private Word[] words; // array of words constituting a sentence
private int count; // current number of words added
public Sentence(int size) {
// initializing words list
words = new Word[size];
count = 0; // current length
}
// method to check if sentence is valid or not
public boolean isValid() {
if (count == 0) {
// empty sentence
return false;
}
// variable to store count of verbs
int countVerbs = 0;
// looping through words in sentence
for (int i = 0; i < count; i++) {
// checking if current word is NOUN
if (words[i].type == Type.NOUN) {
// checking if there exists a next word
if ((i + 1) < count) {
// next word should be a VERB
if (words[i + 1].type != Type.VERB) {
// not a VERB, invalid
return false;
}
}
} else if (words[i].type == Type.ADJECTIVE) {
// ADJECTIVE should be followed by a NOUN or ADJECTIVE
if ((i + 1) < count) {
if (words[i + 1].type != Type.ADJECTIVE
&& words[i + 1].type != Type.NOUN) {
// other then adjective or noun
return false;
}
} else {
// no next word.
return false;
}
} else if (words[i].type == Type.VERB) {
// incrementing verbs count
countVerbs++;
if (countVerbs > 1) {
// more than 1 VERB found
return false;
}
}
}
// checking word ends with NOUN or VERB
if (words[count - 1].type != Type.NOUN
&& words[count - 1].type != Type.VERB) {
// it doesnt.
return false;
}
// all checks completed. valid
return true;
}
// method to add a word to the sentence, returns true if added, else false
public boolean addWord(Word word) {
if (count < words.length) {
words[count] = word;
count++;
return true;
}
return false; // full
}
// Word class
private static class Word {
String word;
Type type;
public Word(String value, Type type) {
this.word = value;
this.type = type;
}
}
// enum Type
public enum Type {
NOUN, ADJECTIVE, VERB;
}
// just a tester program
public static void main(String[] args) {
Sentence sentence = new Sentence(4);
// I dont know much about the grammar and all, just checking if the
// isValid method is working correctly, just mind only the Type, and not
// the words.
sentence.addWord(new Word("Java", Type.NOUN));
sentence.addWord(new Word("is", Type.VERB));
sentence.addWord(new Word("beautifully", Type.ADJECTIVE));
sentence.addWord(new Word("made", Type.NOUN));
System.out.println(sentence.isValid()); //should be true
}
}
/*OUTPUT*/
true
Can you please produce the Java code that solves this non-assessed homework problem? I'm very new...
Can somebody help me with Java programming? please be brief and explain the process and carefully follow the step by step instruction. Thanks Homework 12.1 - Write a program that generates five random sentences (like mad libs), prints them to a file, then reads in the sentences and prints them to the console. Start by creating four string arrays for nouns, verbs, colors, and places. They should store at least 5 of each type of word. You can have more...
Code in JAVA You are asked to implement “Connect 4” which is a two player game. The game will be demonstrated in class. The game is played on a 6x7 grid, and is similar to tic-tac-toe, except that instead of getting three in a row, you must get four in a row. To take your turn, you choose a column, and slide a checker of your color into the column that you choose. The checker drops into the slot, falling...
JAVA :The following are descriptions of classes that you will create. Think of these as service providers. They provide a service to who ever want to use them. You will also write a TestClass with a main() method that fully exercises the classes that you create. To exercise a class, make some instances of the class, and call the methods of the class using these objects. Paste in the output from the test program that demonstrates your classes’ functionality. Testing...
This assignment shpuld be code in java. Thanks
Overview In this assignment you will write a program that will model a pet store. The program will have a Pet class to model individual pets and the Assignment5 class will contain the main and act as the pet store. Users will be able to view the pets, make them age a year at a time, add a new pet, and adopt any of the pets. Note: From this point on, your...
**IN JAVAAssignment 10.1 [95 points]The WordCruncher classWrite 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...
Java - Object Oriented Programming
Declare a class named Customer that has two private fields? Write a set method to make sure that if age > 125 years or less than 0 then it is set to 0 (default value) What does it indicate when declaring a class's instance variables with private access modifier? What is the role of a constructor? The data part of by an object's instance variables is known as? Do all methods have to always return...
Program Description: A Java program is to be created to produce Morse code. The Morse code assigns a series of dots and dashes to each letter of the alphabet, each digit, and a few special characters (such as period, comma, colon, and semicolon). In sound-oriented systems, the dot represents a short sound and the dash represents a long sound. Separation between words is indicated by a space, or, quite simply, the absence of a dot or dash. In a sound-oriented...
code in java please:)
Part I Various public transporation can be described as follows: A PublicTransportation class with the following: ticket price (double type) and mumber of stops (int type). A CityBus is a PublicTransportation that in addition has the following: an route number (long type), an begin operation year (int type), a line name (String type), and driver(smame (String type) -A Tram is a CityBus that in addition has the following: maximum speed (int type), which indicates the maximum...
Hello,
In need of help with this Java pa homework. Thank you!
2. Create a normal (POJo, JavaBean) class to represent, i. e. model, varieties of green beans (also known as string beans or snap beans). Following the steps shown in "Assignment: Tutorial on Creating Classes in IntelliJ", create the class in a file of its own in the same package as class Main. Name the class an appropriate name. The class must have (a) private field variables of appropriate...
write in java and please code the four classes with the
requirements instructed
You will be writing a multiclass user management system using the java. Create a program that implements a minimum of four classes. The classes must include: 1. Employee Class with the attributes of Employee ID, First Name, Middle Initial, Last Name, Date of Employment. 2. Employee Type Class that has two instances of EmployeeType objects: salaried and hourly. Each object will have methods that calculates employees payrol...