Jump to content

Hi Everyone,

 

I've been working on this assignment for a while now and to test it, I ran the "Test Assignment" class. It always stops compiling at getFormattedDate(). I'm not sure what exactly I've done wrong and am looking for help. I'm fairly new to coding so please bear with me~ Thanks

Dom's Assignment2.zip

Link to comment
https://linustechtips.com/topic/610098-java-method-error/
Share on other sites

Link to post
Share on other sites

29 minutes ago, tomoki said:

Hi Everyone,

 

I've been working on this assignment for a while now and to test it, I ran the "Test Assignment" class. It always stops compiling at getFormattedDate(). I'm not sure what exactly I've done wrong and am looking for help. I'm fairly new to coding so please bear with me~ Thanks

Dom's Assignment2.zip

 

23 minutes ago, Nuluvius said:

You need to post your code in code tags. I doubt anyone is going to download and open .zip files.

totally downloaded it.

 

/**
 * This class is specialized towards the creation of objects
 * from the student's project.  It exposes the objects' contents
 * via accessors and System.out.println() statements.
 * 
 * The very first thing it does, is create a TestDate object,
 * which behaves similarly.  TestDate differs from TestAssignment
 * due to its focus on the Date.getDayOfTheWeek() feature.
 * Also, TestDate is a module created by the student, whereas
 * TestAssignment was provided by the instructors.
 * 
 * TestAssignment is not invoked directly during evaluation.
 * The module named RunTest will create a TestAssignment object,
 * and capture the output.  When the output is captured,
 * it is compared to a prepared script.  The whole
 * script must match by content and order.
 */
public class TestAssignment
{

    public TestAssignment()
    {
        TestDate td = new TestDate();

        Date d = new Date();
        System.out.println(d.getFormattedDate());
        // calls private method getPaddedMonth()
        // calls private method getPaddedDay()
        System.out.println(Date.getMonthName(3));
        System.out.println(d.getDayOfTheWeek());
        // calls private method isLeapYear()
        System.out.println(d.getNumberOfDaysInThisMonth ());
        System.out.println(Date.getMonthNumber("December"));

        Date d2 = new Date(2001, 6, 29);
        System.out.println(d2.getFormattedDate());
        System.out.println(d2.getDayOfTheWeek());
        System.out.println(d2.getNumberOfDaysInThisMonth());

        Date d3 = new Date(1995, 14, 8);  // invalid date; month is > 12 
        System.out.println(d3.getFormattedDate());
        System.out.println(d3.getDayOfTheWeek());
        System.out.println(d3.getNumberOfDaysInThisMonth());

        System.out.println(d3.getYear());
        System.out.println(d3.getMonth());
        System.out.println(Date.getMonthName(d3.getMonth()));

        d3.setYear(3500);	// too high; don''t set it
        System.out.println(d3.getYear());
        d3.setYear(-3);	// too low; don't set it
        System.out.println(d3.getYear());
        d3.setYear(2010);	
        System.out.println(d3.getYear());
        d3.setMonth(13);	// too high; don't set it
        System.out.println(d3.getMonth());
        d3.setMonth(0);	// too low ; don't set it
        System.out.println(d3.getMonth());
        d3.setMonth(8);	
        System.out.println(d3.getMonth());

        d3.setDay(32);	// too high; don't set it
        System.out.println(d3.getDay());
        d3.setDay(0);	// too low ; don't set it
        System.out.println(d3.getDay());
        d3.setDay(27);	
        System.out.println(d3.getDay());

        Name n = new Name();
        System.out.println(n.getFullName());
        // calls makePrettyName()
        System.out.println(n.getInitials());
        Name n2 = new Name("sTEphen", "KinG", null);
        System.out.println(n2.getFullName()); 
        System.out.println(n2.getInitials());
        Name nn = new Name("sTEphen","KinG", "eDWin");
        System.out.println(nn.getFullName());
        System.out.println(nn.getInitials());

        Name n3 = new Name(null, "king", "edwin");
        System.out.println(n3.getFullName());
        System.out.println(n3.getInitials()); // first cannot be null

        Name n4 = new Name("stepHEn", null, "edWIN"); 
        System.out.println(n4.getFullName()); // last cannot be null
        System.out.println(n4.getInitials());

        Name n5 = new Name("", "king", "Edwin"); // too short
        System.out.println(n5.getFullName());
        System.out.println(n5.getInitials());

        Name n6 = new Name(null, "king", "edWIn"); // null first not allowed 
        System.out.println(n6.getFullName());
        System.out.println(n6.getInitials());

        Name n7 = new Name("Stephen", "king", "01234567890123456789012345678901234567890123456789");
        System.out.println(n7.getFullName()); // too long
        System.out.println(n7.getInitials());

        Name n8 = new Name("sTEphen", "KinG", null);
        System.out.println(n8.getFirstName());
        System.out.println(n8.getMiddleName());
        System.out.println(n8.getLastName());

        Name n9 = new Name("sTEphen", "KinG", "edWIN");
        System.out.println(n9.getMiddleName());

        Author a = new Author(); // default dates and names
        System.out.println(a.getDateBorn().getFormattedDate());//default
        System.out.println(a.getName().getFullName()); //default
        System.out.println(a.getDateDied().getFormattedDate());//default
        System.out.println(a.getName().getFullName()); //default
        System.out.println(a.isAuthorAlive()); 
        System.out.println(a.getAgeYearsOfAuthor()); // note: it is 2013 now

        Date d1 = new Date(1947, 9, 21);
        Author a1 = new Author(null, d1, null); // null name not allowed
        System.out.println(a1.getName().getFullName());

        Name n1 = new Name("sTEphen", "KinG", null);
        Author a2 = new Author(n1, null, null); // null born not allowed
        System.out.println(a2.getDateBorn().getFormattedDate());

        Author a3 = new Author(n1, d1, null); 
        System.out.println(a3.getDateBorn().getFormattedDate());
        System.out.println(a3.getName().getFullName()); 

        Name name = new Name("sTEphen", "KinG", "edWIN");
        Date born = new Date(1947, 9, 21);
        Name pseudonym = new Name("RichaRD", "BachMAN", null);
        Author a4 = new Author(name, born, null, pseudonym);
        System.out.println(a4.getName().getFullName());
        System.out.println(a4.getDateDied());
        System.out.println(a4.getDateBorn().getFormattedDate());
        System.out.println(a4.getPseudonym().getFullName());

        Name name1 = new Name("sTEphen", "KinG", "edWIN");
        Date born1 = new Date(1947, 9, 21);
        Author a5 = new Author(name1, born1, null); // no pseudonym
        System.out.println(a5.getName().getFullName());
        System.out.println(a5.getDateDied());
        System.out.println(a5.getDateBorn().getFormattedDate());
        System.out.println(a5.getPseudonym());

        Name name2 = new Name("sTEphen", "KinG", "edWIN");
        Date born2 = new Date(1947, 9, 21);
        Date died = new Date(2000, 6, 13);
        Author a6 = new Author(name2, born2, died);
        System.out.println(a6.getDateDied().getFormattedDate());

        Author a7 = new Author("stephen", "king", null, 1947, 9, 21, 2000, 6, 13, "richard", "bachman", null);
        System.out.println(a7.getDateDied().getFormattedDate());
        System.out.println(a7.getName().getFullName());
        System.out.println(a7.getDateBorn().getFormattedDate());
        System.out.println(a7.getPseudonym().getFullName());

        Name name3 = new Name("sTEphen", "KinG", "edWIN");
        Date born3 = new Date(1947, 9, 21);
        Date died3 = new Date(2000, 6, 13);
        Author a8 = new Author(name3, born3, died3);
        System.out.println(a8.getAgeYearsOfAuthor()); // note: it's 2013 now

        Book bb = new Book();	// default title is "Untitled"
        System.out.println(bb.getTitle()); 
        System.out.println(bb.getAuthor().getName().getFullName());
        System.out.println(bb.getAuthor().getPseudonym().getFullName());
        System.out.println(bb.getAuthor().getDateBorn().getFormattedDate());
        System.out.println(bb.getAuthor().getDateDied().getFormattedDate());
        System.out.println(bb.getDatePublished().getFormattedDate());

        Date date = new Date(2000, 4, 29);
        String title = "It";
        Book b2 = new Book(null, date, title);	// Author is null
        System.out.println(b2.getAuthor().getName().getFullName());
        System.out.println(b2.getTitle ());
        System.out.println(b2. getDatePublished().getFormattedDate());

        Book b3 = new Book(null, null, null);	// Date, title are null 
        System.out.println(b3. getDatePublished().getFormattedDate());
        System.out.println(b3.getTitle ());

        Name name4 = new Name("sTEphen", "KinG", "edWIN");
        Date born4 = new Date(1947, 9, 21);
        Date died4 = new Date(2000, 6, 13);
        Author aa = new Author(name4, born4, died4);
        Date published4 = new Date(1988, 10, 31);
        Book b4 = new Book(aa, published4, "Carrie");
        System.out.println(b4.getAuthorName ());
        System.out.println(b4.getDayOfTheWeekBookWasPublished());
        b4.printDetails();

        Name name5 = new Name("sTEphen", "KinG", null);
        Date born5 = new Date(1947, 9, 21);
        Date died5 = new Date(2000, 6, 13);
        Name pseudonym5 = new Name("Richard", "Bachman", null);
        Author aaa = new Author(name5, born5, died5, pseudonym5);
        Date published5 = new Date(1988, 10, 31);
        Book b5 = new Book(aaa, published5, "Carrie");
        b5.printDetails();
    } // end method
} // end class

