工科_Java期末考试编程题题库.doc

上传人:m**** 文档编号:543961839 上传时间:2022-09-26 格式:DOC 页数:15 大小:145KB
返回 下载 相关 举报
工科_Java期末考试编程题题库.doc_第1页
第1页 / 共15页
工科_Java期末考试编程题题库.doc_第2页
第2页 / 共15页
工科_Java期末考试编程题题库.doc_第3页
第3页 / 共15页
工科_Java期末考试编程题题库.doc_第4页
第4页 / 共15页
工科_Java期末考试编程题题库.doc_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《工科_Java期末考试编程题题库.doc》由会员分享,可在线阅读,更多相关《工科_Java期末考试编程题题库.doc(15页珍藏版)》请在金锄头文库上搜索。

1、JAVA程序设计复习题之(四)编程题(计算机科学与技术专业使用)1、编写一个字符界面的Java Application程序,类名为TestApp,用来接受用户从键盘输入的10个整数,并输出10个整数的最大值和最小值。答:参考程序如下:import java.util.Scanner;public class TestApp public static void main(String args) int arr = new int10; Scanner cin = new Scanner(System.in);System.out.println(Input 10 numbers:);for(

2、int i = 0; i 10; i+) try arri = cin.nextInt( ); catch (IOException e) int max,min;max = min = arr0;for(int i = 1; i max ) max = arri;if( arri min ) min = arri;System.out.println(Max= + max);System.out.println(Min= + min);2、模拟银行定期存款功能。创建银行定期存款账户类DepositAccount,其中包括:账号、储户姓名、存款余额、年利率等属性,和开户、存款、查询、计算利息等

3、方法。要求用静态变量存储年利率,用私有实例变量存储其它属性。提供计算年利息的方法和计算月利息(年利息/12)的方法。另外编写一个测试程序测试该类,建立Account的对象saver(账号:1234567890,姓名:Zhang San,定存5000元),设置年利率是3.5%,存款2000元,查询余额,计算并显示年利息。答:参考程序如下:public class TestEx3 public static void main(String args) DepositAccount saver = new DepositAccount(1234567890,Zhang san,5000); Dep

4、ositAccount.setInterest(0.035); /设置年利率 saver.queryBalance(); /查询余额 saver.deposit(2000); /存入2000元 System.out.println(年利息是: + saver.calYearInterest();class DepositAccount private String accountNum; /账号private String name; /姓名private double balance; /余额,均为实例成员变量static double interest; /年利率,类成员变量public

5、static void setInterest(double d ) /设置年利率interest = d;public DepositAccount(String num,String na,double ba) /开户,构造方法accountNum = num;name = na;balance = ba;public double calYearInterest() /计算年利息return balance * interest;public void deposit(double cash) /存款System.out.println(您账户原有余额: + balance);Syste

6、m.out.println(您现在存入: + cash);balance += cash;System.out.println(存款成功,您的余额是: + balance);public void queryBalance() /查询余额System.out.println(您的余额是: + balance);3、设计一个学生类Student,其属性有:姓名(name)、年龄(age)、学历(education),由Student类派生出本科生类Undergraduate和研究生类Graduate,本科生类增加属性:专业(specialty),研究生类增加属性:研究方向(direction)。

7、每个类都有构造方法和用于输出属性信息的show()方法,在测试类TestExtends中测试输出。答:参考程序如下:public class TestExtends public static void main(String args) Student s1 = new Student(张三, 18, 高中);s1.show();System.out.println();Undergraduate u1 = new Undergraduate(李四, 22, 本科, 计算机科学与技术);u1.show();Graduate g1 = new Graduate(王五,26,研究生,分布式计算)

8、;g1.show();class Student /学生类String name; /姓名int age; /年龄String education; /学历public Student(String name, int age, String education) /构造方法this.name = name;this.age = age;this.education = education;public void show() System.out.print(姓名: + name + 年龄: + age + 学历: + education);class Undergraduate exten

9、ds Student /本科生类,从学生类继承String specialty; /专业,新增属性public Undergraduate(String name, int age, String education,String specialty) super(name, age, education);this.specialty = specialty;Overridepublic void show() /覆盖父类的同名show()方法super.show();System.out.println( 专业: + specialty );class Graduate extends S

10、tudent /研究生类,从学生类继承String direction; /研究方向,新增属性public Graduate(String name, int age, String education, String direction) super(name, age, education);this.direction = direction;Overridepublic void show() /覆盖父类的同名show()方法super.show();System.out.println( 研究方向: + direction );4、从键盘输入两行数据,在控制台输出并同时输出到磁盘中的

11、一个文件。答:参考程序如下:import java.io.*;public class TestIO public static void main(String args) String s1,s2;tryBufferedReader stdin = new BufferedReader(new InputStreamReader(System.in);System.out.println(-输入第一行数据-);s1 = stdin.readLine();System.out.println(-输入第二行数据-);s2 = stdin.readLine();System.out.printl

12、n(标准输出: + s1);System.out.println(标准输出: + s2); PrintWriter fout = new PrintWriter( new FileWriter(d:abc.txt) );fout.write(s1 + t);fout.write(s2);fout.close();BufferedReader fin = new BufferedReader( new FileReader(d:abc.txt) ); System.out.println(文件abc.txt内容: + fin.readLine(); fin.close(); catch (Exc

13、eption e) 5、将从键盘输入的学生信息(学号、姓名、年龄、成绩)保存到磁盘文件中,并从文件读取这些信息在控制台输出。答:参考程序如下:import java.io.*;import java.util.*;public class TestEx6StudInfo2 public static void main(String args) long num; /学号 String name; /姓名 int age; /年龄 float score; /成绩 try Scanner sc = new Scanner(System.in); System.out.println(请输入学号:); num = sc.nextLong(); /读入学号 System.out.println(请输入姓名:); name = sc.next(); /读入姓名 S

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号