Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a class called BiblioIterator that implements the Iterator interface. public class BiblioIterator implements Iterator < Publication > This class should contain: A constructor that

Create a class called BiblioIterator that implements the Iterator interface.

public class BiblioIterator implements Iterator< Publication > 

This class should contain:

A constructor that accepts a Bibliography object as a parameter. The constructor should also set a "pointer" before the first Publication in the Bibliography. (similar to the way thatlast worked in PA1)

A method hasNext() that returns a boolean value that indicates whether there is another Publication beyond the current Publication in the Bibliography.

A method called next() that advances the pointer to the next Publication object in the Bibliography and returns it. If there is no next Publication (the pointer is already at the end or the Bibliography is empty), the program should throw a NoSuchElementException.

Add a method to the Bibliography called getIterator() that returns a BiblioIterator for the Bibliography.

public Iterator< Publication > getIterator() 

Since the interface requires that you implement a remove() method as well, you will need to provide one with an empty body (it will not do anything).

 Publication class is needed to compile but no changes need to be made to it **************************************************************** public class Bibliography { public static final int INIT_SIZE = 16; private int last; // pointer to the last slot used in the array private Publication[] bibliography; /** * Default constructor - instantiate array and initialize. */ public Bibliography() { last = -1; bibliography = new Publication[ INIT_SIZE ]; } /** * Add a publication to the collection. If successful, then increment * "last". If array is full, then expand array (private expand method). * * @param pub the Publication object to add * @return true if the addition was successful (Publication.canAdd() returns * true. */ public boolean add( Publication pub ) { boolean success = false; if ( pub.canAdd() ) { // expand if need be if ( size() == capacity() ) { expand(); } // increment last & add to array last++; bibliography[ last ] = pub; success = true; } return success; } /** * Return the capacity (size) of the array. * * @return the capacity of the array */ public int capacity() { return bibliography.length; } /** * Delete the last Publication added to the collection. Return true if * successful, false otherwise (true if something to delete). If successful, * then decrement "last". * * @return true if deletion was successful, false otherwise. */ public boolean deleteLast() { boolean success = false; if ( size() > 0 ) { bibliography[ last ] = null; last--; success = true; } return success; } /** * Retrieve the Publication object at the specified position. If position is * out of bounds or if position is not populated, return null. * * @param whichOne - the position from which to get * @return the */ public Publication get( int whichOne ) { Publication pubToReturn = null; if ( whichOne >= 0 && whichOne < size() ) { pubToReturn = bibliography[ whichOne ]; } return pubToReturn; } /** * Return the size of the collection (how many items does it hold). This * will be equal to the value "last". * * @return the value of "last". */ public int size() { return last + 1; } /** * Double the size of the array. */ private void expand() { Publication[] newBib = new Publication[ capacity() * 2 ]; for ( int i = 0; i < size(); i++ ) { newBib[ i ] = bibliography[ i ]; } bibliography = newBib; } } 

************************************************************************************************

****************************************************************

public class Publication { public static final int CURRENT_YEAR = Calendar.getInstance().get( Calendar.YEAR ); private int year; private String author; private String city; private String publisher; private String title; /** * Explicit Value Constructor. * * @param author - the author for the publication * @param title - the title of the publication * @param city - the city of publication * @param publisher - the publisher of the publicatio * @param year - the year of the publication */ public Publication( String author, String title, String city, String publisher, int year ) { setAuthor( author ); setTitle( title ); setCity( city ); setPublisher( publisher ); setYear( year ); } /** * Can this publication be added? True if author & title have values and * year is valid (0 or between 1450 & current year + 1). * * @return true if this item can be added to the bibliography */ public boolean canAdd() { return isValidAuthor() && isValidTitle() && isValidYear(); } /** * Is this Publication equal to the Publication object in the parameter. * * @param incoming - the object to compare * @return true if the contents are the same */ public boolean equals( Object incoming ) { boolean areEqual = false; Publication compPub = (Publication) incoming; if ( author.equals( compPub.getAuthor() ) && title.equals( compPub.getTitle() ) && city.equals( compPub.getCity() ) && publisher.equals( compPub.getPublisher() ) && year == compPub.getYear() ) { areEqual = true; } return areEqual; } /** * Return the author of the publication. This attribute must exist and have * a length > 0. * * @return the author of the publication */ public String getAuthor() { return author; } /** * Return the city of the publication. This attribute is optional. * * @return the city of the publication */ public String getCity() { return city; } /** * Return the publisher for the publication. This attribute is optional. * * @return the publisher for the publication */ public String getPublisher() { return publisher; } /** * Get author, title up to 60 characters. * * **MLN: 9/29/2017 -- Added for PA1, stage 3 * * @return the short title */ public String getShortTitle() { String shortTitle = author + ", " + title; if ( shortTitle.length() > 60 ) { shortTitle = shortTitle.substring( 0, 57 ) + "..."; } return shortTitle; } /** * Return the title of the publication. This attribute must exist and have a * length > 0. * * @return the title of the publication */ public String getTitle() { return title; } /** * Return the year of the publication. Must be 0 or a year between 1450 and * one year after the current year. * * @return the year of publication */ public int getYear() { return year; } /** * Set the value of the author. If the incoming parameter is invalid (null * or length == 0) then return false, else return true. * * @param author the author of the publication * @return true if valid, false otherwise */ public boolean setAuthor( String author ) { boolean success = true; this.author = author; if ( !isValidAuthor() ) { this.author = ""; success = false; } return success; } /** * Set the value of the city. If the incoming parameter is null, set it to * an empty string. * * @param city the city of publication */ public void setCity( String city ) { this.city = city; if ( this.city == null ) { this.city = ""; } } /** * Set the value of the publisher. If the incoming parameter is null, set it * to an empty string. * * @param publisher the publisher for the publication */ public void setPublisher( String publisher ) { this.publisher = publisher; if ( this.publisher == null ) { this.publisher = ""; } } /** * Set the value of the title. If the incoming parameter is invalid (null or * length == 0) then return false, else return true. * * @param title the title of the publication * @return true if valid, false otherwise */ public boolean setTitle( String title ) { boolean success = true; this.title = title; if ( !isValidTitle() ) { this.title = ""; success = false; } return success; } /** * Set the value of the year. If the incoming parameter is valid (0 or * between 1450 and current year + 1, return true, else return false * * @param year the year of the publication * @return true if valid, false otherwise */ public boolean setYear( int year ) { boolean success = true; this.year = year; if ( !isValidYear() ) { success = false; } return success; } /** * Return the formatted author title for the book (73 characters max). * * @return the formatted string for author, title */ public String toString() { String displayString = author + ", " + title; String bibCity = city.length() > 0 ? city : "N/A"; String bibYear = year != 0 ? "" + year : "N/A"; displayString += " (" + bibCity + ", " + bibYear + ")"; displayString = displayString.length() < 73 ? displayString : displayString.substring( 0, 69 ) + "..."; return displayString; } /****************************** private methods ***************************/ /** * Is the author valid (length > 0)? * * @return true if valid, false otherwise */ private boolean isValidAuthor() { return author != null && author.length() > 0; } /** * Is the title valid (length > 0)? * * @return true if valid, false otherwise */ private boolean isValidTitle() { return title != null && title.length() > 0; } /** * Is this a valid year? * * @return true if valid, false otherwise */ private boolean isValidYear() { return year == 0 || ( year >= 1450 && year <= CURRENT_YEAR + 1 ); } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

llowing logarithm problem for the positive log_(9)x=-3 Submit

Answered: 1 week ago

Question

9. Understand the phenomenon of code switching and interlanguage.

Answered: 1 week ago