CS61B 0A Exercises
虽然以前有Java基础,但是工作四年主要使用的还是python和shell,另外对于数据结构和算法的基础也不牢固。所以开一个坑从头开始学习CS61B,对于Dicussion/Lab/Homework做一个记录,学完之后回头看能有一个收获。整体流程参考CS自学社区,学习课程为B站CS61B,本篇为学习完Lectrue01后的 Homework0A。题目可以在https://practiceit.cs.washington.edu/上找到,用自己的邮箱注册即可
/*
Self-Check 1.26: Confusing
Question:
What is the output from the following Java program?
*/
public class Confusing { public static void method1() { System.out.println("I am method 1."); } public static void method2() { method1(); System.out.println("I am method 2."); } public static void method3() { method2(); System.out.println("I am method 3."); method1(); } public static void main(String[] args) { method1(); method3(); method2(); method3(); } }
//Answer:
I am method 1. I am method 1. I am method 2. I am method 3. I am method 1. I am method 1. I am method 2. I am method 1. I am method 2. I am method 3. I am method 1.
/*
这题没什么难度,涉及到函数的互相调用。
*/
/*
Exercise 2.5: starTriangle
Question:
Write for loops to produce the following output: * ** *** **** ***** */
//Answer:
for (int i = 1; i <= 5; i++){ for (int j = 1; j <= i; j++){ System.out.print('*'); } System.out.println(); }
/*
这题感觉我的解法稍微有点复杂,但是Java中没有 '*' * i 这种语法,ChatGPT也没有给出更好的方案
*/
/* Self-Check 2.25: numberTotal Question: What is the output of the following loop? */ int total = 25; for (int number = 1; number <= (total / 2); number++) { total = total - number; System.out.println(total + " " + number); } //Answer: 24 1 22 2 19 3 15 4 10 5
/*
把number值和total值代入循环,最后要判断循环结束的条件是number==6 && total==10
*/
/* Question: Write a method called printIndexed that accepts a String as its parameter and prints the String's characters in order followed by their indexes in reverse order. For example, the call of printIndexed("ZELDA"); should print Z4E3L2D1A0 to the console. */ //Answer: public static void printIndexed (String str){ int len = str.length(); for (int i = 0 ; i < len; i++){ System.out.print(str.charAt(i)); System.out.print(len - i - 1); } } /* 没啥好说的,用到charAt函数 */
/* Exercise 4.17: stutter Question: Write a method called stutter that accepts a parameter and returns the String with its characters returns repeated twice. For example, stutter("Hello!") returns "HHeelllloo!!" */ //Answer public static String stutter (String str){ String result = ""; for (int i = 0; i < str.length(); i++){ result += str.charAt(i); result += str.charAt(i); } return result; } /* 跟上一题差不多 */
/* Self-Check 4.5: ifElseMystery1 Question: public static void ifElseMystery1(int x, int y) { int z = 4; if (z <= x) { z = x + 1; } else { z = z + 9; } if (z <= y) { y++; } System.out.println(z + " " + y); } */ //Answer: ifElseMystery1(3, 20); 13 21 ifElseMystery1(4, 5); 5 6 ifElseMystery1(5, 5); 6 5 ifElseMystery1(6, 10); 7 11
/*
按顺序来呗,计算xyz的值
*/
/* Exercise 4.19: quadrant Question: Write a static method called quadrant that takes as parameters a pair of real numbers representing an (x, y) point and that returns the quadrant number for that point. Recall that quadrants are numbered as integers from 1 to 4 with the upper-right quadrant numbered 1 and the subsequent quadrants numbered in a counter-clockwise fashion: ^ y-axis | | | Quadrant 2 | Quadrant 1 | <--------------------+--------------------> x-axis | Quadrant 3 | Quadrant 4 | | | V Notice that the quadrant is determined by whether the x and y coordinates are positive or negative numbers. If a point falls on the x-axis or the y-axis, then the method should return 0. Below are sample calls on the method. Call Value Returned quadrant(12.4, 17.8) 1 quadrant(-2.3, 3.5) 2 quadrant(-15.2, -3.1) 3 quadrant(4.5, -42.0) 4 quadrant(0.0, 3.14) 0 */ //Answer: public static int quadrant(double x, double y){ if (x > 0){ if (y > 0) return 1; else if (y < 0) return 4; else return 0; } else if (x < 0){ if (y > 0) return 2; else if (y < 0) return 3; else return 0; } else return 0; } /* 投机取巧没有用到等于0的时候,还要学习下浮点数和int比较的方法,另外觉得if else有点多了。 */ //好蠢,为什么不放在一起呢? public static int quadrant(double x, double y){ if (x > 0 && y > 0) return 1; else if (x < 0 && y > 0) return 2; else if (x < 0 && y < 0) return 3; else if (x > 0 && y < 0) return 4; else return 0; }
posted on 2024-12-23 18:55 s1mplesama 阅读(54) 评论(0) 收藏 举报
浙公网安备 33010602011771号