-3

I have a project in school to solve year 2000 with java. here is the description of the assignment:

Create a Date object with the following: attribute: a julian date representing the number of days since Jan 1, 1501 (the epoch date for this project) - this should be a private member variable of the class with the associated accessor and mutator methods. constants: a suitable constant for each month a suitable contant for each component of the epoch date method: Date(long year, long month, long day) that converts the given year, month, and day to the julian date. method: a method that converts a year, month, and day into the number of days since the project's epoch date. method: a method that determines if a given year is a leap year method: a method that returns the number of days in a given year; replace the current simple if statement with a single statement using the conditional operator (? :) method: a method that returns the number of days in a given month of a given year; method should implement a switch statement using appropriate constants. methods: any of the other methods developed in class should probably also be included. method: returnYear() a method that determines the year component of the julian date value method: returnMonth() a method that determines the month component of the julian date value method: returnDay() a method that determines the day component of the julian date value method: returnMonthName() a method that returns the name of the given month ( if month = JAN return "January" (use a switch statement)) method: returnDate() a method that returns the date in the format monthName day, year class: Utility containing two query methods.

And here is the code I came with so far.. it is not compiling because there is something wrong with the switch!

import java.util.Scanner;

class Date
{

    public final static long EPOCHYEAR = 1501;
    private long julianDate;

    Date(long year, long month, long day)
    {

        julianDate = returnTotalJulianDay(year, month, day);
        System.out.println("Days is " + julianDate);
    }

    boolean isLeapYear(long year)
    {
        boolean answer;

        if(year % 4 == 0 && (year % 4 == 0 || year % 100 != 0))
        {
            answer = true;
        }
        else
        {
            answer = false;
        }

        return answer;
    }

    long returnDaysInMonth(long year, long month)
    {

        if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
        {
            return 31;
        }
        else if(month == 4 || month == 6 || month == 9 || month == 11)
        {
            return 30;
        }
        else if(isLeapYear(year))
        {
            return 29;
        }
        else
        {
            return 28;
        }
    }

    long returnJulianDate(long year, long month, long day)
    {
        long julianDate;
        long monthCounter;

        julianDate = 0;
        monthCounter = 1;

        while(monthCounter < month)
        {

            julianDate += returnDaysInMonth(year, monthCounter);

            monthCounter += 1;

        }

        julianDate += day;

        return julianDate;
    }

    long returnTotalJulianDay(long year, long month, long day)
    {
        long totalJulianDay = 0;
        long yearCounter = 1;

        while(yearCounter < year)
        {

            totalJulianDay += returnJulianDate(year, month, yearCounter);

            yearCounter += 1;

        }

        return totalJulianDay;
    }

    long returnDaysInYear(long year)
    {
        final long DAYSINYEAR = 365;

        if(isLeapYear(year))
        {
            return 366;
        }
        else
        {
            return DAYSINYEAR;
        }
    }

    long returnJulianEpochDays(long year, long month, long day)
    {

        long yearCounter = EPOCHYEAR;
        long total = 0;

        while(yearCounter < year)
        {

            total += returnDaysInYear(yearCounter);

            yearCounter += 1;
        }

        total += returnJulianDate(year, month, day);

        return total;
    }

    long returnYear()
    {
        long dayCounter = 0;
        long yearCounter = EPOCHYEAR;

        for(dayCounter = this.julianDate; dayCounter > returnDaysInYear(yearCounter); yearCounter++)
        {
            dayCounter -= returnDaysInYear(yearCounter);
        }

        return yearCounter;
    }

    long returnMonth()
    {
        long julianEpochDays = julianDate;
        long yearCounter = EPOCHYEAR;
        long monthCounter = 1;

        while(julianEpochDays > returnDaysInYear(yearCounter))
        {
            julianEpochDays -= returnDaysInYear(yearCounter);
            yearCounter++;

        }

        while(julianEpochDays > returnDaysInMonth(yearCounter, monthCounter))
        {
            julianEpochDays -= returnDaysInMonth(yearCounter, monthCounter);
            monthCounter++;

        }

        return monthCounter;
    }

    long returnDay()
    {
        long julianEpochDays = julianDate;
        long yearCounter = EPOCHYEAR;
        long monthCounter = 1;

        while(julianEpochDays > returnDaysInYear(yearCounter))
        {
            julianEpochDays -= returnDaysInYear(yearCounter);
            yearCounter++;

        }

        while(julianEpochDays > returnDaysInMonth(yearCounter, monthCounter))
        {
            julianEpochDays -= returnDaysInMonth(yearCounter, monthCounter);
            monthCounter++;

        }

        return julianEpochDays;
    }

    long returnMonthName()
    {
        int month = 0;

        final int JAN = 1;
        final int FEB = 2;
        final int MAR = 3;
        final int APR = 4;
        final int MAY = 5;
        final int JUN = 6;
        final int JUL = 7;
        final int AUG = 8;
        final int SEP = 9;
        final int OCT = 10;
        final int NOV = 11;
        final int DEC = 12;

        switch(month)
        {

            case JAN:
                return "January";
            case FEB:
                return "Febuary";
            case MAR:
                return "March";
            case APR:
                return "April";
            case MAY:
                return "May";
            case JUN:
                return "June";
            case JUL:
                return "July";
            case AUG:
                return "August";
            case SEP:
                return "September";
            case OCT:
                return "October";
            case NOV:
                return "November";
            case DEC:
                return "December";

        }
    }
}

class utility
{

    public char queryForCharacter(String prompt)
    {
        int typedCharacter = ' ';

        try
        {
            System.out.print(prompt);
            typedCharacter = System.in.read();
        }
        catch(Exception e)
        {
        }

        return (char) typedCharacter;
    }

    public static long queryForLong(String prompt)
    {
        Scanner keyboard;
        long theNumber;

        keyboard = new Scanner(System.in);
        System.out.print(prompt);
        theNumber = keyboard.nextInt();

        return theNumber;
    }
}

public class DateDriver
{

    public static void main(String[] args)
    {
        Date aTestDate = null;
        aTestDate = new Date(1502, 1, 1);
    }
}
4

2 回答 2

2

您的方法returnMonthName()被声明为返回 long,但 switch 案例返回字符串。

switch此外,如果匹配中没有任何案例,则该方法需要返回一些内容。

于 2013-05-24T18:38:38.440 回答
0

您的函数返回一种 long 类型:

“长 returnMonthName() {”

但是您正在返回一种“字符串”类型。即“一月”。

于 2013-05-24T18:40:32.817 回答