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

Java. Throw an exception I need the whole code. A Design a class Publication tha

ID: 3848808 • Letter: J

Question

Java.

Throw an exception
I need the whole code.   
A Design a class Publication that has the following properties: 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. le: 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) toStringo 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

Publication.java


import java.util.Scanner;

public class Publication {
   private String pubType;
   private String Date; // format - DD/MM/YYYY
   private String authors; // List of authors saperated by comma
   private String title;

   public Publication() {
       this.pubType = "";
       this.Date = "01/01/1900";
       this.authors = "";
       this.title = "";
   }

   public Publication(String pubType, String date, String authors, String title) {
       try {
           this.pubType = pubType;
           if (checkDateFormat(date)) {
               Date = date;
           }
           this.authors = authors;
           this.title = title;
       } catch (DateFormatException e) {
           System.err.println(e.getMessage());
       }

   }

   private boolean checkDateFormat(String date) throws DateFormatException {
       boolean isDateOk = false;
       try {
           String[] words = date.split("/");
          
           int day = Integer.parseInt(words[0]);
           int month = Integer.parseInt(words[1]);
           int year = Integer.parseInt(words[2]);
           if (words[0].length() > 2 || words[1].length() > 2 || words[2].length() < 4 || words[2].length() > 4) {
               throw new DateFormatException("Date is not in correct format. Please enter Date in format DD/MM/YYYY");
           } else {
               if (day < 1 || day > 30 || month < 1 || month > 12) {
                   throw new DateFormatException("Date is not valid. Please enter the valid date.");
               } else {
                   isDateOk = true;
               }
           }
       } catch (NumberFormatException e) {
           throw new DateFormatException("Date entered must contain numbers only.");
       }
       return isDateOk;
   }

   public String getPubType() {
       return pubType;
   }

   public void setPubType(String pubType) {
       this.pubType = pubType;
   }

   public String getDate() {
       return Date;
   }

   public void setDate(String date) {
       try {
           if (checkDateFormat(date)) {
               Date = date;
           }
       } catch (DateFormatException e) {
           System.err.println(e.getMessage());
       }

   }

   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() {
       return "Publication [pubType=" + pubType + ", Date=" + Date + ", authors=" + authors + ", title=" + title + "]";
   }

   public void display() {
       System.out.println("Publication type: " + pubType);
       System.out.println("Date of publication: " + Date);
       System.out.println("Authors: " + authors);
       System.out.println("Title of publication: " + title);
   }

   public void getVals(Scanner sc) {
       System.out.println("Enter title of publication: ");
       this.setTitle(sc.nextLine());
       System.out.println("Enter the publication type: (book/article/review/interview)");
       this.setPubType(sc.nextLine());
       System.out.println("Enter the date of publication in format DD/MM/YYYY: ");
       this.setDate(sc.nextLine());
       System.out.println("Enter the name of authors saperate by commas: ");
       this.setAuthors(sc.nextLine());
   }

   @Override
   public int hashCode() {
       final int prime = 31;
       int result = 1;
       result = prime * result + ((Date == null) ? 0 : Date.hashCode());
       result = prime * result + ((authors == null) ? 0 : authors.hashCode());
       result = prime * result + ((pubType == null) ? 0 : pubType.hashCode());
       result = prime * result + ((title == null) ? 0 : title.hashCode());
       return result;
   }

   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Publication other = (Publication) obj;
       if (Date == null) {
           if (other.Date != null)
               return false;
       } else if (!Date.equals(other.Date))
           return false;
       if (authors == null) {
           if (other.authors != null)
               return false;
       } else if (!authors.equals(other.authors))
           return false;
       if (pubType == null) {
           if (other.pubType != null)
               return false;
       } else if (!pubType.equals(other.pubType))
           return false;
       if (title == null) {
           if (other.title != null)
               return false;
       } else if (!title.equals(other.title))
           return false;
       return true;
   }
  
  

}

DateFormatException.java


public class DateFormatException extends Exception {
   private String message;
   public DateFormatException(String mes) {
       this.message = mes;
   }
   public String getMessage() {
       return message;
   }
  
  
}

Article.java


import java.util.Scanner;

public class Article extends Publication {
   private String journalName;
   private String pageNos;

   public Article() {
       this.journalName = "";
       this.pageNos = "";
   }

   public Article(String journalName, String pageNos, String pubType, String date, String authors, String title) {
       super(pubType, date, authors, title);
       try {
           this.journalName = journalName;
           if (checkPageFormat(pageNos)) {
               this.pageNos = pageNos;
           }
       } catch (PageNumberFormatException e) {
           System.err.println(e.getMessage());
       }
   }