I believe you actually have to give it the format such as yyyy-mm-dd

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/610098-java-method-error/#findComment-7891103
Share on other sites

Link to post
Share on other sites

9 hours ago, Nuluvius said:

You need to post your code in code tags. I doubt anyone is going to download and open .zip files.


/**
 * This is a class for date.
 */
class Date{
    private int year;
    private int month;
    private int day;

    public static final int    JANUARY = 1;
    public static final int    FEBRUARY = 2;
    public static final int    MARCH = 3;
    public static final int    APRIL = 4;
    public static final int    MAY = 5;
    public static final int    JUNE = 6;
    public static final int    JULY = 7;
    public static final int    AUGUST = 8;
    public static final int    SEPTEMBER = 9;
    public static final int    OCTOBER = 10;
    public static final int    NOVEMBER = 11;
    public static final int    DECEMBER = 12;
    public static final int    SATURDAY = 0;
    public static final int    SUNDAY = 1;
    public static final int    MONDAY = 2;
    public static final int    TUESDAY = 3;
    public static final int    WEDNESDAY = 4;
    public static final int    THURSDAY = 5;
    public static final int    FRIDAY = 6;
    public static final String JANUARY_STRING = "January";
    public static final String FEBRUARY_STRING = "February";
    public static final String MARCH_STRING = "March";
    public static final String APRIL_STRING = "April";
    public static final String MAY_STRING = "May";
    public static final String JUNE_STRING = "June";
    public static final String JULY_STRING = "July";
    public static final String AUGUST_STRING = "August";
    public static final String SEPTEMBER_STRING = "September";
    public static final String OCTOBER_STRING = "October";
    public static final String NOVEMBER_STRING = "November";
    public static final String DECEMBER_STRING = "December";
    public static final int CURRENT_YEAR = 2013;
    public static final int CODE_ONE = 1;
    public static final int CODE_TWO = 2;
    public static final int CODE_THREE = 3;
    public static final int CODE_FOUR = 4;
    public static final int CODE_FIVE = 5;
    public static final int CODE_SIX = 6;
    public static final int LEAP_ONE = 1;
    public static final int LEAP_TWO = 2;
    public static final int LEAP_THREE = 3;
    public static final int LEAP_FOUR = 4;
    public static final int LEAP_FIVE = 5;
    public static final int LEAP_SIX = 6;
    public static final int FEB_DAYS = 28;
    public static final int FEB_LEAP = 29;
    public static final int EVEN_DAYS = 30;
    public static final int ODD_DAYS = 31;

    /**
     * This is a constructor that sets the date to January 1 2013.
     */
    public Date(){
        year    = CURRENT_YEAR;
        month   = JANUARY;
        day     = SUNDAY;
    }

    /**
     * @param year - this is the year specified
     * @param month - this is the month specified
     * @param day - this is the day specified
     */
    public Date(int year, int month, int day){
        setYear(year);
        setMonth(month);
        setDay(day);
    }

