沈阳师范---Java程序设计---实验题.docx_第1页
沈阳师范---Java程序设计---实验题.docx_第2页
沈阳师范---Java程序设计---实验题.docx_第3页
沈阳师范---Java程序设计---实验题.docx_第4页
沈阳师范---Java程序设计---实验题.docx_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

【沈师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)=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)=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;mision=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()+的月工资为:+puteSalary();System.out.println(销售人员+s.getName()+的月工资为:+puteSalary();System.out.println(工人+w.getName()+的月工资为:+puteSalary();运行结果:经理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) supe

温馨提示

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

评论

0/150

提交评论