1.2.11_14

该代码实现了一个SmartDate类,它包含日期合法性检查、闰年判断、星期几的方法,并抛出非法日期异常。此外,还创建了Transaction类,包含交易方、日期和金额属性,并实现了equals()方法进行对象比较。示例中展示了如何创建和使用这些类。
摘要由CSDN通过智能技术生成
1.2.11 Develop an implementation SmartDate of our Date API that raises an exception if the date is not legal.
1.2.12 Add a method dayOfTheWeek() to SmartDate that returns a String value Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, or Sunday, giving the appropriate day of the week for the date. You may assume that the date is in the 21st century.
public class E1211and1212 {
    public static class SmartDate{
        private final int[] Days = {31,29,31,30,31,30,31,31,30,31,30,31};
        private final int m;//1-12
        private final int d;// in Days
        private final int y;
        private final String[] week = {"Mongday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};//1.2.12
        
        //构造函数
        public SmartDate(int m,int d,int y)
        {
            if(!isValid(m, d, y)) throw new IllegalArgumentException("Invalid date");
            this.m = m;
            this.d = d;
            this.y = y;
        }
        public SmartDate(String date) {
            String[] fields = date.split("/");
            if (fields.length != 3) {
                throw new IllegalArgumentException("Invalid date");
            }
            m = Integer.parseInt(fields[0]);
            d   = Integer.parseInt(fields[1]);
            y  = Integer.parseInt(fields[2]);
            if (!isValid(m, d, y)) throw new IllegalArgumentException("Invalid date");
        }
        
        public boolean isValid(int m,int d,int y) {
            if (m < 1 || m > 12)          return false;
            if (d < 1 || d > Days[m-1])   return false;
            if (m == 2 && !isLeapYear(y)) return d <= 28;
            return true;
        }

        public boolean isLeapYear(int y){
            if(y % 400 == 0)      return true;
            if(y % 100 == 0)      return false;
            return y%4 == 0;
        }

        public int getMonth(){
            return this.m;
        }

        public int getYear(){
            return this.y;
        }

        public int getday(){
            return this.d;
        }
        //1.2.12添加一个dayOfTheWeek方法,1/1/2000为Saturday
        public String dayOfTheWeek(){
            SmartDate startDate = new SmartDate(1,1,2000);//21世纪起始日期,Saturday
            int indexStart = 5;//1/1/2000为Saturday
            int daysNum = 0;//离1/1/2000的天数
            if (this.y < startDate.getYear()) throw new IllegalArgumentException("请输入21世纪的日期!");

            for(int startYear = startDate.getYear();startYear < this.y;startYear++ )
            {
                if (isLeapYear(startYear))  daysNum += 366;//闰年366天
                else                        daysNum += 365;//平年365天
            }
            for(int startMonth = startDate.getMonth();startMonth < this.m;startMonth++)
            {
                
                daysNum += Days[startMonth-1];
                if (startMonth == 2 && !isLeapYear(this.y))  daysNum--;//如果包含了2月且是平年需要减1  
            }
            daysNum += this.d;
            daysNum--;
            int leftdaysNum = daysNum % 7;
            int indexCurrent = (leftdaysNum + indexStart) % 7;
            return week[indexCurrent];
        }
        public String toString() {
            return this.m + "/" + d + "/" + y;
        }
    
    }
    public static void main(String[] args) {
        SmartDate smartDate = new SmartDate("8/8/2021");
        System.out.println(smartDate);//自动调用toStirng
        System.out.println(smartDate.dayOfTheWeek());
    }
    
}
1.2.13 Using our implementation of Date as a model (page 91), develop an implementation of Transaction.
1.2.14 Using our implementation of equals() in Date as a model (page 103), develop implementations of equals() for Transaction
import edu.princeton.cs.algs4.Date;

public class E1213and1214 {
    public static class Transaction {
        private String who;
        private Date when;
        private double amount;
        public Transaction(String who,Date when,double amount)
        {
            this.who = who;
            this.when = when;
            this.amount = amount;
        }
        
        public String getwho() {
            return this.who;
        }

        public Date getwhen() {
            return this.when;
        }

        public double getamount() {
            return this.amount;
        }
        @Override
        public String toString() {
            return this.who + " " + this.when + " " + this.amount;
        }
        //1.2.14
        @Override
        public boolean equals(Object other) {
            if (other == this) return true;
            if (other == null) return false;
            if (other.getClass() != this.getClass()) return false;
            Transaction that = (Transaction) other;
            return (this.who == that.who) && (this.when == that.when) && (this.amount == that.amount);
        }
    }
    public static void main(String[] args) {
        Date date = new Date(8,8,2021);
        Transaction transaction1 = new Transaction("张三",date,11.12);
        Transaction transaction2 = new Transaction("李四",date ,11.12);
        if (transaction2.equals(transaction1)) System.out.println("true");
        else System.out.println("false");

    }
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值