Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your tests will be run against a correct implementation of Magazine. Ideally, all of your tests should pass when run against this correct version. Measures

Your tests will be run against a correct implementation of Magazine.

Ideally, all of your tests should pass when run against this correct version.

Measures will also be collected on how thoroughly you have checked the member functions of the Magazine class.

Your tests will also be run against a number of buggy versions of the code. The goal here is to see if your tests are good enough to detect the bugs.

Files Given:

Magazine.java

import java.time.LocalDate;

import java.time.LocalDateTime;

import java.util.Arrays;

import java.util.Map;

import java.util.Set;

import java.util.TreeMap;

/**

* A magazine containing a collection of articles, organized by (starting) page number.

*

*

* @author

*

*/

public class Magazine implements Cloneable {

private String title;

private Map contents;

private LocalDate date;

/**

* Create a "blank" magazine with empty strings for title, the current

* date for the publication date,

* and an empty (zero-length) list of articles.

*/

public Magazine() {

title = "";

date = LocalDateTime.now().toLocalDate();

contents = new TreeMap();

}

/**

* Create a new magazine.

* @param title title of the magazine

* @param publDate publication date of the magazine.

*/

public Magazine(String title, LocalDate pubDate) {

this.title = title;

this.date = pubDate;

this.contents = new TreeMap();

}

/**

* Get the title of this magazine.

* @return the title

*/

public String getTitle() {

return title;

}

/**

* Set the title of this magazine.

* @param title the title to set

*/

public void setTitle(String title) {

this.title = title;

}

/**

* Get the publication date of this magazine.

* @return the date

*/

public LocalDate getPublicationDate() {

return date;

}

/**

* Set the publication date of this magazine.

* @param pubDate the publication date to set

*/

public void setPublicationDate(LocalDate pubDate) {

this.date = pubDate;

}

/**

* How many articles does this magazine have?

* @return number of articles

*/

public int numArticles() {

return contents.size();

}

/**

* Add an article to the magazine at an indicated starting page.

*

* If an article is already at that page, replaces the existing one.

*

* @param startingPage first page of the article

* @param article author to be added

*/

public void addArticle(int startingPage, Article article) {

contents.put(startingPage, article);

}

/**

* Get the article previously placed at a given starting page.

*

* @param startingAtPage a page number in the magazine

* @return the article starting at that page, or null if no article

* has been put there.

*/

public Article getArticle (int startingAtPage) {

return contents.get(startingAtPage);

}

/**

* Render the magazine as a string in a format guaranteed to

* contain all fields.

*/

public String toString() {

StringBuffer buf = new StringBuffer();

buf.append(title);

buf.append(", ");

buf.append(date.toString());

buf.append(" Contents:");

for (Integer p: contents.keySet()) {

buf.append(" ");

buf.append(p);

buf.append(" ");

buf.append(contents.get(p).toString());

}

return buf.toString();

}

/**

* Compares two magazines for equality. They are considered equal if

* all functions on them return equal results..

*

* @param obj object to be compared for equality with this magazine

* @return true if the specified object is equal to this one

*/

public boolean equals(Object obj) {

if (!(obj instanceof Magazine)) {

return false;

}

Magazine other = (Magazine)obj;

if (!title.equals(other.title))

return false;

if (!date.equals(other.date))

return false;

if (!contents.equals(other.contents))

return false;

return true;

}

/**

* Return a (deep) copy of this object.

*/

@Override

public Object clone() {

Magazine theClone = new Magazine(title, date);

for (Integer p: startingPages()) {

theClone.addArticle(p, getArticle(p));

}

return theClone;

}

/**

* Provide access to the table of contents of

* this magazine. e.g.,

* Magazine magazine = new Magazine(...);

* ...

* for (Integer page: magazine,startingPages()) {

* Article art = magazine.getArticle(page);

* doSomethingWith (page, art);

* }

*

* @return iterator over the starting page numbers. Numbers

* are returned in ascending order.

*/

public Set startingPages() {

return contents.keySet();

}

}

Article.java

/**

* An article within a magazine. An article has a title and an author name.

*

* @author

*

*/

public class Article implements Cloneable {

/**

* The title of this article.

*/

private String title;

/**

* The author of this article.

*/

private String author;

/**

* Create an article.

* @param title the title of the article

* @param author the name of the author

*/

public Article (String title, String author) {

this.title = title;

this.author = author;

}

/**

* Compare two articles for equality.

*

* @param obj another author

* @return true ff they have the same name

*/

public boolean equals(Object obj) {

if (obj instanceof Article) {

Article au = (Article) obj;

return author.equals(au.author)

&& title.equals(au.title);

} else {

return false;

}

}

/**

* Return a description of the article.

* givenName surname

*/

public String toString() {

return '"' + title + '"' + " by " + author;

}

/**

* Hash function for articles.

*/

public int hashCode() {

return title.hashCode() * 13 + author.hashCode();

}

/**

* Return the surname of this author

* @return the surname

*/

public String getTitle() {

return title;

}

/**

* Return the given name of this author

* @return the given name

*/

public String getGivenName() {

return author;

}

public Object clone() {

return new Article(title, author);

}

}

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

Recommended Textbook for

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

More Books

Students also viewed these Databases questions

Question

Who or what is affected by this situation?

Answered: 1 week ago