首页 > 其他分享 >CS61B Lab2 Debugging

CS61B Lab2 Debugging

时间:2024-04-26 15:12:52浏览次数:14  
标签:Debugging head return 10 int CS61B Lab2 lst static

实验2主要内容

  • 教你使用IDE中调试步骤,学会设置断点调试代码
  • 学以只用,学会设置断点之后,就开始改代码错误了

本节需要学什么?

Java配置Configration

当你导入一个项目模块时,需要添加修改configration的以下内容。
image.png

Junit的导入

有时候运行的时候会出现“junit不存在等情况”
image.png
这时候就要导入junit包,调用里面的函数:
在这里导入:
image.png
image.png

assertEquals的应用

这个函数的作用就是评测你输出的代码是否与预期测试一致

开始改代码

Arithmetic

    public static int sum(int a, int b) {
        return a + b;
    }
    //原来是乘法改为加法即可

DeBugExercise1

    public static int divideThenRound(int top, int bottom) {
        float quotient = (float) top / bottom;
        int result = Math.round(quotient);
        return result;
    }

变为浮点数

DeBugExercise2

    public static int max(int a, int b) {
        return a>b?a:b;
    }

改写max函数,原来得到的是a和b当中比较小的值。

InitListExericise

    public static void addConstant(IntList lst, int c) {
        IntList head = lst;
        while (head!= null) {
            head.first += c;
            head = head.rest;
        }
    }

原来是head->rest改为head

    public static boolean firstDigitEqualsLastDigit(int x) {
        int lastDigit = x % 10;
        while (x >= 10) {
            x = x / 10;
        }
        int firstDigit = x % 10;
        return firstDigit == lastDigit;
    }

x>10改为x>=10
最后一个

    public static boolean squarePrimes(IntList lst) {
        // Base Case: we have reached the end of the list
        if (lst == null) {
            return false;
        }

        boolean currElemIsPrime = Primes.isPrime(lst.first);

        if (currElemIsPrime) {
            lst.first *= lst.first;
        }

        //return currElemIsPrime || squarePrimes(lst.rest);
        return squarePrimes(lst.rest)||currElemIsPrime;
    }

这个比较巧妙,原来是currElemIsPrime || squarePrimes(lst.rest),这里如果currElemlsPrime如果为真值,后面就不进行递归了,因此需要交换位置。
提交:
image.png

评测

image.png
全对

标签:Debugging,head,return,10,int,CS61B,Lab2,lst,static
From: https://www.cnblogs.com/ahnultq/p/18160141

相关文章

  • 伯克利大学 CS61B Lab配置教程
    基本过程:首先将伯克利大学的代码框架下载到自己的电脑,然后我们直接在框架里修改就行将自己的代码上传到github上,然后使用伯克利大学的Gradescope评测自己写的代码下载代码在自己电脑桌面新建一个文件夹,这里我命名为:cs61b,打开gitbash,使用cd进入我们新创建的文件夹,注意路径......
  • Advanced .Net Debugging 1:你必须知道的调试工具
    Advanced.NetDebugging1:你必须知道的调试工具合集-Net高级调试(基于原著)(7) 1.Advanced.NetDebugging1:你必须知道的调试工具01-242.Advanced.NetDebugging2:CLR基础02-273.Advanced.NetDebugging3:基本调试任务(调试目标、符号、控制调试目标执行和设置断点)03-04......
  • Advanced .Net Debugging 7:托管堆与垃圾收集
    一、简介这是我的《Advanced.NetDebugging》这个系列的第七篇文章。这篇文章的内容是原书的第二部分的【调试实战】的第五章,这一章主要讲的是从根本上认识托管堆和垃圾回收。软件系统的内存管理方式有两种,第一种是手动管理内存,这种方式容易产生一些问题产生,比如:悬空指针......
  • MIT6.S081 - Lab2: system calls
    Lab2:systemcalls预备知识执行一次系统调用的流程:USERMODEstep1:系统调用声明user/user.h:系统调用函数(如intfork(void))step2:ecall进入内核态user/usys.S(该文件由user/usys.pl生成,后续添加函数可以在这里添加):执行如下命令.globalforkfork:lia7,SYS_f......
  • MIT 6.5830 simpleDB Lab2
    Exercise1需要完成的是:src/java/simpledb/execution/Predicate.javasrc/java/simpledb/execution/JoinPredicate.javasrc/java/simpledb/execution/Filter.javasrc/java/simpledb/execution/Join.java这里主要实现两个功能:Filter:通过Predicate过滤一部分满足条件的Tupl......
  • GRPC - Debugging: Using Wireshark
      ......
  • GRPC - Debugging: Server Reflection
      ......
  • Advanced .Net Debugging 6:程序集加载器
    一、简介这是我的《Advanced.NetDebugging》这个系列的第六篇文章。这篇文章的内容是原书的第二部分的【调试实战】的第四章。这章主要讲的是程序集加载器,比如:CLR加载器简介、简单的程序集加载故障、加载上下文故障、互用性与DllNotFoundException和轻量级代码生成的......
  • Lab2:System Call
    trace该系统调用程序,可以跟踪其他的系统调用命令,该系统调用的形参为一个整数掩码。其具体实参为1<<sys_call所得到的整数值,sys_call是一个系统调用指令在内核中定义的系统调用编号。返回值包含进程id,系统调用sys_call的名称和返回值。并且trace指令可以跟踪当前进程和它派生的......
  • UVM - 13 (lab2)
    Makefile传递参数到SV中在仿真阶段使用$value$plusargs函数传递字符串//接收Makefile中传递过来的参数if($value$plusargs("UVM_TESTNAME",test_name))begin//传递参数之后执行这里的内容end应用举例//sv文件中,用于接收仿真的时候传入的number_packetsif($(va......