Java作业三
1、设计一个日期类。该类中有一些方法需要实现
a)可以根据输入的年份判断判断是否为闰年
b)可以根据当前输入的日期判断是星期几
package homework_three;
class Date{
public void IfLeap(int year) {
if(year%4==0 && year%100!=0 || year%400==0)
System.out.println(year+"是闰年");
else
System.out.println(year+"不是闰年");
}
public int GetWeek(int year,int month,int day) {
if(month == 1 || month == 2){
month += 12;
year--;
}
int c = year / 100;
int y = year % 100;
int m = month;
int d = day;
int w = c / 4 - 2 *c + y + y / 4 + 26*(m+1) / 10 + d - 1;
if(w < 0)
return (w + (-w / 7 + 1) * 7) % 7;
return w % 7;
}
}
public class three {
public static void main(String[] args) {
Date date = new Date();
date.IfLeap(2020);
System.out.println("星期:"+date.GetWeek(2020,11,28));
}
}
2、设计一个水果类,包括水果的类型,适合吃的季结,作用;设计其子类苹果类,适合在冬天吃,作用减肥;设计水果的子类西瓜类,适合在夏天吃,解渴;设计水果的子类橙子,适合在春天吃,补充维生素;设计水果的子类梨,适合在秋天吃,止咳。任意给出一种水果,给出其类型、适合的季结、功效等信息;
package homework_three;
class Fruit{
String name;
String season;
String effect;
public String getName() {
return name;
}
/*public void setName(String name) {
this.name = name;
} */
public String getSeason() {
return season;
}
/*public void setSeason(String season) {
this.season = season;
}*/
public String getEffect() {
return effect;
}
/*public void setEffect(String effect) {
this.effect = effect;
}*/
}
class Apple extends Fruit{
public Apple() {
name = "苹果";
season="冬季";
effect="减肥";
}
}
class Melon extends Fruit{
public Melon() {
name = "西瓜";
season="夏季";
effect="解渴";
}
}
class Orange extends Fruit{
public Orange() {
name = "橙子";
season="春季";
effect="补充维生素";
}
}
class Pear extends Fruit{
public Pear() {
name = "梨";
season="秋季";
effect="止咳";
}
}
public class three_2 {
public static void main(String[] args) {
Fruit apple = new Apple();//只需要更改Apple为其他水果类
System.out.println(apple.getName());
System.out.println(apple.getSeason());
System.out.print(apple.getEffect());
}
}