📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
楼主: 小亩_0718
跳转到指定楼层
上一主题 下一主题
收起左侧

[入门|算法|数据结构] CS 61B Data Structures 2014Spring 每日打卡

🔗
 楼主| 小亩_0718 2020-4-6 12:22:56 | 只看该作者
全局:
计划明天完成5跟6, lab 嘛取决于是不是非得用emacs,捂脸🤦‍♀️

评分

参与人数 1大米 +3 收起 理由
五农 + 3 很有用的信息!

查看全部评分

回复

使用道具 举报

🔗
 楼主| 小亩_0718 2020-4-7 11:03:50 | 只看该作者
全局:
已看完5跟6reading

评分

参与人数 1大米 +3 收起 理由
五农 + 3 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
 楼主| 小亩_0718 2020-4-8 06:28:01 | 只看该作者
全局:
loop 练习题
/*Write a code segment that allows the user to enter 10 numbers and computes
and prints out their sum, average, product.*/

答案
class tenNum{
        public static void main(String[] arg){
                int count = 0;
                double sum = 0.0;
                double product = 1;
                double avg = 0;

                Scanner kb = new Scanner(System.in);
                while (count<10){
                        System.out.println("Please enter a number");
                        double input=kb.nextDouble();
                        sum += input;
                        product += input;
                        avg += input;
                        count++;
                }
                avg = sum/10;
                System.out.println("Sum: " + sum + " Product: " +product + "Avg: " +avg);

        }
}

评分

参与人数 1大米 +3 收起 理由
五农 + 3 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
 楼主| 小亩_0718 2020-4-8 06:30:31 | 只看该作者
全局:
本帖最后由 小亩_0718 于 2020-4-8 06:34 编辑

loop练习题
/* Write a code segment that asks the user how many numbers there are in a list,
then allows the user to enter the numbers and computes and prints out the smallest and the largest number*/

答案:

class MinMax{
        public static void main(String[] arg){

                Scanner kb = new Scanner(System.in);
                System.out.println("How many numbers in a list?");
                int listAmt=kb.nextInt();
               
                if(listAmt<1){
                        System.out.println("Invalid input");
                }
                else if (listAmt == 1){
                        System.out.println("Enter a number");
                        double x=kb.nextDouble();
                        System.out.println("Smallest number: " + x + "Largest number: " + x);
                }else{
                        System.out.println("Enter a number");
                        double input=kb.nextDouble();
                        double smallest=input,largest=input;
                        for(int i=1;i<listAmt;i++){
                                System.out.println("Enter a number");
                                double secInput=kb.nextDouble();
                                smallest=Math.min(secInput,smallest);
                                largest=Math.max(secInput,largest);
                        }
                        System.out.println("Smallest number: " + smallest + " Largest number: " + largest);
                }
        }
}

评分

参与人数 1大米 +3 收起 理由
五农 + 3 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
 楼主| 小亩_0718 2020-4-8 08:57:00 | 只看该作者
全局:
loop练习题
/*Write a code segment that continues to read user input until the user enters a number between 0 and 9.*/
答案
class zeroNineTerminator{
        public static void main(String[] arg){
                Scanner kb = new Scanner(System.in);
                double num=0;
                do
                {
                        System.out.println("Enter a number. A number between 0 and 9 will terminate this program");
                        num = kb.nextDouble();
                }while(num<0||num>9);
        }
}

评分

参与人数 1大米 +3 收起 理由
五农 + 3 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
 楼主| 小亩_0718 2020-4-13 12:50:58 | 只看该作者
全局:
HW2,已写完part1、2、4以及3的isBefore( )。
测完isBefore()发现自己犯了个...洒洒的错误😐,其不但导致不符合requirments的date object的instance variables 的值都为0/0/0(见红色标注),还让我怀疑自己写错isBefore() method body里的内容,因为测isBefore测出来的结果不对(见蓝色处)

Testing constructors.
Invalid date!!
Date should be 1/1/1: 0/0/0
Invalid date!!!
Date should be 2/4/2: 0/0/0
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
Invalid date!!!
Date should be 1/1/1976: 0/0/0
Invalid date!!!
Date should be 1/2/1976: 0/0/0
Invalid date!!!
12/31/1975 before 0/0/0 should be true: false
0/0/0 before 0/0/0 should be true: false
12/31/1975 before 12/31/1975 should be false: false
0/0/0 before 12/31/1975 should be false: true
0/0/0 before 0/0/0 should be false: false



-------------
改错:
写constructor的时候只将符合要求的arguments 赋值给了this的instance variables,没管不符合要求的arguments,也就是之前没写以下橘色部分。
    public Date(int month, int day, int year) {
        if(Date.isValidDate(month, day, year)) {
            this.month = month;
            this.day = day;
            this.year = year;
        } else {
            System.out.println("Invalid date!!");
            this.month = month;
            this.day = day;
            this.year = year;

            // System.exit(0);
        }

    }

评分

参与人数 1大米 +3 收起 理由
五农 + 3 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
 楼主| 小亩_0718 2020-4-16 11:33:23 | 只看该作者
全局:
HW2交作业:Constructors

class Date {

    private int month;
    private int day;
    private int year;

public Date(int month, int day, int year) {
        if(Date.isValidDate(month, day, year)) {
            this.month = month;
            this.day = day;
            this.year = year;
        } else {
            System.out.println("Invalid date!!");
            this.month = month;
            this.day = day;
            this.year = year;
            // System.exit(0);
        }

    }

public Date(String s) {
        int firstSlash = s.indexOf("/");
        int secondSlash = s.lastIndexOf("/");

        int month = Integer.parseInt(s.substring(0, firstSlash));
        int day = Integer.parseInt(s.substring(firstSlash + 1, secondSlash));
        int year = Integer.parseInt(s.substring(secondSlash + 1));
        if(Date.isValidDate(month, day, year)) {
            this.month = month;
            this.day = day;
            this.year = year;
        } else {
            System.out.println("Invalid date!!!");
            this.month = month;
            this.day = day;
            this.year = year;
            // System.exit(0);
        }
    }

评分

参与人数 1大米 +3 收起 理由
五农 + 3 欢迎分享你知道的情况,会给更多积分奖励!

查看全部评分

回复

使用道具 举报

🔗
 楼主| 小亩_0718 2020-4-16 11:44:36 | 只看该作者
全局:
HW2交作业:Methods

public static boolean isLeapYear(int year) {
        return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))?true:false;
}

public static int daysInMonth(int month, int year) {
        if(month == 4 || month == 6 || month == 9 || month == 11) {
            return 30;
        } else if(month == 2) {
            return Date.isLeapYear(year)?29:28;
        } else {
            return 31;
        }
}

public static boolean isValidDate(int month, int day, int year) {
        boolean validDay = (day == Date.daysInMonth(month, year));
        boolean validMonth = month > 0 && month < 13;
        boolean validYear = year > 0;
        return validDay && validMonth && validDay;
}

    public String toString() {
        return this.month + "/" + this.day + "/" + this.year;
}

public boolean isBefore(Date d) {
        return this.year < d.year ||
               this.year == d.year && this.month < d.month ||
               this.year == d.year && this.month == d.month && this.day < d.day;
    }
    public boolean isAfter(Date d) {
        return this.year > d.year ||
               this.year == d.year && this.month > d.month ||
               this.year == d.year && this.month == d.month && this.day > d.day;
    }
   public int dayInYear() {
        int numMonth = this.month;
        int totalDays = 0;
        for (int i = 1; i < numMonth; i++) {
            totalDays += this.daysInMonth(i, this.year);
        }
        return totalDays += this.day;
    }

评分

参与人数 1大米 +3 收起 理由
五农 + 3 给你点个赞!

查看全部评分

回复

使用道具 举报

🔗
 楼主| 小亩_0718 2020-4-16 11:46:24 | 只看该作者
全局:
本帖最后由 小亩_0718 于 2020-4-16 11:47 编辑

HW2交作业:为了 difference method 🐝

public boolean equals(Object obj){
            if(this == obj){
                    return true;
            }
            if(this.getClass()!=obj.getClass()){
                    return false;
            }
            Date d = (Date) obj;
            return this.day == d.day && this.month == d.month && this.year == d.year;
    }


public int difference(Date d) {
        
        if(this.equals(d)){
                return 0;
        }else if(this.isBefore(d)){
                return -(d.difference(this));
        }else{
                return (this.year-d.year)*365 + this.dayInYear() - d.dayInYear() + countLeapYears(d,this);
        }          //this(date) is after d(date)
    }

    private static int countLeapYears(Date a, Date b) {
        int numLeapYear=0;
        for (int y=a.year; y<b.year; y++){
                if(Date.isLeapYear(y)){
                        numLeapYear+=1;
                }
        }
        return numLeapYear;
    }

评分

参与人数 1大米 +3 收起 理由
五农 + 3 很有用的信息!

查看全部评分

回复

使用道具 举报

🔗
 楼主| 小亩_0718 2020-4-18 10:25:00 | 只看该作者
全局:
LAB2- Part1 :

public Fraction(int n, int d) {
    if (n < 0) {
      System.out.println("Fatal error:  Negative numerator.");
      System.exit(0);
    }
    if (d <=0 ) {
      System.out.println("Fatal error:  Nonpositive denominator.");
      System.exit(0);
    }
    numberOfFractions++;
    this.numerator = n;
    this.denominator = d;
  }

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

public Fraction() {
    this(0);
  }

  public Fraction(Fraction original) {
    this(original.numerator, original.denominator);
  }
----------------
参考:https://www.geeksforgeeks.org/constructor-chaining-java-examples/

评分

参与人数 1大米 +3 收起 理由
五农 + 3 给你点个赞!

查看全部评分

回复

使用道具 举报

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

本版积分规则

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