-4

I cant get these to compile it seems to be where I'm trying to pass the Boolean value. The first one has 2 errors that don't make any sense to me

    public class Date {
    public int m;
    public int d;
    public int y;
    boolean isLeapYear;

    public String monthIs(){
        return month;
        m = Integer.parseInt(month);
    }

    public String dayIs(){
        return day;
        d = Integer.parseInt(day);
    }

    public Date(String year){
        y = Integer.parseInt(year);
        // Is y Divisible by 4
        if (y % 4 == 0){

            // Is y Divisible by 4 but not 100
            if (y % 100 != 0)
                isLeapYear = true;

            // Is y Divisible by 4 and 100 and 400
            else if (y % 400 == 0)
                isLeapYear = true;

            // It's Divisible by 4 and 100 but not 400
            else
                isLeapYear = false;
        }
        // It's not divisible by 4
        else
        {
            isLeapYear = false;
            public boolean getisLeapYear()
            {
                return isLeapYear;
            }
        }
    }
}

DateJDialog class:

import javax.swing.JOptionPane;
/** This program runs the Date class to determine if
 *  the date entered falls within a leap year.
 */
public class DateJDialog
{
    public static void main(String[] args)
    {
        String month;
        String day;
        String year;
        boolean isitLeapYear;
        Date date;
        //Get Input
        JOptionPane.showMessageDialog(null, "This program determines if the date 
                                            entered falls within a leap year.");
        month = JOptionPane.showInputDialog("What month?");
        day = JOptionPane.showInputDialog("What day?");
        year = JOptionPane.showInputDialog("What year?");

        //Create Date object
        date = new Date(year);

        if (date.getisLeapYear()==true);
        if (isLeapYear = true)
            JOptionPane.showMessageDialog(null, month + "-" + day + "-" + year 
            + " does fall within a leap year.");
        else
            JOptionPane.showMessageDialog(null, month + "-" + day + "-" + year
            + " does not fall within a leap year.");
        System.exit(0);
    }
}
Dennis
  • 3,683
  • 1
  • 21
  • 43
DoubleD
  • 1
  • 1
  • 9
    Step 1: Format your code to be human-readable. Step 2: Look at the error message(s). Don't just give up when an error occurs. *Read the error* to try to resolve it. – David Dec 01 '14 at 20:11
  • 2
    You can't define a method (`getIsLeapYear`) in the middle of another method. – ajb Dec 01 '14 at 20:11
  • You need to do `isLeapYear == true` instead of `isLeapYear = true`- Also, always post the error you are getting - don't leave us to guess what you're seeing on your screen. – nos Dec 01 '14 at 20:11
  • possible duplicate of [Why does Java have an "unreachable statement" compiler error?](http://stackoverflow.com/questions/3795585/why-does-java-have-an-unreachable-statement-compiler-error) – Jean-François Corbett Dec 01 '14 at 20:12
  • 2
    Oh dear. So much wrong with this code. – Drew Kennedy Dec 01 '14 at 20:18
  • Your most obvious problem is having `public boolean getisLeapYear()` in the middle of another method. – Hot Licks Dec 01 '14 at 20:22
  • @HotLicks Most obvious is the `if-statement` ending with a semicolon instead of having a body. – Drew Kennedy Dec 01 '14 at 20:23
  • @DrewKennedy - Not if you're scanning for "boolean", since that's what Opie said he had trouble with. – Hot Licks Dec 01 '14 at 20:26

1 Answers1

1

You have a few errors in your code, and I'll do my best to point them out.

Firstly, you have this if-statement:

if (date.getisLeapYear()==true);

An if-statement must have a body made up of an opening brace and closing brace (I believe this would compile, but serves no purpose). Instead:

if (date.getisLeapYear()==true) {
    //do something
}

Since this appears to be a boolean getter, the conditional check can be shortened to:

if (date.getisLeapYear()) {//this checks for a "true" value, !date.getisLeapYear() checks for false
    //do something
}

Your check for isLeapYear being true is incorrect, and again typed out in length when not necessary.

if (isLeapYear = true)

Should be:

if (isLeapYear)//again checking if true. !isLeapYear would be checking for false.

Your constructor is built weird and completely wrong. First of all, if you're using an if-else statement, it must utilize braces.

The following is valid, but considered to be bad practice:

if (condition)
    //do soemthing

The following requires braces:

if (condition) {

} else if (another condition) {

} (...) 

Finally, you're declaring a getter inside the constructor. Methods can only be created at the class scope, meaning methods cannot be created inside a method. To solve this:

public Date() {
    (...)
}

public boolean getisLeapYear() {
    return isLeapYear;
}

Also, just as a tip, you can chain multiple instances of a variable in one declaration:

String month;
String day;
String year;

Can be written as:

String month, day, year;

I have no idea if that covers all your errors, but that's a healthy start.

Drew Kennedy
  • 4,118
  • 4
  • 24
  • 34