cs61B-sp21 | lab6

cs61B-sp21 | lab6

TODO 1

在 CapersRepository.java 中

static final File CAPERS_FOLDER = null; 
// TODO Hint: look at the `join`  
//      function in Utils

在 Utils.java 我们找到 join 函数,第一个 join 的作用是将 first 和 others 连接起来形成一个路径,并将其转换为 File 对象返回。而第二个 join 的作用是将 first 对象的路径和 others 连接起来形成一个新的对象并转换为 File 返回。

回到 CAPERS_FOLDER,实际上我们是要创建主工作目录,也就是 ( u s e r . d i r ) / . c a p e r s (user.dir)/.capers (user.dir)/.capers

static final File CAPERS_FOLDER = join(CWD, ".capers");

TODO 2

在 Dog.java 中

public class Dog { // TODO...
}

我们需要添加 Serializable 接口

public class Dog implements Serializable

TODO 4

static final File DOG_FOLDER = null; // TODO (hint: look at the `join`  //      function in Utils)

对于 DOG_FOLDER,我们要在 .capers 下创建 dogs 文件夹

static final File DOG_FOLDER = join(".capers", "dogs");

TODO 5

根据注释,这段代码要执行持久化的操作,也就是创建文件夹或者文件,让数据在程序关闭后仍然存在。

/**  * Does required filesystem operations to allow for persistence. * (creates any necessary folders or files) * Remember: recommended structure (you do not have to follow): * * .capers/ -- top level folder for all persistent data in your lab12 folder *    - dogs/ -- folder containing all of the persistent data for dogs *    - story -- file containing the current story 
*/
public static void setupPersistence() {  // TODO  
}

创建 .capers 文件夹,以及 .capers/dogs 文件夹。

CAPERS_FOLDER.mkdir();
Dog.DOG_FOLDER.mkdir();

TODO 6

/**  * Appends the first non-command argument in args * to a file called `story` in the .capers directory. * @param text String of the text to be appended to the story  */public static void writeStory(String text) {  // TODO  
}

需要先在 .capers 文件夹下创建一个叫做 “story” 的文件,用 join 函数将 .capers 路径和 story 文件名连接。如果该文件存在,说明之前已经写入过字符串,那么提取原来的内容并且与新内容连接。如果该文件不存在,那么文件中内容就是 text。

public static void writeStory(String text) {  File f = join(CAPERS_FOLDER, "story");  String newStory;  if(!f.exists()) {  newStory = text;  } else {  String originStory = readContentsAsString(f);  newStory = originStory + '\n' + text;  }    writeContents(f, newStory);  System.out.println(newStory);  
}

TODO 7

在 Main.java 中的 main 函数

case "dog":  validateNumArgs("dog", args, 4);  // TODO: make a dog  break;

首先,输入的参数为\dog [name] [breed] [age],name 对应 args[1],breed 对应 args[2],age 对应 args[3]。再调用 CapersRepository 中的 makeDog 函数即可。注意 age 需要整型,所以我们要进行类型转换。

case "dog":  validateNumArgs("dog", args, 4);  String name = args[1];  String breed = args[2];  int age = Integer.parseInt(args[3]);  CapersRepository.makeDog(name, breed, age);  break;

TODO 8

需要创建并且保存 dog 并传入它的三个参数,并且需要打印这只狗的信息。

/**  * Creates and persistently saves a dog using the first * three non-command arguments of args (name, breed, age). * Also prints out the dog's information using toString(). 
*/
public static void makeDog(String name, String breed, int age) {  // TODO  
}

观察 Dog.java


saveDog 用来将 dog 存储至文件中,toString() 返回一个包含狗的信息的字符串。

public static void makeDog(String name, String breed, int age) {  Dog newDog = new Dog(name, breed, age);  newDog.saveDog();  System.out.println(newDog);  
}

TODO 9

/**  * Saves a dog to a file for future use. 
*/
public void saveDog() {  // TODO (hint: don't forget dog names are unique)  
}

在 Dogs 文件夹下创建这只新狗狗的文件并写入对象。

public void saveDog() {  File newDog = join(DOG_FOLDER, name);  writeObject(newDog, this);  
}

TODO 10

case "birthday":  validateNumArgs("birthday", args, 2);  // TODO: celebrate this dog's birthday  break;