    /**
     * @param year - the mutator accepts anything year under the current year
     */
    public void setYear(int year){
        if(year <= CURRENT_YEAR){
            this.year = year;
        }
    }

    /**
     * @param month - the mutator accepts integer dates ranging from January to December. 
     */
    public void setMonth(int month){
        if(month >= JANUARY && month <= DECEMBER){
            this.month = month;
        }
    }

    /**
     * @param day - the mutator allows for storing dates ranging from 28 to 31 days depending on the month.
     */
    public void setDay(int day){
        if(month == JANUARY && month <= ODD_DAYS){
            this.day = day;
        }else if(month == FEBRUARY && isLeapYear(year) == true){
            this.day = day;
        }else if(month == FEBRUARY && isLeapYear(year) == false){
            this.day = day;
        }else if(month == MARCH && month <= ODD_DAYS){
            this.day = day;
        }else if(month == APRIL && month <= EVEN_DAYS){
            this.day = day;
        }else if(month == MAY && month <= ODD_DAYS){
            this.day = day;
        }else if(month == JUNE && month <= EVEN_DAYS){
            this.day = day;
        }else if(month == JULY && month <= ODD_DAYS){
            this.day = day;
        }else if(month == AUGUST && month <= ODD_DAYS){
            this.day = day;
        }else if(month == SEPTEMBER && month <= EVEN_DAYS){
            this.day = day;
        }else if(month == OCTOBER && month <= ODD_DAYS){
            this.day = day;
        }else if(month == NOVEMBER && month <= EVEN_DAYS){
            this.day = day;
        }else if(month == DECEMBER && month <= ODD_DAYS){
            this.day = day;
        }else{
            System.out.println("Error. Cannot set day.");
        }
    }

    /**
     * @return the year
     */
    public int getYear(){
        return year;
    }

    /**
     * @return the month
     */
    public int getMonth(){
        return month;
    }

    /**
     * @return the day
     */
    public int getDay(){
        return day;
    }

    /**
     * @return the given day of the week with leap year changes if true. 
     */
    public int getDayOfTheWeek(){
        int i;
        int x;
        int stepOne;
        int stepTwo;
        int stepThree;
        int stepFour;
        int stepFive;
        int stepSix;

        i = year % 100;
        x = year / 100;
        if(x == 16){
            return LEAP_SIX;
        }else if(x == 17){
            return LEAP_FOUR;
        }else if(x == 18){
            return LEAP_TWO;
        }else if(x == 20){
            return LEAP_SIX;
        }else if(x == 21){
            return LEAP_FOUR;
        }
        stepOne = i / 12;
        stepTwo = i - stepOne;
        stepThree = stepTwo / 4;
        stepFour = day;
        if(month == JANUARY){
            if(isLeapYear(year) == true){
                stepFive = CODE_ONE + x - LEAP_ONE;
            }else{
                stepFive = CODE_ONE;
            }
        }else if(month == FEBRUARY){
            if(isLeapYear(year) == true){
                stepFive = CODE_FOUR + x - LEAP_ONE;
            }else{stepFive = CODE_FOUR;
            }
        }else if(month == MARCH){
            if(isLeapYear(year) == true){
                stepFive = CODE_FOUR + x;
            }else{stepFive = CODE_FOUR;
            }
        }else if(month == APRIL){
            if(isLeapYear(year) == true){
                stepFive = 0 + x;
            }else{
                stepFive = 0;
            }
        }else if(month == MAY){
            if(isLeapYear(year) == true){
                stepFive = CODE_TWO + x;
            }else{
                stepFive = CODE_TWO;}
        }else if(month == JUNE){
            if(isLeapYear(year) == true){
                stepFive = CODE_FIVE + x;
            }else{
                stepFive = CODE_FIVE;
            }
        }else if(month == JULY){
            if(isLeapYear(year) == true){
                stepFive = 0 + x;
            }else{
                stepFive = 0;
            }
        }else if(month == AUGUST){
            if(isLeapYear(year) == true){
                stepFive = CODE_THREE + x;
            }else{
                stepFive = CODE_THREE;
            }
        }else if(month == SEPTEMBER){
            if(isLeapYear(year) == true){
                stepFive = CODE_SIX + x;
            }else{
                stepFive = CODE_SIX;
            }
        }else if(month == OCTOBER){
            if(isLeapYear(year) == true){
                stepFive = CODE_ONE + x;
            }else{
                stepFive = CODE_ONE;
            }
        }else if(month == NOVEMBER){
            if(isLeapYear(year) == true){
                stepFive = CODE_FOUR + x;
            }else{
                stepFive = CODE_FOUR;
            }
        }else if(month == DECEMBER){
            if(isLeapYear(year) == true){
                stepFive = CODE_SIX + x;
            }else{
                stepFive = CODE_SIX;
            }
        }else{
            return 0;
        }
        stepSix = (stepOne + stepTwo + stepThree + stepFour + stepFive) % 7; 
        if(stepSix == SATURDAY){
            System.out.println("Saturday");
        }else if(stepSix == SUNDAY){
            System.out.println("Sunday");
        }else if(stepSix == MONDAY){
            System.out.println("Monday");
        }else if(stepSix == TUESDAY){
            System.out.println("Tuesday");
        }else if(stepSix == WEDNESDAY){
            System.out.println("Wednesday");
        }else if(stepSix == THURSDAY){
            System.out.println("Thursday");
        }else if(stepSix == FRIDAY){
            System.out.println("Friday");
        }else{
            return stepSix;
        }
        return 0;
    }

    /*
     * This method tests if the year is a leap year. 
     */
    private boolean isLeapYear(int year){
        if(year % 4 !=0){
            return false;
        }else if (year % 400 == 0){
            return true;
        }else if (year % 100 == 0){
            return false;
        }else{
            return true;
        }
    }

