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

Berkeley CS 61B Data Structures(in Java) Lab2 讨论帖

 
🔗
ypandxy 2015-5-11 22:56:30 | 只看该作者
全局:
amyzen 发表于 2015-4-23 13:21
补交作业,为什么大家total number都是6或7,而我的是8呢?
是因为使用add方法时多new了几个分数出来么? ...

我是f0.add(sumoftwo)
回复

使用道具 举报

🔗
ypandxy 2015-5-11 22:57:18 | 只看该作者
全局:
L.r_yoga 发表于 2015-4-12 16:40
继续补进度O(∩_∩)O~
PS.这个total number大家做的有6有7,但是被定义的只有f0-f3,两个sum要不要算进去呢 ...

不用啊,在ADD里面不是调用构造函数了么,
回复

使用道具 举报

🔗
ypandxy 2015-5-11 22:59:59 | 只看该作者
全局:
加油
回复

使用道具 举报

🔗
Lauren_Zhang 2015-5-13 21:33:11 | 只看该作者
全局:
哎lab2理应很简单才对还搞了这么久。。。。忧伤

Screen Shot 2015-05-13 at 21.31.36.png (46.95 KB, 下载次数: 5)

Screen Shot 2015-05-13 at 21.31.36.png
回复

使用道具 举报

🔗
Joseph_Liu 2015-5-23 10:29:24 | 只看该作者
全局:
本帖最后由 Joseph_Liu 于 2015-5-23 10:54 编辑


顺带一提,虽然定义Fraction类时,变量numerator和denominator都是private类型的,但只要是同类型的object都是可以access这两个private类型的变量的。

private means that only code within the same class can access the private thing. It means private to the class, not private to the object. One dog can see another Dog object's stuff, but a Cat can't see a Dog's private.
回复

使用道具 举报

🔗
smallmikko 2015-5-24 06:49:56 | 只看该作者
全局:
为什么上了static运行的时候会有警报,虽然结果也是出来了
回复

使用道具 举报

全局:
Berkeley CS61B Lab2 Results

分享一下代码吧!

1. public Fraction(int n) {    this(n, 1);
  }

  /** Constructs a Fraction 0/1.
   */
  public Fraction() {
        this(0,1);  
          
  }

  /** Copies the Fraction "original".
   */
  public Fraction(Fraction original) {
        this(original.numerator, original.denominator);  
          
  }

2. Fraction sumOfTwo = f1.add(f2);                              // Sum of f1 and f2.
    Fraction sumOfThree = f0.add(f1).add(f2);             // Sum of f0, f1, and f2.

3. public void changeNumerator(int numerator) { // DO NOT CHANGE THIS SIGNATURE!
    // Fix the bug that prevents this method from working correctly.
    if (numerator < 0) {
      System.out.println("Fatal error:  Negative numerator.");
      System.exit(0);
    }
    this.numerator = numerator;                // this.numerator means the origial one
                                                                    // numerator is the parameter
  }

  private static int numberOfFractions = 0;                // static means it can be used in the whole class

  public static int fracs() {                         // DO NOT CHANGE THIS SIGNATURE!
    // Fix the bug that prevents this method from working correctly.
    return numberOfFractions;
  }
  //虽然要求不改,但是static类型不用object调用,better style

4. private static int gcd (int x, int y) {
    /* Replace the following line with your solution. */
       
        if (x < 0)
        {
                System.out.println("Fatal error:  Negative x.");
            System.exit(0);
        }
       
        if (y < 0)
        {
                System.out.println("Fatal error:  Negative y.");
            System.exit(0);
        }
       
        int remainder;

        while( ( remainder = x % y ) != 0)
        {
                x = y;        // denominator becomes numerator
                y = remainder; //remainder becomes denominator
               
        }
       
        return y;                //return the denominator
          
  }
回复

使用道具 举报

全局:
耗时:两个小时多一些(不知道大家都是怎么样,想有个对比)
通过这个lab更深刻地理解了this的用法,受益匪浅!

另外:感觉自己转fx转数字分子分母的算法有点垃圾,思路是f1转字符串,indexOf()找到“/”的位置,用substring()分别找到两段数字,再字符串转数字。不知道有更好的解法吗,非常感谢!

点评

确实this的用法被锻炼了。。  发表于 2015-7-20 03:08

评分

参与人数 1大米 +5 收起 理由
忆梦前尘 + 5 感谢分享!

查看全部评分

回复

使用道具 举报

🔗
sheepmiemie 2015-7-19 01:39:15 | 只看该作者
全局:
但是我的是6, 哪里错了吗

Screen Shot 2015-07-18 at 12.38.32 PM.png (31.34 KB, 下载次数: 4)

Screen Shot 2015-07-18 at 12.38.32 PM.png
回复

使用道具 举报

🔗
忆梦前尘 2015-7-20 03:06:44 | 只看该作者
全局:
lab2 成功刷完!!objects of fraction 之前要添加static憋了我好一段时间。。




我竟然是第一个先把gcd改出来了。。

求加分~!
回复

使用道具 举报

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

本版积分规则

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