Hi all, i have to make an application that can take the user's input and finds out if the year is a leap year. The input has to be divisible by 4 and 400 but not by 100. This would then produce an answer if the user's input is true (the input is a leap year) or false (the input is not a leap year).

right it sounds easy (and probably is for you guys!) but there one small thing i would like to ask. In my program i have a class called "Year" and an application called "YearApp". In my program "Year" i have the code to prompt the user to enter the year. In my application "YearApp" how do i call that code so it can be act apon?

my codes are as follows:

program "Year.java"

// An application that can take the user's input and finds out if the year is a
// leap year. The input has to be divisible by 4 and 400 but not by 100. This
// would then produce an answer if the user's input is true (the input is a leap// year) or false (the input is not a leap year).

//11/11/08

import java.io.*;

public class Year {

    
   public static void main (String[] args) throws IOException{
        int uInput;
// Create BufferedReader instance to enable input from keyboard
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader keyboardInput = new BufferedReader(input);

System.out.println("please enter the year and I will find out if it's a leap year");

uInput = new Integer(keyboardInput.readLine()).intValue();

isLeapYear(uInput);
    }
    public static final int getYear(int u  ){
	int uInput = u;

	return u;
    }

public static final boolean isLeapYear(int getYear){
    boolean aLeapYear;
 if (getYear%4 == 0)
	if (getYear%100 != 0)
	    if (getYear%400 != 0)
		aLeapYear = true;
	    else aLeapYear = false;
	else aLeapYear = false;
    else aLeapYear = false;
    return aLeapYear;
}
}

my application "YearApp":

// An application that can take the user's input and finds out if the year is a
// leap year. The input has to be divisible by 4 and 400 but not by 100. This
// would then produce an answer if the user's input is true (the input is a leap// year) or false (the input is not a leap year).

//11/11/08

public class YearApp {

    public static void main(String[] args){
    Year LeapYear = new Year();

    System.out.println(LeapYear.isLeapYear);
    }
}

i haven't quite finish YearApp yet, that why im wondoring how to prompt the user for the input.


EDIT: To fix this issue do i have to prompt the user in my "YearApp" and implement everything that way?
Thanks

I'm going to assume you had to create a class called Year, because otherwise you would have used the built-in GregorianCalender class that has a method isLeapYear(int year) :)

What I would do is get the input using the YearApp class and pass that input into the parameter of your isLeapYear method.

I'm going to assume you had to create a class called Year, because otherwise you would have used the built-in GregorianCalender class that has a method isLeapYear(int year) :)

What I would do is get the input using the YearApp class and pass that input into the parameter of your isLeapYear method.

ah, thanks for spotting that one out about the class. I fogotten about that supprisingly :-/
i give the second part a go... but how would i go abouts passing the input to the isLeapYear method?

Here is a code example to answer a few of your questions;

//This creates a 'Scanner' object that is attached to the keyboard (since System.in) is attached, by default, to the keyboard. To learn how to use your Scanner object (I called it input), google 'Java Scanner' and look at the methods. You want to use one called nextInt() because it gets the nextInt from the user. 
Scanner input = new Scanner(System.in);


If we have a method called isEqualTo5, that returns 'true' if an integer = 5, we'd call it like this

boolean isItAnInteger = isEqualTo5(aValue);

I will leave it up to you to figure out how these examples apply to your problem. Keep in mind that all methods in Java can only be called in two ways, the above example being one of them.

hi I've managed to get the program to work but it still comes up with the year being false if its a leap year. i have tried to fix this issue but have not succeeded. please help with this matter.

// An application that can take the user's input and finds out if the year is a
// leap year. The input has to be divisible by 4 and 400 but not by 100. This
// would then produce an answer if the user's input is true (the input is a leap// year) or false (the input is not a leap year).

//11/11/08
import java.io.*;
public class YearApp {
  public static  int uInput;
    public static boolean aLeapYear;
    public static void main(String[] args)throws IOException{
  
// Create BufferedReader instance to enable input from keyboard
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader keyboardInput = new BufferedReader(input);

System.out.println("please enter the year and I will find out if it's a leap year");

uInput = new Integer(keyboardInput.readLine()).intValue();

printYear();
    }
public YearApp(int uInput,boolean aLeapYear ){
    Year.aLeapYear = aLeapYear;
 this.uInput = uInput;
 this.aLeapYear = aLeapYear;
    }
    public int uInput(){
return uInput;
    }
    public boolean aLeapYear(){
	return Year.aLeapYear;
    }
    YearApp year1 = new YearApp(uInput, aLeapYear);

    private static  void printYear(){
	System.out.println("" + aLeapYear);
    }
}

my boolean code are as follows:

public boolean isLeapYear(int getYear){
  
  getYear = YearApp.uInput;

 if (getYear%4 == 0)
	if (getYear%100 != 0)
	    if (getYear%400 != 0)
		aLeapYear = true;
	    else aLeapYear = false;
	else aLeapYear = false;
    else aLeapYear = false;
    return aLeapYear;
}

any year i type in always comes up as false
i think maybe the problem is that aLeapYear is static. and if i delete this than the rest of my program dont work
if so how should i fix it?

thanks in advance

Also, your problem lies in your logic/algorithm, not in what type of variable you made. According to you,

"The input has to be divisible by 4 and 400 but not by 100."

You could accomplish this just by writing

public static boolean isLeapYear(Integer theYear){
if (theYear % 4 == 0 && theYear % 400 == 0 && theYear % 100 != 0){
return true;
} 

return false;
}

Also, it is important to note that the way you stated the problem, the code I gave you above is correct. However, you didn't state the problem correctly. According to what I just read online, a leap year occurs according to the following rules:

The rules for determining a leap year: Most years that can be divided evenly by 4 are leap years. Exception: Century years are NOT leap years UNLESS they can be evenly divided by 400.

This changes the code; I will let you figure out how. Hint: it requires a conditional (if) statement to decide whether or not it is a century year.

In my program "Year" i have the code to prompt the user to enter the year. In my application "YearApp" how do i call that code so it can be act apon?

my response would be, don't.
with this I mean, it's meaningless to prompt for input in the 'Year-class' (I take it it is a class)
since that's not really what it's ment for.

try rather:

public class Year{
  private int year;
  public Year(int year){
    this.year = year;
  }
  public boolean isLeapYear(){
    boolean returnVal = false;
    // check if year is a leap year, and if so, set returnVal to true
   return returnVal;
  }
}
public class YearApp{
  public static void main(String args[]){
    Year test = new Year(2008);
    if (test.isLeapYear())
       System.out.println("2008 is a leap year");
    else
      System.out.println("2008 is not a leap year");
  }
}
This article has been dead for over six months. Start a new discussion instead.