获取要庆祝生日的小狗名称,也就是 args[1],并且调用 CapersRepository 中的 celebrateBirthday 函数。

case "birthday":  validateNumArgs("birthday", args, 2);  name = args[1];  CapersRepository.celebrateBirthday(name);  break;

TODO 11

/**  * Advances a dog's age persistently and prints out a celebratory message. * Also prints out the dog's information using toString(). * Chooses dog to advance based on the first non-command argument of args. * @param name String name of the Dog whose birthday we're celebrating.  */public static void celebrateBirthday(String name) {  // TODO  
}

先从文件中获取 Dog,再调用 haveBirthday 函数,最终保存。

public static void celebrateBirthday(String name) {  // TODO  Dog theDog = Dog.fromFile(name);  theDog.haveBirthday();  theDog.saveDog();  
}

TODO 12

/**  * Reads in and deserializes a dog from a file with name NAME in DOG_FOLDER. * @param name Name of dog to load  * @return Dog read from file  */public static Dog fromFile(String name) {  // TODO (hint: look at the Utils file)  return null;  
}

先获取文件,再读取里面的对象即可。

public static Dog fromFile(String name) {  File f = join(DOG_FOLDER, name);  return readObject(f, Dog.class);    
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/18400.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

IDEA2024创建maven项目

1、new->project 2、创建后展示 3、生成resources文件夹 4、测试--编写一个hello文件

react-server-side-render最新学习与实践

写在前面 server side render(ssr)服务端渲染 ,亦即同构应用。主要有利于 seo 和首屏渲染,这是一篇比较新的可运行的结构设计,基于比较新的 react v16、react-router v5 的同构设计。结合了 redux(Flux 数据流实现)。 项目地址:react-ssr-s…

[书生·浦语大模型实战营]——在茴香豆 Web 版中创建自己领域的知识问答助手

茴香豆是一个基于LLM的领域知识助手,可以用于解答群聊中的问题。接下来是创建过程。 1.打开茴香豆Web版,创建自己的领域库。 地址:茴香豆Web版 这里类似于注册账号,你输入知识库的名称以及密码,然后它就会创建一个知识…

Vue3实战笔记(49)—Vue 3响应式魔法:ref vs reactive深入对决

文章目录 前言一、 ref 和 reactive主要差异总结 前言 Vue 3 中的 ref 和 reactive 都是用于创建响应式数据的工具,但它们之间存在一些重要的区别。今天聊聊它们之间的主要差异: 一、 ref 和 reactive主要差异 数据类型: ref 主要用于处理…

【微服务】部署mysql集群,主从复制,读写分离

两台服务器做如下操作 1.安装mysqldocker pull mysql:5.72.启动以及数据挂载 mkdir /root/mysql/data /root/mysql/log /root/mysql/conf touch my.conf //mysql的配置文件docker run --name mysql \ -e MYSQL_ROOT_PASSWORD123456 \ -v /root/mysql/data:/var/lib/mysql \ -v…

飞睿智能高精度、低功耗测距,无线室内定位UWB芯片如何改变智能家居

在数字化和智能化快速发展的今天,定位技术已经成为我们日常生活中不可或缺的一部分。然而,传统的GPS定位技术在室内环境中往往束手无策,给我们的生活带来了诸多不便。幸运的是,随着科技的不断进步,一种名为UWB&#xf…

智能座舱-车载声学技术训练营

语音交互赋能车载智能终端,成为智能座舱生态构建的核心功能 曾几何时,至少十年前,车内语音交互,大家都认为是“智障”阶段,基本上除了难用作为评价找不到其他的形容词做修饰。 但是随着技术的不断发展,特别…

STM32Cube系列教程11:使用STM32 RNG硬件随机数模块生成彩票号码

文章目录 配置RNG模块编写代码获取生成的随机数运行测试 今天写段代码测试一下STM32U083RC的(RNG)硬件随机数模块 顺便写个小demo生成7位真随机数的彩票号码,帮助那些买彩票还有选择困难症的人群 (doge)(手动狗头)。 全部代码以上传到github:https://gi…

【Unity】实现轮盘抽奖

简介 示例一:使用协程完成轮盘转动 using System; using System.Collections; using System.Collections.Generic; using UnityEngine;public class Lunpan : MonoBehaviour {[Tooltip("轮盘节点")]public Transform Roulette;[Tooltip("轮盘旋转的…

SpringBoot 微服务中怎么获取用户信息 token

SpringBoot 微服务中怎么获取用户信息 token 当我们写了一个A接口,这个接口需要调用B接口,但是B接口需要包含请求头内容,比如需要用户信息、用户id等内容,由于不在同一个线程中,使用ThreadLocal去获取数据是无法获取的…

如何高效测试防火墙的NAT64与ALG应用协议转换能力

在本文开始介绍如何去验证防火墙(DUT)支持NAT64 ALG应用协议转换能力之前,我们先要简单了解2个比较重要的知识点,即,NAT64和ALG这两个家伙到底是什么? 网络世界中的“翻译官” - NAT64技术 简而言之&…

如何批量提取pdf文件名?批量提取文件夹里的文件名,只要用对方法!

在数字化时代,PDF文件已经成为我们日常工作中不可或缺的一部分。然而,随着PDF文件数量的不断增加,如何高效地管理这些文件成为了一个挑战。批量提取PDF文件名,就是解决这一问题的关键所在。本文将为你介绍几种实用的方法&#xff…

长效IP和短效IP的使用指南分享

随着网络技术的发展,代理IP已经成为许多人在网络活动中不可或缺的工具。 代理IP不仅有助于保护用户的真实IP地址,保护用户的使用隐私,还可以帮助用户提升网络访问的速度等。 然而,在挑选代理IP时,用户常常会面临一个…

GDAL读取shp文件1

我们知道shp文件是一种gis文件,里面包含一张属性数据表,可以用GIS桌面软件打开; GDAL先初步读一下一个示例shp文件的信息, #include "stdafx.h" #include <ogrsf_frmts.h> #include <ogr_spatialref.h>int main() {// 为了使属性表字段支持中文,请…

图像分割模型LViT-- (Language meets Vision Transformer)

参考&#xff1a;LViT&#xff1a;语言与视觉Transformer在医学图像分割-CSDN博客 背景 标注成本过高而无法获得足够高质量标记数据医学文本注释被纳入以弥补图像数据的质量缺陷半监督学习&#xff1a;引导生成质量提高的伪标签医学图像中不同区域之间的边界往往是模糊的&…

笔记-Python读写文件

Python读写文件 1.open 使用open打开文件后一定要记得调用文件对象的close()方法。比如可以用try/finally语句来确保最后能关闭文件。 file_object open(‘thefile.txt’) try: all_the_text file_object.read( ) finally: file_object.close( ) 注&#xff1a;不能把open语…

Java | Leetcode Java题解之第118题杨辉三角

题目&#xff1a; 题解&#xff1a; class Solution {public List<List<Integer>> generate(int numRows) {List<List<Integer>> ret new ArrayList<List<Integer>>();for (int i 0; i < numRows; i) {List<Integer> row new…

嵌入式学习(Day:28 进程间通信2 -> 信号通信)

进程间通信 》信号通信 1. 64个信号 应用&#xff1a;异步通信。 中断&#xff0c;&#xff0c; &#xff08;PCBC块中&#xff0c;64个信号&#xff0c;大部分是&#xff1a;关闭&#xff0c;暂停&#xff0c;继续&#xff09; linuxubuntu:~$ kill -l &am…

防御恶意爬虫攻击

数据抓取爬虫 数据抓取爬虫是攻击者使用自动化脚本或工具在移动应用程序中抓取敏感数据的一种方式。这些爬虫可以定向抓取用户信息、产品列表、评论和评级等数据。攻击者可能会将这些数据用于非法目的&#xff0c;例如进行身份盗窃、诈骗活动或者卖给其他恶意方。 对于移动应用…

[机缘参悟-192] - 《道家-水木然人间清醒1》读书笔记 -15- 关系界限 - IT互联网时代下的真爱的形态

目录 前言&#xff1a; 1、 既独立又结盟&#xff0c;才是最好的关系 2、世间所有的好关系&#xff0c;一定要先谈钱 3、怎么建立高品质的关系 4、恋爱是情感组合&#xff0c;婚姻是价值组合 5、什么是成熟的爱情 6、婚姻的难点 7、这个时代稀缺的女性特质 8、恋爱和婚…