    /**
     * @return the number of days in a particular month. 
     */
    public int getNumberOfDaysInThisMonth(){
        int x;
        if(month == JANUARY){
            x = ODD_DAYS;
        }else if(month == FEBRUARY){
            if(isLeapYear(year) == true){
                x = FEB_DAYS + CODE_ONE;
            }else
                x = FEB_DAYS;
        }else if(month == MARCH){
            x = ODD_DAYS; 
        }else if(month == APRIL){
            x = EVEN_DAYS;
        }else if(month == MAY){
            x = ODD_DAYS;
        }else if(month == JUNE){
            x = EVEN_DAYS;
        }else if(month == JULY){
            x = ODD_DAYS;
        }else if(month == AUGUST){
            x = ODD_DAYS;
        }else if(month == SEPTEMBER){
            x = EVEN_DAYS;
        }else if(month == OCTOBER){
            x = ODD_DAYS;
        }else if(month == NOVEMBER){
            x = EVEN_DAYS;
        }else if(month == DECEMBER){
            x = ODD_DAYS;
        }else{
            return 0;
        }
        return x;
    }

    /**
     * @return the date in a format (yyyy-mm-dd).
     */
    public String getFormattedDate(){
        String prettyMonth = "" + month;
        String prettyDay   = "" + day;

        if(month < 10){
            prettyMonth = "" + 0 + month;
        }

        if(day < 10){
            prettyDay = "" + 0 + day;
        }        

        return (year + "-" + prettyMonth + "-" + prettyDay); 
    }

    /*
     * @return a string in a particular format which gives a 0 in front of months less than 10.
     */
    private String getPaddedMonth(){
        if(month < 10 && month >= 1){
            return "'0" + month + "'";
        }else{
            return "'" + month + "'";
        }
    }

    /*
     * @return a string in a particular format which gives a 0 in front of days less than 10.
     */
    private String getPaddedDay(){
        if(day < 10 && day >= 1){
            return "'0" + day + "'";
        }else{
            return "'" + day + "'";
        }
    }

    /**
     * @returns the month name in exchange for month value.
     */
    public static String getMonthName(int monthNumber){
        if(monthNumber == JANUARY){
            return JANUARY_STRING;
        }else if(monthNumber == FEBRUARY){
            return FEBRUARY_STRING;
        }else if(monthNumber == MARCH){
            return MARCH_STRING;
        }else if(monthNumber == APRIL){
            return APRIL_STRING;
        }else if(monthNumber == MAY){
            return MAY_STRING;
        }else if(monthNumber == JUNE){
            return JUNE_STRING;
        }else if(monthNumber == JULY){
            return JULY_STRING;
        }else if(monthNumber == AUGUST){
            return AUGUST_STRING;
        }else if(monthNumber == SEPTEMBER){
            return SEPTEMBER_STRING;
        }else if(monthNumber == OCTOBER){
            return OCTOBER_STRING;
        }else if(monthNumber == NOVEMBER){
            return NOVEMBER_STRING;
        }else if(monthNumber == DECEMBER){
            return DECEMBER_STRING;
        }else{
            return "Invalid getMonthName";
        }
    }

    public static int getMonthNumber(String monthName){
        if(monthName == JANUARY_STRING){
            return JANUARY;
        }else if(monthName == FEBRUARY_STRING){
            return FEBRUARY;
        }else if(monthName == MARCH_STRING){
            return MARCH;
        }else if(monthName == APRIL_STRING){
            return APRIL;
        }else if(monthName == MAY_STRING){
            return MAY;
        }else if(monthName == JUNE_STRING){
            return JUNE;
        }else if(monthName == JULY_STRING){
            return JULY;
        }else if(monthName == AUGUST_STRING){
            return AUGUST;
        }else if(monthName == SEPTEMBER_STRING){
            return SEPTEMBER;
        }else if(monthName == OCTOBER_STRING){
            return OCTOBER;
        }else if(monthName == NOVEMBER_STRING){
            return NOVEMBER;
        }else if(monthName == DECEMBER_STRING){
            return DECEMBER;
        }else{
            return 0;
        }
    }
}


/**
 * This class describes a name.
 * @author: Dominic Ng
 * @version: 1.0
 */
class Name{
    private String first;
    private String last;
    private String middle;

    public static final int MINIMUM_NAME_LENGTH = 1;
    public static final int MAXIMUM_NAME_LENGTH = 40;

    /**
     * This is the first constructor.
     */
    public Name(){
        first = "A. ";
        middle = "Non";
        last = "Ymous";
    }

    /**
     * This is the second constructor. 
     * @param first - the first name. 
     * @param middle - the middle name.
     * @param last - the last name. 
     */
    public Name(String first, String last, String middle){
        if(first == null && first.length() < MINIMUM_NAME_LENGTH && first.length() > MAXIMUM_NAME_LENGTH){
            this.first = first;
        }else if(last == null && last.length() < MINIMUM_NAME_LENGTH && last.length() > MAXIMUM_NAME_LENGTH){
            this.last = last;
        }else if(middle.length() < MINIMUM_NAME_LENGTH && middle.length() > MAXIMUM_NAME_LENGTH){
            this.middle = middle;
        }else{
            return;
        }
    }

    /**
     * This mutator sets it so that the first name cannot be null and first name must be between MINIMUM_NAME_LENGTH and MAXIMUM_NAME_LENGTH characters.
     */
    public void setFirst(String first){
        if(first != null && first.length() < MINIMUM_NAME_LENGTH && first.length() > MAXIMUM_NAME_LENGTH){
            this.first = first;
        }
    }

    /**
     * This mutator sets it so that middle name must be between MINIMUM_NAME_LENGTH and MAXIMUM_NAME_LENGTH characters.
     */
    public void setMiddle(String middle){
        if(middle.length() < MINIMUM_NAME_LENGTH && middle.length() > MAXIMUM_NAME_LENGTH){
            this.middle = middle;
        }
    }

    /**
     * This mutator sets it so that the last name cannot be null and last name must be between MINIMUM_NAME_LENGTH and MAXIMUM_NAME_LENGTH characters.
     */
    public void setLast(String last){
        if(last != null && last.length() < MINIMUM_NAME_LENGTH && last.length() > MAXIMUM_NAME_LENGTH){
            this.last = last;
        }
    }

    /**
     * @return the first name.
     */
    public String getFirstName(){
        return first;
    }

    /**
     * @return the middle name.
     */
    public String getMiddleName(){
        return middle;
    }

    /**
     * @return the last name.
     */
    public String getLastName(){
        return last;
    }

    /*
     * This method is to make the first letter capitalized in the name and everything else lower case. 
     */
    private String makePrettyName(String s){
        if(s == null || s.length() == 0){
            return s;
        }else if(s.length() == 1){
            return s.toUpperCase();
        }else{
            return s.substring(0,1).toUpperCase() + s.substring(1).toLowerCase();
        }
    }

