CS61B srping 2018 proj1b https://sp18.datastructur.es/
Despite the sufferring from the former disc04 ,i get the golden rule of extended classes' type discrmination ,that is , in a 赋值过程中,在类型层级上,右边的一定要小于左边的。(你可以把小的类型的塞进大的类型中去)。
lab4 是一个讨论课,讨论主题是之前的proj1a的内容
获取需要的文件
As before, pull the skeleton using the command git pull skeleton master.
In the skeleton, we have provided the following files:
PalindromeFinder.java: Class that helps identify generalized Palindromes in English.
CharacterComparator.java: An interface for comparing characters.
TestPalindrome.java: A class for JUnit tests for Palindrome.
TestOffByOne.java: A class for JUnit tests for OffByOne.
In addition you will create the following files:
Palindrome.java: A class for palindrome operations.
OffByOne.java: A class for off-by-1 comparators.
OffByN.java: A class for off-by-N comparators.
You’ll also need to cd into your library-sp18 folder. Once you’re there, use git pull origin master. If everything works as it should, you should see file called words.txt appear in the library-sp18/data folder.
In summary, these should be the shell commands to use:
Additional Optional Manual Download
本节需要ArrayDeque或者是LinkedListDeque ,如果你没有,可以点击这里下载 :下载链接,2025年一月还好用。
任务1 Deque Interface/ Deque接口
创建一个 Deque.interface,包含ArrayDeque和LinkedListDeque 里所有方法, 这个链接有具体内容: project 1a spec,(idea中默认创建class,记得选接口),Deque必须是接受泛型的接口,建立好之后,去到ArrayDeque和LinkedListDeque ,让两个类实现Deque, implements Deque<T>,T是什么可以自选,然后添加@Override,这在CS61B中必须,希望也成为你的java 必须。
如果你用了前面 课程提供的
LinkedListDeque,那得这么写类定义public class LinkedListDeque<Item> extends LinkedList<Item> implements Deque<Item>
任务2 wordToDeque /
新建一个Palindrome.java文件,并加入下面一条语句:
public Deque<Character> wordToDeque(String word){
return null;
}
输入一个字符串,wordToDeque 应返回一个双端队列,其中字符的出现顺序与字符串中的顺序相同。例如,如果单词是“persiflage”,那么返回的双端队列应该在前面有“p”,后面是“e”,依此类推。
Uncomment TestPalindrome 中的代码并运行文件中包含的测试(例如,右键单击它并选择“运行 TestPalindrome”)。现在应该无法通过所提供的测试。目标是通过正确实现 wordToDeque 通过此测试。通过测试后,请继续完成本作业的下一部分。确保你没有删除奇怪的行static Palindrome palindrome = new Palindrome();。它对于本次作业任务没有用,但稍后会需要。
提示:搜索网络以了解如何获取字符串中的第 i 个字符。
提示:将字符插入 Deque<Character> 和将整数插入 LinkedListDeque<Integer> 类似。
提示:testWordToDeque 的细心读者可能想知道为什么我们不直接创建一个正确的 Deque,然后调用 assertEquals。原因是我们的 Deque 类没有提供 equals 方法,因此它不会按您期望的方式工作。我们很快就会在课堂上讨论这个问题。
任务3: isPalindrome
任务3A isPalindrome测试
在Palindrome.java中加入public boolean isPalindrome(String word)方法, 先别管它,随便给个return值,先写测试,
在TestPalindrome 里增加至少一条测试,来测试isPalindrome方法,可以用assertTrue 、assertFalse 或者其他需要的JUnit 提供的方法
任务3B isPalindrome方法
- 不让用 Deque的get方法。
- 可以考虑recursion。
不包甜的解答isPalindrome
public class Palindrome {
public Deque<Character> wordToDeque(String word){
char[] strToArray= word.toCharArray();
int length = strToArray.length;
//arrayDeque use
Deque<Character> ad= new ArrayDeque<>();
for (int i = 0;i<length;i++){
ad.addLast(strToArray[i]);
}
return ad;
}
public boolean isPalindrome(String word) {
Deque<Character> wordDQ = wordToDeque(word);
// Deque emptyDq =new ArrayDeque();
int N = wordDQ.size();
if(N==0||N==1){
return true;
}
/* method 0
不让用 get方法
for(int i = 0;i<N/2;i++){
if(wordDQ.get(i)!=wordDQ.get(N-1-i)){
return false;
}
}
*/
/* method1
for(int i=0;i<N/2;i++){
if(wordDQ.removeFirst()!=wordDQ.removeLast()){
return false;
}
}
*/
return helper(wordDQ, wordDQ.size());
}
private Boolean helper(Deque<Character> wordDQ,int N){
if(N==0||N==1){
return true;
}
if(wordDQ.removeFirst()!=wordDQ.removeLast()){
return false;
}
else{
return helper(wordDQ,wordDQ.size());
}
}
public static void main(String[] args) {
System.out.print("ab".length());
}
}
TestPalindrome
import org.junit.Test;
import static org.junit.Assert.*;
public class TestPalindrome {
// You must use this palindrome, and not instantiate
// new Palindromes, or the autograder might be upset.
static Palindrome palindrome = new Palindrome();
@Test
public void testWordToDeque() {
Deque d = palindrome.wordToDeque("persiflage");
String actual = "";
for (int i = 0; i < "persiflage".length(); i++) {
actual += d.removeFirst();
}
assertEquals("persiflage", actual);
}
// Uncomment this class once you've created your Palindrome class.
@Test
public void testIsPalindrome(){
}
@Test
public void testEmpty(){
String emptyStr ="";
assertTrue(palindrome.isPalindrome(emptyStr));
String notEmptyStr="not Empty";
assertFalse(palindrome.isPalindrome(notEmptyStr));
}
@Test
public void testChar(){
String character ="c";
assertTrue(palindrome.isPalindrome(character));
String longerStr="longer Str";
assertFalse(palindrome.isPalindrome(longerStr));
}
@Test
public void testSomePalindrome(){
String[] yes ={"acca","racecar","noon","a","",};
for(int i =0;i<yes.length;i++){
assertTrue(palindrome.isPalindrome(yes[i]));
}
}
@Test
public void testSomeNotPalindrome(){
String[] no ={"ymca","moon","123456","ab","extra Longgggggg","xx123xx",};
boolean tmpResult= false;
for(int i =0;i<no.length;i++){
tmpResult= palindrome.isPalindrome(no[i]);
assertFalse(tmpResult);
}
}
}
任务4 一般化的(Generalized )Palindrome 和 OffByOne
课程推荐按照以下6步骤展开任务:
- 创建一个名为“OffByOne”的类,该类实现
CharacterComparator - 向
TestOffByOne添加OffByOne类中equalChars方法的测试。 - 完成
equalChars方法并验证其是否有效。 - 添加一个重载
isPalindrome的新方法 - 将测试添加到
TestPalindrome,以测试isPalindrome中的新方法。可以使用new OffByOne()进行这些测试。 - 完成
isPalindrome中的新方法并验证其是否有效。
以上步骤不是必须严格遵守,基本上,最终目标是使用以下函数签名向Palindrome 类添加第三个public方法:
public boolean isPalindrome(String word, CharacterComparator cc)
根据作为参数 cc 传入的 CharacterComparator 提供的比较测试功能,如果该单词是(CharacterComparator中定义的某种规则下的)回文,则该方法将返回 true。字符比较器定义如下:
/** This interface defines a method for determining equality of characters. */
public interface CharacterComparator {
/** Returns true if characters are equal by the rules of the implementing class. */
public boolean equalChars(char x, char y);
}
需要创建一个OffByOne.java类,该类实现CharacterComparator ,equalChars 方法对两个比较的字符,如果两个字符的ASCII码就相差一位,那就会返回true,如下2个示范例子都会返回true,注意,Java中的字符以单引号'c'修饰,而不是字符串的双引号"string"。
OffByOne obo = new OffByOne();
obo.equalChars('a', 'b');
obo.equalChars('r', 'q');
如下三个例子,因为比较的字符不是只相差一位,所以返回false:
obo.equalChars('a', 'e');
obo.equalChars('z', 'a');
obo.equalChars('a', 'a');
Spec clarification, 2/7/2018: Characters in Java include non-alphabetical characters. For example ‘%’ and ‘&’ are off by one. This might seem strange (especially since they’re not even next to each other on the keyboard), but char values in Java are really just integers. For example ‘%’ is actually just another way of writing 37, and ‘&’ is another way of writing 38. That is, the code below is valid and the first print statement will print 38. The second two print statements are exactly equivalent, and they both simply print 1.
int x = '&';
System.out.println(x); // prints 38
System.out.println(38 - 37); // prints 1
System.out.println('&' - '%'); // prints 1
obo.equalChars('&', '%'); //return true
同样,“a”和“B”也不会相差 1,因为“a”-“B”是 31。如果您对许多熟悉字符的具体值感到好奇,请参阅这篇维基百科文章底部的表格。 [规格澄清自 2018 年 2 月 7 日起结束]
针对奇数长度回文,我们不检查中间字符与其自身是否相等。因此,“flake”是一个相差 1 的回文,尽管“a”与其自身相差一个字符。
与我们之前的 isPalindrome 方法一样,任何长度为零或 1 个字符的单词都被视为回文。
提示:确保在实现 equalChars 时包含 @Override。虽然它对程序的功能没有影响,但,这是一个好习惯。
提示:要计算两个字符之间的差异,只需在 java 中“计算”它们的差异即可。例如 int diff = 'd' - 'a';将返回 diff 作为 -3。
提示:Palindrome 和 OffByOne方法对非常见字符可能会出bug 。
提示:尝试通过修改 PalindromeFinder.java 打印出所有长度为 4 或以上的OffByOne回文(words.txt中的内容作为数据源)。例如,“flake”是一个OffByOne 的回文,因为“f”和“e”相隔一个字母,“k”和“l”相隔一个字母。
4.1 OffByOne
public class OffByOne implements CharacterComparator {
@Override
public boolean equalChars(char x, char y) {
if (Math.abs(x-y)==1){
return true;
}
return false;
}
}
4.2 isPalindrome的重载 with CharacterComparator
public boolean isPalindrome(String word, CharacterComparator cc){
Deque<Character> wordDQ = wordToDeque(word);
int N = wordDQ.size();
if(N==0||N==1){
return true;
}
for(int i=0;i<N/2;i++){
if( !cc.equalChars(wordDQ.removeFirst(),wordDQ.removeLast())){
return false;
}
}
return true;
}
4.3 some test
public void testSomeOffByOnePalindrom(){
String[] yes ={"ab", "cd", "ef", "gh", "ij", "kl", "nm", "op", "po","yz","flake","avxub","avxwb",};
boolean tmpResult= false;
for(int i =0;i<yes.length;i++){
tmpResult= palindrome.isPalindrome(yes[i],offByOne);
assertTrue(tmpResult);
}
}
@Test
public void testNotOffByOnePalindrom(){
String[] no ={"aaa" , "ac" , "XxUuYyZz" , "AZaZ" , "taste","CCC"};
boolean tmpResult= false;
for(int i =0;i<no.length;i++){
tmpResult= palindrome.isPalindrome(no[i],offByOne);
assertFalse(tmpResult);
}
}
Task 5: OffByN 任务5 ,在offByOne的基础上,offByN
内容类似,就是为实现CharacterComparator的OffByN 增加一个参数,让使用者决定到底offBy 多少"N";
5.1 OffByN
public class OffByN implements CharacterComparator{
private int n ;
public OffByN(int N){
n=N;
}
public boolean equalChars(char x, char y) {
if (Math.abs(x-y)==n){
return true;
}
return false;
}
}
5.2 testOffByN
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class TestOffByN {
static Palindrome palindrome = new Palindrome();
static CharacterComparator offBy5 = new OffByN(5);
@Test
public void testSomeOffBy5() {
String[] yes = {"pink",
"spun",
"sworn",
"tidy",
"tinny",
"tiny",};
boolean tmpResult = false;
for (int i = 0; i < yes.length; i++) {
tmpResult = palindrome.isPalindrome(yes[i], offBy5);
assertTrue(tmpResult);
}
}
@Test
public void testNotOffBy5() {
String[] no = {"ab", "cd", "ef", "gh", "ij", "kl", "nm", "op", "po", "yz", "flake", "avxub", "avxwb",};
boolean tmpResult = false;
for (int i = 0; i < no.length; i++) {
tmpResult = palindrome.isPalindrome(no[i], offBy5);
assertFalse(tmpResult);
}
}
}

浙公网安备 33010602011771号