版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、/第二章:1.编写程序,实现计算“1+3+5+.+99”的值public class Add public static void main(String args)int sum=0;for(int i=1;i<100;i+ )if(i%2!=0)sum+=i;System.out.print(sum);/第二章 2返回y值public class ReturnY public static void main(String args)int y=function(3);System.out.print(y); public static int function(int x) int
2、 y; if(x>0) y=x+3; else if(x=0) y=0; else y=x*x-1; return y; /第二章 3编写程序,冒泡法排序public class MaoPao public static void main(String args) int arr=25,24,12,76,101,96,28;for(int i=0;i<arr.length-1;i+)/定义内层循环for(int j=0;j<arr.length-i-1;j+)if(arrj>arrj+1)/比较相邻元素/ 下面三行代码用来交换两个元素int temp=arrj;arr
3、j=arrj+1;arrj+1=temp;for(int i=0;i<arr.length;i+)System.out.println(arri+"");/ 第三章 1.设计学生类Student,并进行测试class Studentprivate String name;private double grade;public Student()public Student (String name,double grade)=name;this.grade=grade;public String getName()return name;public
4、 void setName(String name)=name;public double getGrade()return grade;public void setGrade(double grade)this.grade=grade;public class Test public static void main(String args) Student s=new Student();s.setName("zhangsan");s.setGrade(90);Student s1=new Student("lisi",100);
5、/第三章2.定义一个Father和Child类,并进行测试class Fatherprivate String name="zhangjun"class Childpublic void introFather()System.out.println("My Fathers name"+name);public class Test0 public static void main(String args)Father.Child child=new Father().new Child();roFather();/第四章1设计一个学生
6、类Students和它的一个子类Undergraduateclass Studentspublic String name;public int age;public Students(String name,int age)=name;this.age=age;public void show()System.out.println("name:"+name+"age:"+age);class UnderGraduate extends Studentspublic String degree;public UnderGraduate
7、(String name,int age,String degree)super(name,age);this.degree=degree;public void show()System.out.println("name:"+name+"age:"+age+"degree:"+degree);public class Test1 public static void main(String args)Students student=new Students("zhangsan",16);student.sho
8、w();UnderGraduate underGraduate=new UnderGraduate("lisi",20,"bechalor");underGraduate.show();/第四章2设计一个Shap接口和它的两个实现类Square和Circleinterface Shapedouble area(double givenValue);class Square implements Shapepublic double area(double sideLength)return sideLength*sideLength;class Circ
9、le implements Shapepublic double area(double r)return Math.PI*r*r;public class test2 public static void main(String args) Shape square=new Square();Shape circle=new Circle();System.out.println(square.area(2);System.out.println(circle.area(3);/第四章3自定义一个异常类NoThisSoundException和Player类,在Player的play()方法
10、中使用自定义异常class NoThisSoundException extends Exceptionpublic NoThisSoundException()super();public NoThisSoundException(String message)super(message);class Playerpublic void play(int index) throws NoThisSoundExceptionif(index>10)throw new NoThisSoundException("你播放的歌曲不存在");System.out.print(
11、"正在播放歌曲");public class Test3 public static void main(String args)Player player=new Player();tryplayer.play(13);catch (NoThisSoundException e)System.out.print("异常信息为:"+e.getMessage();第五章5简答题1、一种是继承java.lang包下的Thread类,覆写Thread类的run()方法,在run()方法中实现运行在线程上的代码。new Thread() public void
12、run().start();另一种就是实现java.lang.Runnable接口,同样是在run()方法中实现运行在线程上的代码。new Thread(new Runnable() public void run().start() 2、调用sleep()方法,正在执行的线程主动让出CPU去执行其他线程,在sleep()方法指定的时间过后,CPU才会回到这个线程上继续往下执行,如果当前线程进入了同步锁,sleep()方法并不会释放锁,即使当前线程使用sleep()方法让出了CPU,但其它被同步锁挡住了的线程也无法得到执行。wait()在一个已经进入了同步锁的线程内进行调用,让当前线程暂时让出
13、同步锁,以便其它正在等待此锁的线程可以得到同步锁并运行。当其它线程调用了notify()方法后,调用wait()方法的线程就会解除wait状态,当再次获得同步锁后,程序可以继续向下执行。/通过继承Thread类方式创建两个线程,在Thread构造方法中指定线程的名称并打印出来public class MyThread extends Thread public MyThread (String name)super(name);public void run()System.out.println(this.getName();public static void main(String ar
14、gs)new MyThread("Thread1").start();new MyThread("Thread2").start();/通过实现Runnable接口的方式创建一个新的线程,要求main线程打印100次“main”,新线程打印50次“new”public class MyRunnable implements Runnable public void run()for(int i=0;i<50;i+)System.out.println("new");public static void main(String a
15、rgs) new Thread(new MyRunnable().start();for (int i=0;i<100;i+)System.out.println("main");/模拟三个老师同时分发80份学习笔记,每个老师相当于一个线程class Teacher implements Runnableprivate int notes=80;public void run()while(true)dispatchNotes();/调用售票方法if(notes<=0)break;private synchronized void dispatchNotes()
16、if(notes>0)tryThread.sleep(10);/经过的线程休眠10scatch(InterruptedException e)e.printStackTrace();System.out.println(Thread.currentThread().getName()+"-发出的笔记"+notes+"-");public class Test4 public static void main(String args) Teacher t=new Teacher();new Thread(t,"陈老师").star
17、t();new Thread(t,"高老师").start(); new Thread(t,"李老师").start();/第五章4.编写10个线程,第一个从1加到10,第二个从11加到20.第十个线程从91加到100,最后把10个线程结果相加public class Accumulator extends Thread private int startNum;public static int sum;public Accumulator(int startNum)this.startNum=startNum; public static synch
18、ronized void add(int num)sum+=num;public void run()int sum=0;for(int i=0;i<10;i+)sum+=startNum+i;add(sum);public static void main(String args) throws ExceptionThread threadlist= new Thread10;for(int i=0;i<10;i+)threadlisti= new Accumulator(10*i+1);threadlisti.start();for(int i=0;i<10;i+)threadlisti.join();System.out.println("sum is:"+sum);/第六章1.编写程序,实现字符串大小写的转换并倒序输出public class Test public static void
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年鄂尔多斯生态环境职业学院单招综合素质笔试参考题库带答案解析
- 2026江西九江市修水县投资集团有限公司招聘21人考试备考题库及答案解析
- 2026贵州铜仁市第二人民医院收费室见习生招募1人考试备考试题及答案解析
- 2026自然资源部海岛研究中心专业技术人员招聘15人考试备考题库及答案解析
- 2026江西农业大学国土资源与环境学院国土学院招聘临时工1人考试备考题库及答案解析
- 2026河北石家庄市供热管理集团有限公司劳务派遣制人员招聘2人考试备考试题及答案解析
- 2026年泸州市部分企事业单位人才引进88人备考题库附答案详解
- 2026年衡水市景县人民医院公开招聘医护人员备考题库及一套参考答案详解
- 2026年郑州市管城回族区紫东路社区卫生服务中心招聘康复技士备考题库及参考答案详解一套
- 2026年维西县人力资源市场关于公开招聘二名森林草原专业扑火队队员备考题库及参考答案详解一套
- 二十届四中全会测试题及参考答案(第三套)超难
- 机器人行业薪酬调查
- 2025年事业单位面试心理素质测试模拟试卷及答案
- 2025-2030疫苗冷链物流体系建设标准与第三方服务市场机会报告
- 2025年江苏省事业单位招聘考试教师招聘体育学科专业知识试卷(秋季篇)
- 2025年中国橡胶粉改性沥青(AR)行业市场分析及投资价值评估前景预测报告
- 【完整版】2025年自考《马克思基本原理概论》真题及答案
- 胸外科围手术期护理指南
- 大数据中心建设项目标准与工程造价指标分析
- 2025年中山城市建设集团有限公司“鸿鹄”专项人才引进笔试参考题库附带答案详解
- 吸塑机安全教育培训内容课件
评论
0/150
提交评论