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

上传人:gbs****77 文档编号:10182923 上传时间:2020-04-10 格式:DOCX 页数:7 大小:26.59KB
返回 下载 相关 举报
沈阳师范---Java程序设计---实验题_第1页
第1页 / 共7页
沈阳师范---Java程序设计---实验题_第2页
第2页 / 共7页
沈阳师范---Java程序设计---实验题_第3页
第3页 / 共7页
点击查看更多>>
资源描述
【沈师710寝室】Java程序题1假定根据学生的3门学位课程的分数决定其是否可以拿到学位,对于本科生,如果3门课程的平均分数超过60分即表示通过,而对于研究生,则需要平均超过80分才能够通过。根据上述要求,请完成以下Java类的设计:class Studentprivate 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 Studentpublic 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()+,可以拿到学士学位。);elseSystem.out.println(本科生+getName()+的三科平均分为:+getAverage()+,不能拿到学士学位。);class Graduate extends Studentpublic 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()+,可以拿到硕士学位。);elseSystem.out.println(研究生+getName()+的三科平均分为:+getAverage()+,不能拿到硕士学位。);public class StudentDemopublic 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 Employeeprivate String name;public Employee(String name)this.name=name;public String getName()return name;public abstract double computeSalary();class Manager extends Employeedouble monthSalary;public Manager(String name,double monthSalary)super(name);this.monthSalary=monthSalary;public double computeSalary()return monthSalary;class Salesman extends Employeedouble baseSalary;double commision;int quantity;public Salesman(String name,double baseSalary,double commision,int quantity)super(name);this.baseSalary=baseSalary;this.commision=commision;this.quantity=quantity;public double computeSalary()return baseSalary+commision*quantity;class Worker extends Employeedouble 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 EmployeeDemopublic 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()+的月工资为:+m.computeSalary();System.out.println(销售人员+s.getName()+的月工资为:+s.computeSalary();System.out.println(工人+w.getName()+的月工资为:+w.computeSalary();运行结果:经理Tom的月工资为:10000.0 销售人员Mary的月工资为:4700.0 工人John的月工资为:1680.03输入给定的Java Application程序,其中文件Rectangle.java和Point.java放入C:javaexam中;文件TestPackage.java包含main( )方法的测试程序放在目录C:javaexamtest下,写出运行结果,并简述打包过程。(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 TestPackagepublic 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:1004. 在类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(i4) num+=di;i+;System.out.println(“Test1”);catch(ArrayIndexOutOfBoundsException e) System.out.println(“Test2”);finally System.out.println(“Test3”);System.out.println(“Test4”);运行结果:Test2Test3Test46. 编写程序,要求程序功能:首先输出“这是一个异常处理的例子”,然后在你程序中主动地产生一个 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 zero7利用下面的关键代码编写一个完整的程序,理解String和StringBuffer类的使用。public class StringDemopublic 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:37SubString:demoHello World! And Hello Java!HhhelloWorld! And Hello Java!8编写一个Java Application程序,实现读取并输出指定文件的内容的功能。import java.io.*; public class ReadFilepublic static void main(String args) throws IOExceptionBufferedReader 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 WriteFile2public static void main(String args) throws IOExceptionBufferedReader 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(ss0);b=Integer.parseInt(ss1);c=Integer.parseInt(ss2);System.err.println(a,b,c);s=br.readLine();br.close();12编写一个Java Application程序,实现如下的设计功能:运行该程序可以列出当前目录下的文件。import java.io.*;public class FileList2public static void main(String args)File dir=new File(.);File files=dir.listFiles();System.out.println(dir);for(int i=0;ifiles.length;i+)if(filesi.isFile()System.out.println(t+filesi.getName();elseSystem.out.println(t+filesi.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(); /第一个线程的名字为Firstnew SimpleThread(Second).start(); /第二个线程的名字为Second功能:上述程序通过继承Tread类创建了两个线程对象,分别命名为First和Second。线程体循环输出所属线程名称10次,并且在每次输出后随机休眠N毫秒(N1000)。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
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案


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

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


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