Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java. Use exception and throws. A Design a class Publication that has the follow

ID: 3848807 • Letter: J

Question

Java.
Use exception and throws.
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 toString0 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 joumal 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 members to values of your choice Provide a constructor that accepts all NEEDED attributes. Throw an exception (y our 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 our 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.text.DateFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.Scanner;

/**
*
* @author Sam
*/
public class Publication {
    private String type;
    private String date;
    private String authors;
    private String title;

    public Publication() { //default constructor
       type = "book";
       date = new SimpleDateFormat("dd/MM/yyyy").format(new Date());
       authors = "Sam";
       title = "New Book";
    }

    public Publication(String type, String date, String authors, String title) { //parameterized constructor
        this.type = type;
        this.date = date;
        this.authors = authors;
        this.title = title;
      
        try { //checks if the date entered is valid
            DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
            df.setLenient(false);
            df.parse(date);
        } catch (ParseException e) {
            throw new IllegalArgumentException("Date invalid."); //throws a checked exception if invalid
        }
    }
    /*
    Getters
    and
    Setters  
    */
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
        try {
            DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
            df.setLenient(false);
            df.parse(date);
        } catch (ParseException e) {
            throw new IllegalArgumentException("Date invalid.");
        }
    }

    public String getAuthors() {
        return authors;
    }

    public void setAuthors(String authors) {
        this.authors = authors;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String toString() { //overidden toString method
        return "Publication{" + " type=" + type + " date=" + date + " authors=" + authors + " title=" + title + " }";
    }

    public void printDetails() { //metod to print class details
        System.out.println(toString());
    }

    @Override
    public boolean equals(Object obj) { //checks equal or not
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Publication other = (Publication) obj;
        if (!Objects.equals(this.type, other.type)) {
            return false;
        }
        if (!Objects.equals(this.date, other.date)) {
            return false;
        }
        if (!Objects.equals(this.authors, other.authors)) {
            return false;
        }
        if (!Objects.equals(this.title, other.title)) {
            return false;
        }
        return true;
    }

    public void populate(Scanner sc){ //populate class from passed scanner class whicn reads line
        type = sc.nextLine();
        date = sc.nextLine();
        try {
            DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
            df.setLenient(false);
            df.parse(date);
        } catch (ParseException e) {
            throw new IllegalArgumentException("Date invalid.");
        }
        authors = sc.nextLine();
        title = sc.nextLine();
    }
}

class Article extends Publication { //derived class
    private String journal;
    private int beginPage;
    private int endPage;

    public Article() {
        super(); //You may skip this line, since java calls super() by default
        journal = "IEEE Transaction on Robotics" ;
        beginPage = 143;
        endPage = 204;
    }

    public Article(String journal, int beginPage, int endPage, String type, String date, String authors, String title) throws PageException {
        super(type, date, authors, title);
        this.journal = journal;
        this.beginPage = beginPage;
        this.endPage = endPage;
      
        if (beginPage < 1 && endPage < 1 && endPage < beginPage) //checks pagenumbers, if invalid throws 'our exception'
            throw new PageException();
    }
    /*
    Getters
    and
    Setters
    */
    public String getJournal() {
        return journal;
    }

    public void setJournal(String journal) {
        this.journal = journal;
    }

    public int getBeginPage() {
        return beginPage;
    }

    public void setBeginPage(int beginPage) throws PageException {
        this.beginPage = beginPage;
        if (beginPage < 1 && endPage < 1 && endPage < beginPage)
            throw new PageException();
    }

    public int getEndPage() {
        return endPage;
    }

    public void setEndPage(int endPage) throws PageException {
        this.endPage = endPage;
        if (beginPage < 1 && endPage < 1 && endPage < beginPage)
            throw new PageException();
    }
    /*
    Some overridden methods
    */
    @Override
    public String toString() {
        return "Article{ " + super.toString() + " journal=" + journal + " beginPage=" + beginPage + " endPage=" + endPage + " }";
    }

    @Override
    public void printDetails() {
        System.out.println(toString());
    }

    @Override
    public void populate(Scanner sc) {
        super.populate(sc);
        journal = sc.nextLine();
        beginPage = Integer.parseInt(sc.nextLine());
        endPage = Integer.parseInt(sc.nextLine());
    }
  
}
//terster class
class Tester {
    public static void main(String[] args) {
        Article a1 = new Article();
        a1.printDetails();
        System.out.println("Enter Type, Date(dd/mm/yy or dd/mm/yyyy), Authors, Title, Journal, Begin page and End page. Press 'return' after each inputs");
        a1.populate(new Scanner(System.in));
        a1.printDetails();
    }
}
//our exception class
class PageException extends Exception {

    public PageException() {
        super("Check page numbers");
    }
  
}

I tried my best to keep it short and wrote these 220 lines of code just for you. Since was running out of time, had to keep the comments generous. If incase you need any clarification on this code, please let me know. I shall be happy to help you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote