手机网站主页设计,微商城微网站开发,宁波网站建设哪家强,广告公关公司Java基础-OJ Java面向对象程序设计OJ题目 目录 Java基础-OJJava基础-回文数Java基础-计算奇数和Java基础-计算素数Java基础-设计分段函数Java数组-歌手打分Java数组-实现冒泡排序Java继承-类的继承Java类的组合-类的组合Java类及对象-类方法使用Java面向对象-编写汽车类Java面向…Java基础-OJ Java面向对象程序设计OJ题目 目录 Java基础-OJJava基础-回文数Java基础-计算奇数和Java基础-计算素数Java基础-设计分段函数Java数组-歌手打分Java数组-实现冒泡排序Java继承-类的继承Java类的组合-类的组合Java类及对象-类方法使用Java面向对象-编写汽车类Java面向对象-抽象类Java面向对象-多态Java面向对象-构造方法Java面向对象-构造方法与重载Java面向对象-计算重量Java面向对象-接口使用Java面向对象-面向对象综合题1Java面向对象-面向对象综合题2Java面向对象-银行账户-面向对象综合实验Java常用类-字母反转Java常用类-统计数字次数Java常用类-日期比较Java常用类-计算字符串次数Java容器-List练习Java容器-Map的使用Java容器-计算平均分Java容器-世界杯查询Java容器-书籍排序及查找Java多线程-过山洞Java多线程-多线程打印数字Java多线程-子弹射击 Java基础-回文数
Problem Description
1.实验目的掌握if-else if多分支语句 2.实验内容 用户输入一个1-99999之间的整数程序判断这个数是几位数并判断是否回文数回文数将指该数含有的数字逆序排列后得到的数和原数相同例如12121、3223等。3.实验要求 请将下列代码补充完整import java.util.*;public class Main{ public static void main(String args[]){int d5,d4,d3,d2,d1;Scanner cinnew Scanner(System.in);int number cin.nextInt();if(number99999number1) //判断number在1至99999之间的条件{d5number/10000; //计算number的最高位万位d5d4number%10000/1000; //计算number的千位d4// 你的代码Input Description
1-99999之间的整数Output Description
有下列三种输出情况例如(1) 3223 is 4-digit number, is a palindrome number(2) 34561 is 5-digit number, isnt a palindrome number(3) 567890 is not in 1-99999Sample Input
3445Sample Output
3445 is 4-digit number, isnt a palindrome numberHint
提示获取命令行输入内容
Scanner cinnew Scanner(System.in);
int number cin.nextInt();解题代码 /*** 这题是一个代码补全题* 根据题意可知应该只是判断4位数和五位数是否为回文数* 对于五位数 个位 万位 并且 十位 千位 即为回文数* 对于四位数 个位 千位 并且 十位 百位 即为回文数*/// 计算number的百位d3d3 number % 1000 / 100;// 计算number的十位d2d2 number % 100 / 10;// 计算number的个位d1d1 number % 10;// 如果万位d5为0 则代表这个数是四位数if (d5 0){// 如果个位 千位 并且 十位 百位 即为回文数 输出结果if(d1 d4 d2 d3){System.out.println(number is 4-digit number, is a palindrome number);// 否则 不为回文数 输出结果} else{System.out.println(number is 4-digit number, isnt a palindrome number);}// 如果是五位数}else {// 如果个位 万位 并且 十位 千位 即为回文数 输出相关信息if(d1 d5 d2 d4){System.out.println(number is 5-digit number, is a palindrome number);// 不是回文数 输出相关信息} else{System.out.println(number is 5-digit number, isnt a palindrome number);}}// 不是1-99999之间的数 输出相关信息}else {System.out.println(number is not in 1-99999);}// 关闭Scanner 输入流cin.close();}
}Java基础-计算奇数和
Problem Description
请编写程序计算“1357...N”的值Input Description
输入一个奇数N(N0)回车Output Description
输出计算结果Sample Input
5Sample Output
9解题代码
// 导入Scanner类
import java.util.Scanner;
// OJ规定上主类的名字为Main
public class Main {// 主方法public static void main(String[] args) {// 创建Scanner对象接收输入Scanner in new Scanner(System.in);// 接受一个int类型的输入int num in.nextInt();// 结果变量int sum 0;// 循环从1开始 每次加2 直到输入的数字 for(int i 1;i num;i2){// 累加sum i;}// 输出结果System.out.println(sum);// 关闭Scanner 输入流in.close();}
}Java基础-计算素数
Problem Description
1.实验目的掌握程序流程控制的实现方法2.实验内容 计算1---50之间所有素数3.实验要求 补充完整下列代码public class Main{public static void main(String args[]){int count 0;for(int i 2;i50;i){// 你的代码if(count0){ System.out.print(i );}count 0;}}}Output Description
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 解题代码
// 这一题是一个代码补全题补全的是判断数是否为素数的逻辑
// 素数: 只能被1和它本身整除的数
// 循环判断数是否为素数 2 到 这个数的一半即可
for(int j 2;j i/2;j){// 如果为能被其它数整除 即代表这个数不是素数 将count设置为1if(i%j 0){count1;// 跳出循环break;}
}Java基础-设计分段函数
Problem Description
已知函数:(1) x0,yx3; (2) x0,y0; (3) x0,yx2-1,
请设计一个方程序实现上面的函数根据传入的值x的不同返回对应的y值xxxxxxxxxx1 1请编写程序计算“1357...N”的值Input Description
输入一个数值然后回车Output Description
输出计算后的数值Sample Input
32Sample Output
35解题代码
import java.util.Scanner;public class Main{public static void main(String[] args) {// 创建scanner对象Scanner in new Scanner(System.in);// 接受输入的xint x in.nextInt();// 结果变量 如果x0 y0int res 0;// 如果x 0 y x 3if (x 0 ) res x 3;// x 0 y x^2 - 1 else if(x 0) res x * x -1;// 最近C/C的代码写的有点多 这个if-else if没有加括号System.out.println(res);// 关闭scanner 输入流in.close();}
}Java数组-歌手打分
Problem Description
在歌唱比赛中共有10位评委进行打分在计算歌手得分时去掉一个最高分去掉一个最低分然后剩余的8位评委的分数进行平均就是该选手的最终得分。输入每个评委的评分求某选手的得分。Input Description
98 91 94 89 90 92 96 88 93 95Output Description
92解题代码
// 导入Scanner类
import java.util.Scanner;
class Main{public static void main(String[] args) {// 创建Scanner对象Scanner in new Scanner(System.in);// 读取输入的一行数据并存储为字符串String line in.nextLine();// 切割字符串 得到数字的字符数组String[] nums line.split( );// 定义max min 最大值 最小值变量 初始值为输入数据的第一个数字int max Integer.parseInt(nums[0]);int min Integer.parseInt(nums[0]);// 创建int数组 长度为字符数组的长度int [] scores new int[nums.length];// 定义结果变量int sum 0;// 循环将字符数组的数字转换为int存放到int数组for (int i 0;i nums.length;i){scores[i] Integer.parseInt(nums[i]);// 寻找最小值if (scores[i] min) min scores[i];// 寻找最大值if (scores[i] max) max scores[i];}// 求和for(int i 0;i scores.length;i){sum scores[i];}// 将总分数 减去最高分和最低分sum - (max min);// 输出结果System.out.println(sum / (nums.length-2));// 关闭Scanner 输入流in.close();}
}Java数组-实现冒泡排序
Problem Description
输入任意顺序的整数序列输出结果为从小到大的排序结果Input Description
输入一个整数序列整数之间用空格隔开输入完最后一个整数回车Output Description
从小到大的排序结果Sample Input
9 8 7 6 5 4Sample Output
4 5 6 7 8 9Hint
注意主类名称务必为Main其他类修饰符不可为public解题代码
// 导入Scanner类
import java.util.Scanner;
class Main{public static void main(String[] args) {// 创建Scanner对象 接受输入Scanner scanner new Scanner(System.in);// 接受一行输入 将输入的数字使用字符串存储String line scanner.nextLine();// 使用split方法将字符串切分成字符数组String[] nums line.split( );// 创建存放数字的int数组 长度和字符数组一样int arr[] new int[nums.length];// 将字符数组的字符数字转换为int存入数组for(int i 0;i arr.length;i){// Integer.parseInt将字符转换为数字arr[i] Integer.parseInt(nums[i]);}// 冒泡排序算法for(int i 0;i arr.length - 1;i){for(int j 0;j arr.length - i - 1;j){if(arr[j] arr[j1]){int temp arr[j];arr[j] arr[j1];arr[j1] temp;}}}// 遍历数组 输出结果for (int i 0;i arr.length;i){System.out.print(arr[i] );}}
}Java继承-类的继承
Problem Description
给出父类Student请完成其子类UnderGraduate并增加一个成员属性degree(String类型)并按照主类要求完成其构造方法及show方法的编写。class Student {public String name;public int age;public Student(String name,int age){this.namename;this.ageage;}public void show(){System.out.println(name: name age: age);}
}
// 你的代码将被嵌于此处
public class Main{public static void main(String[] args) {Student student new Student(zhangsan, 16);student.show();UnderGraduate underGraduate new UnderGraduate(lisi, 20, bechalor);underGraduate.show();
}
}Output Description
name: zhangsan age:lisiname: lisi age: 20 degree: bechalor解题代码
// UnderGraduate类继承Student类 extends关键代表继承
class UnderGraduate extends Student{// degree属性public String degree;// 带参构造器public UnderGraduate(String name,int age,String degree){// 调用父类带参构造器super(name,age);// 成员赋值this.degree degree;}// 重写父类的show方法Overridepublic void show(){System.out.println(name: name age: age degree: degree);}}Java类的组合-类的组合
Problem Description
给出Car类的元件类EngineWheelWindowDoor要求将其组装成一个Car类并测试结果程序如下class Engine
{void start(){System.out.println(启动引擎!);
}class Wheel
{void inflate(int psi){System.out.println(车胎充气psi升!);}
}class Window
{void open(){System.out.println(打开车窗!);}void close(){System.out.println(关闭车窗!);}
}class Door
{Window windownew Window();void open(){System.out.println(打开车门!);}void close(){System.out.println(关闭车门!);}
}// 你的代码嵌入这里Output Description
车胎充气4升!
车胎充气4升!
车胎充气4升!
车胎充气4升!
打开车门!
打开车窗!
启动引擎!解题代码
class Car{Wheel wheel1 new Wheel();Wheel wheel2 new Wheel();Wheel wheel3 new Wheel();Wheel wheel4 new Wheel();Door door new Door();Window window new Window();Engine engine new Engine();void start(){wheel1.inflate(4);wheel2.inflate(4);wheel3.inflate(4);wheel4.inflate(4);door.open();window.open();engine.start();}
}class Main{public static void main(String[] args) {new Car().start();}
}Java类及对象-类方法使用
Problem Description
下面是一个Student类具有两个成员属性: name、grade请根据测试结果将下列该类的方法填写完整。class Student {private String name;private double grade;//你的代码将被嵌入此处}
class Main{public static void main(String[] args) {Student stu1 new Student(zhangsan,32);stu1.setName(lisi);stu1.setGrade(89);System.out.println(stu1.getName());System.out.println(stu1.getGrade());}
}Output Description
lisi89.0解题代码 // 无参构造器public Student(){}// 带参构造器public Student(String name,double grade){this.grade grade;this.name name;}// set get 方法public String getName() {return name;}public void setName(String name) {this.name name;}public double getGrade() {return grade;}public void setGrade(double grade) {this.grade grade;}Java面向对象-编写汽车类
Problem Description
1.实验目的(1) 熟悉类的创建方法(2) 掌握对象的声明与创建(3) 能利用面向对象的思想解决一般问题2.实验内容 编写一个java程序设计一个汽车类Vehicle包含的属性有车轮的个数wheels和车重weight。小汽车类Car是Vehicle的子类包含的属性有载人数loader。卡车类Truck是Car类的子类其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。3.实验要求补充完整下面的代码public class Main{public static void main(String[] args){Vehicle vnew Vehicle(8,10.00);smallCar cnew smallCar(6);Truck tnew Truck(10);v.disMessage();c.disM();t.disM2();t.disM3();}
}// 你的代码Output Description
The number of wheels in this car is 8, weight is 10.0
This car can carry 6 persons
The load of this truck is 10
The number of wheels in this truck is 8, weight is 10.0, can carry 6 persons, load of this truck is 10解题代码
// Vehicle 类
class Vehicle{// wheels 车轮个数public int wheels;// 重量public double weight;// 带参构造方法public Vehicle(int wheels, double weight) {this.wheels wheels;this.weight weight;}// 无参构造方法public Vehicle() {}// 打印信息public void disMessage(){System.out.println(The number of wheels in this car is wheels , weight is weight);}
}// smallCar类继承Vehicle类
class smallCar extends Vehicle{// loader 载人数public int loader;// 带参构造 三个参数public smallCar(int wheels, double weight, int loader) {// 调用父类构造器super(wheels, weight);this.loader loader;}// 带参构造 一个参数public smallCar(int loader) {// 调用父类构造 根据题目输出 wheels 8 weight10.00super(8, 10.00);this.loader loader;}// 无参构造public smallCar() {}// 打印信息public void disM(){System.out.println(This car can carry loader persons);}
}
// Truck类继承smallCar类
class Truck extends smallCar{// payload 载重量private int payload;// 带参构造 一个参数public Truck(int payload) {// 调用父类构造 根据题目输出 loader 6super(6);this.payload payload;}// 带参构造 四个参数public Truck(int wheels, double weight, int loader, int payload) {// 调用父类构造super(wheels, weight, loader);this.payload payload;}// 无参构造public Truck() {}// 打印信息public void disM2(){System.out.println(The load of this truck is payload);}// 打印信息public void disM3(){System.out.println(The number of wheels in this truck is wheels , weight is weight , can carry loader persons, load of this truck is payload);}
}Java面向对象-抽象类
Problem Description
创建一个Vehicle类并将它声明为抽象类。在Vehicle类中声明一个NoOfWheels方法使它返回一个字符串值。创建两个类Car和Motorbike从Vehicle类继承并在这两个类中实现NoOfWheels方法。在Car类中应当显示“四轮车”信息而在Motorbike类中 应当显示“双轮车”信息。创建另一个带main方法的类在该类中创建Car和Motorbike的实例 并在控制台中显示消息。abstract class Vehicle// 你的代码嵌入这里class Main{public static void main(String[] args){Car cnew Car();c.NoOfWheels();Motorbike mnew Motorbike();m.NoOfWheels();}
}Output Description
four wheelstwo wheels解题代码
// 补全抽象类
{// 抽象类中的抽象方法需要使用 abstract关键字修饰abstract void NoOfWheels();
}
// Car类继承 Vehicle类
class Car extends Vehicle{// 实现父类的NoOfWheels方法Overridevoid NoOfWheels() {System.out.println(four wheels);}
}// Motorbike类继承 Vehicle类
class Motorbike extends Vehicle{// 实现父类的NoOfWheels方法Overridevoid NoOfWheels() {System.out.println(two wheels);}
}Java面向对象-多态
Problem Description
完成下列多态程序class Animal {public void eat(){System.out.println(eat);}
}class Dog extends Animal {public void eat() {System.out.println(eat bones);}public void lookDoor() {System.out.println(look door);}
}class Cat extends Animal {public void eat() {System.out.println(eat fish);}public void playGame() {System.out.println(hide-and-seek);}
}class Main {public static void main(String[] args) {Animal a,b;// 你的代码嵌在这里a.eat();b.eat();}
}Output Description
eat boneseat fish解题代码
// 多态 子类的实例赋值给父类的引用
// dog类的实例赋值给Animal的引用
a new Dog();
// cat类的实例赋值给Animal的引用
b new Cat();Java面向对象-构造方法
Problem Description
编写Java程序模拟简单的计算器。定义名为Number的类其中有两个整型数据成员x和y声明为私有。编写构造方法赋予x和y初始值再为该类定义加add、减sub、乘mul、除div等公有成员方法分别对两个成员变量执行加、减、乘、除的运算。 在main方法中创建Number类的对象调用各个方法并显示计算结果。// 你的代码将嵌入这里public class Main{public static void main(String[] args){Number numnew Number(4,4);num.add();num.sub();num.mul();num.div();}
}Output Description
the result is:8
the result is:0
the result is:16
the result is:1解题代码
// Number类
class Number{// 成员xprivate int x;// 成员yprivate int y;// 带参构造public Number(int x, int y) {this.x x;this.y y;}// 加运算的方法void add(){int res x y;System.out.println(the result is: res);}// 减运算的方法void sub(){int res x - y;System.out.println(the result is: res);}// 乘运算的方法void mul(){int res x * y;System.out.println(the result is: res);}// 除运算的方法void div(){int res x / y;System.out.println(the result is: res);}
}Java面向对象-构造方法与重载
Problem Description
定义一个网络用户类 要处理的信息有用户ID、用户密码、email地址。在建立类的实例时 把以上三个信息都作为构造函数的参数输入其中用户ID和用户密码时必须的缺省的email地址是用户ID加上字符串21cn.com。// 你的代码将嵌入这里class Main{public static void main(String[] args){UerInfo uernew UerInfo(0215301,Jack,jack21cn.com);UerInfo unew UerInfo(0215303,Ma);}
}Output Description
id0215301 pwd:Jack email:jack21cn.comid0215303 pwd:Ma email:021530321cn.com解题代码
// UerInfo类 这个类名不想吐槽了
class UerInfo{// ID 属性private String ID;// pwd 属性private String pwd;// email 属性private String email;// 带参构造方法 三个参数public UerInfo(String ID, String pwd, String email) {// 如果没有传入email email ID21cn.comif (.equals(email)) email ID21cn.com;this.email email;// 打印信息System.out.println(id ID pwd:pwd email:email);}// 带参构造方法 两个参数public UerInfo(String ID, String pwd) {// 调用三个参数的构造方法this(ID,pwd,);}
}Java面向对象-计算重量
Problem Description
1.实验目的(1) 熟悉接口的声明、创建、使用(2) 能利用接口的思想解决一般问题2.实验内容 有一个ComputerWeight接口该接口有一个方法public double computeWeight()有三个实现该接口的类Television、Computer、WashMachine这三个类通过实现接口ComputerWeight计算自身的重量其中Television重量为45.5Computer重量为65.5WashMachine重量为145
有一个Car类该类用ComputerWeight数组作为成员ComputerWeight数组的单元可以存放Television、Computer、WashMachine对象的引用程序能输出Car类对象的总重量。3.实验要求补充完整下面的代码// 你的代码public class Main
{public static void main(String args[]){ ComputerWeight[] goodsOnenew ComputerWeight[50],goodsTwonew ComputerWeight[22] ; for(int i0;igoodsOne.length;i){ if(i%30)goodsOne[i]new Television();else if(i%31)goodsOne[i]new Computer();else if(i%32)goodsOne[i]new WashMachine();} for(int i0;igoodsTwo.length;i){ if(i%30)goodsTwo[i]new Television();else if(i%31)goodsTwo[i]new Computer();else if(i%32)goodsTwo[i]new WashMachine();} Car largeTrucknew Car(goodsOne);System.out.println(The weight of the goods loaded in the large truck: largeTruck.getTotalWeights());Car pickupnew Car(goodsTwo);System.out.println(The weight of the goods loaded in the pickup: pickup.getTotalWeights());}
}Output Description
The weight of the goods loaded in the large truck: 4207.0
The weight of the goods loaded in the pickup: 1837.5解题代码
// ComputerWeight 接口
interface ComputerWeight{// 抽象方法computeWeightdouble computeWeight();
}
// Television类实现了 ComputerWeight 接口
class Television implements ComputerWeight{// 实现computeWeight方法Overridepublic double computeWeight() {// 返回重量return 45.5;}
}
// Computer类实现了 ComputerWeight 接口
class Computer implements ComputerWeight{// 实现computeWeight方法Overridepublic double computeWeight() {// 返回重量return 65.5;}
}
// WashMachine类实现了 ComputerWeight 接口
class WashMachine implements ComputerWeight{// 实现computeWeight方法Overridepublic double computeWeight() {// 返回重量return 145;}
}// Car类
class Car{// 成员goods ComputerWeight类型数组private ComputerWeight[] goods null;// 带参构造方法public Car(ComputerWeight[] goods) {this.goods goods;}// 无参构造public Car() {}// 获取总重量的方法public double getTotalWeights(){double total 0.;// 循环遍历goods 计算总重量for (ComputerWeight computerWeight: goods){total computerWeight.computeWeight();}// 返回结果return total;}
}Java面向对象-接口使用
Problem Description
创建一个名称为Vehicle的接口。在接口中添加两个方法start()和stop()。在两个名称分别为Bike和Bus的类中实现Vehicle接口。创建一个名称为Main的类在Main的main()方法中创建Bike和Bus对象并访问start()和stop()方法。代码如下// 你的代码将被嵌入这里class Main
{public static void main(String[] args){Bike bikenew Bike();bike.start();bike.stop();Bus busnew Bus();bus.start();bus.stop();}
}Output Description
i am bike,i am runningi am bike,i am stopi am bus,i am runningi am bus,i am stop解题代码
// Vehicle 接口
interface Vehicle{// start方法 抽象方法只有方法头 没有方法体 默认修饰符为publicvoid start();// stop方法void stop();
}
// Bike类 实现Vehicle接口
class Bike implements Vehicle{// 实现Vehicle接口的start方法Overridepublic void start() {System.out.println(i am bike,i am running);}// 实现Vehicle接口的stop方法Overridepublic void stop() {System.out.println(i am bike,i am stop);}
}
// Bus类 实现Vehicle接口
class Bus implements Vehicle{// 实现Vehicle接口的start方法Overridepublic void start() {System.out.println(i am bus,i am running);}// 实现Vehicle接口的stop方法Overridepublic void stop() {System.out.println(i am bus,i am stop);}
}Java面向对象-面向对象综合题1
Problem Description
设计一个系统 XXX门的实现过程。流程设计一张抽象的门Door那么对于这张门来说就应该拥有所有门的共性开门openDoor()和关门closeDoor() 然后对门进行另外的功能设计防盗--theftproof()、防水--waterproof()、防弹--bulletproof()。要求 利用继承、抽象类、接口的知识设计该门接口。代码如下abstract class Door
{public void opendoor(){};public void closedoor(){};
}
// 你的代码嵌入在这里class Main
{public static void main(String[] args){GoodDoor gnew GoodDoor();g.opendoor();g.threfproof();g.waterproof();g.fireproof();g.closedoor();}
}Output Description
i can open the door
i can threfproof
i can waterproof
i can fireproof
i can close the door解题代码
// GoodDoor 继承Door类
class GoodDoor extends Door{// 重写父类的opendoor方法Overridepublic void opendoor(){System.out.println(i can open the door);}// threfproof方法public void threfproof(){System.out.println(i can threfproof);}// waterproof方法public void waterproof(){System.out.println(i can waterproof);}// fireproof方法public void fireproof(){System.out.println(i can fireproof);}// 重写父类的closedoor方法Overridepublic void closedoor(){System.out.println(i can close the door);}
}Java面向对象-面向对象综合题2
Problem Description
要求如下
1.定义一个抽象的Role类有姓名、年龄、性别等成员变量。要求尽可能隐藏所有变量(能够私有就私有,能够保护就不要公有) 。具有一个抽象的play()方法该方法不返回任何值同时至少定义两个构造方法。
2. 从Role类派生出一个Employee类 该类具有Role类的所有成员并扩展 salary成员变量同时增加一个静态成员变量职工编号id。同样要有至少两个构造方法要体现出this和super的几种用法还要求覆盖play()方法并提供一个final sing()方法。
3. Manager类继承Employee类有一个final成员变量vehicle 。
4. 在Main类中产生Manager和Employee对象,并测试这些对象的方法。代码如下class Employee extends Role1
{protected int id;protected int salary;public Employee(){}public Employee(String name,int age,String sex,int id,int salary)
// 你的代码将被嵌入这里class Main{public static void main(String[] dsa){Employee enew Employee(you xiao,20,Man,1201012204,15000);Manager mnew Manager();System.out.println(e.id);System.out.println(e.salary);System.out.println(e.name);System.out.println(e.age);System.out.println(e.sex);System.out.println(m.vehicle);m.play();m.sing();}
}Output Description
1201012204
15000
you xiao
20
Man
Lamborghini
i can paly
i can sing解题代码
// 补全Employee类// 构造方法{// 调用父类的带三个参数的构造方法super(name,age,sex);this.id id;this.salary salary;}// 重写父类的play方法Overridepublic void play() {System.out.println(i can paly);}// sing方法public final void sing() {System.out.println(i can sing);}
}
// 抽象类Role1
abstract class Role1 {// 受保护的name 属性protected String name;// 受保护的age 属性protected int age;// 受保护的sex 属性protected String sex;// 无参构造public Role1() {}// 带参构造public Role1(String name, int age, String sex) {this.name name;this.age age;this.sex sex;}// 抽象方法playpublic abstract void play();
}
// Manager类继承Employee类
class Manager extends Employee {// 常量vehicle 常量名应该大写public final String vehicle Lamborghini;// 无参构造public Manager(){// 调用父类的无参构造 默认会调用super();}// 带参构造public Manager(String name, int age, String sex, int id, int salary) {// 调用父类的带参构造super(name,age,sex,id,salary);}
}Java面向对象-银行账户-面向对象综合实验
Problem Description
1.实验目的
(1) 熟悉类的定义
(2) 掌握对象的声明、实例化及成员的引用
(3) 掌握构造方法及实例方法的区别与用法。2.实验内容多数用户对去银行办理存款、取款等业务并不默生用户自然感觉到了通过计算机办理业务的方便、快捷也自然对编写出银行系统程序的程序员发出由衷的敬意。实际上当我们具备了面向对象编程的知识以后我们也能编写出相应的程序。设计一个银行帐户类成员变量包括账号、储户姓名、开户时间、身份证、存款余额等帐户信息成员方法包括存款、取款操作。3.实验要求将下列代码补充完整import java.util.*; //引入程序包public class Main{public static void main(String args[]){//实现账户的相关操作BCOption bco new BCOption();bco.kaihu(new BankCount(1,张三,2020-04-30,12));bco.query(0);}
}// 你的代码Output Description
1 zhangsan 2020-04-30 12.0解题代码
// BankCount类 银行账户类
class BankCount{// id 编号private int id;// name 姓名private String name;// time 开户时间private String time;// no 身份证号private String no;// 余额 这里使用double在进行计算的时候可能出现精度丢失导致出现问题 // 应该使用专业的BigDecimal类private double balance;// 无参构造public BankCount() {}// 带参构造 全部成员public BankCount(int id, String name, String time, String no, double balance) {this.id id;this.name name;this.time time;this.no no;this.balance balance;}// 带参构造 部分成员public BankCount(int id, String name, String time, double balance) {this.id id;this.name name;this.time time;this.balance balance;}// 重写ToString方法Overridepublic String toString() {return id name time balance;}
}// BCOption类 账户操作类
class BCOption{// counts 存储账户信息 这里也是有问题的 不过只要能过OJ 这都无所谓了private ArrayListBankCount counts new ArrayList();// 开户方法public void kaihu(BankCount bankCount){counts.add(bankCount);}// 查询账户方法public void query(int index){System.out.println(counts.get(index));}
}Java常用类-字母反转
Problem Description
编写一个程序输入任意一段文本将其中的各个单词的字母顺序翻转。考虑用StringBuffer类Input Description
my name is tomOutput Description
ym eman si mot.解题代码
import java.util.Scanner;public class Main{public static void main(String[] args) {// 创建Scanner对象 接收控制台输入Scanner in new Scanner(System.in);// 读取一行数据String line in.nextLine();// 切分为字符串数组 数组的每一位存放一个单词String[] strs line.split( );// 遍历数组for (int i 0;i strs.length;i){// 将字符串创建为StringBuffer类 调用reverse方法反转 直接打印System.out.print(new StringBuffer(strs[i]).reverse());// 如果不是最后一个单词 打印空格if (i ! strs.length-1){System.out.print( );// 如果是最后一个单词 打印换行}else {System.out.print(.\n);}}// 关闭Scanner 输入流in.close();}
}Java常用类-统计数字次数
Problem Description
命令行输入一个由数字组成的任意字符串,统计出每个数字出现的次数。Input Description
1239586838Output Description
0 counts:01 counts:12 counts:13 counts:24 counts:05 counts:16 counts:17 counts:08 counts:39 counts:1解题代码
import java.util.Scanner;public class Main{public static void main(String[] args) {// 创建Scanner对象 接收控制台输入Scanner in new Scanner(System.in);// 接收输入的一行String line in.nextLine();// 切分为字符串数组String[] nums line.split();// 创建数字数组 用于存放数字出现次数int cunts[] new int[10];// 遍历字符串数组for (int i 0; i nums.length;i){// switch语句统计数字出现次数switch (nums[i]){case 0: cunts[0];break;case 1: cunts[1];break;case 2: cunts[2];break;case 3: cunts[3];break;case 4: cunts[4];break;case 5: cunts[5];break;case 6: cunts[6];break;case 7: cunts[7];break;case 8: cunts[8];break;case 9: cunts[9];break;}}// 遍历数组 打印结果for (int i 0;i cunts.length;i) System.out.println(i counts: cunts[i]);// 关闭Scanner 输入流in.close();}
}Java常用类-日期比较
Problem Description
1.实验目的掌握SimpleDateFormat、Date日期类用法 2.实验内容编写程序输入一个日期字符串拆分成两个日期然后比较两个日期大小并给出两个日期相差天数3.实验要求请将下列代码补充完整import java.util.*;import java.text.SimpleDateFormat;public class Main{
public static void main(String [] args){Date date1,date2;String[] time;Scanner cinnew Scanner(System.in);time cin.next().split(,);// 你的代码Input Description
输入一个日期字符串(含两个日期)例如2018-09-01,2019-08-01Output Description
输出有三种情况
1.第一个日期比第二个日期小显示如下
less than
334 days apart!
2.第一个日期比第二个日期大显示如下
greater than
220 days apart!
3.两个日期相等
equal
0 days apart! 解题代码 // 此题为代码补全题// 创建SimpleDateFormat对象并指定格式为yyyy-MM-dd // SimpleDateFormat对象可以将指定格式的字符串转换为Date类型对象SimpleDateFormat sdf new SimpleDateFormat(yyyy-MM-dd);// 异常捕获 在将字符串转化成Date对象的时候可能出现异常try {// 将输入的第一个日期字符串转换为Date类型对象date1 sdf.parse(time[0]);// 将输入的第二个日期字符串转换为Date类型对象date2 sdf.parse(time[1]);// 使用Date类的compareTo方法比较两个日期的大小// compareTo方法会返回一个int变量// 1 代表前面的日期大于后面的日期 // 0 代表两个日期相等 // -1 代表前面的日期大于后面的日期int i date1.compareTo(date2);// 天数 结果变量long day 0;// 如果面的日期大于后面的日期if (i 1){// 输出System.out.println(greater than);// getTime()方法返回的是一个从1970年到当前日期的时间戳 单位为毫秒// 使用前面的日期的时间戳减去后边日期的时间戳// 将时间戳代表的时间转换成天// 1 day 1ms / (1000 * 60 * 60 * 24) day ((date1.getTime() - date2.getTime()) / 1000 / 60 / 60 /24);// 如果两个日期相等}else if (i 0){System.out.println(equal);// 如果前面的日期小于后面的日期}else {System.out.println(less than);day ((date2.getTime() - date1.getTime()) / 1000 / 60 / 60 /24);}// 打印相差的天数System.out.println(day days apart! );// 异常捕获 这里捕获Exception 因为不用导包} catch (Exception e) {e.printStackTrace();}// 关闭Scanner 输入流cin.close();}
}Java常用类-计算字符串次数
Problem Description
1.实验目的掌握String、StringBuffer等常用类用法 2.实验内容编写程序输入任意一个字符串计算字符串java出现的次数3.实验要求请将下列代码补充完整import java.util.*;public class Main {public static void main(String[] args) {Scanner cinnew Scanner(System.in);String s cin.next();// 你的代码Input Description
sunjavahpjavaokjavajjavahahajavajavagoodjavaOutput Description
7解题代码 // 此题为代码补全题// 定义int型 count变量 用于记录单词出现次数int count 0;// 外层循环为起始索引 由于统计的出现次数的单词为java 长度为4// 因此从0开始到3 也就是对原字符串每四个截取一次进行比较for (int i 0;i 4;i){// 内层循环为结束索引 结束索引不能大于原字符串减4 否则会出现索引越界for (int j i;j s.length() - 4;j4){// 使用substring方法截取子字符串// 字符串是否相等使用equals方法判断 直接使用 比较的是hashcode// 如果相等 代表出现一次 count加1if (java.equals(s.substring(j,j4))) count;}}// 输出结果System.out.println(count);// 关闭Scanner 输入流cin.close();}
}Java容器-List练习
Problem Description
运用List完成下面的要求:
1) 创建一个List在List中增加三个工人基本信息如下
姓名 年龄 工资
Tom 18 3000
Peter 25 3500
Mark 22 3200
2) 插入一个工人信息为姓名Robert年龄24工资3300
3) 删除姓名为Mark的工人
4) 利用for 循环遍历打印List中所有工人的信息
5) 利用迭代遍历对List中所有的工人调用work方法。 完成下列代码:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;class Worker{private int age;private String name;private double salary;public Worker(String name,int age,double salary)// 你的代码嵌入这里class Main{public static void main(String [] args){workerHelper wh new workerHelper();ListWorker ls new ArrayList();wh.add(ls);wh.append(ls);wh.del(ls,Mark);wh.print(ls);wh.callwork(ls);}
}Output Description
Workers info:Tom 18 3000.0
Workers info:Peter 25 3500.0
Workers info:Robert 24 3300.0
Tom work
Peter work
Robert work解题代码 // 这题是一个代码补全题 // Worker类的带参构造方法{this.name name;this.age age;this.salary salary;}// get setpublic int getAge() {return age;}public void setAge(int age) {this.age age;}public String getName() {return name;}public void setName(String name) {this.name name;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary salary;}// toStringOverridepublic String toString() {return Workers info:name age salary;}
}// workerHelper类 用于处理存储worker的List集合数据
// workerHelper不规范 应该改为WorkerHelper
class workerHelper{// 添加Worker 传入一个worker的List集合void add(ListWorker ls) {// 创建三个woerker对象 具体的信息根据题目Worker tom new Worker(Tom, 18, 3000);Worker peter new Worker(Peter, 25, 3500);Worker mark new Worker(Mark, 22, 3200);// 将三个worker对象存入List集合ls.add(tom);ls.add(peter);ls.add(mark);}// 向List集合中添加worker对象void append(ListWorker ls){Worker robert new Worker(Robert, 25, 3300);ls.add(robert);}// 将List集合中的worker对象删除 根据worker姓名void del(ListWorker ls,String name){int index -1;// 遍历list集合 查找要删除worker对象的索引for (Worker w : ls){if (w.getName().equals(name)) index ls.indexOf(w);}// 如果找到了 就删除if (index ! -1) ls.remove(index);}// 打印List集合中所有worker信息void print(ListWorker ls){for (Worker w : ls) System.out.println(w);}// 呼叫List集合中所有worker对象void callwork(ListWorker ls){for (Worker w: ls) System.out.println(w.getName() work);}}Java容器-Map的使用
Problem Description
1.实验目的
(1) 掌握Map的创建及遍历2.实验容编写Account类含账户idInteger型、余额balanceDouble型属性及相应get、set方法接着完成类Helper的input方法完成账户输入(注意输入串要含有账户id及余额两类信息)进Account对象并存储进Map此处用linkedHashMap保证输入顺序在输出时不变中接着遍历Map并给出信息提示。3.实验要求请完成Account类、Helper类import java.util.*;public class Main { public static void main(String[] args) {MapInteger,Account mp new linkedHashMap();Helper helper new Helper();// 输入数据进Maphelper.inputMap(mp);// 遍历Maphelper.visitMap(mp);}}// 你的代码Input Description
12,450;67,780;56,1000Output Description
id12, balance450.0
id67, balance780.0
id56, balance1000.0解题代码
// 账户 Account类
class Account{// 账户idprivate Integer id;// 余额 balance private Double balance;// 带参构造public Account(Integer id, Double balance) {this.id id;this.balance balance;}// 无参构造public Account() {}// get setpublic Integer getId() {return id;}public void setId(Integer id) {this.id id;}public Double getBalance() {return balance;}public void setBalance(Double balance) {this.balance balance;}// toString方法Overridepublic String toString() {return id id , balance balance;}
}// Helper类
class Helper{// inputMap方法 接收输入的账户数据 存入传递的Map集合中public void inputMap(Map map){Scanner in new Scanner(System.in);String line in.nextLine();String[] strs line.split(;);for(String s:strs){String[] infos s.split(,);Account account new Account(Integer.parseInt(infos[0]), Double.parseDouble(infos[1]));map.put(account.getId(),account);}in.close();}// 显示所有的账户信息public void visitMap(Map map){Set keys map.keySet();for (Object key: keys){System.out.println(map.get(key));}}
}Java容器-计算平均分
Problem Description
1.实验目的
(1) 掌握容器的创建、使用2.实验内容编写Student类含分数score属性及相应get、set方法接着完成类Compute的input方法完成分数输入进Student对象并存储进容器list最后完成Compute类的average方法完成容器内的平均分计算。3.实验要求请将下列代码补充完整import java.util.*;class Compute{ListStudent list new ArrayList();// 你的代码public class Main {public static void main(String[] args) {Compute comp new Compute();comp.input();System.out.println(学生的平均分为 comp.average());}
}Input Description
20,80,52,68,80Output Description
Student average score: 60解题代码
// 此题为代码补全题// 补全Compute类中的input方法public void input(){// 创建Scanner对象Scanner in new Scanner(System.in);// 接收控制台输入的一行数据 存储为字符串 输入数据的分隔符为,String line in.nextLine();// 切割字符串 获取字符串数组String[] scores line.split(,);// 遍历字符串数组 得到每一个分数 字符串for (String score: scores){// 将字符串形式的分数转换为int存入List集合list.add(new Student(Integer.parseInt(score)));}// 关闭Scanner对象in.close();}// 计算平均分的方法public int average(){// 总分int sum 0;// 遍历List集合 累加得到总分for (Student s :list){sum s.getScore();}// 返回平均分return sum/list.size();}
}// Student类
class Student{// 分数属性private int score;// 带参构造器public Student(int score) {this.score score;}// 无参构造public Student() {}// get setpublic int getScore() {return score;}public void setScore(int score) {this.score score;}
}Java容器-世界杯查询
Problem Description
1.从命令行读入一个队名输出该队获得冠军是哪一年以下面五届数据作为基础数据。如果该队没有获得冠军则输出队名“ lose the championship!”。2.下面是五届世界杯数据
届数 举办年份 举办地点 冠军
第一届1930年UruguayUruguay
第三届1938年FranceItaly
第五届1954年SwissWest Germany
第七届1962年ChileBrazil
第十三届1986年MexicoArgentina3.要求(用Map实现下面的m即是Map对象)下面是后半部分代码请补充前半部分if(m.get(temp).equals(str)){System.out.println(temp);}}}else{System.out.println( str lose the championship!);}}
}Input Description
ItalyOutput Description
1938解题代码
// 这题是一个代码补全题
// 下面三行可以替换为 import java.util.*
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;public class Main{public static void main(String[] args) {// 创建一个HashMap对象 根据后面的代码 变量名必须为mHashMapString, String m new HashMap();// 将世界杯数据存入map存储m.put(1930,Uruguay);m.put(1938,Italy);m.put(1954,West Germany);m.put(1962,Brazil);m.put(1986,Argentina);// 创建Scanner对象 接收控制台输入Scanner in new Scanner(System.in);// 接收输入的队名 存储为字符串String str in.nextLine();// 关闭Scanner对象输入流in.close();// 如果队名为存储数据中的有的队名if (Uruguay.equals(str) || Italy.equals(str) || West Germany.equals(str) || Brazil.equals(str) || Argentina.equals(str)){// 获取所有key的集合SetString keys m.keySet();// 增强for循环遍历所有的key 变量名为tempfor (String temp:keys){Java容器-书籍排序及查找
Problem Description
1.实验目的
(1) 掌握容器的排序及查找2.实验内容编写Book类含书名name、价格price属性及相应get、set方法接着完成类Helper的input方法完成书籍输入(注意输入串要含有书名及价格两类信息)进Book对象并存储进容器bookList接着对bookList排序并查找某本书是否在bookList中并给出信息提示。3.实验要求请将下列代码补充完整完成Helper类的编写import java.util.*;public class Main{ public static void main(String args[]){ linkedList bookListnew linkedList();Helper helper new Helper();// 输入数据helper.input(bookList);// 排序helper.sort(bookList);// 查找产生一本新书然后查和它重复的书并给出提示Book newBooknew Book();newBook.setPrice(29);newBook.setName(Java);Book searchBooknewBook;helper.query(bookList,searchBook);}} class Book implements Comparable{ double price;String name;public void setPrice(double c){ pricec;}public double getPrice(){ return price;} public void setName(String n){ namen;}public String getName(){ return name;} public int compareTo(object object){ Book bk(Book)object;int difference(int)(this.getPrice()-bk.getPrice());return difference;}}// 你的代码Input Description
java,29;c,35;python,56;r,37Output Description
(1) 书籍列表中有该书则显示
new book,Java(29.0),same as the following books: java(29.0).
(2) 书籍列表中没有该书则显示
new book,Java(29.0),same as the following books: not exist.Hint
输入一行书籍信息字符串分类信息用;或,隔开因此解析时要用到数组及split方法提示如下Scanner cinnew Scanner(System.in);String[] s1 cin.next().split(;);String [][]s2new String[s1.length][];for(int i0;is1.length;i){s2[i] s1[i].split(,);}解题代码
// Helper类 处理存放书籍链表
class Helper{// input()方法接收书籍输入 存入传进来的LinkedList对象public void input(LinkedList bookList){// 创建Scanner对象 接收控制台输入Scanner in new Scanner(System.in);// 接收一行输入String line in.nextLine();// 使用;切割 获取字符串数组 数组中的每一个元素为一个书籍信息 String[] strs line.split(;);// 遍历数组 for(String s:strs){// 使用,分割获取每本书籍的具体信息String[] infos s.split(,);// 创建Book对象Book book new Book();// 设置书名book.setName(infos[0]);// 设置价格book.setPrice(Double.parseDouble(infos[1]));// 将书籍对象放入list集合bookList.add(book);}}// 排序方法 public void sort(LinkedList bookList){// 由于Book已经实现了 Comparable接口// 这里直接使用Collections.sort对象LinkedList中的书籍信息进行排序Collections.sort(bookList);}// query()方法 根据传递的Book对象在LinkedList查找是否有相同的书籍public void query(LinkedList bookList,Book book){// 索引 -1代表没有int index -1;// 遍历存放Book信息的LinkedList 查找书籍信息for (Object b: bookList){// 由于LinkedList存放的为Object 需要将Object类型强制转换为Book类型Book book1 (Book) b;// 如果书籍名称相同 获取书籍的索引if (book1.getName().equalsIgnoreCase(book.getName())) index bookList.indexOf(book1);}// 如果index不是-1 代表有同名书籍if (index ! -1) {// 打印相关信息System.out.println(new book, book.getName() ( book.getPrice() ),same as the following books: ((Book)bookList.get(index)).getName() ( ((Book)bookList.get(index)).getPrice() ).);// 否则代表没有同名书籍}else {// 打印相关信息System.out.println(new book, book.getName() ( book.getPrice() ),same as the following books: not exist.);}}
}Java多线程-过山洞
Problem Description
编写多线程应用程序模拟三个人Tom,Peter,Bob过山洞
1、这个山洞每次只能通过一个人每个人通过山洞的时间为1秒
2、过山洞次序为Tom,Peter,Bob将下列代码补充完整public class Main{public static void main(String[] args) {Tunnel tul new Tunnel();Thread tom new Thread(tul,Tom);
// 你的代码将嵌入这里Output Description
Tom have Crossed the tunnel!This is 1th
Peter have Crossed the tunnel!This is 2th
Bob have Crossed the tunnel!This is 3th解题代码 // 此题为代码补全题// 创建线程peter 并设置线程名为peterThread peter new Thread(tul, Peter);// 创建线程 bob 并设置线程名为BobThread bob new Thread(tul, Bob);// 线程休眠可能发生异常 这里直接try catch 因为上面mian方法写死了try {// 启动tom线程tom.start();// 主线程休眠1s 每个人通过山洞的时间为1秒Thread.sleep(1000);// 启动peter线程peter.start();// 主线程休眠1sThread.sleep(1000);// 启动bob线程bob.start();// 主线程休眠1sThread.sleep(1000);} catch (Exception e) {e.printStackTrace();}}
}// Tunnel类 实现了Runable接口 创建线程除了实现Runnable接口 还可以继承Thread类
class Tunnel implements Runnable{// 变量i用于记录序号 // 由于创建使用的Tunnel实例时同一个 这个变量相当于是共享的private int i 0;// 实现run方法 线程在启动之后会执行run方法Overridepublic void run() {// 序号加1i;// 打印信息System.out.println(Thread.currentThread().getName() have Crossed the tunnel!This is i th);}
}Java多线程-多线程打印数字
Problem Description
利用多线程按要求打印数字线程计数从100开始Thread1先打印101,102,103,104,105,106,107,108然后是Thread2打印109,110,111,112,113,114,115,116,然后是Thread3打印117,118,119,120,121,122,123,124接着再由Thread1打印125,126,127,128,129,130,131,132….以此类推, 直到打印到204。
请将前半部分代码补充完整。// 你的代码将嵌入这里
public class Main {public static void main(String[] args) throws InterruptedException {object o new object(); // 注意O这里应该是大写是Java祖先对象object因为系统原因改不过来new Thread(new PrintRunnable(1,o)).start();new Thread(new PrintRunnable(2,o)).start();new Thread(new PrintRunnable(3,o)).start();}
}Output Description
Thread1:101
Thread1:102
Thread1:103
Thread1:104
Thread1:105
Thread1:106
Thread1:107
Thread1:108
Thread2:109
Thread2:110
Thread2:111
Thread2:112
Thread2:113
…此处省去中间打印的内容
Thread1:200
Thread1:201
Thread1:202
Thread1:203
Thread1:204解题代码
// PrintRunnable类 实现Runnable接口
class PrintRunnable implements Runnable {// 全局变量num 用于记录打印的数字private static int num 100;// 全局变量 cur 用于记录当前运行的线程idprivate static int cur 1;// lock锁对象private Object lock null;// 线程的idprivate int id;// 构造器 接收id和锁对象public PrintRunnable(int id, Object lock) {this.lock lock;this.id id;}// 打印数字的方法 public void print() {// 每个线程只打印8次for (int i 0; i 8; i) {// 将num num;// 如果num 204 退出循环 只打印到204if (num 204) break;// 打印线程名信息 和 数字System.out.println(Thread id : num);}}// 实现run方法 线程启动之后会执行run方法中的逻辑Overridepublic void run() {// 循环while (true) {// 使用synchronized 同步代码块 防止线程并发// synchronized是比较重量级的锁 对性能有一定的影响synchronized (lock) {// 判断当前打印数字的线程id是否等于正在执行线程id// 如果相等if (cur id) {// 调用打印数字的方法print();// 设置下一个打印线程的idcur cur 1;// 由于只有三个线程 因此当前运行的线程id大于3时 将值设置为1if (cur 3) cur 1;// 如果没有轮到当前执行的线程打印} else {// 唤醒其它线程lock.notifyAll();// 异常处理 try catchtry {// 如果数字大于204 结束循环 // 结束循环意味着run方法执行完毕 线程执行完成if (num 204) {break;// 否则 线程进入等待状态} else {// wait方法会释放synchronized同步锁并使当前线程进入等待状态lock.wait();}} catch (Exception e) {e.printStackTrace();}}}}}
}Java多线程-子弹射击
Problem Description
采用Java线程计数实现一个射击场景的生产者消费者程序每上膛一颗就射击一颗。
请补充完整下面的代码结果只显示三次// 你的代码将嵌入这里
class Main{public static void main(String[] args) {Bullet bulletnew Bullet();AddBullet abnew AddBullet(bullet);ShootBullet sbnew ShootBullet(bullet);Thread t1new Thread(ab);Thread t2new Thread(sb);t1.start();t2.start();}
}Output Description
lock and load~~~~
fire!!!
lock and load~~~~
fire!!!
lock and load~~~~
fire!!!解题代码
// Bullet 类
class Bullet {// isLoad 是否上膛private boolean isLoad false;// count 射击次数private int count 0;// set get方法public boolean isLoad() {return isLoad;}public void setLoad(boolean load) {isLoad load;}public int getCount() {return count;}public void setCount(int count) {this.count count;}
}// AddBullet类 相当于生产者线程 实现Runnable接口
class AddBullet implements Runnable {// bullet成员 子弹private Bullet bullet null;// 构造方法public AddBullet(Bullet bullet) {this.bullet bullet;}// 实现run方法 线程启动之后 会执行run方法Overridepublic void run() {// 循环while (true) {// 同步代码块 synchronized (bullet) {// 如果 子弹没有上膛 并且 射击次数小于等于2 count从0开始if (!bullet.isLoad() bullet.getCount() 2) {// 打印信息System.out.println(lock and load~~~~);// 将子弹设置为上膛状态bullet.setLoad(true);}// 唤醒其它线程(消费者线程) bullet.notifyAll();try {// 如果射击次数大于2 代表已经射击了3次 0 1 2if (bullet.getCount() 2) {// 跳出循环 break;// 如果没有射击3次} else {// 调用wait方法 释放锁并使当前执行线程处于等待状态bullet.wait();}} catch (Exception e) {e.printStackTrace();}}}}
}// ShootBullet类 相当于消费者线程 实现Runnable接口
class ShootBullet implements Runnable {// bullet 成员 子弹private Bullet bullet null;// 构造方法public ShootBullet(Bullet bullet) {this.bullet bullet;}// 实现run方法Overridepublic void run() {// 循环while (true) {// 同步代码块synchronized (bullet) {// 如果 子弹已经上膛 并且 射击次数小于等于2if (bullet.isLoad() bullet.getCount() 2) {// 打印信息System.out.println(fire!!!);// 将射击次数加1bullet.setCount(bullet.getCount() 1);// 将子弹设置为未上膛状态bullet.setLoad(false);}// 唤醒其它所有线程(生产者线程)bullet.notifyAll();try {// 如果射击次数大于2 代表已经射击了3次 0 1 2if (bullet.getCount() 2) {// 跳出循环 break;// 如果没有射击3次} else {// 调用wait方法 释放锁并使当前执行线程处于等待状态bullet.wait();}} catch (Exception e) {e.printStackTrace();}}}}
}