Skip to content

Commit 846736e

Browse files
committed
Add ch07 code
1 parent 7e907d2 commit 846736e

File tree

108 files changed

+1978
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+1978
-0
lines changed

code/chapter07/A.class

893 Bytes
Binary file not shown.

code/chapter07/A01.class

436 Bytes
Binary file not shown.

code/chapter07/A02.class

416 Bytes
Binary file not shown.

code/chapter07/A03.class

329 Bytes
Binary file not shown.

code/chapter07/AA.class

727 Bytes
Binary file not shown.

code/chapter07/ABC.class

183 Bytes
Binary file not shown.

code/chapter07/B.class

814 Bytes
Binary file not shown.

code/chapter07/Book.class

897 Bytes
Binary file not shown.

code/chapter07/Cale.class

796 Bytes
Binary file not shown.

code/chapter07/Car.class

296 Bytes
Binary file not shown.

code/chapter07/Cat.class

1.03 KB
Binary file not shown.

code/chapter07/Circle.class

446 Bytes
Binary file not shown.

code/chapter07/Constructor01.class

856 Bytes
Binary file not shown.

code/chapter07/Constructor01.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
public class Constructor01 {
3+
4+
//编写一个main方法
5+
public static void main(String[] args) {
6+
//当我们new 一个对象时,直接通过构造器指定名字和年龄
7+
Person p1 = new Person("smith", 80);
8+
System.out.println("p1的信息如下");
9+
System.out.println("p1对象name=" + p1.name);//smith
10+
System.out.println("p1对象age=" + p1.age);//80
11+
}
12+
}
13+
14+
//在创建人类的对象时,就直接指定这个对象的年龄和姓名
15+
//
16+
class Person {
17+
String name;
18+
int age;
19+
//构造器
20+
//老韩解读
21+
//1. 构造器没有返回值, 也不能写void
22+
//2. 构造器的名称和类Person一样
23+
//3. (String pName, int pAge) 是构造器形参列表,规则和成员方法一样
24+
public Person(String pName, int pAge) {
25+
System.out.println("构造器被调用~~ 完成对象的属性初始化");
26+
name = pName;
27+
age = pAge;
28+
}
29+
}
435 Bytes
Binary file not shown.

code/chapter07/ConstructorDetail.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
public class ConstructorDetail {
3+
4+
//编写一个main方法
5+
public static void main(String[] args) {
6+
Person p1 = new Person("king", 40);//第1个构造器
7+
Person p2 = new Person("tom");//第2个构造器
8+
9+
Dog dog1 = new Dog();//使用的是默认的无参构造器
10+
11+
}
12+
}
13+
class Dog {
14+
//如果程序员没有定义构造器,系统会自动给类生成一个默认无参构造器(也叫默认构造器)
15+
//使用javap指令 反编译看看
16+
/*
17+
默认构造器
18+
Dog() {
19+
20+
}
21+
*/
22+
//一旦定义了自己的构造器,默认的构造器就覆盖了,就不能再使用默认的无参构造器,
23+
//除非显式的定义一下,即: Dog(){} 写 (这点很重要)
24+
//
25+
public Dog(String dName) {
26+
//...
27+
}
28+
Dog() { //显式的定义一下 无参构造器
29+
30+
}
31+
}
32+
33+
class Person {
34+
String name;
35+
int age;//默认0
36+
//第1个构造器
37+
public Person(String pName, int pAge) {
38+
name = pName;
39+
age = pAge;
40+
}
41+
//第2个构造器, 只指定人名,不需要指定年龄
42+
public Person(String pName) {
43+
name = pName;
44+
}
45+
}
894 Bytes
Binary file not shown.
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
public class ConstructorExercise {
3+
4+
//编写一个main方法
5+
public static void main(String[] args) {
6+
Person p1 = new Person();//无参构造器
7+
8+
//下面输出 name = null, age = 18
9+
System.out.println("p1的信息 name=" + p1.name + " age=" + p1.age);
10+
11+
Person p2 = new Person("scott", 50);
12+
//下面输出 name = scott, age = 50
13+
System.out.println("p2的信息 name=" + p2.name + " age=" + p2.age);
14+
15+
}
16+
}
17+
18+
/**
19+
* 在前面定义的Person类中添加两个构造器:
20+
* 第一个无参构造器:利用构造器设置所有人的age属性初始值都为18
21+
* 第二个带pName和pAge两个参数的构造器:
22+
* 使得每次创建Person对象的同时初始化对象的age属性值和name属性值。
23+
* 分别使用不同的构造器,创建对象.
24+
*/
25+
26+
class Person {
27+
String name;//默认值 null
28+
int age;//默认 0
29+
//第一个无参构造器:利用构造器设置所有人的age属性初始值都为18
30+
public Person() {
31+
age = 18;//
32+
}
33+
//第二个带pName和pAge两个参数的构造器
34+
public Person(String pName, int pAge) {
35+
name = pName;
36+
age = pAge;
37+
}
38+
}