    /**
     * @return the initials of the name.
     */
    public String getInitials(){
        return first.toUpperCase().charAt(0) + "." + 
        last.toUpperCase().charAt(0) + ".";
    }

    /**
     * @return the full name.
     */
    public String getFullName(){
        return makePrettyName(first) + " " + makePrettyName(middle) + " " + makePrettyName(last);
    }
}


/**
 * This class describes an author.
 * @author: Dominic Ng
 * @version: 1.0
 */
class Author{
    private Name name;
    private Date born;
    private Date died;
    private Name pseudonym;

    public static final int CURRENT_YEAR = 2013;
    public Author(){
        Name name = new Name();
        Date born = new Date();
    }

    public Author(String firstName, String lastName, String middleName, int yearBorn,
    int monthBorn,int dayBorn, int yearDied, int monthDied, int dayDied, String pseudonymFirstName,
    String pseudonymLastName, String pseudonymMiddleName){
        this.name = new Name(firstName, middleName, lastName);
        this.born = new Date(yearBorn,monthBorn, dayBorn);
        this.died = new Date(yearDied, monthDied, dayDied);
        this.pseudonym = new Name (pseudonymFirstName, pseudonymMiddleName, pseudonymLastName);

    }

    public Author(Name name, Date born, Date died, Name pseudonym){
        if(name == null){
            this.name = new Name();
        }else{
            this.name = name;
        }
        if(born == null){
            this.born = new Date();
        }else{
            this.born = born;
        }
        this.died = died;
        this.pseudonym = pseudonym;
    }

    public Author(Name name, Date born, Date died){
        if(name == null){
            this.name = new Name();
        }else{
            this.name = name;
        }
        if(born == null){
            this.born = new Date();
        }else{
            this.born = born;
        }
        this.died = died;
        pseudonym = null;
    }

    /**
     * @return the author's first name.
     */
    public String getFirstName(){
        return name.getFirstName();
    }

    /**
     * @return the author's middle name.
     */
    public String getMiddleName(){
        return name.getMiddleName();
    }

    /**
     * @return the author's last name.
     */
    public String getLastName(){
        return name.getLastName();
    }

    /**
     * @return the date born in year.
     */
    public String getDateBorn(){
        return born.getFormattedDate();
    }

    /**
     * @return the full name.
     */
    public String getName(){
        return name.getFullName();
    }

    /**
     * @param isAuthorAlive returns true if died is null and returns false otherwise.
     */
    public boolean isAuthorAlive(){
        if(died == null){
            return true;
        }else{
            return false;
        }
    }

    /**
     * @return the author's age
     */
    public int getAgeYearsOfAuthor(){
        return CURRENT_YEAR - born.getYear();
    }

}


/**
 * This class describes a book.
 * @author: Dominic Ng
 * @version: 1.0
 */
class Book{
    private Author  author;
    private Date    published;
    private String  title;

    /**
     * This constructor sets the new Author and Date to their default constructors.
     */
    public Book(){
        this.author = new Author();
        this.published = new Date();
        this.title = "Untitled";
    }

    /**
     * This constructor stores the parameters and if author and date is null, it will call default constructor. 
     * If title is null, it will be set to untitled. 
     */
    public Book(Author author, Date published, String title){
        if(author == null){
            author = new Author();
        }else{
            author = author; 
        }
        if(published == null){
            published = new Date();
        }else{
            published = published;
        }
        if(title == null){
            title = "Untitled";
        }
    }

    /**
     * @return the name of the author.
     */
    public String getAuthorName(){
        return author.getName();
    } 

    /*public String getDayOfTheWeekBookWasPublished(){
        return published.getDayOfTheWeek();
    }*/

    /*public void printDetails(){
        System.out.println(getAuthorName() + "wrote " + title + "on " + getDayOfTheWeekBookWasPublished() + getMonthName);
    }*/
}

 

For testing the other classes:


/**
 * This class is TestDate which tests the date.
 */
class TestDate{
    /**
     * This constructor creates dates to test.
     */
    public TestDate(){
        Date d1 = new Date(1990, 11, 15);
        Date d2 = new Date(1887, 7, 31);
        Date d3 = new Date(1966, 5, 2);
        Date d4 = new Date(1980, 8, 19);
        Date d5 = new Date(2001, 9, 11);
        Date d6 = new Date(1900, 6, 26);
        Date d7 = new Date(1940, 2, 28);
        Date d8 = new Date(1974, 10, 30);
        Date d9 = new Date(1914, 1, 15);
        Date d10 = new Date(1840, 10, 1);
        Date d11 = new Date(1999, 12, 31);
        Date d12 = new Date(1988, 5, 20);
        Date d13 = new Date(2012, 3, 10);
        Date d14 = new Date(2006, 4, 1);
        Date d15 = new Date(1992, 2, 29);

    }
}


/**
 * This class is specialized towards the creation of objects
 * from the student's project.  It exposes the objects' contents
 * via accessors and System.out.println() statements.
 * 
 * The very first thing it does, is create a TestDate object,
 * which behaves similarly.  TestDate differs from TestAssignment
 * due to its focus on the Date.getDayOfTheWeek() feature.
 * Also, TestDate is a module created by the student, whereas
 * TestAssignment was provided by the instructors.
 * 
 * TestAssignment is not invoked directly during evaluation.
 * The module named RunTest will create a TestAssignment object,
 * and capture the output.  When the output is captured,
 * it is compared to a prepared script.  The whole
 * script must match by content and order.
 */
public class TestAssignment
{

