第一章-对象和封装

上机练习1-5答案略

简答题4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class Student1 {
String name; //姓名
private int age; //年龄

public int getAge() {
return age;
}

public void setAge(int age) {
if (age>=16) {
this.age = age;
} else {
System.out.println("输入错误!");
}
}

public void print() {
System.out.println("该学生姓名为:" + name + ",年龄为:" + this.getAge());
}
}

class Student1Test {
public static void main(String[] args) {
//第一章简答题4(第32页)
Student1 s = new Student1();
s.name = "小黑";
s.setAge(10);
s.print();

Student1 s1 = new Student1();
s1.name = "小白";
s1.setAge(16);
s1.print();
}
}

简答题5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class Student2 {
String name; //姓名
int age; //年龄
String sex; //性别
String subject; //专业

public void print() {
System.out.println("该学生姓名为:" + name + ",年龄为:" + age + ",性别为:" + sex + ",专业是:" + subject);
}

public Student2(String name,int age) {
this.sex = "男";
this.subject = "ACCP";
this.name = name;
this.age = age;
this.print();
}

public Student2(String name,int age,String sex,String subject) {
this.name = name;
this.age = age;
this.sex = sex;
this.subject = subject;
this.print();
}

}
class Student2Test {
public static void main(String[] args) {
//第一章简答题5(第32页)
Student2 s1 = new Student2("张三", 18);
Student2 s2 = new Student2("李四", 18,"男","计算机");
}
}

第二章-继承

上机练习1

宠物类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.lx1;

/**
* 宠物类
*/
public class Pet {
private String name; //名字
private int health; //健康值
private int love; //亲密度

public String getName() {
return name;
}

public int getHealth() {
return health;
}

public int getLove() {
return love;
}

//打印输出
public void print() {
System.out.println("宠物名字是:" + getName() + ",健康值为:" + getHealth() + ",亲密度为:" + getLove());
}

//默认构造方法
public Pet() {}

//有参构造方法
public Pet(String name) {
this.name = name;
this.health = 100;
this.love = 50;
}
}

狗类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.lx1;

public class Dog extends Pet{
private String strain;//简介

public String getStrain() {
return strain;
}

/*构造方法*/
public Dog(String name, String strain) {
super(name);//调用父类构造方法,传递参数覆盖父类属性
this.strain = strain;
}

@Override
public void print() {
System.out.println("名字是:" + getName() + ",健康值为:" + getHealth() + ",亲密度为:" + getLove() + ",简介为:" + getStrain());
}
}

企鹅类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.lx1;

