+1-415-670-9189
info@expertsmind.com
Create classes implement java interface
Course:- JAVA Programming
Reference No.:- EM13448




Assignment Help
Assignment Help >> JAVA Programming

Overview: In this assignment, you are provided with an interface that contains a generic type.

You are asked to create two classes that implement this interface.

A. The Sequenced Interface

In the provided code, you are given an interface called Sequenced that is used by classes that have a sequential structure to them. The interface is short, containing only 3 abstract methods.

In fact, the interface in its entirety can be shown here:

import java.util.ArrayList;

public interface Sequenced<T>
{
public T getFirst();
public T getLast();
public ArrayList<T> getSequence();
}

You will be creating two classes that each implement Sequenced, and so each of them must contain implementations of these 3 methods.

You will notice that Sequenced has a generic type parameter T. Your subclass definitions will each need to instantiate T when they implement the interface.

B. The Word Class

You should create a class Word that represents words in a language. Word implements the Sequenced interface, because a word is a sequence of characters. The Word class should have two instance fields:

? An instance field of type ArrayList<Character> which will store a word's character sequence. Note: Java has a Character wrapper class that you should use here.

? An instance field of type int, representing the Word's position in the sentence (with the first Word in a sentence being position 0).
Word should have the following methods:

? The getFirst() method should return the first Character of the word,

? the getLast() method should return the last Character of the word,

? getSequence() should return an ArrayList<Character> of all the characters, and

? getPosition() should return the int representing the Word's position in the sentence Based on the previous two paragraphs, it should be clear to you how Word instantiates the T parameter when it implements the Sequenced interface. If not, here is a hint: look at the Sequenced interface and notice the use of T in the return types of the methods. Then think about what the Word class is going to return in each of the methods.

The Word constructor should take two parameters:

? it should take a String parameter and add the individual Characters of the String to the ArrayList<Character>.

? it should take an int parameter representing the position of the Word in a sentence and set the relevant instance field accordingly.

C. The Sentence Class

You should create a second class Sentence that represents sentences in a language. Sentence implements the Sequenced interface, because a sentence is a sequence of words. Sentence should have a single instance field of type ArrayList<Word> which will store the Words of a sentence. This relationship between the Sentence and Word classes is called composition, because a Sentence is composed of Words. Note that there is NOT an inheritance relationship between Sentence and Word.Sentence should have the following methods:

? The getFirst() method should return the first Word of the Sentence,

? the getLast() method should return the last Word of the Sentence, and

? getSequence() should return an ArrayList<Word>.

Based on the previous two paragraphs, it should be clear to you how Sentence instantiates the T parameter when it implements the Sequenced interface. If not, here is a hint: look at the Sequenced interface and notice the use of T in the return types of the methods. Then think about what the Sentence class is going to return in each of the methods.

The Sentence constructor should take a single String parameter representing the

Sentence, and add each Word of the sentence to the ArrayList<Word>. You can split the String into Words by using the String split() method in the following way:

String[] spl = s.split("\\s+");

Notice that the split() method will give you an array of Strings, and you will need to go through that array, creating Words and adding them to the ArrayList<Word>.

D. SequenceTester Class

A tester class is provided for you, demonstrating how Sentence and Word can be used and indicating what the output should be.




Put your comment
 
Minimize


Ask Question & Get Answers from Experts
Browse some more (JAVA Programming) Materials
Modify the Java application using Java NetBeans IDE to meet these additional and changed business requirements: The company has recently changed its total annual compensatio
Write the program in Java (with a graphical user interface) and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage.
Constructors use the same exception handling rules as methods: In a try block, include the parsing of the month and day substrings and other error-checking logic that will n
Write a program that allows a single player to practice the game (that is, there will be no opponent, just a single player). Play 5 turns in the game to get a total score fo
An elementary calculation yields the result that theoretically, the probability of turning up 7 when two dice are thrown is 1/6, or .166666. But what if more dice are thrown?
Instructions: Code a 2-dimensional (2D) array that stores the average scores for 2 football teams across 2 years. In a method called setAvgScores, prompt for the averages an
Create a program that asks a user for a number between 1 and 100, the program should then output to the user higher, lower, or correct, depending upon the input from the use
What are some of the more popular methods utilizing the "String" class? Show examples of their usage. Java gives us the ability to do output to the console screen in many way