目录
L6 对象简介: 编写你自己的对象, 生命周期, 静态变量
L6L5 Cat 计数器
在 Cat 类构造方法 public Cat() 中,将 Cat 计数器(Cat 类的 static 变量 catCount)加 1。在 finalize 方法中将其减 1。
Requirements:
- 将不带参数的构造方法 public Cat() 添加到 Cat 类中。
- 构造方法必须将变量 catCount 的值加 1。
- 将 finalize 方法添加到 Cat 类。
- finalize 方法不应返回任何内容(返回类型为 void)。
- finalize 方法应将变量 catCount 减 1。
public class Cat {
public static int catCount = 0;
//在此编写你的代码
public Cat(){
catCount ++;
}
protected void finalize() throws Throwable{
catCount --;
}
public static void main(String[] args) {
}
}
protected
https://blog.csdn.net/asahinokawa/article/details/80777302
finalize()
https://blog.csdn.net/zyt425916200/article/details/78419410
throws Throwable, 异常的处理
https://blog.csdn.net/Jack_cherisf/article/details/89525420
L6L8 猫的 static 方法
向 Cat 类中添加两个 static 方法:int getCatCount() 和 setCatCount(int),用于获取/更改猫的数量(变量 catCount)。
Requirements:
-
- 向类中添加 getCatCount 方法。
-
- getCatCount 方法必须返回 int 值。
-
- getCatCount 方法必须返回变量 catCount 的值。
-
- 向类中添加带 int 参数的 setCatCount 方法。
-
- setCatCount 方法不应返回任何内容。
-
- setCatCount 方法必须将变量 catCount 设置为传递的值。
public class Cat {
private static int catCount = 0;
public Cat() {
catCount++;
}
public static int getCatCount() {
//在此编写你的代码
return catCount;
}
public static void setCatCount(int catCount) {
//在此编写你的代码
Cat.catCount = catCount;
}
public static void main(String[] args) {
}
}
static下无this, 并且static void时的用法不要忘记了
L6L8 两点之间的距离
实现 static double getDistance(x1, y1, x2, y2) 方法。该方法应计算两点之间的距离。使用 double Math.sqrt(double a) 方法,该方法将计算所传递参数的平方根。
Requirements:
-
- getDistance 方法必须返回 double 值。
-
- getDistance 方法必须为 static。
-
- getDistance 方法必须为 public。
-
- getDistance 方法必须返回两点之间的距离。
-
- getDistance 方法必须使用 double Math.sqrt(double a) 方法。
public class Util {
public static double getDistance(int x1, int y1, int x2, int y2) {
//在此编写你的代码
double a = (x2-x1)*(x2-x1) + (y2 - y1)*(y2 - y1);
return Math.sqrt(a);
}
public static void main(String[] args) {
}
}
Java sqrt()
https://www.runoob.com/java/number-sqrt.html
L6L8 ConsoleReader 类
创建 ConsoleReader 类,其中包含 4 个 static 方法:String readString() - 从键盘读取字符串int readInt() - 从键盘读取数字double readDouble() - 从键盘读取小数boolean readBoolean() - 从键盘读取字符串“true”或“false”,并返回相应的 boolean 值(true 或 false)
**请注意:**在每个方法中,创建从控制台(BufferedReader 或 Scanner)读取数据的变量。
Requirements:
-
- readString 方法必须读取和返回 String 值。
-
- readInt 方法必须读取和返回 int 值。
-
- readDouble 方法必须读取和返回 double 值。
-
- readBoolean 方法必须读取和返回 boolean 值。
public class ConsoleReader {
public static String readString() throws Exception {
//在此编写你的代码
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String a = reader.readLine();
return a;
}
public static int readInt() throws Exception {
//在此编写你的代码
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(reader.readLine());
return a;
}
public static double readDouble() throws Exception {
//在此编写你的代码
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
double a = Double.parseDouble(reader.readLine());
return a;
}
public static boolean readBoolean() throws Exception {
//在此编写你的代码
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
boolean a = Boolean.parseBoolean(reader.readLine());
return a;
}
public static void main(String[] args) {
}
}
做对了但是感觉可以单独发在 CSDN
L6L8 计算器
创建 Calculator 类,其中包含 5 个 static 方法:int plus(int a, int b) - 返回 a 和 b 的总和int minus(int a, int b) - 返回 a 和 b 之间的差int multiply(int a, int b) - 返回 a 和 b 的乘积double divide(int a, int b) - 返回 a 除以 b 的结果double percent(int a, int b) - 返回数字 a 的 b%
Requirements:
-
- plus 方法必须返回 a 和 b 的总和。
-
- minus 方法必须返回 a 和 b 之间的差。
-
- multiply 方法必须返回 a 和 b 的乘积。
-
- divide 方法必须返回 a 除以 b 的结果。
-
- percent 方法必须返回数字 a 的 b%。
public class Calculator {
public static int plus(int a, int b) {
//在此编写你的代码
return a+b;
}
public static int minus(int a, int b) {
//在此编写你的代码
return a-b;
}
public static int multiply(int a, int b) {
//在此编写你的代码
return a*b;
}
public static double divide(int a, int b) {
//在此编写你的代码
return a*1.0/b;
}
public static double percent(int a, int b) {
//在此编写你的代码
return a*1.0*b/100;
}
public static void main(String[] args) {
}
}
L6L11 Static 猫(构造方法并引用)
1.在 Cat 类中,添加 public static ArrayList 变量 cats。2.每次创建一只新猫(新的 Cat 对象)时,都将其添加到变量 cats。创建 10 个 Cat 对象。3.printCats 方法应在屏幕上显示所有的猫。你需要使用变量 cats。
Requirements:
- 向 Cat 类中添加 public static ArrayList 变量 cats。
- 变量 cats 必须被初始化。
- main 方法应创建 10 个 Cat 对象(使用 Cat() 构造方法)。
- main 方法必须将创建的所有猫添加到变量 cats。
- printCats 方法应显示变量 cats 中的所有 Cat 对象。每行显示一个对象。
public class Solution {
public static void main(String[] args) {
// 创建 10 个 Cat 对象
Cat a1 = new Cat();
Cat a2 = new Cat();
Cat a3 = new Cat();
Cat a4 = new Cat();
Cat a5 = new Cat();
Cat a6 = new Cat();
Cat a7 = new Cat();
Cat a8 = new Cat();
Cat a9 = new Cat();
Cat a10 = new Cat();
// 显示变量 catCount 的值
System.out.println(Cat.catCount);
}
public static class Cat {
// 创建 static 变量 catCount
public static int catCount;
;
// 声明构造方法
public Cat() {
catCount++;
}
}
}
L6L11 最小数量的 static
放入使代码开始工作和程序成功完成所需的最小数量的 static 修饰符。
Requirements:
-
- 不要更改方法实现或访问修饰符。
-
- 将 static 修饰符添加到所需位置。
-
- 程序只能包含 4 个 static 修饰符。
-
- 程序应在屏幕上显示文本。
public class Solution {
public static int step;
public static void main(String[] args) {
method1();
}
public static void method1() {
method2();
}
public static void method2() {
new Solution().method3();
}
public void method3() {
method4();
}
public void method4() {
step++;
for (StackTraceElement element : Thread.currentThread().getStackTrace())
System.out.println(element);
if (step > 1)
return;
main(null);
}
}
L6L11 新想法的记事本
1.在 Solution 类中,创建 public static Idea 类2.在 Idea 类中,声明返回任意非空字符串的 public String getDescription() 方法3.在 Solution 类中,创建将显示想法描述(getDescription 方法返回的内容)的 static public void printIdea(Idea idea) 方法
Requirements:
-
- 在 Solution 类中,创建 public static Idea 类。
-
- 在 Idea 类中,创建 public String getDescription() 方法。
-
- getDescription 方法必须返回任意非空字符串。
-
- 在 Solution 类中,创建 public static void printIdea(Idea idea) 方法。
-
- printIdea 方法应在屏幕上显示想法描述。
public class Solution {
public static void main(String[] args) {
printIdea(new Idea());
}
//在此编写你的代码
public static class Idea{
public String getDescription(){
return "123";
}
}
public static void printIdea(Idea idea){
System.out.println(idea.getDescription());
}
}
L6L11 static 猫
1.在 Cat 类中,添加 public static ArrayList 变量 cats。2.每次创建一只新猫(新的 Cat 对象)时,都将其添加到变量 cats。创建 10 个 Cat 对象。3.printCats 方法应在屏幕上显示所有的猫。你需要使用变量 cats。
Requirements:
-
- 向 Cat 类中添加 public static ArrayList 变量 cats。
-
- 变量 cats 必须被初始化。
-
- main 方法应创建 10 个 Cat 对象(使用 Cat() 构造方法)。
-
- main 方法必须将创建的所有猫添加到变量 cats。
-
- printCats 方法应显示变量 cats 中的所有 Cat 对象。每行显示一个对象。
public class Cat {
//在此编写你的代码
public static ArrayList<Cat> cats = new ArrayList<>();
public Cat() {
}
public static void main(String[] args) {
//在此编写你的代码
for (int i = 0; i < 10; i++) {
cats.add(new Cat());
}
printCats();
}
public static void printCats() {
//在此编写你的代码
for (int i = 0; i < cats.size(); i++) {
System.out.println(cats.get(i));
}
}
}
Arraylist使用方法
https://blog.csdn.net/fu_manxing/article/details/52186927
Arraylist 对比 vector linkedlist vector
https://blog.csdn.net/qq_35190492/article/details/103883964
Arraylist 介绍
https://juejin.cn/post/6844903566331609096
L6L11 升序数字
**任务:**编写程序,该程序从键盘读取 5 个数字,并按升序显示这些数字。
示例输入:3215617
示例输出:2361517
Requirements:
-
- 程序应从键盘读取 5 个数字。
-
- 程序应显示 5 个数字,每行显示一个。
-
- 输出内容必须包含与输入相同的数字(顺序不重要)。
-
- 所显示的数字应按升序排序。
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//在此编写你的代码
int a = Integer.parseInt(reader.readLine());
int b = Integer.parseInt(reader.readLine());
int c = Integer.parseInt(reader.readLine());
int d = Integer.parseInt(reader.readLine());
int e = Integer.parseInt(reader.readLine());
int[] arr = {a,b,c,d,e};
sort(arr);
for (int x : arr) {
System.out.println(x);
}
}
public static void sort(int[] array) {
for (int i = 0; i < array.length; i++)
for (int j = i; j < array.length; j++)
if (array[i] > array[j]) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
冒泡排序