全国2011年1月Java语言程序设计自考试题_第1页
全国2011年1月Java语言程序设计自考试题_第2页
全国2011年1月Java语言程序设计自考试题_第3页
全国2011年1月Java语言程序设计自考试题_第4页
全国2011年1月Java语言程序设计自考试题_第5页
已阅读5页,还剩48页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、河北工业大学计算机学院网络编程实验报告实验一:Java 基本语法实验目的:了解 Java 的数据类型,掌握各种变量的声明方式,理解运算符的优先级,掌握 Java 基本数据类型、运算符与表达式,掌握顺序结构、选择结构和循环结构语法的程序设计方法。实验要求: 1、编写一个声明 Java 不同数据类型变量的程序。2、编写使用不同选择结构的程序。3、编写使用不同循环结构结构的程序。实验内容: 1、声明不同数据类型变量1)编写声明不同数据类型变量的程序,代码见实验指导书。运行结果:2)Integer类在某对象中打包了原始类型为int的值。Integer类型对象包含int型的单个域。此外,此类提供了许多方

2、法,可以将int型转换为string 型,也可以将Sring型转换为int型,还包含处理int类型时的其他有用常量和方法。代码见实验指导书。运行结果:2、使用选择结构使用 if.else 语句,编写源程序文件,代码见实验指导书。 运行结果:使用 switch 语句1)编写程序用swith语句实现从键盘读如1,2,3时,屏幕提示不同信息。代码见实验指导书。运行结果: 3、使用循环结构使用for语句1)程序源代码见实验指导书。 2)改正源程序中错误:public static void main (String args) throws java.io.IOException 运行结果: 使用wh

3、ile语句1)程序源代码实验指导书: 运行结果:多重循环,输出九九乘法表的程序。 运行结果:4、编程题,编写程序并写出运行结果1)用分支语句编程,输入一个学生成绩,给出相应等级: 90100 优 8089 良 7079 中 6960 及格 059 不及格代码:import java.io.*;class aaa public static void main(String args) throws IOException BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in); System.out.

4、println(Please press the score in:); int num=Integer.parseInt(stdin.readLine(); num=(int)(num/10); switch(num) case 10: case 9: System.out.println(The students level is A!); break; case 8: System.out.println(The students level is B!); break; case 7: System.out.println(The students level is C!); brea

5、k; case 6: System.out.println(The students level is D!); break; case 5: case 4: case 3: case 2: case 1: case 0: System.out.println(The students level is E!); break; default: System.out.println(The key press is wrong!); 结果:2) 采用for循环求1至1000之内的所有“完全数”。所谓“完全数”是指一个数,恰好等于它的因子之和。例如,6是一个完全数,因为6的因子为1、2、3,而6

6、1十2十30 代码:class aaa public static void main(String args) System.out.println(Now looking for the number between 1 to 1000 :);for(int i=1;i=1000;i+)int sum=0;for(int j=1;j=i/2;j+)if(i%j=0) sum+=j;if(sum=i) System.out.print(t+i+t);System.out.println();System.out.println(select over!); 结果:3)一个整数的各位数字之和能

7、被9整除,则该数也能被9整除。编程验证给定的整数能否被9整除。代码:import java.io.*;class ssspublic static void main(String arg) throws IOExceptionBufferedReader stdin = new BufferedReader(new InputStreamReader(System.in);int num=0;int NO;System.out.print(请输入给定的整数:);String str=stdin.readLine();NO=Integer.parseInt(str); for(int i=0;

8、istr.length();i+) num+=NO%10; NO=(int)(NO/10);if(num%9=0)System.out.println(str+可以被9整除。);else System.out.println(str+不可以被9整除。);结果:4)已知XYZ+YZZ=532,其中X、Y和Z为数字,编程求出X,Y和Z的值。 代码:class ssspublic static void main(String arg)int X,Y,Z;for(X=1;X=4;X+)for(Y=1;Y=4;Y+)for(Z=0;Z=2;Z+)int sum=(100*X+10*Y+Z)+(100*

9、Y+10*Z+Z);if(sum=532)System.out.println(X= +X+ , Y= +Y+ , Z= +Z); 结果:实验二:面向对象编程实验目的:通过编程和上机实验理解 Java 语言是如何体现面向对象编程基本思想,熟悉类的封装方法以及如何创建类和对象,熟悉成员变量和成员方法的特性,熟悉类的继承性和多态性的作用,熟悉包、接口的使用方法,掌握OOP方式进行程序设计的方法。实验要求: 1、编写程序实现类的定义和使用。2、编写不同成员和不同成员方法修饰方法的程序。3、编写体现类的继承性(成员变量、成员方法、成员变量隐藏)的程序和多态性(成员方法重载、构造方法重载)的程序。4、编