    public TestAssignment()
    {
        TestDate td = new TestDate();

        Date d = new Date();
        System.out.println(d.getFormattedDate());
        // calls private method getPaddedMonth()
        // calls private method getPaddedDay()
        System.out.println(Date.getMonthName(3));
        System.out.println(d.getDayOfTheWeek());
        // calls private method isLeapYear()
        System.out.println(d.getNumberOfDaysInThisMonth ());
        System.out.println(Date.getMonthNumber("December"));

        Date d2 = new Date(2001, 6, 29);
        System.out.println(d2.getFormattedDate());
        System.out.println(d2.getDayOfTheWeek());
        System.out.println(d2.getNumberOfDaysInThisMonth());

        Date d3 = new Date(1995, 14, 8);  // invalid date; month is > 12 
        System.out.println(d3.getFormattedDate());
        System.out.println(d3.getDayOfTheWeek());
        System.out.println(d3.getNumberOfDaysInThisMonth());

        System.out.println(d3.getYear());
        System.out.println(d3.getMonth());
        System.out.println(Date.getMonthName(d3.getMonth()));

        d3.setYear(3500);    // too high; don''t set it
        System.out.println(d3.getYear());
        d3.setYear(-3);    // too low; don't set it
        System.out.println(d3.getYear());
        d3.setYear(2010);    
        System.out.println(d3.getYear());
        d3.setMonth(13);    // too high; don't set it
        System.out.println(d3.getMonth());
        d3.setMonth(0);    // too low ; don't set it
        System.out.println(d3.getMonth());
        d3.setMonth(8);    
        System.out.println(d3.getMonth());

        d3.setDay(32);    // too high; don't set it
        System.out.println(d3.getDay());
        d3.setDay(0);    // too low ; don't set it
        System.out.println(d3.getDay());
        d3.setDay(27);    
        System.out.println(d3.getDay());

        Name n = new Name();
        System.out.println(n.getFullName());
        // calls makePrettyName()
        System.out.println(n.getInitials());
        Name n2 = new Name("sTEphen", "KinG", null);
        System.out.println(n2.getFullName()); 
        System.out.println(n2.getInitials());
        Name nn = new Name("sTEphen","KinG", "eDWin");
        System.out.println(nn.getFullName());
        System.out.println(nn.getInitials());

        Name n3 = new Name(null, "king", "edwin");
        System.out.println(n3.getFullName());
        System.out.println(n3.getInitials()); // first cannot be null

        Name n4 = new Name("stepHEn", null, "edWIN"); 
        System.out.println(n4.getFullName()); // last cannot be null
        System.out.println(n4.getInitials());

        Name n5 = new Name("", "king", "Edwin"); // too short
        System.out.println(n5.getFullName());
        System.out.println(n5.getInitials());

        Name n6 = new Name(null, "king", "edWIn"); // null first not allowed 
        System.out.println(n6.getFullName());
        System.out.println(n6.getInitials());

        Name n7 = new Name("Stephen", "king", "01234567890123456789012345678901234567890123456789");
        System.out.println(n7.getFullName()); // too long
        System.out.println(n7.getInitials());

        Name n8 = new Name("sTEphen", "KinG", null);
        System.out.println(n8.getFirstName());
        System.out.println(n8.getMiddleName());
        System.out.println(n8.getLastName());

        Name n9 = new Name("sTEphen", "KinG", "edWIN");
        System.out.println(n9.getMiddleName());

        Author a = new Author(); // default dates and names
        System.out.println(a.getDateBorn().getFormattedDate());//default
        System.out.println(a.getName().getFullName()); //default
        System.out.println(a.getDateDied().getFormattedDate());//default
        System.out.println(a.getName().getFullName()); //default
        System.out.println(a.isAuthorAlive()); 
        System.out.println(a.getAgeYearsOfAuthor()); // note: it is 2013 now

        Date d1 = new Date(1947, 9, 21);
        Author a1 = new Author(null, d1, null); // null name not allowed
        System.out.println(a1.getName().getFullName());

        Name n1 = new Name("sTEphen", "KinG", null);
        Author a2 = new Author(n1, null, null); // null born not allowed
        System.out.println(a2.getDateBorn().getFormattedDate());

        Author a3 = new Author(n1, d1, null); 
        System.out.println(a3.getDateBorn().getFormattedDate());
        System.out.println(a3.getName().getFullName()); 

        Name name = new Name("sTEphen", "KinG", "edWIN");
        Date born = new Date(1947, 9, 21);
        Name pseudonym = new Name("RichaRD", "BachMAN", null);
        Author a4 = new Author(name, born, null, pseudonym);
        System.out.println(a4.getName().getFullName());
        System.out.println(a4.getDateDied());
        System.out.println(a4.getDateBorn().getFormattedDate());
        System.out.println(a4.getPseudonym().getFullName());

        Name name1 = new Name("sTEphen", "KinG", "edWIN");
        Date born1 = new Date(1947, 9, 21);
        Author a5 = new Author(name1, born1, null); // no pseudonym
        System.out.println(a5.getName().getFullName());
        System.out.println(a5.getDateDied());
        System.out.println(a5.getDateBorn().getFormattedDate());
        System.out.println(a5.getPseudonym());

        Name name2 = new Name("sTEphen", "KinG", "edWIN");
        Date born2 = new Date(1947, 9, 21);
        Date died = new Date(2000, 6, 13);
        Author a6 = new Author(name2, born2, died);
        System.out.println(a6.getDateDied().getFormattedDate());

        Author a7 = new Author("stephen", "king", null, 1947, 9, 21, 2000, 6, 13, "richard", "bachman", null);
        System.out.println(a7.getDateDied().getFormattedDate());
        System.out.println(a7.getName().getFullName());
        System.out.println(a7.getDateBorn().getFormattedDate());
        System.out.println(a7.getPseudonym().getFullName());

        Name name3 = new Name("sTEphen", "KinG", "edWIN");
        Date born3 = new Date(1947, 9, 21);
        Date died3 = new Date(2000, 6, 13);
        Author a8 = new Author(name3, born3, died3);
        System.out.println(a8.getAgeYearsOfAuthor()); // note: it's 2013 now

        Book bb = new Book();    // default title is "Untitled"
        System.out.println(bb.getTitle()); 
        System.out.println(bb.getAuthor().getName().getFullName());
        System.out.println(bb.getAuthor().getPseudonym().getFullName());
        System.out.println(bb.getAuthor().getDateBorn().getFormattedDate());
        System.out.println(bb.getAuthor().getDateDied().getFormattedDate());
        System.out.println(bb.getDatePublished().getFormattedDate());

        Date date = new Date(2000, 4, 29);
        String title = "It";
        Book b2 = new Book(null, date, title);    // Author is null
        System.out.println(b2.getAuthor().getName().getFullName());
        System.out.println(b2.getTitle ());
        System.out.println(b2. getDatePublished().getFormattedDate());

        Book b3 = new Book(null, null, null);    // Date, title are null 
        System.out.println(b3. getDatePublished().getFormattedDate());
        System.out.println(b3.getTitle ());

        Name name4 = new Name("sTEphen", "KinG", "edWIN");
        Date born4 = new Date(1947, 9, 21);
        Date died4 = new Date(2000, 6, 13);
        Author aa = new Author(name4, born4, died4);
        Date published4 = new Date(1988, 10, 31);
        Book b4 = new Book(aa, published4, "Carrie");
        System.out.println(b4.getAuthorName ());
        System.out.println(b4.getDayOfTheWeekBookWasPublished());
        b4.printDetails();

        Name name5 = new Name("sTEphen", "KinG", null);
        Date born5 = new Date(1947, 9, 21);
        Date died5 = new Date(2000, 6, 13);
        Name pseudonym5 = new Name("Richard", "Bachman", null);
        Author aaa = new Author(name5, born5, died5, pseudonym5);
        Date published5 = new Date(1988, 10, 31);
        Book b5 = new Book(aaa, published5, "Carrie");
        b5.printDetails();
    } // end method
} // end class

