Jump to content

Needing help with Java + Months corresponding to integers

Hi, I'm doing this one assignment but on one of the questions, it says "Create a new method called getMonthName() ...which returns the month name that matches the monthPublished number (e.g. "January" for month 1). I'm very new to Java..any help is much appreciated. 

It didn't compile fully cause "public String getMonth()" doesn't work at the bottom of the code. 

 

 

 

 

 

/**
 * This class describes a book.
 * @author: tomoki
 * @version: 1.0
 */
class Book{
    private String  title;
    private int     ISBN;
    private String  lastName;
    private String  firstName;
    private int     publishYear;
    private int     monthPublished;
    
    /**
     * A contstructor for parameters of a book.
     * @param title          - the book's title
     * @param ISBN           - the book's ISBN number
     * @param lastName       - the book's author last name
     * @param firstName      - the book's author first name
     * @param publishTitle   - the book's publishing in year
     * @param monthPublished - the book's publishing month
     */
    public Book(String newTitle, int newISBN, String newLastName, String newFirstName, int newPublishYear, int newMonthPublished){
        setTitle(newTitle); 
        setISBN(newISBN); 
        setLastName(newLastName); 
        setFirstName(newFirstName); 
        setPublishTitle(newPublishYear);
        setMonthPublished(newMonthPublished);
    }
        
    /**
     * This method sets the acceptable title length.
     */
        public void setTitle(String newTitle){
        if(newTitle.length() <= 3){
        /* if the title length is less than or equal to 3 characters, it will not be accepted */
            System.out.println("error; title length too short");
        }else{
         System.out.println("accepted");
         title = newTitle;
        }
    }
    /** 
     * This method sets the amount of characters accepted for the ISBN. 
     */
    public void setISBN(int newISBN){
        if(newISBN < 10000 && newISBN > 30000){
        /* less than 10000 and more than 30000 will produce an error */
            System.out.println("error");
        }else{
        System.out.println("ISBN accepted");
            ISBN = newISBN;
        }
    }
    /**
     * This method sets it so the lastnames cannot be null.
     */
    public void setLastName(String newLastName){
        if(newLastName == null){
        /* not storing this; last name cannot be null */
        System.out.println("I'm not storing a null name");
        }else{
        System.out.println("Last name accepted");
            lastName = newLastName;
        }
    }
    /**
     * This method sets it so that first names cannot be null.
     */
    public void setFirstName(String newFirstName){
        if(newFirstName == null){
        /* not storing this; first name cannot be null */
        System.out.println("I'm not storing a null name");
        }else{
        System.out.println("First name accepted");
            firstName = newFirstName;
        }
    }
    /**
     * This method sets it so that the year published must be between 1870 - 2013 inclusive. 
     */
    public void setPublishTitle(int newPublishYear){
        if(newPublishYear < 1870 && newPublishYear > 2013){
        /* not storing anything older than 1870 and later than 2013 */
        System.out.println("I'm not storing anything older than 1870 and later than 2013");
        }else{
        System.out.println("Publish title accepted");
            publishYear = newPublishYear;
        }
    }
    /**
     * This method sets it so that the month published must be between 1 - 12 inclusive.
     */
    public void setMonthPublished(int newMonthPublished){
        if(newMonthPublished < 1 && newMonthPublished > 12){
        /* not storing month published if it is outside of the 1-12 range */
        System.out.println("publishing month must be between 1-12");
        }else{
        System.out.println("Month published accepted");
            monthPublished = newMonthPublished;
        }
    }
    /** 
     * @return the title of the book
     */
    public String getTitle(){
    return title;
    }
    /**
     * @return the ISBN of the book
     */
    public int getISBN(){
    return ISBN;
    }
    /**
     * @return the last name of the author 
     */
    public String getLastName(){
    return lastName;
    }
    /**
     * @return the first name of the author
     */
    public String getFirstName(){
    return firstName;
    }
    /**
     * @return the publish year of the book
     */
    public int getPublishYear(){
    return publishYear;
    }
    /**
     * @return the month in which the book was published
     */
    public int getMonthPublished(){
    return monthPublished;
    }
    /**
     * @return the month name that corresponds to the month published number
     */
    public String getMonthName(){
    return monthName;
    }
}
   

Link to post
Share on other sites

11 minutes ago, elpiop said:

You don't have a variable named 'monthName'. You can't return it if it doesn't exist. 