10、写接口的定义和使用的程序。5、编写包的定义和使用的程序。实验内容: 1、类的定义和使用代码:class Tablepublic String name;public int weight,X,Y,high;public Table(String nwname,int nwweight,int nwX,int nwY,int nwhigh)=nwname;this.weight=nwweight;this.X=nwX;this.Y=nwY;this.high=nwhigh;public int Area(int X,int Y)int Area=X*Y;return Area;

11、public void ChangeWeight(int NewWeight)weight=NewWeight;public void Display()System.out.println(The Tables Name: Name= +name);System.out.println(The Tables Size: X= +X+ ,Y= +Y);System.out.println(The Tables Area: Area= +Area(X,Y);System.out.println(The Tables Weight: Weight= +weight);System.out.prin

12、tln(The Tables High: High= +high);public static void main(String arg)System.out.println(Now Display MyTables Information:);Table tb=new Table(Mytable,170,10,15,75);tb.Display();System.out.println(Now Change MyTables Weight to 200!);tb.ChangeWeight(200);System.out.println(Now Display MyTables New Inf

13、ormation:);tb.Display();结果:2、修饰符的使用程序功能:通过两个类StaticDemo、TestDemo说明静态变量/方法与实例变量/方法的区别,程序源代码见实验指导书。对源程序进行编译,查错并运行。静态方法不能访问实例变量。3、继承和多态的作用代码:abstract class RodentString name;public String getName()return name;public void setName(String name)=name;public abstract void run();class Mouse extends

14、Rodentpublic void run()class Gerbil extends Mouseclass Hamster extends Mouse4、接口的定义和使用5、包的定义和使用 创建自定义包Mypackage 在包中创建类,编译Test_YMD.java文件,分析运行结果。 编写使用包Mypackage中Test_YMD类的程序,编译并运行程序,分析程序的运行结果。6、编程题,编写程序并写出运行结果 (1)创建一个名称为Pay的类,该类包括工作小时、每小时工资、扣缴率、应得工资总额和实付工资等5个双精度型的成员变量。创建3个重载的应得工资computeNetPay()方法。应得工

15、资是工时乘以每小时工资的计算结果。当computeNetPay()接收代表小时、扣缴率和工资率的数值时,计算出应得工资=工作小时*每小时工资*(1扣缴率)*(1工资率)。当computeNetPay()接收两个参数时,扣缴率假定为15%,计算出应得工资=工作小时*每小时工资*(10.15)*(1工资率) 当computeNetPay()接收一个参数时,扣缴率假定为15%,每小时工资率为4.65%。同时编写一个测试类,该测试类的main方法测试所有3个重载的方法。代码:class Paydouble work_hour,price=100,deduct_rate;double should_pa

16、y,real_pay=0;public double computeNetPay(double work_hour)should_pay=work_hour*this.price*(1-0.15)*(1-0.0465);return should_pay;public double computeNetPay(double work_hour,double price_rate)should_pay=work_hour*this.price*(1-0.15)*(1-price_rate);return should_pay;public double computeNetPay(double

17、work_hour,double price_rate,double deduct_rate)should_pay=work_hour*this.price*(1-deduct_rate)*(1-price_rate);return should_pay;public void display()System.out.println(The employee should be pay $ +should_pay); public static void main(String args) Pay pay=new Pay(); System.out.println(The first empl

18、oyee worked 8hour); puteNetPay(8.0); pay.display(); System.out.println(The first employee worked 8hour and price_rate 8%); puteNetPay(8.0,0.08); pay.display(); System.out.println(The first employee worked 8hour and price_rate 10% and deduct_rate is 18%); puteNetPay(8.0,0.1,0.18); pay.display(); 结果:(

19、2)商店销售某一件商品,商店每天公布统一的折扣(discount)。同时允许销售人员在销售时灵活掌握价格(price),在统一折扣的基础上,对一次购入10件以上者,还可以销售9.5折优惠。现已知当天5名售货员的销售情况为: 售货员编号(num) 销售件数(quantity) 销售单价(price) 101 3 126.8 221 8 125.6 325 10 124.8 108 45 123.4 901 100 121.5 编写销售商品类Sale和含有main方法的公共类Test,计算当天此商品的总销售额sum,以及每件商品的平均售价,并在显示器上显示。代码:import java.io.*;

20、class Saledouble discount,price;int sale_sum=0;double sum=0.0,price_avg=0.0;public void change_discount(double discount)this.discount=discount;public void SaleCount(int person_sale_num,double price)if(person_sale_num10) sum+=price*discount*person_sale_num;else sum+=0.95*price*discount*person_sale_nu

21、m;sale_sum+=person_sale_num;price_avg=sum/sale_sum;/return sale_sum;public void display()System.out.println(The total sale namber is: +sale_sum);System.out.println(The total sale money is: +sum);System.out.println(The avg of the price is: +price_avg); class Testpublic static void main(String args)th

22、rows IOException Sale sale=new Sale(); System.out.println(Press the discount :); BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in); double disc=Double.parseDouble(stdin.readLine(); sale.change_discount(disc); System.out.println(The saler 101 saled 3 goods at price 126.8;); s

23、ale.SaleCount(3,126.8); System.out.println(The saler 221 saled 8 goods at price 125.6;); sale.SaleCount(8,125.6); System.out.println(The saler 325 saled 10 goods at price 124.8;); sale.SaleCount(10,124.8); System.out.println(The saler 108 saled 45 goods at price 123.4;); sale.SaleCount(45,123.4); Sy

24、stem.out.println(The saler 901 saled 100 goods at price 121.5;); sale.SaleCount(100,121.5); sale.display(); 结果:(3)定义接口Shape及其抽象方法getArea()和getPerimeter()用于计算图形和面积和周长。定义类Rectangle(矩形)、类Circle(圆形)、类Triangle(三角形),要求这些类继承点类Coordinates()并实现接口的抽象方法。代码:class Test public static void main(String args)circle

25、c=new circle();c.display();rectangle r=new rectangle();r.display();triangle t=new triangle();t.display(); interface cal_Shapefinal double PI=3.14;abstract double getArea(double r);abstract double getArea(double a,double b);abstract double getArea(double a,double b,double c);abstract double getPerime

26、ter(double r);abstract double getPerimeter(double a,double b);abstract double getPerimeter(double a,double b,double c);class Coordinates implements cal_Shapepublic double getArea(double r) return PI*r*r;public double getPerimeter(double r)return 2*PI*r;public double getArea(double a,double b)return

27、a*b;public double getPerimeter(double a,double b)return 2*(a+b);public double getArea(double a,double b,double c)double s=(a+b+c)/2;return Math.sqrt(s*(s-a)*(s-b)*(s-c);public double getPerimeter(double a,double b,double c)return a+b+c;class circle extends Coordinatespublic void display()double circ

28、le_area,circle_perimeter;Coordinates x=new Coordinates();circle_area=x.getArea(2.0);circle_perimeter=x.getPerimeter(2.0);System.out.println(半径为2的圆的面积为:+circle_area+,周长为:+circle_perimeter);class rectangle extends Coordinatespublic void display() double rectangle_area,rectangle_perimeter;Coordinates y

29、=new Coordinates();rectangle_area=y.getArea(2.0,3.0);rectangle_perimeter=y.getPerimeter(2.0,3.0);System.out.println(长,宽为2,3的矩形的面积为:+rectangle_area+,周长为:+rectangle_perimeter);class triangle extends Coordinatespublic void display()double triangle_area,triangle_perimeter;Coordinates z=new Coordinates()

30、;triangle_area=z.getArea(3.0,4.0,5.0);triangle_perimeter=z.getPerimeter(3.0,4.0,5.0);System.out.println(三边长为3,4,5的三角形的面积为:+triangle_area+,周长为:+triangle_perimeter);结果:实验三:异常处理程序设计 实验目的:了解Java中异常处理(exception)的作用及常用的异常类,掌握异常处理的设计方法。实验要求:理解系统异常处理的机制和创建自定义异常的方法。实验内容: 1)下面的程序中定义了一个用户程序的异常InsuffivientFound

31、sException,这个异常用来处理帐户资金不足的逻辑错误。2)3)根据题目要求,编写程序并写出运行结果 1、设计一个Java程序,自定义异常类,从命令行(键盘)输入一个字符串,如果该字符串值为“XYZ”,则抛出一个异常信息“This is a XYZ”,如果从命令行输入ABC,则没有抛出异常。(只有XYZ和ABC两种输入)。代码:import java.io.*; class UserExceptionDemopublic static void main(String args)throws IOException BufferedReader stdin = new BufferedR

