zoukankan      html  css  js  c++  java
  • Algs4-1.2.14实现Transaction中的equals()方法

    1.2.14用我们对Date中的equals()方法的实现(请见1.2.5.8节中的Date类代码框)作为模板,实现Transaction中的equals()方法。
    答:
    图片
    Code:
    import java.util.Date;
    public class Transaction
    {
        private final String myWho;
        private final Date myWhen;
        private final double myAmount;
       
        public Transaction(String who,Date when,double amount)
        {
            myWho=who;
            myWhen=when;
            myAmount=amount;
          }
       
        public Transaction(String transaction)
        {
            String[] words=transaction.split("  ");
            myWho=words[0];
            myWhen=new Date(words[1]);
            myAmount=Double.parseDouble(words[2]);
        }
       
        public String who()
        {
            return myWho;
        }
       
        public Date when()
        {
            return myWhen;
        }
       
        public double amount()
        {
            return myAmount;
        }
       
        public boolean equals(Object x)
        {
            if(this==x) return true;
            if(x==null) return false;
            if(this.getClass()!=x.getClass())  return false;
            Transaction that=(Transaction) x;
            if(!this.myWho.equals(that.who())) return false;
            if(!this.myWhen.equals(that.when())) return false;
            if(this.myAmount!=that.amount()) return false;
            return true;
        }
        public static void main(String[] args)
        {
            Transaction t1=new Transaction("Jack",new Date("19/9/2016 13:00:00"),1000000);
            StdOut.printf("t1.who=%s,when=%s,amount=%.2f ",t1.who(),t1.when(),t1.amount());
           
            Transaction t2=new Transaction("LiLi  19/9/2016 13:25:00  1000002");
            StdOut.printf(" t2.who=%s,when=%s,amount=%.2f ",t2.who(),t2.when(),t2.amount());
           
            Transaction t3=new Transaction("Jack  19/9/2016 13:00:00  1000000");
            StdOut.printf(" t3.who=%s,when=%s,amount=%.2f ",t3.who(),t3.when(),t3.amount());
           
            StdOut.printf("t1=t2 is:%s ",t1.equals(t2));
            StdOut.printf("t1=t3 is:%s",t1.equals(t3));
        }
    }

  • 相关阅读:
    iptables允许FTP
    FTP服务添加用户及设置权限
    Python之异步IO&RabbitMQ&Redis
    Python之生产者&、消费者模型
    如何使用Git上传项目代码到github
    11-3 基于cookie和session的登录模块
    11-1 会话控制cookie
    11-2 会话控制session
    10-4 文件的下载
    10-3 文件的上传
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9848917.html
Copyright © 2011-2022 走看看