湖南大学2012年JAVA课程考试试卷

上传人:ch****o 文档编号:158136301 上传时间:2022-10-03 格式:DOC 页数:5 大小:25.01KB
返回 下载 相关 举报
湖南大学2012年JAVA课程考试试卷_第1页
第1页 / 共5页
湖南大学2012年JAVA课程考试试卷_第2页
第2页 / 共5页
湖南大学2012年JAVA课程考试试卷_第3页
第3页 / 共5页
点击查看更多>>
资源描述
2012-2013第一学期Java 程序设计 专业班级:_学号:_ 姓名:_ 分数:_第一题、 写出下面程序的运行结果.(10分)import java.io.*;public class abc public static void main(String args ) AB s = new AB(Hello!,I love JAVA.); System.out.println(s.toString( ); class AB String s1; String s2; AB( String str1 , String str2 ) s1 = str1; s2 = str2; public String toString( ) return s1+s2;第二题、编程题(90分)1. 创建一个桌子Table类,该类中有桌子名称,重量,桌面宽度,长度及桌子高度属性。其中有:构造函数初始化所有数据成员;Area() :计算桌面的面积;Display(): 在屏幕上输出所有数据成员的值;ChangeWeight(int ):改变桌子重量的函数。在main()中实现创建一个桌子对象,计算桌面的面积,改变桌子重量,并在屏幕上输出所有桌子数据成员的值。(15分)2. 用main()创建一个类,令其掷出try块内的Exception类的一个对象。为Exception的构建器赋予一个字串参数。在catch从句内捕获例外,并打印出字串参数。添加一个finally从句,并打印一条消息,证明自己真正到达那里。(15分)class TestTest() throws Exceptionthrow new Exception(自定义异常!);public class ExceptionTest public static void main(String args)tryTest test=new Test();catch(Exception e)System.out.println(catch快捕获异常:+e.getMessage();finallySystem.out.println(进入finally块);3. 创建一个double 数组,随即产生数组元素的数值。然后将其排序,打印排序结果。查找第5个元素,打印其值。(10分)package sort;public class sort public static void main(String args)double db=new double10;for(int i=0;i10;i+)dbi=Math.random();for(int i=0;i10;i+)System.out.println(dbi);for(int i=0;ii;j-)if(dbjdbj-1)double t=dbj;dbj=dbj-1;dbj-1=t;System.out.println(排序之后的结果为:);for(int i=0;i10;i+)System.out.println(dbi);4. 用HashMap存放一组星期一到星期日的组对,1-7对应Mon-Sun.。(15分) a.打印这组数据 b.分别打印1-7 和Mon-Sunc.查找3对应的英文星期package hashmap;import java.util.HashMap;public class hashmap public static void main(String args)HashMap map=new HashMap();map.put(1, mon);map.put(2,tue);map.put(3,wend);for(int i=1;i=3;i+)System.out.println(星期+i+是:+map.get(i);5. 编写两个线程:一个线程向数组中存数据,一个线程向数组中取数据。练习如何实现线程的同步。(15分)import java.io.*;import java.util.*;class WriteThread extends Threadpublic ArrayList List=new ArrayList();public WriteThread(ArrayList a)List=a;public void run()synchronized (this) for(int i=0;i10;i+)List.add(i);System.out.printf(写入:%dn, i);trysleep(500);catch(Exception e)return;class ReadThread extends Threadpublic ArrayList List=new ArrayList();public ReadThread(ArrayList a)List=a;public void run()synchronized (this) for(int i=0;i10;i+)System.out.printf(读取:%dn,List.get(i);trysleep(500);catch(Exception e)return;public class RWthread public static ArrayList List=new ArrayList();public static void main(String args)WriteThread writeThread=new WriteThread(List);ReadThread readThread=new ReadThread(List);writeThread.start();readThread.start();6. 从标准设备中输入若干行英文句子,直到输入bye结束,将这些字符串写入文件。(10分)/通过下面这个方法可以实现标准输入和输出,使用scanner类package standarinput;import java.io.*;import java.util.*;public class standarinput public static void main(String args)Scanner scanner=new Scanner(System.in);System.out.println(请输入你的名字:);String tempString=scanner.next();System.out.println(你的名字是:+tempString);System.out.println(请输入一个整数:);int a=scanner.nextInt();System.out.printf(你输入的整数是:%dn,a);/通过BufferedReader进行标准输入以及输出到文本当中import java.io.*;public class BufferReader public static void main(String args) throws IOExceptionFile fileName=new File(F:/abc.txt);BufferedReader br=new BufferedReader(new InputStreamReader(System.in);BufferedWriter bw=new BufferedWriter(new FileWriter(fileName,true);while(true)String temp=br.readLine();bw.write(temp+rn);if(temp.equals(bye)System.out.println(输入结束);break;bw.flush();bw.close();br.close();7. 打印出所有的“水仙花数”,所谓的“水仙花数”是指一个三位数,其各位数字的立方和等于其本身。例如:153是个“水仙花数”,因为153= (10分)package waterflowers;public class waterflowers public static int judge(int x)int a,b,c,t=x;a=x%10;x/=10;b=x%10;x/=10;c=x%10;if(a*a*a+b*b*b+c*c*c=t)return 1;else return 0;public static void main(String args)for(int i=100;i1000;i+)if(judge(i)=1)System.out.println(i+是一个水仙花数);
展开阅读全文
相关资源
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 考试试卷


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!