   private boolean checkPageFormat(String pageNos) throws PageNumberFormatException {
       boolean arepagesOk = false;
       String[] words = pageNos.split("-");
      
       if (words.length != 2) {
           throw new PageNumberFormatException("Please enter a valid string eg 21-24");
       } else {
       try {
           int startPageNo2 = Integer.parseInt(words[0]);
           int endPageNo2 = Integer.parseInt(words[1]);
           if(startPageNo2 < 0 || endPageNo2 < 0) {
               throw new PageNumberFormatException("page number cannot be negative.");
           } else if (startPageNo2 > endPageNo2) {
               throw new PageNumberFormatException("start page must be smaller or equal to end page.");
           } else {
               arepagesOk = true;
           }
       } catch (NumberFormatException e) {
           throw new PageNumberFormatException("Pages entered must contain numbers only.");
       }
       }

       return arepagesOk;
   }

   public String getJournalName() {
       return journalName;
   }

   public void setJournalName(String journalName) {
       this.journalName = journalName;
   }

   public String getPageNos() {
       return pageNos;
   }

   public void setPageNos(String pageNos) {
       try {
           if (checkPageFormat(pageNos)) {
               this.pageNos = pageNos;
           }
       } catch (PageNumberFormatException e) {
           System.out.println(e.getMessage());
       }
   }

   @Override
   public String toString() {
       return super.toString()+" Article [journalName=" + journalName + ", pageNos=" + pageNos + "]";
   }
  
   public void display() {
       super.display();
       System.out.println("Journel name: "+this.journalName);
       System.out.println("page numbers: "+this.pageNos);
   }
   public void getVals(Scanner sc) {
       super.getVals(sc);
       System.out.println("Enter journal name: ");
       this.setJournalName(sc.nextLine());
       System.out.println("Enter page number in format eg 21-34");
       this.setPageNos(sc.nextLine());
   }

   @Override
   public int hashCode() {
       final int prime = 31;
       int result = super.hashCode();
       result = prime * result + ((journalName == null) ? 0 : journalName.hashCode());
       result = prime * result + ((pageNos == null) ? 0 : pageNos.hashCode());
       return result;
   }

   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (!super.equals(obj))
           return false;
       if (getClass() != obj.getClass())
           return false;
       Article other = (Article) obj;
       if (journalName == null) {
           if (other.journalName != null)
               return false;
       } else if (!journalName.equals(other.journalName))
           return false;
       if (pageNos == null) {
           if (other.pageNos != null)
               return false;
       } else if (!pageNos.equals(other.pageNos))
           return false;
       return true;
   }
  
}

PageNumberFormatException.java


public class PageNumberFormatException extends Exception {
   private String message;

   public PageNumberFormatException(String mes) {
       this.message = mes;
   }

   public String getMessage() {
       return message;
   }
}

Tester.java


public class Tester {
   public static void main(String[] args) {
       Article a1 = new Article("The Science of Dragons", "22-23", "review", "03/03/1992", "Mishra Nifh,Elv Enfsi",
               "Oh my Dragons");
       Article a2 = new Article("The Science of Dragons", "22-23", "review", "03/03/1992", "Mishra Nifh,Elv Enfsi",
               "Oh my Dragons");

       System.out.println("Article 1 details using toString: ");
       System.out.println(a1);
       System.out.println("Article 1 details using display method");
       a1.display();
       System.out.println();
       System.out.println("Article 2 details using toString: ");
       System.out.println(a2);
       System.out.println("Article 2 details using display method");
       a2.display();
      
       System.out.println("Are both articles equal: "+a1.equals(a2));
       Article a3 = new Article();
      
       a3.setDate("323/32/32");
       a3.setPageNos("Av2= 232");
       a3.setPageNos("21-21");
   }
}

Sample Run: -

Article 1 details using toString:
Publication [pubType=review, Date=03/03/1992, authors=Mishra Nifh,Elv Enfsi, title=Oh my Dragons]
Article [journalName=The Science of Dragons, pageNos=22-23]
Article 1 details using display method
Publication type: review
Date of publication: 03/03/1992
Authors: Mishra Nifh,Elv Enfsi
Title of publication: Oh my Dragons
Journel name: The Science of Dragons
page numbers: 22-23

Article 2 details using toString:
Publication [pubType=review, Date=03/03/1992, authors=Mishra Nifh,Elv Enfsi, title=Oh my Dragons]
Article [journalName=The Science of Dragons, pageNos=22-23]
Article 2 details using display method
Publication type: review
Date of publication: 03/03/1992
Authors: Mishra Nifh,Elv Enfsi
Title of publication: Oh my Dragons
Journel name: The Science of Dragons
page numbers: 22-23
Are both articles equal: true
Please enter a valid string eg 21-24
Date is not in correct format. Please enter Date in format DD/MM/YYYY

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