收藏 分销(赏)

沈阳师范---Java程序设计---实验题.docx

上传人:xrp****65 文档编号:5913089 上传时间:2024-11-23 格式:DOCX 页数:7 大小:26.59KB 下载积分:10 金币
下载 相关 举报
沈阳师范---Java程序设计---实验题.docx_第1页
第1页 / 共7页
沈阳师范---Java程序设计---实验题.docx_第2页
第2页 / 共7页


点击查看更多>>
资源描述
【沈师710寝室】Java—程序题 1.假定根据学生的3门学位课程的分数决定其是否可以拿到学位,对于本科生,如果3门课程的平均分数超过60分即表示通过,而对于研究生,则需要平均超过80分才能够通过。根据上述要求,请完成以下Java类的设计: class Student{ private String name; private int classA,classB,classC; public Student(String name,int classA,int classB,int classC){ this.name=name; this.classA=classA; this.classB=classB; this.classC=classC; } public String getName(){return name;} public int getAverage(){return (classA+classB+classC)/3;}} class UnderGraduate extends Student{ public UnderGraduate(String name,int classA,int classB,int classC){ super(name,classA,classB,classC); } public void isPass(){ if(getAverage()>=60) System.out.println("本科生"+getName()+"的三科平均分为:"+getAverage()+",可以拿到学士学位。"); else System.out.println("本科生"+getName()+"的三科平均分为:"+getAverage()+",不能拿到学士学位。"); }} class Graduate extends Student{ public Graduate(String name,int classA,int classB,int classC){ super(name,classA,classB,classC); } public void isPass(){ if(getAverage()>=80) System.out.println("研究生"+getName()+"的三科平均分为:"+getAverage()+",可以拿到硕士学位。"); else System.out.println("研究生"+getName()+"的三科平均分为:"+getAverage()+",不能拿到硕士学位。"); }} public class StudentDemo{ public static void main(String[] args){ UnderGraduate s1=new UnderGraduate("Tom",55,75,81); Graduate s2=new Graduate("Mary",72,81,68); s1.isPass(); s2.isPass();}} 运行结果: 本科生Tom的三科平均分为:70,可以拿到学士学位。 研究生Mary的三科平均分为:73,不能拿到硕士学位。 2. 假定要为某个公司编写雇员工资支付程序,这个公司有各种类型的雇员(Employee),不同类型的雇员按不同的方式支付工资: abstract class Employee{ private String name; public Employee(String name){ this.name=name; } public String getName(){ return name; } public abstract double computeSalary(); } class Manager extends Employee{ double monthSalary; public Manager(String name,double monthSalary){ super(name); this.monthSalary=monthSalary; } public double computeSalary(){ return monthSalary; }} class Salesman extends Employee{ double baseSalary; double commision; int quantity; public Salesman(String name,double baseSalary,double commision,int quantity){ super(name); this.baseSalary=baseSalary; mision=commision; this.quantity=quantity; } public double computeSalary(){ return baseSalary+commision*quantity; }} class Worker extends Employee{ double dailySalary; int days; public Worker(String name,double dailySalary,int days){ super(name); this.dailySalary=dailySalary; this.days=days; } public double computeSalary(){ return days*dailySalary;}} public class EmployeeDemo{ public static void main(String args[]){ Manager m=new Manager("Tom",10000); Salesman s=new Salesman("Mary",2000,45,60); Worker w=new Worker("John",60,28); System.out.println("经理"+m.getName()+"的月工资为:"+puteSalary()); System.out.println("销售人员"+s.getName()+"的月工资为:"+puteSalary()); System.out.println("工人"+w.getName()+"的月工资为:"+puteSalary()); } } 运行结果:经理Tom的月工资为:10000.0 销售人员Mary的月工资为:4700.0 工人John的月工资为:1680.0 3.输入给定的Java Application程序,其中文件Rectangle.java和Point.java放入C:\javaexam中;文件TestPackage.java包含main( )方法的测试程序放在目录C:\javaexam\test下,写出运行结果,并简述打包过程。 (1)文件Rectangle.java。 package graphics.twoD; public class Rectangle { public int width = 0; public int height = 0; public Point origin; public Rectangle(Point p, int w, int h) { origin = p; width = w; height = h; } public void move(int x, int y) { origin.x = x; origin.y = y; } public int area( ) { return width*height; }} (2) 文件Point.java。 package graphics.twoD; public class Point { public int x = 0; public int y = 0; public Point(int x, int y) { this.x = x; this.y = y; } (3)文件TestPackage.java。 import graphics.twoD.*;; public class TestPackage{ public static void main(String args[]){ Point p=new Point(2,3); Rectangle r=new Rectangle(p,10,10); System.out.println("The area of the rectangle is: "+r.area()); } } 打包过程: (1)将C:\mypkg添加到classpath变量中,使该路径作为一个包的根路径。 (2)在命令行窗口中将C:\javaexam作为当前目录,输入编译指令javac –d C:\mypkg Point.java Circle.java。 (3)在命令行窗口中改变当前目录为C:\javaexam\ test,输入编译指令javac TestPackage.java,再输入解释指令java TestPackage,那么就可得到TestPackage.java的执行结果。 运行结果: The area of the rectangle is:100 4. 在类A中有两个默认的方法a、b,一个私有方法c。在A的派生类B中有3个公共的方法b、c、d。 写出定义这两个类的Java源代码,并说明哪些方法是多态的?(选择) class A { void a( ){ } void b( ) { } private void c ( ) { } } class B extends A{ public void b( ){ } public void c( ){ } public void d( ){ } } 只有方法b是多态的。注意:父类A中的方法c是私有的private,因此不能被子类B重写,不属于多态。 5. 输入如下所示的Java Application程序,写出运行结果 public class TestException { public static void main (String args[]) { int i=0; double num=0; double d[]={2.1, 3.0, 5.6}; try { while(i<4) { num+=d[i]; i++; } System.out.println(“Test1”); }catch(ArrayIndexOutOfBoundsException e) { System.out.println(“Test2”); }finally { System.out.println(“Test3”); } System.out.println(“Test4”); } } 运行结果: Test2 Test3 Test4 6. 编写程序,要求程序功能:首先输出“这是一个异常处理的例子”,然后在你程序中主动地产生一个 ArithmeticException 类型被0 除而产生的异常,并用catch 语句捕获这个异常。最后通过ArithmeticException类的对象e的方法getMessage() 给出异常的具体类型并显示出来。 public class ExceptionExam{ public static void main(String[] args){ int a=10,b=0; double c=0.0; System.out.println("这是一个异常处理的例子。"); try{ c=a/b; System.out.println(a+"/"+b+"="+c); }catch(ArithmeticException e){ System.out.println("Caught ArithmeticException: "+e.getMessage()); } } } 运行结果:这是一个异常处理的例子。 Caught ArithmeticException: / by zero 7.利用下面的关键代码编写一个完整的程序,理解String和StringBuffer类的使用。 public class StringDemo{ public static void main(String args[ ]) { String s=new String("This is an demo of the String method."); System.out.println("Length: "+s.length()); System.out.println("SubString: "+s.substring(11,15)); StringBuffer sb=new StringBuffer("Hello World!"); sb.append(" Hello Java!"); sb.insert(12," And"); System.out.println(sb); System.out.println(sb.charAt(0)); sb.setCharAt(0,'h'); System.out.println(sb.charAt(0)); System.out.println(sb); }} 运行结果: Length:37 SubString:demo Hello World! And Hello Java! H h helloWorld! And Hello Java! 8.编写一个Java Application程序,实现读取并输出指定文件的内容的功能。 import java.io.*; public class ReadFile{ public static void main(String[] args) throws IOException{ BufferedReader br=new BufferedReader(new FileReader("ReadFile.java")); String s=br.readLine(); while(s!=null){ System.out.println(s); s=br.readLine(); } br.close(); }} 9.编写一个Java Application程序,实现接收键盘输入的数据,并写入到指定文件中的功能。 import java.io.*; public class WriteFile2{ public static void main(String[] args) throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw=new BufferedWriter(new FileWriter("tt.txt")); String s=br.readLine(); while(!s.equals("exit")){ bw.write(s); bw.newLine(); s=br.readLine(); } br.close(); bw.close();}} 10.输入下面的Java Application程序,运行该程序,说明程序的功能。 1: import java.io.*; 2: public class CopyFile { 3: public static void main(String[] args) { 4: try { 5: FileInputStream fis = new FileInputStream("CopyFile.java"); 6: FileOutputStream fos = new FileOutputStream("temp.txt"); 7: int read = fis.read(); 8: while ( read != -1 ) { 9: fos.write(read); 10: read = fis.read(); 11: } 12: fis.close(); 13: fos.close(); 14: } 15: catch (IOException e) { 16: System.out.println(e); 17: } 18: } 19:} 其功能是完成文件的复制:通过字节流从“copyFile.java”文件中读取数据并写入到“temp.txt”文件中去,实现copy功能。 11.通过系统标准输入流System.in读取从键盘输入的三个整数,通过System.out从屏幕输出。 import java.io.*; public class SystemStreamTest { public static void main(String[] args) { InputStreamReader isr=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(isr); String s=null; String[] ss=null; int a,b,c; System.out.println("请输入三个整数"); s=br.readLine(); while(!s.equals("exit")) { ss=s.split(","); if(ss.length!=3) System.err.println("数据少于三个"); else { a=Integer.parseInt(ss[0]); b=Integer.parseInt(ss[1]); c=Integer.parseInt(ss[2]); System.err.println(a,b,c); } s=br.readLine(); } br.close(); }} 12.编写一个Java Application程序,实现如下的设计功能:运行该程序可以列出当前目录下的文件。 import java.io.*; public class FileList2{ public static void main(String[] args){ File dir=new File("."); File files[]=dir.listFiles(); System.out.println(dir); for(int i=0;i<files.length;i++){ if(files[i].isFile()) System.out.println("\t"+files[i].getName()); else System.out.println("<DIR>\t"+files[i].getName()); }}} 13.输入下面的Java Application程序,运行该程序,并简要分析程序的运行结果。 class SimpleThread extends Thread { public SimpleThread(String str) { super(str); //调用其父类的构造方法 } public void run() { //重写run方法 for (int i = 0; i < 10; i++) { System.out.println(i + " " + getName()); //打印次数和线程的名字 try { sleep((int)(Math.random() * 1000)); //线程睡眠,把控制权交出去 } catch (InterruptedException e) { }} System.out.println("DONE! " + getName()); //线程执行结束 }} public class TwoThreadsTest { public static void main (String args[]) { new SimpleThread("First").start(); //第一个线程的名字为First new SimpleThread("Second").start(); //第二个线程的名字为Second}} 功能:上述程序通过继承Tread类创建了两个线程对象,分别命名为First和Second。线程体循环输出所属线程名称10次,并且在每次输出后随机休眠N毫秒(N<1000)。 14.补充适当语句,使下面的Java Application程序正常运行,并写出程序的运行结果 01. public class WhatThread implements Runnable { //类通过实现接口提供线程体方法 02. public static void main(String[] args){ 03. WhatThread wh= new WhatThread(); // 提供线程体方法 04. Thread t= new Thread(wh); //创建线程对象 05. t.start();//启动线程 06. } 07. public void run() { //定义线程体方法 08. System.out.println("Hello"); 09. } 10. } 运行结果:Hello
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 应用文书 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服