32、eader(new InputStreamReader(System.in);System.out.print(请输入字符串XYZ/ABC:);String str=stdin.readLine(); try if(str=ABC)System.out.println(Normal);elsethrow new UserDefineException(); catch(UserDefineException e) System.out.println(e.toString(); class UserDefineException extends ExceptionUserDefineExcep

33、tion()System.out.println(Exception occured);public String toString()returnThis is a XYZ;结果:2、使用命令行方式输入四个参数,分别是姓名、数学成绩、英语成绩、Java成绩,求总成绩和平均成绩,处理数组下标越界、成绩不是数组、成绩输入不合理(不在1-100之间)的异常。要去:自己定义输出成绩不合理的异常。提示1:数组下标越界异常为:ArrayIndexOutOfBoundsException 成绩不是数组的异常采用Java中的异常:NumberFormatException 提示2:自定义的异常通常是Exce

34、ption的子类。代码:import java.io.*; class Scorepublic static void main(String args)throws IOException String name;int info=new int3;int i;float sum=0.0f;float avg=0.0f;System.out.println(请输入学生的姓名:);BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in);name=stdin.readLine();System.out.

35、println(请输入学生的数学成绩、英语成绩、Java成绩:);tryfor(i=0;i3;i+)infoi=Integer.parseInt(stdin.readLine(); catch(ArrayIndexOutOfBoundsException e1)e1.printStackTrace();catch(NumberFormatException e2)e2.printStackTrace(); for(i=0;i3;i+) int Temp=infoi; try if(Temp100)throw new UserDefineException();elsesum+=infoi; c

36、atch(UserDefineException e)System.out.println(e.toString(Temp); avg=sum/3.0f; System.out.println(The sum=+sum+,the avg=+avg);class UserDefineException extends ExceptionUserDefineException()System.out.println(Exception occured);public String toString(int a)returnThe number +a+isnt a score!;结果:实验四:多线程

37、程序设计实验目的:理解线程的概念、线程的生命周期,掌握多线程的编程:继承Thread类与使用Runnable接口。实验要求:1、掌握两种创建线程的方法:一种是创建用户自己的线程子类,另一种是在用户自己的类中实现Runnable接口。2、掌握线程优先极。3、掌握线程的同步方法。实验内容: 用创建Thread类的子类的方法实现多线程 用实现Runnable接口的方法实现多线程 线程优先级的作用:用继承Thread类和执行Runnable接口的方法创建两个线程,并测试这两个线程的同时运行情况。 a. 将两个线程设为同优先级,比较运行情况。 代码:class outputClass implement

38、s RunnableString name;outputClass(String s)name=s;public void run()for(int i=0;i10;i+)System.out.println(name);class runThreadspublic static void main(String args)outputClass out1=new outputClass(Thread 1);outputClass out2=new outputClass(Thread 2);Thread t1=new Thread(out1);Thread t2=new Thread(out

39、2);t1.setPriority(1);t2.setPriority(1);t1.start();t2.start();结果:b. 将两个线程设为同优先级, 比较线程调用sleep()/yeild()方法后出现什么情况。代码:class outputClass implements RunnableString name;outputClass(String s)name=s;public void run()for(int i=0;i10;i+)System.out.println(name);tryThread.sleep(400); catch(InterruptedException

40、 e)class runThreadspublic static void main(String args)outputClass out1=new outputClass(Thread 1);outputClass out2=new outputClass(Thread 2);Thread t1=new Thread(out1);Thread t2=new Thread(out2);t1.setPriority(1);t2.setPriority(10);t1.start();t2.start();结果:c.将两个线程设为不同优先级,比较以上两种情况。代码:t1.setPriority(1

41、);t2.setPriority(2);结果:代码:t1.setPriority(1);t2.setPriority(10);结果:请根据题目要求,编写程序并写出运行结果 1、 编写一个应用程序,创建三个线程分别显示各自的时间。代码:import java.util.*;import java.text.*; class ThreeTimeThread extends Threadpublic ThreeTimeThread(String str)super(str);public void run() while (true)SimpleDateFormat formatter = new

42、SimpleDateFormat (yyyy.MM.dd G at hh:mm:ss z);Date currentTime = new Date();trysleep(1000);catch (Exception e)String dateString = formatter.format(currentTime);System.out.println(getName()+:+dateString);public static void main(String args)throws Exceptionnew ThreeTimeThread(first).start(); new Three

43、TimeThread(second).start();new ThreeTimeThread(third).start(); 结果:2、 利用Java中的waitnotify调度实现操作系统课程中介绍的生产者/消费者模型,要求系统中有3个生产者线程和3个消费者线程,用于存放数据的缓冲区采用字符数组来模拟,缓冲区的个数为10个。 代码:class SyncStackprivate int index=0;private char buffer=new char10;public synchronized char pop()while(index=0)trySystem.out.println(

44、pop waiting.);this.wait();catch(InterruptedException e)index-;this.notify();return bufferindex;public synchronized void push(char c)while(index=buffer.length)trySystem.out.println(push waiting.);this.wait(); catch(InterruptedException e)bufferindex=c;index+;this.notify();public synchronized void sho

45、w()for(int i=0;iindex;i+)System.out.println(bufferi);System.out.println();class Producer implements RunnableSyncStack theStack;public Producer(SyncStack s)theStack=s;public void run()char c;for(;)c=(char)(Math.random()*26+A);theStack.push(c);System.out.println(Produced:+c);theStack.show();tryThread.

46、sleep(int)(Math.random()*1000);catch(InterruptedException e)class Consumer implements RunnableSyncStack theStack;public Consumer(SyncStack s)theStack=s;public void run()char c;for(;)c=theStack.pop();System.out.println(Consumed:+c);theStack.show();tryThread.sleep(int)(Math.random()*1000);catch(Interr

47、uptedException e)public class SyncTestpublic static void main(String args)SyncStack stack=new SyncStack();Producer pro=new Producer(stack);Thread t1=new Thread(pro);Thread t2=new Thread(pro);Thread t3=new Thread(pro);t1.start();t2.start();t3.start();Consumer con=new Consumer(stack);Thread t4=new Thread(con);Thread t5=new Thread(con);Thread t6=new

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论