Hahaha yup. I figured it out! Um... I didn't know how to delete a post off the thread so.... :D

 

	/**
	 * This class describes a book.
	 * @author: tomoki
	 * @version: 1.0
	 */
	class Book{
	    private String  title;
	    private int     ISBN;
	    private String  lastName;
	    private String  firstName;
	    private int     publishYear;
	    private int     monthPublished;
	    
	    /**
	     * A contstructor for parameters of a book.
	     * @param title          - the book's title
	     * @param ISBN           - the book's ISBN number
	     * @param lastName       - the book's author last name
	     * @param firstName      - the book's author first name
	     * @param publishTitle   - the book's publishing in year
	     * @param monthPublished - the book's publishing month
	     */
	    public Book(String newTitle, int newISBN, String newLastName, String newFirstName, int newPublishYear, int newMonthPublished){
	        setTitle(newTitle); 
	        setISBN(newISBN); 
	        setLastName(newLastName); 
	        setFirstName(newFirstName); 
	        setPublishTitle(newPublishYear);
	        setMonthPublished(newMonthPublished);
	    }
	        
	    /**
	     * This method sets the acceptable title length.
	     */
	        public void setTitle(String newTitle){
	        if(newTitle.length() &lt;= 3){
	        /* if the title length is less than or equal to 3 characters, it will not be accepted */
	            System.out.println("error; title length too short");
	        }else{
	         System.out.println("accepted");
	         title = newTitle;
	        }
	    }
	    /** 
	     * This method sets the amount of characters accepted for the ISBN. 
	     */
	    public void setISBN(int newISBN){
	        if(newISBN &lt; 10000 &amp;&amp; newISBN &gt; 30000){
	        /* less than 10000 and more than 30000 will produce an error */
	            System.out.println("error");
	        }else{
	        System.out.println("ISBN accepted");
	            ISBN = newISBN;
	        }
	    }
	    /**
	     * This method sets it so the lastnames cannot be null.
	     */
	    public void setLastName(String newLastName){
	        if(newLastName == null){
	        /* not storing this; last name cannot be null */
	        System.out.println("I'm not storing a null name");
	        }else{
	        System.out.println("Last name accepted");
	            lastName = newLastName;
	        }
	    }
	    /**
	     * This method sets it so that first names cannot be null.
	     */
	    public void setFirstName(String newFirstName){
	        if(newFirstName == null){
	        /* not storing this; first name cannot be null */
	        System.out.println("I'm not storing a null name");
	        }else{
	        System.out.println("First name accepted");
	            firstName = newFirstName;
	        }
	    }
	    /**
	     * This method sets it so that the year published must be between 1870 - 2013 inclusive. 
	     */
	    public void setPublishTitle(int newPublishYear){
	        if(newPublishYear &lt; 1870 &amp;&amp; newPublishYear &gt; 2013){
	        /* not storing anything older than 1870 and later than 2013 */
	        System.out.println("I'm not storing anything older than 1870 and later than 2013");
	        }else{
	        System.out.println("Publish title accepted");
	            publishYear = newPublishYear;
	        }
	    }
	    /**
	     * This method sets it so that the month published must be between 1 - 12 inclusive.
	     */
	    public void setMonthPublished(int newMonthPublished){
	        if(newMonthPublished &lt; 1 &amp;&amp; newMonthPublished &gt; 12){
	        /* not storing month published if it is outside of the 1-12 range */
	        System.out.println("publishing month must be between 1-12");
	        }else{
	        System.out.println("Month published accepted");
	            monthPublished = newMonthPublished;
	        }
	    }
	    public void printDetails(String printDetails){
	    System.out.println("Title:" + "The Brothers Karamazov");
	    System.out.println("ISBN:" + "15000");
	    System.out.println("Author:" + "Fyodor Dostoyevsky");
	    System.out.println("Published:" + "November 1880");
	    }
	    /** 
	     * @return the title of the book
	     */
	    public String getTitle(){
	    return title;
	    }
	    /**
	     * @return the ISBN of the book
	     */
	    public int getISBN(){
	    return ISBN;
	    }
	    /**
	     * @return the last name of the author 
	     */
	    public String getLastName(){
	    return lastName;
	    }
	    /**
	     * @return the first name of the author
	     */
	    public String getFirstName(){
	    return firstName;
	    }
	    /**
	     * @return the publish year of the book
	     */
	    public int getPublishYear(){
	    return publishYear;
	    }
	    /**
	     * @return the month in which the book was published
	     */
	    public int getMonthPublished(){
	    return monthPublished;
	    }
	    /**
	     * @return the month name which corresponds to the month integer
	     */
	    public String getMonthName(){
	    if(monthPublished == 1){
	    return "January";
	    }else if(monthPublished == 2){
	    return "February";
	    }else if(monthPublished == 3){
	    return "March";
	    }else if(monthPublished == 4){
	    return "April";
	    }else if(monthPublished == 5){
	    return "May";
	    }else if(monthPublished == 6){
	    return "June";
	    }else if(monthPublished == 7){
	    return "July";
	    }else if(monthPublished == 8){
	    return "August";
	    }else if(monthPublished == 9){
	    return "September";
	    }else if(monthPublished == 10){
	    return "October";
	    }else if(monthPublished == 11){
	    return "November";
	    }else if(monthPublished == 12){
	    return "December";
	    }else{
	    return "invalid";
	    }
	    }
	    /**
	     * This accessor returns the first name and last name of the author in order.
	     */
	    public String getFullName(){
	    return firstName + " " + lastName;
	    }
	}
	   

Link to post
Share on other sites

Also, wouldn't you want to be using OR ( || ) in your if statements? For example, 

 public void setPublishTitle(int newPublishYear){
       if(newPublishYear < 1870 && newPublishYear > 2013){

You would want to not store the book if the year is less than 1870 OR greater than 2013. If you use && (AND) it will accept any year that you input since the year will never be both below 1870 and above 2013. Only one or the other. 

 

This applies to setISBN and setMonthPublished too. 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×