Link to comment
https://linustechtips.com/topic/610098-java-method-error/#findComment-7893977
Share on other sites

Link to post
Share on other sites

9 hours ago, vorticalbox said:

 

totally downloaded it.

 


/**
 * This class is specialized towards the creation of objects
 * from the student's project.  It exposes the objects' contents
 * via accessors and System.out.println() statements.
 * 
 * The very first thing it does, is create a TestDate object,
 * which behaves similarly.  TestDate differs from TestAssignment
 * due to its focus on the Date.getDayOfTheWeek() feature.
 * Also, TestDate is a module created by the student, whereas
 * TestAssignment was provided by the instructors.
 * 
 * TestAssignment is not invoked directly during evaluation.
 * The module named RunTest will create a TestAssignment object,
 * and capture the output.  When the output is captured,
 * it is compared to a prepared script.  The whole
 * script must match by content and order.
 */
public class TestAssignment
{

    public TestAssignment()
    {
        TestDate td = new TestDate();

        Date d = new Date();
        System.out.println(d.getFormattedDate());
        // calls private method getPaddedMonth()
        // calls private method getPaddedDay()
        System.out.println(Date.getMonthName(3));
        System.out.println(d.getDayOfTheWeek());
        // calls private method isLeapYear()
        System.out.println(d.getNumberOfDaysInThisMonth ());
        System.out.println(Date.getMonthNumber("December"));

        Date d2 = new Date(2001, 6, 29);
        System.out.println(d2.getFormattedDate());
        System.out.println(d2.getDayOfTheWeek());
        System.out.println(d2.getNumberOfDaysInThisMonth());

        Date d3 = new Date(1995, 14, 8);  // invalid date; month is > 12 
        System.out.println(d3.getFormattedDate());
        System.out.println(d3.getDayOfTheWeek());
        System.out.println(d3.getNumberOfDaysInThisMonth());

        System.out.println(d3.getYear());
        System.out.println(d3.getMonth());
        System.out.println(Date.getMonthName(d3.getMonth()));

        d3.setYear(3500);	// too high; don''t set it
        System.out.println(d3.getYear());
        d3.setYear(-3);	// too low; don't set it
        System.out.println(d3.getYear());
        d3.setYear(2010);	
        System.out.println(d3.getYear());
        d3.setMonth(13);	// too high; don't set it
        System.out.println(d3.getMonth());
        d3.setMonth(0);	// too low ; don't set it
        System.out.println(d3.getMonth());
        d3.setMonth(8);	
        System.out.println(d3.getMonth());

        d3.setDay(32);	// too high; don't set it
        System.out.println(d3.getDay());
        d3.setDay(0);	// too low ; don't set it
        System.out.println(d3.getDay());
        d3.setDay(27);	
        System.out.println(d3.getDay());

        Name n = new Name();
        System.out.println(n.getFullName());
        // calls makePrettyName()
        System.out.println(n.getInitials());
        Name n2 = new Name("sTEphen", "KinG", null);
        System.out.println(n2.getFullName()); 
        System.out.println(n2.getInitials());
        Name nn = new Name("sTEphen","KinG", "eDWin");
        System.out.println(nn.getFullName());
        System.out.println(nn.getInitials());

        Name n3 = new Name(null, "king", "edwin");
        System.out.println(n3.getFullName());
        System.out.println(n3.getInitials()); // first cannot be null

        Name n4 = new Name("stepHEn", null, "edWIN"); 
        System.out.println(n4.getFullName()); // last cannot be null
        System.out.println(n4.getInitials());

        Name n5 = new Name("", "king", "Edwin"); // too short
        System.out.println(n5.getFullName());
        System.out.println(n5.getInitials());

        Name n6 = new Name(null, "king", "edWIn"); // null first not allowed 
        System.out.println(n6.getFullName());
        System.out.println(n6.getInitials());

        Name n7 = new Name("Stephen", "king", "01234567890123456789012345678901234567890123456789");
        System.out.println(n7.getFullName()); // too long
        System.out.println(n7.getInitials());

        Name n8 = new Name("sTEphen", "KinG", null);
        System.out.println(n8.getFirstName());
        System.out.println(n8.getMiddleName());
        System.out.println(n8.getLastName());

        Name n9 = new Name("sTEphen", "KinG", "edWIN");
        System.out.println(n9.getMiddleName());

        Author a = new Author(); // default dates and names
        System.out.println(a.getDateBorn().getFormattedDate());//default
        System.out.println(a.getName().getFullName()); //default
        System.out.println(a.getDateDied().getFormattedDate());//default
        System.out.println(a.getName().getFullName()); //default
        System.out.println(a.isAuthorAlive()); 
        System.out.println(a.getAgeYearsOfAuthor()); // note: it is 2013 now

        Date d1 = new Date(1947, 9, 21);
        Author a1 = new Author(null, d1, null); // null name not allowed
        System.out.println(a1.getName().getFullName());

        Name n1 = new Name("sTEphen", "KinG", null);
        Author a2 = new Author(n1, null, null); // null born not allowed
        System.out.println(a2.getDateBorn().getFormattedDate());

        Author a3 = new Author(n1, d1, null); 
        System.out.println(a3.getDateBorn().getFormattedDate());
        System.out.println(a3.getName().getFullName()); 

        Name name = new Name("sTEphen", "KinG", "edWIN");
        Date born = new Date(1947, 9, 21);
        Name pseudonym = new Name("RichaRD", "BachMAN", null);
        Author a4 = new Author(name, born, null, pseudonym);
        System.out.println(a4.getName().getFullName());
        System.out.println(a4.getDateDied());
        System.out.println(a4.getDateBorn().getFormattedDate());
        System.out.println(a4.getPseudonym().getFullName());

        Name name1 = new Name("sTEphen", "KinG", "edWIN");
        Date born1 = new Date(1947, 9, 21);
        Author a5 = new Author(name1, born1, null); // no pseudonym
        System.out.println(a5.getName().getFullName());
        System.out.println(a5.getDateDied());
        System.out.println(a5.getDateBorn().getFormattedDate());
        System.out.println(a5.getPseudonym());

        Name name2 = new Name("sTEphen", "KinG", "edWIN");
        Date born2 = new Date(1947, 9, 21);
        Date died = new Date(2000, 6, 13);
        Author a6 = new Author(name2, born2, died);
        System.out.println(a6.getDateDied().getFormattedDate());

        Author a7 = new Author("stephen", "king", null, 1947, 9, 21, 2000, 6, 13, "richard", "bachman", null);
        System.out.println(a7.getDateDied().getFormattedDate());
        System.out.println(a7.getName().getFullName());
        System.out.println(a7.getDateBorn().getFormattedDate());
        System.out.println(a7.getPseudonym().getFullName());

        Name name3 = new Name("sTEphen", "KinG", "edWIN");
        Date born3 = new Date(1947, 9, 21);
        Date died3 = new Date(2000, 6, 13);
        Author a8 = new Author(name3, born3, died3);
        System.out.println(a8.getAgeYearsOfAuthor()); // note: it's 2013 now

        Book bb = new Book();	// default title is "Untitled"
        System.out.println(bb.getTitle()); 
        System.out.println(bb.getAuthor().getName().getFullName());
        System.out.println(bb.getAuthor().getPseudonym().getFullName());
        System.out.println(bb.getAuthor().getDateBorn().getFormattedDate());
        System.out.println(bb.getAuthor().getDateDied().getFormattedDate());
        System.out.println(bb.getDatePublished().getFormattedDate());

        Date date = new Date(2000, 4, 29);
        String title = "It";
        Book b2 = new Book(null, date, title);	// Author is null
        System.out.println(b2.getAuthor().getName().getFullName());
        System.out.println(b2.getTitle ());
        System.out.println(b2. getDatePublished().getFormattedDate());

        Book b3 = new Book(null, null, null);	// Date, title are null 
        System.out.println(b3. getDatePublished().getFormattedDate());
        System.out.println(b3.getTitle ());

        Name name4 = new Name("sTEphen", "KinG", "edWIN");
        Date born4 = new Date(1947, 9, 21);
        Date died4 = new Date(2000, 6, 13);
        Author aa = new Author(name4, born4, died4);
        Date published4 = new Date(1988, 10, 31);
        Book b4 = new Book(aa, published4, "Carrie");
        System.out.println(b4.getAuthorName ());
        System.out.println(b4.getDayOfTheWeekBookWasPublished());
        b4.printDetails();

        Name name5 = new Name("sTEphen", "KinG", null);
        Date born5 = new Date(1947, 9, 21);
        Date died5 = new Date(2000, 6, 13);
        Name pseudonym5 = new Name("Richard", "Bachman", null);
        Author aaa = new Author(name5, born5, died5, pseudonym5);
        Date published5 = new Date(1988, 10, 31);
        Book b5 = new Book(aaa, published5, "Carrie");
        b5.printDetails();
    } // end method
} // end class

