楼主: jaly50
跳转到指定楼层
上一主题 下一主题
收起左侧

Berkeley CS 61B Data Structures(in Java) Homework2 加分+讨论帖

 
🔗
J.www 2018-6-7 16:35:13 | 只看该作者
全局:
交作业咯~

回复

使用道具 举报

全局:
补交作业,截图放到了drive~https://drive.google.com/file/d/1fvW9yAln-6A6R_9AR3rWTs_IMCfx3xoT/view?usp=sharing
回复

使用道具 举报

🔗
copyrightly 2018-6-22 00:02:59 | 只看该作者
全局:
问两个问题:
1. 我的最后最后一行test总是不对
8/31/2110 - 2/27/1977 should be 48762: 48884
感觉可能是difference 的方法写错了,但又找不出原因,请高人看看哪里有错:
public int difference(Date d) {
        int x = 0;
        int i = Math.min(this.year, d.year);
        while (Math.min(this.year, d.year) <= i && i < Math.max(this.year, d.year)){
            if(isLeapYear(i) == true){
                x += 366;
            }else x += 365;
            i ++;
        }
        if(this.isBefore(d) == true){
            x = -(x - this.dayInYear() + d.dayInYear());
        }else{
            x = x - d.dayInYear() + this.dayInYear();
        }
        return x;
    }

2. 第二个constructor: public Date(String s)
我已经从s 中提取了month, day, year, 但是如果我调用之前的constructor,写成Date(month, day, year),最后得到的日期总是0/0/0,请问是什么原因?

感谢诸位!

回复

使用道具 举报

🔗
copyrightly 2018-6-22 03:35:10 | 只看该作者
全局:
copyrightly 发表于 2018-6-22 00:02
问两个问题:
1. 我的最后最后一行test总是不对
8/31/2110 - 2/27/1977 should be 48762: 48884

第一个问题已经解决,是我在dayInYear()里面的switch忘记写break了
回复

使用道具 举报

🔗
zhouyj96180 2018-6-22 23:08:53 | 只看该作者
全局:


求学分呀
回复

使用道具 举报

🔗
GenBry 2018-7-2 16:48:43 | 只看该作者
全局:
如果不是这个作业的话,我可能一辈子也不会再去想这些和日期有关的东西,虽然很想搞清楚
回复

使用道具 举报

全局:
想问一下,dayInYear()方法是不是得用递归?然后在difference()方法里调用这个方法就好了? 感谢
回复

使用道具 举报

全局:
搞了3个小时唉,不弄图了直接粘了,求学分呀!另外我想问各位一个愚蠢的问题:
比如说  public static boolean isLeapYear(int year) {
               if((year%4==0 && year%100!=0)||(year%400==0))
                      return true;
               else
                     return false;
              }这个方法如果我不写else return false他就给我报错“Thismethod must return a result of type boolean”是为什么呢,我只写一个if不可以吗?

运行结果:

Testing constructors.
Date should be 1/1/1: 1/1/1
Date should be 2/4/2: 2/4/2
Date should be 2/29/2000: 2/29/2000
Date should be 2/29/1904: 2/29/1904
Date should be 12/31/1975: 12/31/1975
Date should be 1/1/1976: 1/1/1976
Date should be 1/2/1976: 1/2/1976

Testing isLeapYear.
2014 is leap year should be false:false
2000 is leap year should be true:true
1000 is leap year should be false:false

Testing before and after.
1/1/1976 after 12/31/1975 should be true: true
1/2/1976 after 1/1/1976 should be true: true
12/31/1975 after 12/31/1975 should be false: false
12/31/1975 after 1/1/1976 should be false: false
1/1/1976 after 1/2/1976 should be false: false
12/31/1975 before 1/1/1976 should be true: true
1/1/1976 before 1/2/1976 should be true: true
12/31/1975 before 12/31/1975 should be false: false
1/1/1976 before 12/31/1975 should be false: false
1/2/1976 before 1/1/1976 should be false: false

Testing difference.
12/31/1975 - 12/31/1975 should be 0: 0
1/1/1976 - 12/31/1975 should be 1: 1
1/2/1976 - 12/31/1975 should be 2: 2
1/2/1976 - 2/27/1977 should be -422: -422
8/31/2110 - 2/27/1977 should be 48762: 48762

Testing dayInYear.
12/31/1975是第365天


回复

使用道具 举报

🔗
martinma 2018-7-24 21:53:05 | 只看该作者
全局:
上传截图,还可以加学分吗


回复

使用道具 举报

🔗
hanroro 2018-7-25 10:15:16 | 只看该作者
全局:
VarlarMorghulis 发表于 2018-7-24 17:08
搞了3个小时唉,不弄图了直接粘了,求学分呀!另外我想问各位一个愚蠢的问题:
比如说  public static boo ...

就是一个函数对于所有输入都要有返回值吧,按你原来的写法有些输入在if判断为false,就不能return了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表