Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Book class : public class Book { private String title; // The title of the book private String author; // the author of the book

Book class :

public class Book { private String title; // The title of the book private String author; // the author of the book private String publisher; // the publisher of the book private int year; // The year when the book was published /** * Create a book. * * @param title The title of the book. * @param author The title of the book. * @param publisher The publisher of the book. * @param year The year when the book was published. */ public Book(String title, String author, String publisher, int year) { this.title = title; this.author = author; this.publisher = publisher; this.year = year; }

/** * Get the title of the book * * @return The title of the book */ public String getTitle() { return title; } /** * Get the author of the book * * @return The author of the book */ public String getAuthor() { return author; } /** * Get the publisher of the book * * @return The publisher of the book */ public String getPublisher() { return publisher; } /** * Get the year of the book when it was published * * @return The year of the book when it was published */ public int getYear() { return year; } /** * Get the details of the book * * @return The details of the book including * the title, author, publisher and year */ public String toString () { return "Book: " + title + " Year: " + year + " Author: " + author + " Publisher: " + publisher; } /** * Check if the book is the same as the given one. * * @param obj The given object. * * @return true if the book and the given object are the same * or they have the same title, author, publisher and year; * false otherwise */ public boolean equals(Object obj) { if (obj == this) return true; if ( !(obj instanceof Book) ) return false; Book aBook = (Book) obj; return this.title.equals(aBook.title) && this.author.equals(aBook.author) && this.publisher.equals(aBook.publisher) && this.year == aBook.year; } }

Journal Class :

public class Journal { private String title; // The title of the journal private int year; // The year when the journal was published private int month; // The month when the journal was published

/** * Create a journal. * * @param title The title of the journal. * @param month The month when the journal was published. * @param year The year when the journal was published. */ public Journal(String title, int month, int year) { this.title = title; this.month = month; this.year = year; }

/** * Get the title of the journal * * @return The title of the journal */ public String getTitle() { return title; }

/** * Get the month of the journal when it was published * * @return The month of the journal when it was published */ public int getMonth() { return month; } /** * Get the year of the journal when it was published * * @return The year of the journal when it was published */ public int getYear() { return year; }

/** * Get the details of the journal * * @return The details of the journal including * the title, publisher, month and year */ public String toString() { return "Journal: " + title + " Year: " + year + " Month: " + getMonthName(month); } /** * Check if the journal is the same as the given one. * * @param obj The given object. * * @return true if the journal and the given object are the same * or they have the same title, year and month; * false otherwise */ public boolean equals(Object obj) { if (this == obj) return true; if ( !(obj instanceof Journal) ) return false; Journal aJournal = (Journal) obj; return this.getTitle().equals(aJournal.getTitle()) && this.month == aJournal.month && this.getYear() == aJournal.getYear(); } /** * To get the first three letters of a given month's name * * @param month A given month * @return The first three letters of the given month's name */ private String getMonthName(int month) { switch (month) { case 1: return "Jan"; case 2: return "Feb"; case 3: return "Mar"; case 4: return "Apr"; case 5: return "May"; case 6: return "Jun"; case 7: return "Jul"; case 8: return "Aug"; case 9: return "Sep"; case 10: return "Oct"; case 11: return "Nov"; case 12: return "Dec"; default: return "Unknow"; } } }

Question: Done in Java

Moving the title field In order to move the title field you will need to engage in a process called refactoring. The aim is to improve the class structures by moving code around, but we are not aiming to introduce new functionality. Once these changes are made, all the existing tests should still pass.

Start by creating a new class called Publication.

Modify Book and Journal to indicate that both are subclasses of Publication.

Place a title field in Publication and remove the title fields from both Book and Journal classes. Make sure that the title has an appropriate comment.

Place a getTitle accessor method in Publication and remove similar definitions from Book and Journal. Make sure the text of the method comment is appropriate to a shared method.

With title now a private field of Publication, subclasses cannot use the field directly in their methods. Both Book and Journal must replace direct accesses with calls to the public method getTitle they inherit from Publication. Make these changes to Book and Journal and check that all classes compile correctly.

Check for any errors introduced during the refactoring process. One particular problem to look out for is whether a book/journal is initialised correctly. If you have not already done so, you might need to think about how the title of the book/journal is set when it is created. This means you will have to ensure that the subclass constructors call the superclass constructor correctly.

b) Moving the year field When you have successfully moved the title field to the Publication class you can move the year field. The process will be similar.

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

Automating Access Databases With Macros

Authors: Fish Davis

1st Edition

1797816349, 978-1797816340

More Books

Students also viewed these Databases questions

Question

What are Decision Trees?

Answered: 1 week ago

Question

What is meant by the Term Glass Ceiling?

Answered: 1 week ago