code/chapter07/Demo.class

677 Bytes
Binary file not shown.

code/chapter07/Dog.class

829 Bytes
Binary file not shown.

code/chapter07/Employee.class

622 Bytes
Binary file not shown.

code/chapter07/HanoiTower.class

336 Bytes
Binary file not shown.

code/chapter07/HanoiTower.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
public class HanoiTower {
3+
4+
//编写一个main方法
5+
public static void main(String[] args) {
6+
7+
Tower tower = new Tower();
8+
tower.move(64, 'A', 'B', 'C');
9+
}
10+
}
11+
12+
class Tower {
13+
14+
//方法
15+
//num 表示要移动的个数, a, b, c 分别表示A塔,B 塔, C 塔
16+
public void move(int num , char a, char b ,char c) {
17+
//如果只有一个盘 num = 1
18+
if(num == 1) {
19+
System.out.println(a + "->" + c);
20+
} else {
21+
//如果有多个盘,可以看成两个 , 最下面的和上面的所有盘(num-1)
22+
//(1)先移动上面所有的盘到 b, 借助 c
23+
move(num - 1 , a, c, b);
24+
//(2)把最下面的这个盘,移动到 c
25+
System.out.println(a + "->" + c);
26+
//(3)再把 b塔的所有盘,移动到c ,借助a
27+
move(num - 1, b, a, c);
28+
}
29+
}
30+
}

code/chapter07/Homework01.class

963 Bytes
Binary file not shown.

code/chapter07/Homework01.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
public class Homework01 {
3+
4+
//编写一个main方法
5+
public static void main(String[] args) {
6+
A01 a01 = new A01();
7+
double[] arr = {1, 1.4, -1.3, 89.8, 123.8 , 66}; //;{};
8+
Double res = a01.max(arr);
9+
if(res != null) {
10+
System.out.println("arr的最大值=" + res);
11+
} else {
12+
System.out.println("arr的输入有误, 数组不能为null, 或者{}");
13+
}
14+
}
15+
}
16+
/*
17+
编写类A01,定义方法max,实现求某个double数组的最大值,并返回
18+
19+
思路分析
20+
1. 类名 A01
21+
2. 方法名 max
22+
3. 形参 (double[])
23+
4. 返回值 double
24+
25+
先完成正常业务,然后再考虑代码健壮性
26+
*/
27+
class A01 {
28+
public Double max(double[] arr) {
29+
//老韩先判断arr是否为null,然后再判断 length 是否>0
30+
if( arr!= null && arr.length > 0 ) {
31+
32+
//保证arr至少有一个元素
33+
double max = arr[0];//假定第一个元素就是最大值
34+
for(int i = 1; i < arr.length; i++) {
35+
if(max < arr[i]) {
36+
max = arr[i];
37+
}
38+
}
39+
40+
return max;//double
41+
} else {
42+
return null;
43+
}
44+
}
45+
}

code/chapter07/Homework02.class

821 Bytes
Binary file not shown.