I believe you actually have to give it the format such as yyyy-mm-dd

I sent it to one of my classmates and he told me my method getFormattedDate() works fine but it might be some other method before that, that is causing trouble. 

6 hours ago, jslowik said:

What is it saying when it fails to compile?

"cannot find symbol - method  getFormattedDate()"

The specific line is from Test Assignment : System.out.println(a.getDateBorn().getFormattedDate());//default  

 

 

Link to comment
https://linustechtips.com/topic/610098-java-method-error/#findComment-7893989
Share on other sites

Link to post
Share on other sites

1 hour ago, applekillsmsft said:

If It is a cannot find symbol, that would tell me that a variable was incorrectly named or never instanciated. 

Yes but I don't see anything wrong with it from my point of view haha I'm not sure. 

 

1 hour ago, jslowik said:

Where is your getFormattedDate method?

It's located in the Date class. 

Link to comment
https://linustechtips.com/topic/610098-java-method-error/#findComment-7895168
Share on other sites

Link to post
Share on other sites

@tomoki that's your problem. In the line

 

System.out.println(a.getDateDied().getFormattedDate());//default

It has no idea what you're talking about. Your method probably works fine when working with Date objects right? You're attempting to use a method in the Date class while working with an Author object.

Link to comment
https://linustechtips.com/topic/610098-java-method-error/#findComment-7895402
Share on other sites

Link to post
Share on other sites

8 minutes ago, jslowik said:

@tomoki that's your problem. In the line

 


System.out.println(a.getDateDied().getFormattedDate());//default

It has no idea what you're talking about. Your method probably works fine when working with Date objects right? You're attempting to use a method in the Date class while working with an Author object.

Hahaha except I'm so newb to this that... I don't know what exactly I wrote wrong. The methods both look fine to me but apparently they're not :c

Link to comment
https://linustechtips.com/topic/610098-java-method-error/#findComment-7895423
Share on other sites

Link to post
Share on other sites

The getFormattedDate method will only be found in objects that are of the type Date if the method is found in the Date class. This means that when you create objects of other types (such as Author) they will not have this method, and attempting to use a method that doesn't exist will cause your program to fail to compile.

 

You could do something like create a utility class to handle formatting, or a helper method in the class. You could also handle the date formatting in your getter if there's no need to ever get the un-formatted date. Instead of simply having it return the date have it format the date as you require then return that so you don't have to do anything further. I'm sure there are other approaches to handling that, but those most quickly come to mind.

 

I am making a few assumptions about the way your classes are written, but given your error this is (to the best of my knowledge) where the problem lies.

Link to comment
https://linustechtips.com/topic/610098-java-method-error/#findComment-7895442
Share on other sites

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

×