Interfaces and generics, JAVA Programming

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
{
public T getFirst();
public T getLast();
public ArrayList 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 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 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.
? 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 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.

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.

Posted Date: 5/6/2013 2:23:18 AM | Location : United States







Related Discussions:- Interfaces and generics, Assignment Help, Ask Question on Interfaces and generics, Get Answer, Expert's Help, Interfaces and generics Discussions

Write discussion on Interfaces and generics
Your posts are moderated
Related Questions
An application or procedure that produces and/or receives messages.

write code employe managment

Create an online multiple-choice quiz using JSP/Servlets technology. The quiz should draw questions from an array of predefined questions (at least fifteen). You should use a meani

List down the names of two array techniques available in JavaScript. The two techniques thata rae available in java script is: 1. sort( ) [sorts elements within alphabetical

Output first x values in the fibonacci sequence, where x is an argument to the program. 0, 1, 1, 2, 3, 5, 8, ... Write the fibonacci creation function separate from the main func

A NEW Dynamic Website I have the subsequent requirement which needs to be designed in 30 days (Max). 1) Construct a website which is a dynamic, means allow me to change conte

Are constructors inherited ? Can a subclass call the parent class constructor and When?

When wiring beans, if a bean element is embedded to a property tag directly, then that bean is said to the Inner Bean. The disadvantage of this bean is that it cannot be reused any

Explain System Colors in java 1.1? In Java 1.1, the java.awt.SystemColor class is a subclass of java.awt.Color that gives color constants in which match native component colors

Write a short note on image preloading process? The primary use of image preloading procedure is to download the image within the cache before it is actually required to displa