code/chapter07/Homework02.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
public class Homework02 {
3+
4+
//编写一个main方法
5+
public static void main(String[] args) {
6+
7+
String[] strs = {"jack", "tom", "mary","milan"};
8+
A02 a02 = new A02();
9+
int index = a02.find("milan", strs);
10+
System.out.println("查找的index=" + index);
11+
}
12+
}
13+
14+
//编写类A02,定义方法find,实现查找某字符串是否在字符串数组中,
15+
//并返回索引,如果找不到,返回-1
16+
//分析
17+
//1. 类名 A02
18+
//2. 方法名 find
19+
//3. 返回值 int
20+
//4. 形参 (String , String[])
21+
//
22+
//自己补充代码健壮性
23+
class A02 {
24+
25+
public int find(String findStr, String[] strs) {
26+
//直接遍历字符串数组,如果找到,则返回索引
27+
for(int i = 0; i < strs.length; i++) {
28+
if(findStr.equals(strs[i])) {
29+
return i;
30+
}
31+
}
32+
//如果没有,就返回-1
33+
return -1;
34+
}
35+
}

code/chapter07/Homework03.class

419 Bytes
Binary file not shown.

code/chapter07/Homework03.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
public class Homework03 {
3+
4+
//编写一个main方法
5+
public static void main(String[] args) {
6+
7+
//测试
8+
Book book = new Book("笑傲江湖", 300);
9+
book.info();
10+
book.updatePrice();//更新价格
11+
book.info();
12+
}
13+
}
14+
/*
15+
编写类Book, 定义方法updatePrice,实现更改某本书的价格,
16+
具体:如果价格>150,则更改为150,如果价格>100,更改为100,否则不变
17+
18+
分析
19+
1. 类名 Book
20+
2. 属性 price, name
21+
3. 方法名 updatePrice
22+
4. 形参 ()
23+
5. 返回值 void
24+
6. 提供一个构造器
25+
*/
26+
27+
class Book {
28+
String name;
29+
double price;
30+
public Book(String name, double price) {
31+
this.name = name;
32+
this.price = price;
33+
}
34+
public void updatePrice() {
35+
//如果方法中,没有 price 局部变量, this.price 等价 price
36+
if(price > 150) {
37+
price = 150;
38+
} else if(price > 100 ) {
39+
price = 100;
40+
}
41+
}
42+
43+
//显示书籍情况
44+
public void info() {
45+
System.out.println("书名=" + this.name + " 价格=" + this.price);
46+
}
47+
}

code/chapter07/Homework04.class

883 Bytes
Binary file not shown.

code/chapter07/Homework04.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
public class Homework04 {
3+
4+
//编写一个main方法
5+
public static void main(String[] args) {
6+
int[] oldArr = {10, 30, 50};
7+
A03 a03 = new A03();
8+
int[] newArr = a03.copyArr(oldArr);
9+
//遍历newArr,验证
10+
System.out.println("==返回的newArr元素情况==");
11+
for(int i = 0; i < newArr.length; i++) {
12+
System.out.print(newArr[i] + "\t");
13+
}
14+
}
15+
}
16+
17+
/*
18+
编写类A03, 实现数组的复制功能copyArr,输入旧数组,返回一个新数组,元素和旧数组一样
19+
*/
20+
class A03 {
21+
public int[] copyArr(int[] oldArr) {
22+
//在堆中,创建一个长度为 oldArr.length 数组
23+
int[] newArr = new int[oldArr.length];
24+
//遍历 oldArr,将元素拷贝到 newArr
25+
for(int i = 0; i < oldArr.length; i++) {
26+
newArr[i] = oldArr[i];
27+
}
28+
29+
return newArr;
30+
31+
}
32+
}

code/chapter07/Homework05.class

764 Bytes
Binary file not shown.

code/chapter07/Homework05.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
public class Homework05 {
3+
4+
//编写一个main方法
5+
public static void main(String[] args) {
6+
Circle circle = new Circle(3);
7+
System.out.println("面积=" + circle.area());
8+
System.out.println("周长=" + circle.len());
9+
}
10+
}
11+
/*
12+
定义一个圆类Circle, 定义属性:半径,提供显示圆周长功能的方法, 提供显示圆面积的方法
13+
*/
14+
class Circle {
15+
double radius;
16+
17+
public Circle(double radius) {
18+
this.radius = radius;
19+
}
20+
21+
public double area() { //面积
22+
return Math.PI * radius * radius;
23+
}
24+
25+
public double len() { //周长
26+
return 2 * Math.PI * radius;
27+
}
28+
}

code/chapter07/Homework06.class

1.01 KB
Binary file not shown.

0 commit comments

Comments
 (0)