完善类例题3.2

package mydate;

public class mydate {

private int year,month,day;
private static int thisYear;

static 
{thisYear=2014;}
public mydate(int year,int month,int day)
{ this.set(year,month,day);}
public mydate()
{ this(1970,1,1);}
public mydate(mydate d)
{ this.set(d);}
public void set(int year,int month,int day)
{
this.year=year;
this.month=(month>=1&&month<=12)?month:1;
this.day=(day>=1&&day<=31)?day:1;
}
public void set(mydate d)
{ set(d.year,d.month,d.day);}
public int getYear()
{ return this.year;}
public int getMonth() {return this.month;}
public int getDay() {return this.day;}
public String toString()
{ return year+"年"+String.format("%02d",month)+"月"+String.format("%02d",day)+"日";}
public static int getThisYear()
{ return thisYear;}
public static boolean isLeapYear(int year)
{ return year%400==0||year%100!=0&&year%4==0;}
public boolean equals(mydate d)
{ return this==d||d!=null&&this.day==d.year&&this.month==d.month&&this.day==d.day;}
public static int daysOfMonth(int year,int month)
{
switch(month)
{ case 1:case 3:case 5:case 7:case 8:case 10:case 12: return 31;
case 4:case 6:case 9:case 11: return 30;
case 2: return mydate.isLeapYear(year)?29:28;
default: return 0;
}
}
public int daysOfMonth()
{ return daysOfMonth(this.year,this.month);}
public void tomorrow()
{
this.day++;
if(this.day>this.daysOfMonth())
{
this.day=1;
this.month++;
if(this.month>12)
{this.month=1;
this.year++;}
}

}
public mydate yestoday()
{
mydate date=new mydate(this);
date.day--;
if(date.day==0)
{
date.month--;
if(date.month==0)
{ date.month=12; date.year--;}
date.day=daysOfMonth(date.year,date.month);
}
return date;
}

}
class mydate_ex
{
public static void main(String args[])
{
System.out.println("今年是"+mydate.getThisYear()+
",闰年?"+mydate.isLeapYear(mydate.getThisYear()));
mydate d1=new mydate(2014,12,11);
mydate d2=new mydate(d1);
System.out.println("d1:"+d1+",d2:"+d2+",d1==d2?"+(d1==d2)+",d1.equals(d2)?"+d1.equals(d2));
System.out.print(d1+"的明天是");
d1.tomorrow();
System.out.println(d1+"\n"+d1+"的昨天是"+(d2=d1.yestoday()));
}

}

时间: 2024-08-04 12:34:54

完善类例题3.2的相关文章

string类例题

//string a = "aldsfdh"; ////bool q = a.Contains("dd");//是否包含此字符串 //int o = a.IndexOf("h"); //Console.WriteLine(o); ////int p = a.LastIndexOf("d"); ////Console.WriteLine(p); //int b = a.Length;//长度 //Console.WriteLin

宠物类例题

public class Person { private String name; private Pet pet; public Person() {} public Person(String name,Pet pet){ this.name=name; this.pet=pet; } public void petHappy(){ pet.scream(); } public String getName() { return name; } public void setName(St

完善类3.2的数据类型

public class MyDate {  private int year,month,day;  private static int thisYear;  static   {   thisYear=2012;  }  public MyDate(int year,int month,int day)  {   this.set(year,month,day);  }  public MyDate()  {   this(1970,1,1);  }  public MyDate(MyDa

java面向对象编程(1)-类与对象

1.问题的提出      张老太养了两只猫猫:一只名字叫小白,今年3岁,白色.还有一只叫小花,今年100岁,花色.请编写一个程序,当用户输入小猫的名字时,就显示该猫的名字,年龄,颜色.如果用户输入的小猫名错误,则显示张老太没有这只猫. //用前面学习过的知识写出代码如下: public class Demo107{ public static void main(String []args){ int a=49;//输入的名字49,50 int cat1age=3; //第一只猫 String

2015_1_28_IO与常用类

/*日志名格式  年_月_日_内容 如2015_1_28_IO与常用类*/ ************************************************************************************************************************************************************** 日期:2015年1月 28日 主题:IO .常用类 相关文件夹:oracle/相关课件/第12章_Java

C#通用类Helper整理

★前言     最近下载了tita_chou在CSDN上传的一个资源,是在工作中整理的C#帮助类,里面包含了很多实用的类,想到我之前收集过自己用到少的可怜的类,心生敬意啊.当粗略的查看了那个资源,发现有一些是重复的,有一些我的里面有的那个没有,于是乎就萌生了重新整理一个属于自己的帮助类,于是乎就花了几个晚上的时间,重新过了一遍,还借用SandCastle工具生成了帮助文档(CHM),这一个开源的工具,感觉生成帮助文档挺方便的,下面会介绍这个工具. ★为什么要整理      为什么要整理这个帮助类

完整类实现:构造,析构,遍历二叉树

根据前面一个博文内容已经讲述了如何根据两种遍历方式进行构建二叉树 这里利用递归方式遍历二叉树,递归方式比较简单,后续补充其余非递归方式 再此主要是完善类的使用: 其中重点在于:接口定义 二叉树的析构删除 以及类成员变量中如果有指针,同时涉及复制构造函数和赋值操作符函数时需要用到的智能指针 如果接口方面定义不够好,还望包涵 如果有对智能指针不理解的地方,可以移步 http://blog.csdn.net/xietingcandice/article/details/39670269 .h文件 #i

C++ Primer 学习笔记_15_类与数据抽象(1)_类的定义和声明

C++ Primer 学习笔记_15_类与数据抽象(1)_类的定义和声明 在C++中,用类来定义自己的抽象数据类型.通过定义类型来对应所要解决的问题中的各种概念,可以使我们更容易编写.调试和修改程序.可以使得自己定义的数据类型用起来与内置类型一样容易和直观. 看一下Sales_item类: class Sales_item { private: std::string isbn; unsigned units_sold; double revenue; public: double ave_pr

UML建模类图

1.UML面向对象建模过程思路 first:通过需求分析,熟悉业务流程,可画出BPMN或者UML活动图 second:画出用例图 third:分析用例图找出确定的类以及相关类,以及参与者类:创建这些类的关联,画出初始类图:将有相同功能的类提取父类,形成继承或接口:类加关联多重性. -----上面三点是业务过程的类以及关联,另外更成软件系统还需要软件的界面,操作以及入口类---- forth:通过BPMN或者UML活动图,找出软件系统在实现过程中还需要的类以及关系,并完善类. 2.实践:某高校教务