用Java写一段代码,建立包,建立 Tool 类,完成判断某年是否为闰年的方法。编写另一个项目,复用 Java 包,调用你自己写的包中 Tool 类及方法。
时间: 2023-03-25 22:03:03 浏览: 48
好的,这是一个关于 Java 编程的问题。以下是代码示例:
在包 com.example 中创建 Tool 类:
```
package com.example;
public class Tool {
public static boolean isLeapYear(int year) {
if (year % 4 != ) {
return false;
} else if (year % 100 != ) {
return true;
} else if (year % 400 != ) {
return false;
} else {
return true;
}
}
}
```
在另一个项目中,调用 Tool 类中的 isLeapYear 方法:
```
import com.example.Tool;
public class Main {
public static void main(String[] args) {
int year = 202;
if (Tool.isLeapYear(year)) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}
}
```
这样就可以判断某年是否为闰年了。