Java class ASAP. A Design a class Publication that has the following properties:
ID: 3848816 • Letter: J
Question
Java class ASAP.A Design a class Publication that has the following properties: 1. Type of publication: book, article, review, interview .. 2. Date of publication: 09/06/2011 3. Authors: list of authors separated by comma: Noam Chomsky, Robert W. McChesney 4. aTitle: example "Profit over People: Neoliberalism & Global Order" Provide a default constructor that sets all members to default values of your choice. provide a constructor that accepts specific values for each of the private members. Bonus: throw an exception (your exception) if the date is not in the right format or if the day is negative or larger than 30 or if the month is negative or larger than 12 (disregard the year) Provide accessors for each of the members Provide mutators for each of the private members Provide a method (override) toString() that returns a string with all attributes of the object Provide a method display that displays all attributes of an object Provide a method that accepts a Scanner and reads all attributes of an object from the keyboard. Provide a method Equal that accepts an object Publication and returns true if the object is equal to current object and false otherwise. B. Derive a class Article that has in addition to members of Publication, the following: 1. Name of the journal in which the article is published: example: IEEE Transactions on Robotics 2. Beginning page number and ending page number (in form: 21-34). Provide a default constructor that sets all NEEDED attributes. Throw an exception (your exception) if the page numbers are negative, wrong order or not in the right format. Provide Accessors for the new members Provide Mutators for the new members. Throw an exception (your exception) if the page numbers are negative, wrong order or not in the right format. Override all methods that need to be overridden C. Write a class tester (main) where you test all constructors and all methods (including mutators and accessors) with appropriate variables of your choice.
Explanation / Answer
import java.util.Scanner;
public class Publication {
private String typeOfPublication;
private String date;
private String authors;
private String aTitle;
public Publication() {
// TODO Auto-generated constructor stub
this.typeOfPublication = "";
this.date = "";
this.authors = "";
this.aTitle = "";
}
/**
* @param typeOfPublication
* @param date
* @param authors
* @param aTitle
*/
public Publication(String typeOfPublication, String date, String authors,
String aTitle) {
this.typeOfPublication = typeOfPublication;
this.date = date;
this.authors = authors;
this.aTitle = aTitle;
}
/**
* @return the typeOfPublication
*/
public String getTypeOfPublication() {
return typeOfPublication;
}
/**
* @return the date
*/
public String getDate() {
return date;
}
/**
* @return the authors
*/
public String getAuthors() {
return authors;
}
/**
* @return the aTitle
*/
public String getaTitle() {
return aTitle;
}
/**
* @param typeOfPublication
* the typeOfPublication to set
*/
public void setTypeOfPublication(String typeOfPublication) {
this.typeOfPublication = typeOfPublication;
}
/**
* @param date
* the date to set
*/
public void setDate(String date) {
this.date = date;
}
/**
* @param authors
* the authors to set
*/
public void setAuthors(String authors) {
this.authors = authors;
}
/**
* @param aTitle
* the aTitle to set
*/
public void setaTitle(String aTitle) {
this.aTitle = aTitle;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Publication [typeOfPublication=" + typeOfPublication
+ ", date=" + date + ", authors=" + authors + ", aTitle="
+ aTitle + "]";
}
public void display() {
System.out.println("Type Of Publication: " + typeOfPublication
+ " Date: " + date + " authors: " + authors + " aTitle: "
+ aTitle);
}
public void readPublication() {
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
System.out.print("Enter the Type of Publication:");
this.typeOfPublication = scanner.next();
System.out.print("Enter the Date:");
this.date = scanner.next();
System.out.print("Enter the Author:");
this.authors = scanner.next();
System.out.print("Enter the Title:");
this.aTitle = scanner.next();
} catch (Exception e) {
// TODO: handle exception
}
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((aTitle == null) ? 0 : aTitle.hashCode());
result = prime * result + ((authors == null) ? 0 : authors.hashCode());
result = prime * result + ((date == null) ? 0 : date.hashCode());
result = prime
* result
+ ((typeOfPublication == null) ? 0 : typeOfPublication
.hashCode());
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Publication))
return false;
Publication other = (Publication) obj;
if (aTitle == null) {
if (other.aTitle != null)
return false;
} else if (!aTitle.equals(other.aTitle))
return false;
if (authors == null) {
if (other.authors != null)
return false;
} else if (!authors.equals(other.authors))
return false;
if (date == null) {
if (other.date != null)
return false;
} else if (!date.equals(other.date))
return false;
if (typeOfPublication == null) {
if (other.typeOfPublication != null)
return false;
} else if (!typeOfPublication.equals(other.typeOfPublication))
return false;
return true;
}
}
import java.util.Scanner;
public class Article extends Publication {
private String nameOfJournal;
private int pageNumberStart;
private int pageNumberEnd;
public Article() {
// TODO Auto-generated constructor stub
super();
this.nameOfJournal = "";
this.pageNumberStart = 0;
this.pageNumberEnd = 0;
}
/**
* @param nameOfJournal
* @param pageNumberStart
* @param pageNumberEnd
*/
public Article(String nameOfJournal, int pageNumberStart,
int pageNumberEnd, String typeOfPublication, String date,
String authors, String aTitle) {
super(typeOfPublication, date, authors, aTitle);
this.nameOfJournal = nameOfJournal;
this.pageNumberStart = pageNumberStart;
this.pageNumberEnd = pageNumberEnd;
}
/**
* @return the nameOfJournal
*/
public String getNameOfJournal() {
return nameOfJournal;
}
/**
* @return the pageNumberStart
*/
public int getPageNumberStart() {
return pageNumberStart;
}
/**
* @return the pageNumberEnd
*/
public int getPageNumberEnd() {
return pageNumberEnd;
}
/**
* @param nameOfJournal
* the nameOfJournal to set
*/
public void setNameOfJournal(String nameOfJournal) {
this.nameOfJournal = nameOfJournal;
}
/**
* @param pageNumberStart
* the pageNumberStart to set
*/
public void setPageNumberStart(int pageNumberStart) {
this.pageNumberStart = pageNumberStart;
}
/**
* @param pageNumberEnd
* the pageNumberEnd to set
*/
public void setPageNumberEnd(int pageNumberEnd) {
this.pageNumberEnd = pageNumberEnd;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Article [nameOfJournal=" + nameOfJournal + ", pageNumberStart="
+ pageNumberStart + ", pageNumberEnd=" + pageNumberEnd
+ ", toString()=" + super.toString() + "]";
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result
+ ((nameOfJournal == null) ? 0 : nameOfJournal.hashCode());
result = prime * result + pageNumberEnd;
result = prime * result + pageNumberStart;
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (!(obj instanceof Article))
return false;
Article other = (Article) obj;
if (nameOfJournal == null) {
if (other.nameOfJournal != null)
return false;
} else if (!nameOfJournal.equals(other.nameOfJournal))
return false;
if (pageNumberEnd != other.pageNumberEnd)
return false;
if (pageNumberStart != other.pageNumberStart)
return false;
return true;
}
@Override
public void display() {
super.display();
System.out.println("Name of Journal: " + nameOfJournal
+ " Beginning Page Number: " + pageNumberStart
+ " Ending Page Number: " + pageNumberEnd);
}
@Override
public void readPublication() {
// TODO Auto-generated method stub
super.readPublication();
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
System.out.print("Enter the Name of Journal:");
this.nameOfJournal = scanner.next();
System.out.print("Enter the Beginning page number:");
this.pageNumberStart = scanner.nextInt();
System.out.print("Enter the Ending page number:");
this.pageNumberEnd = scanner.nextInt();
} catch (Exception e) {
// TODO: handle exception
}
}
}
public class TestPublication {
public static void main(String[] args) {
Publication publication = new Publication();
publication.readPublication();
System.out.println("Publication Details:");
publication.display();
Article article = new Article();
article.readPublication();
System.out.println("Article Details:");
article.display();
}
}
OUTPUT:
Enter the Type of Publication:pubname
Enter the Date:10/10/2017
Enter the Author:authonme
Enter the Title:title
Publication Details:
Type Of Publication: pubname
Date: 10/10/2017
authors: authonme
aTitle: title
Enter the Type of Publication:typepub
Enter the Date:10/10/2017
Enter the Author:auth
Enter the Title:title
Enter the Name of Journal:cse
Enter the Beginning page number:12
Enter the Ending page number:22
Article Details:
Type Of Publication: typepub
Date: 10/10/2017
authors: auth
aTitle: title
Name of Journal: cse
Beginning Page Number: 12
Ending Page Number: 22
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.