public class Penguin extends Pet{
private String sex;//性别

public String getSex() {
return sex;
}

public Penguin(String name,String sex) {
super(name);
this.sex = sex;
}

@Override
public void print() {
System.out.println("名字是:" + getName() + ",健康值为:" + getHealth() + ",亲密度为:" + getLove() + ",性别为:" + getSex());
}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.lx1;

public class Test {
public static void main(String[] args) {
//第二章上机练习1(第48页)
Pet pet = new Pet("贝贝");
pet.print();

Dog d = new Dog("小黑","聪明的拉布拉多");
d.print();

Penguin p = new Penguin("小白","Q妹");
p.print();
}
}

上机练习2

父类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.lx2;

/**
* 宠物类:抽象类,必须被继承
*/
public abstract class Pet {
private String name; //名字
private int health; //健康值
private int love; //亲密度

public String getName() {
return name;
}

public int getHealth() {
return health;
}

public int getLove() {
return love;
}

//抽象方法:必须被重写
public abstract void print();

//默认构造方法
public Pet() {}

//有参构造方法
public Pet(String name) {
this.name = name;
this.health = 100;
this.love = 50;
}
}

子类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.lx2;

public class Dog extends Pet {
private String strain; //简介

public String getStrain() {
return strain;
}

public Dog(String name, String strain) {
super(name);
this.strain = strain;
}

@Override
public void print() {
System.out.println("名字是:" + getName() + ",健康值为:" + getHealth() + ",亲密度为:" + getLove() + ",简介为:" + getStrain());
}
}

测试类

1
2
3
4
5
6
7
8
9
10
package com.lx2;

public class Test {
public static void main(String[] args) {
//第二章上机练习2(第52页)
Dog d = new Dog("小黑","聪明的拉布拉多");
d.print();
}
}

综合练习

汽车类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.zh;

/**
* 汽车抽象类,必须被继承
*/
public abstract class MotoVehicle {
private String no; // 汽车牌号
private String brand; // 汽车品牌
/**
* 无参构造方法。
*/
public MotoVehicle() {}
/**
* 有参构造方法。
* @param no 汽车牌号
* @param brand 汽车品牌
*/
public MotoVehicle(String no, String brand) {
this.no = no;
this.brand = brand;
}

/**
* 抽象方法,计算汽车租赁价。
* */
public abstract int calRent(int days);
}

客车类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.zh;

/**
* 客车类,继承汽车类,final修饰该方法的作用是使该方法不能再被继承。
*/
public final class Bus extends MotoVehicle {
private int seatCount; //座位数
//默认构造方法
public Bus() {}
//带参构造方法
public Bus(String no, String brand, int seatCount) {
super(no, brand);
this.seatCount = seatCount;
}
/**
* 计算客车租赁价
*/
public int calRent(int days) {
if (seatCount <= 16) {//当座位数<=16时,800一天
return days * 800;
} else {//当座位数>16时,1500一天
return days * 1500;
}
}
}

轿车类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.zh;

/**
* 轿车类,继承汽车类,final修饰该方法的作用是使该方法不能再被继承。
*/
public final class Car extends MotoVehicle {
private String type;// 汽车型号
//默认构造方法
public Car() {}
//带参构造方法
public Car(String no, String brand, String type) {
super(no, brand);//调用父类的带参构造方法进行传参
this.type = type;
}
/**
* 计算轿车租赁价
*/
public int calRent(int days) {
if ("1".equals(type)) {// 代表550i,500一天
return days * 500;
} else if ("2".equals(type)) {// 2代表商务舱GL8,600一天
return 600 * days;
} else {//林荫大道 300一天
return 300 * days;
}
}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.zh;

import java.util.Scanner;
/**
* 测试类。
*/
public class TestRent {
public static void main(String[] args) {
String no; //车牌号
String brand; //汽车品牌编号

int money; //租赁总钱数

Scanner input = new Scanner(System.in);
System.out.println("欢迎您来到汽车租赁公司!");

System.out.print("请输入要租赁的天数:");
int days = input.nextInt();

System.out.print("请输入要租赁的汽车类型(1:轿车\t2、客车):");
String mtype = input.next();

if("1".equals(mtype)){
System.out.print("请输入要租赁的汽车品牌(1、宝马\t2、别克):");
brand = input.next();
System.out.print("请输入轿车的型号:");
if("1".equals(brand)) {
System.out.print("1、550i:");
} else {
System.out.print("2、商务舱GL8\t3、林荫大道");
}
String type = input.next();
no = "京BK5543";//直接指定汽车牌号
System.out.println("分配给您的汽车牌号是:" + no);
Car c = new Car(no,brand,type);
money = c.calRent(days);//调用轿车计算租金方法
} else {
System.out.print("请输入要租赁的客车品牌(1、金杯\t2、金龙):");
brand = input.next();
System.out.print("请输入客车的座位数:");
int seatCount = input.nextInt();
no = "京AU8769";//直接指定汽车牌号
System.out.println("分配给您的汽车牌号是:" + no);
Bus b = new Bus(no,brand,seatCount);//实例化bus对象
money = b.calRent(days);//调用bus的计算租金方法
}
System.out.println("\n顾客您好!您需要支付的租赁费用是" + money + "。");
}
}

简答题5

动物类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.jd5;

/*抽象类:强制被继承*/
public abstract class Animal {
private int age;//属性:年龄

/*取年龄的值*/
public int getAge() {
return age;
}

/*给年龄赋值*/
public void setAge(int age) {
this.age = age;
}

/*构造方法:设置年龄*/
public Animal(int age) {
setAge(age);
}

/*抽象方法:强制被重写*/
public abstract void info();
}

鸟类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.jd5;

/*鸟类:继承Animal类*/
public class Bird extends Animal{
private String color;//鸟类特有属性:颜色

/*得到颜色的值*/
public String getColor() {
return color;
}

/*给属性颜色赋值*/
public void setColor(String color) {
this.color = color;
}

/*鸟类构造方法*/
public Bird(int age, String color) {
super(age);//调用父类的构造方法
setColor(color);
}

/*重写方法*/
@Override
public void info() {
System.out.println("我是一只" + getColor() + "的鸟");
System.out.println("我今年" + super.getAge() + "岁了!");
}
}

鱼类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.jd5;

/*鱼类:继承Animal类*/
public class Fish extends Animal{
private String weight; //鱼类特有属性:重量

//得到重量的值
public String getWeight() {
return weight;
}

//给重量赋值
public void setWeight(String weight) {
this.weight = weight;
}

//鱼类构造方法
public Fish(int age, String weight) {
super(age); //调用父类的构造方法
setWeight(weight);
}

/*重写方法*/
@Override
public void info() {
System.out.println("我是一只" + getWeight() + "的鱼");
System.out.println("我今年" + super.getAge() + "岁了!");
}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.jd5;

public class Test {
public static void main(String[] args) {
//第二章简答题5(第61页)
//实例化鸟类
Bird b = new Bird(4,"红色");
b.info();

System.out.println();
//实例化鱼类
Fish f = new Fish(2,"五斤重");
f.info();
}
}

第三章-多态

简答题5

1
2
3
4
5
6
7
8
package com.jd5;

/*打印机类,作为父类*/
public abstract class Printer {

public abstract void print();
}

1
2
3
4
5
6
7
8
9
package com.jd5;

public class InkpetPrinter extends Printer{
@Override
public void print() {
System.out.println("这是喷墨打印机。");
}
}

1
2
3
4
5
6
7
8
9
10
package com.jd5;

public class DotMatrixPrinter extends Printer{

@Override
public void print() {
System.out.println("这是针式打印机。");
}
}

1
2
3
4
5
6
7
8
9
package com.jd5;

public class LaserPrinter extends Printer{
@Override
public void print() {
System.out.println("这是激光打印机。");
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.jd5;

public class Test {
public static void main(String[] args) {
//第三章简答题5(第88页)
Printer p1 = new DotMatrixPrinter();
p1.print();
Printer p2 = new InkpetPrinter();
p2.print();
Printer p3 = new LaserPrinter();
p3.print();
}
}

简答题6

朋友类,作为父类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.jd6;

public abstract class Friend {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Friend(String name) {
setName(name);
}

public abstract void eat();

public abstract void play();
}

朋友1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.jd6;

public class Fd1 extends Friend{
public Fd1(String name) {
super(name);
}

@Override
public void eat() {
System.out.println(getName());
System.out.print("给你做披萨。");
}

@Override
public void play() {
System.out.println("去打橄榄球。");
}
}

朋友2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.jd6;

public class Fd2 extends Friend{

public Fd2(String name) {
super(name);
}

@Override
public void eat() {
System.out.println(getName());
System.out.print("给你做四川凉菜。");
}

@Override
public void play() {
System.out.print("去练太极拳。");
}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.jd6;

public class Test {
public static void main(String[] args) {
//第三章简答题6(第89页)
Friend f1 = new Fd1("约翰");
Friend f2 = new Fd2("王小强");

f1.eat();
f1.play();
System.out.println();

f2.eat();
f2.play();
}
}

第四章-接口

静等更新~~ ~~

第五章-异常

上机练习1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.lx1;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Test {
public static void main(String[] args) {
//第五章上机练习1(第129页)
Scanner sc = new Scanner(System.in);
System.out.print("请输入课程代号(1~3之间数字):");

try {
int num = sc.nextInt();
switch (num) {
case 1:
System.out.println("C#编程");
break;
case 2:
System.out.println("Java编程");
break;
case 3:
System.out.println("MySQL编程");
break;
default:
System.out.println("输入有误!");
break;
}
} catch (InputMismatchException x) {
System.out.println("请输入数字!");
return;
} catch (Exception x) {
System.out.println("未知异常!");
}

System.out.println("欢迎提出建议!");
}
}

上机练习2

用户类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.lx2;

public class Person {
private String name; //姓名
private int age; //年龄
private String sex = "女"; //性别

public void setAge(int age) throws Exception {
if (!(age < 1 || age > 100)) {
this.age = age;
} else {
throw new Exception("年龄必须在1~100之间!");
}
}

public void print() {
System.out.println("姓名是:" + this.name + "年龄是:" + this.age + "性别是:" + this.sex);
}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.lx2;

import org.apache.log4j.Logger;

public class TestException {
public static void main(String[] args) {
//第五章上机练习2(第135页)
Logger log = Logger.getLogger(TestException.class);
Person p = new Person();
try {
p.setAge(120);
p.print();
} catch (Exception x) {
x.printStackTrace();
log.error("输入有误!");
}
}
}

上机练习3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.lx3;

import org.apache.log4j.Logger;

import java.util.InputMismatchException;
import java.util.Scanner;

public class TestLog1 {
public static void main(String[] args) {
//第五章上机练习3(第141页)
Scanner sc = new Scanner(System.in);
Logger log = Logger.getLogger(TestLog1.class);
try {
System.out.print("请输入被除数:");
int num1 = sc.nextInt();
System.out.print("请输入除数:");
int num2 = sc.nextInt();
System.out.println("结果为:" + (num1 / num2));
} catch (ArithmeticException e) {
log.warn("非法参数");
// throw new ArithmeticException("除数不能为零!");
e.printStackTrace();
} catch (InputMismatchException e) {
throw new InputMismatchException("请输入整数!");
} catch (Exception e) {
System.out.println("未知错误!");
} finally {
System.out.println("感谢您使用本程序!");
}
}
}

简答题3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.jd3;

public class TestJd3 {
public static void main(String[] args) {
//第五章简答题3(第145页)
int[] nums = {1,2,3,4,5};

try {
for (int i = 0; i <= nums.length; i++) {
System.out.print(nums[i] + "\t");
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("\n数组下标越界异常。");
e.printStackTrace();
}
}
}

简答题5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.jd5;

import org.apache.log4j.Logger;

public class TestJd5 {
public static void main(String[] args) {
//第五章简答题5(第145页)
Logger log = Logger.getLogger(TestJd5.class);
int[] nums = {1,2,3,4,5};

try {
for (int i = 0; i <= nums.length; i++) {
System.out.print(nums[i] + "\t");
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println();
log.error("数组下标越界异常!");
e.printStackTrace();
}
}
}

第六章-集合框架

企鹅类统一代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.lx1;

public class Penguin {
private String name;
private String strain;

public Penguin(String name, String strain) {
setName(name);
setStrain(strain);
}

public void print() {
System.out.println("我叫:" + name + "简介是:" + strain);
}


public void setName(String name) {
this.name = name;
}


public void setStrain(String strain) {
this.strain = strain;
}
}

上机练习1

企鹅类引自上文

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.lx1;

import java.util.ArrayList;
import java.util.List;

public class Test {
public static void main(String[] args) {
//第六章上机练习1(第155页)
List<Penguin> list = new ArrayList<Penguin>();

Penguin p1 = new Penguin("小白","可爱的小白");
Penguin p2 = new Penguin("小黑","可爱的小黑");
Penguin p3 = new Penguin("小黄","可爱的小黄");

//添加企鹅到list集合
list.add(p1);
list.add(p2);
list.add(p3);

//查看企鹅数量
System.out.println("企鹅的数量是:" + list.size());

//遍历所有企鹅信息
for (Penguin p : list) {
p.print();
}
System.out.println();

//删除企鹅信息
list.remove(0);
for (Penguin p : list) {
p.print();
}
System.out.println();

//判断企鹅中是否包含指定企鹅
if (list.contains(p3)) {
System.out.println("包含此企鹅!");
} else {
System.out.println("抱歉。不包含此企鹅!");
}
}
}

上机练习2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.lx2;

import java.util.*;

public class Test {
public static void main(String[] args) {
//第六章上机练习2(第159页)
Map<String, String> m = new HashMap<String, String>();
Scanner sc = new Scanner(System.in);

//进行赋值
m.put("小白","聪明的小白");
m.put("小黑","聪明的小黑");
m.put("小黄","聪明的小黄");

System.out.print("请输入宠物昵称:");
String name = sc.next();

if (m.containsKey(name)) {//若查找到宠物
System.out.println(m.get(name));//则打印宠物信息
} else {
System.out.println("抱歉。不包含此宠物!");
}
}
}

上机练习3

企鹅类引自上文

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.lx3;

import java.util.*;

public class Test {
public static void main(String[] args) {
//第六章上机练习3(第161页)
//使用HashMap存储多个企鹅信息,然后统一使用Iterator进行遍历
Map<String,String> penguin = new HashMap<String,String>();
penguin.put("小白", "可爱的小白");
penguin.put("小黑", "可爱的小黑");
penguin.put("小黄", "可爱的小黄");
//使用Iterator迭代器遍历集合,需要先使用Set 存储集合数据
//得到所有键
Set<String> keys = penguin.keySet();
Iterator<String> it = keys.iterator();
//使用Iterator遍历
System.out.println("宠物信息是:");
while(it.hasNext()){
String key = it.next();
String value = penguin.get(key);
System.out.println(key + "\t" + value);
}
}
}

上机练习4

企鹅类引自上文

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.lx4;

import java.util.*;

public class Test {
public static void main(String[] args) {
//第六章上机练习4(第166页)
//使用HashMap存储多个企鹅信息,然后统一使用Iterator进行遍历
Map<String, String> penguin = new HashMap<String, String>();
penguin.put("小白", "可爱的小白");
penguin.put("小黑", "可爱的小黑");
penguin.put("小黄", "可爱的小黄");
//使用Iterator迭代器遍历集合,需要先使用Set 存储集合数据
//得到所有键
Set<String> keys = penguin.keySet();
Iterator<String> it = keys.iterator();
//使用Iterator遍历
System.out.println("使用iterator遍历宠物信息是:");
int num = 0;
while (it.hasNext()) {
String key = it.next();
String value = penguin.get(key);
System.out.println((num + 1) + key + "\t" + value);
num++;
}

System.out.println();
System.out.println("用foreach遍历宠物信息是:");
num = 0;
for (String key : keys) {
String value = penguin.get(key);
System.out.println((num + 1) + key + "\t" + value);
num++;
}

}
}


简答题5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.jd5;

import java.util.*;

public class TestJd5 {
public static void main(String[] args) {
//第六章简答题5(第169页)
Map<String, Integer> m = new HashMap<String, Integer>();
Scanner sc = new Scanner(System.in);
m.put("张三", 75);
m.put("李四", 85);
m.put("王五", 50);

System.out.println(m.keySet());//得到键值
System.out.println(m.values());//得到值


System.out.print("请输入学员昵称:");
String name = sc.next();

if (m.containsKey(name)) {
System.out.print("请输入要修改的成绩是:");
int score = sc.nextInt();
m.put(name,score);
System.out.println(m.keySet());
System.out.println(m.values());
} else {
System.out.println("抱歉。没有此成员信息!");
}

}
}

第七章-多线程

上机练习1

需求说明

创建两个线程。每个线程均输出20次信息文字,”你好“,线程名。并观察多个线程交替执行的过程,运行结果如图所示:

上机练习1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.lx1;

public class MyThread extends Thread{

@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println((i + 1) + ",你好,来自线程" + Thread.currentThread().getName());
}
}

public static void main(String[] args) {
//上机练习1(第181页)
MyThread t = new MyThread();
t.start();

MyThread t2 = new MyThread();
t2.start();
}
}

上机练习2

需求说明

修改上机练习1,要求线程类使用实现Runnable接口的方式创建,并修改测试类。运行结果如上机练习1所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.lx2;

public class MyThread implements Runnable{

@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println((i + 1) + ",你好,来自线程" + Thread.currentThread().getName());
}
}

public static void main(String[] args) {
//上机练习2(第182页)
MyThread m = new MyThread();
Thread t = new Thread(m);
Thread t2 = new Thread(m);

t.start();
t2.start();
}
}

上机练习4

需求说明

任意创建一个线程类,在测试类中创建子程序对象,显示主线程、子线程默认优先级,将主线程设置为最高优先级、子线程设置为最低优先级并显示。运行结果如图所示:

上机练习4

线程类

1
2
3
4
5
6
7
8
package com.lx4;

public class ThreadMe extends Thread{

@Override
public void run() {}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.lx4;

public class Test {
public static void main(String[] args) {
//上机练习4(第192页)
ThreadMe td1 = new ThreadMe(); //子线程
Thread t = Thread.currentThread(); //主线程

System.out.println("--------显示默认优先级--------");
System.out.println("主线程名:" + t.getName() + ",优先级:" + t.getPriority());
System.out.println("子线程名:" + td1.getName() + ",优先级:" + td1.getPriority());

t.setPriority(Thread.MAX_PRIORITY);
td1.setPriority(Thread.MIN_PRIORITY);

System.out.println("--------修改默认优先级后--------");
System.out.println("主线程名:" + t.getName() + ",优先级:" + t.getPriority());
System.out.println("子线程名:" + td1.getName() + ",优先级:" + td1.getPriority());
}
}

上机练习5

需求说明

某医院设置有普通号和特需号,假设某科室一天需看普通号50个、特需号10个,每个特需号的看病时间约是普通号的两倍,开始时普通号和特需号并行叫号,叫到特需号的概率比普通号高,当普通号叫完第10号时,要求先看完全部特需号,再看普通号,请使用多线程模拟这一过程,运行结果如图所示

上机练习5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.lx5;

public class Test extends Thread{

@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println("特需号:" + (i + 1) + "号病人正在看病!");
}
}

public static void main(String[] args) {
//上机练习5(第192页)
Test t = new Test();
t.setPriority(Thread.MAX_PRIORITY);
t.start();

for (int i = 0; i < 50; i++) {
if (i == 10) {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("普通号:" + (i + 1) + "号病人正在看病!");
}
}
}

上机练习7

需求说明

“桃跑跑” “张票票” “黄牛党” 共同抢十张票,限制黄牛党只能抢一次票,请使用多线程模拟此过程。运行如图所示

上机练习7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.lx7;

public class Test implements Runnable{

private int count = 10; //订单剩余票数
private int num; //订单抢购票数

@Override
public void run() {
while (true) {
synchronized (this) {
if (count == 0) {
return;
}
if ("黄牛党".equals(Thread.currentThread().getName())) {
if (num == 1) {
break;
}
}
count--;
num++;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ",抢到了第" + count + "张票," + "剩余:" + num + "张票");
}

}
}

public static void main(String[] args) {
//第七章上机练习7(第202页)
Test test = new Test();
Thread t1 = new Thread(test,"桃跑跑");
Thread t2 = new Thread(test,"张票票");
Thread t3 = new Thread(test,"黄牛党");

t1.start();
t2.start();
t3.start();
}
}

简答题4

编写一个程序,创建两个线程对象,每个线程输出1到5个数。要求线程类分别使用继承Thread类和实现Runnable接口两种方式创建。

使用Runnable接口实现

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.jd5;

public class MyRunnable implements Runnable {

@Override
public void run() {
System.out.println("使用Runnable接口方式创建线程------");
for (int i = 0; i < 5; i++) {
System.out.println("当前线程名是" + Thread.currentThread().getName() + "\t" + (i + 1) + "\t");
}
}
}

使用Thread继承实现

1
2
3
4
5
6
7
8
9
10
11
12
package com.jd5;

public class MyThread extends Thread {
@Override
public void run() {
System.out.println("使用Thread继承方式创建线程------");
for (int i = 0; i < 5; i++) {
System.out.println("当前线程名是" + Thread.currentThread().getName() + "\t" + (i + 1) + "\t");
}
}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.jd5;

public class Test {
public static void main(String[] args) {
//简答题4(第205页)
MyThread m = new MyThread();
m.start();

Thread t = new Thread(new MyRunnable());
System.out.println();
t.start();
}
}

简答题5

张三和他的妻子各拥有一张银行卡和存折,可以对同一个银行账户进行存取款操作,现银行账户中余额为500元,每人各取五次,每次取款100元,在取款过程中存在网络延时。现使用多线程模拟这个过程。运行如下

本题由@栗子团友情赞助:QQ:2663272630

简答题5

线程类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.jd5;

public class MyThread implements Runnable{

private int money = 500; //钱钱

@Override
public void run() {
for (int i = 1; i <= 5; i++) {
synchronized (this) {
if (money == 0) {
System.out.println("余额不足以支付" + Thread.currentThread().getName() + "的取款,余额为:" + money);
continue;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
money -= 100;
System.out.println(Thread.currentThread().getName() + "准备取款!");
System.out.println(Thread.currentThread().getName() + "完成取款!");
}
}

}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.jd5;

public class Test {
public static void main(String[] args) {
//第七章简答题5(第205页)
MyThread m = new MyThread();
//张三
Thread a = new Thread(m,"张三");
//张三妻子
Thread b = new Thread(m,"张三的妻子");

a.start();
b.start();
}
}