已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1 面向对象程序设计上机复习面向对象程序设计上机复习 1 用 用 for 循环求循环求 1 3 99 之和 之和 Programme Name Sum java public class Sum public static void main String args int i sum 0 for i 1 i 50 i sum sum 2 i 1 System out println 1 3 99 sum 2 编写一个 编写一个 Java 程序 用程序 用 if else 语句判断某年份是否为闰年 语句判断某年份是否为闰年 Programme Name LeapYear java public class LeapYear public static void main String args int year 2010 if args length 0 year Integer parseInt args 0 if year 4 0 else System out println year 年不是闰年 if else 语句 3 已知圆的周长计算公式为 已知圆的周长计算公式为 S 2 r 圆的面积计算公式为 圆的面积计算公式为 A r r 设计一个 设计一个 Java 类类 Circle 编写求解圆的周长和面积的函数 并求解当 编写求解圆的周长和面积的函数 并求解当 r 10 时 圆的周长与面积 时 圆的周长与面积 Programme Name Circle java public class Circle private double radius final double PI 3 1415926 public Circle double r this radius r public double getArea return PI this radius this radius public double getPerometer return 2 PI this radius void display System out printf 圆的半径是 f 面积 f 周长 f n this radius this getArea this getPerometer public static void main String args System out println Circle Circle ci new Circle 10 ci display 2 4 利用线程方法编写利用线程方法编写 JApplet 程序 实现在浏览器端实时动态显示本地系统时钟程序 实现在浏览器端实时动态显示本地系统时钟 Programme Name Watch java import java applet Applet import java awt import java text DateFormat import java util public class Watch extends Applet public void paint Graphics g Date d new Date DateFormat ldf DateFormat getDateTimeInstance DateFormat LONG DateFormat LONG System out println 现在系统时间是 long ldf format d String time ldf format d toString g drawString time 100 100 try Thread sleep 1000 catch InterruptedException e repaint JavaAppletDemo 保存为 Watch html 文件 5 1 编写一个圆类 编写一个圆类 Circle 该类拥有 该类拥有 一个成员变量一个成员变量 radius 私有 浮点型 私有 浮点型 存放圆的半径 存放圆的半径 两个构造方法两个构造方法 Circle 将半径设为将半径设为 0 Circle double r 创建创建 Circle 对象时将半径初始化为对象时将半径初始化为 r 三个成员方法三个成员方法 double getArea 获取圆的面积获取圆的面积 double getPerimeter 获取圆的周长获取圆的周长 void show 将圆的半径 周长 面积输出到屏幕将圆的半径 周长 面积输出到屏幕 2 编写一个圆柱体类 编写一个圆柱体类 Cylinder 它继承于上面的 它继承于上面的 Circle 类 还拥有 类 还拥有 一个成员变量一个成员变量 3 double hight 私有 浮点型 私有 浮点型 圆柱体的高 圆柱体的高 构造方法构造方法 Cylinder double r double h 创建创建 Circle 对象时将半径初始化为对象时将半径初始化为 r 成员方法成员方法 double getVolume 获取圆柱体的体积获取圆柱体的体积 void showVolume 将圆柱体的体积输出到屏幕将圆柱体的体积输出到屏幕 编写应用程序 创建类的对象 分别设置圆的半径 圆柱体的高 计算并分别显示圆半径 圆面积 编写应用程序 创建类的对象 分别设置圆的半径 圆柱体的高 计算并分别显示圆半径 圆面积 圆周长 圆柱体的体积 圆周长 圆柱体的体积 Programme Name TestCylinder java class Circle 定义父类 园类 private double radius 成员变量 园半径 Circle 构造方法 radius 0 0 Circle double r 构造方法 radius r double getPerimeter 成员方法 求园周长 return 2 Math PI radius double getArea 成员方法 求园面积 return Math PI radius radius void disp 成员方法 显示园半径 周长 面积 System out println 园半径 radius System out println 园周长 getPerimeter System out println 园面积 getArea class Cylinder extends Circle 定义子类 圆柱类 private double hight 成员变量 园柱高 Cylinder double r double h 构造方法 super r hight h public double getVol 成员方法 求园柱体积 return getArea hight public void dispVol 成员方法 显示园柱体积 System out println 圆柱体积 getVol public class TestCylinder 定义主类 public static void main String args 主程入口 4 Circle Ci new Circle 10 0 生成园类实例 Ci disp 调用园类的方法 Cylinder Cyl new Cylinder 5 0 10 0 生成圆柱类实例 Cyl disp 调用父类方法 Cyl dispVol 调用子类方法 6 应用应用 FileInputStream 类 编写应用程序 从磁盘上读取一个类 编写应用程序 从磁盘上读取一个 Java 程序 并将源程序代码显示在程序 并将源程序代码显示在 屏幕上 屏幕上 被读取的文件路径为 被读取的文件路径为 E test Hello java Programme Name FISDemo java import java io public class FISDemo public static void main String args byte buf new byte 2056 try FileInputStream fileIn new FileInputStream e test Hello java int bytes fileIn read buf 0 2056 String str new String buf 0 bytes System out println str catch Exception e e printStackTrace 7 设计一个表示用户的类设计一个表示用户的类 User 类中有用户名 口令 私有的 和记录用户数 静态 的成员变 类中有用户名 口令 私有的 和记录用户数 静态 的成员变 量 定义类的构造方法 设置和获取口令的方法及返回类对象信息的方法 包括用户名和口令 量 定义类的构造方法 设置和获取口令的方法及返回类对象信息的方法 包括用户名和口令 编写应 编写应 用程序测试用程序测试 User 类 类 Pragramme name UserExample classclass User privateprivate String name password staticstatic intint count 0 publicpublic User count 无参构造方法 publicpublic User String name thisthis name name count 一个参数构造方法 publicpublic User String name String password thisthis name name thisthis password password count 两个参数构造方法 publicpublic voidvoid setPassword String password thisthis password password publicpublic String getPassword String name returnreturn password publicpublic String message returnreturn Name name Password password publicpublic staticstatic intint getCount returnreturn count 5 publicpublic classclass UserExample publicpublic staticstatic voidvoid main String args 主程序入口 User tom newnew User Tom tom setPassword tom123 System out println tom message Count User getCount User Kate newnew User Kate Kate666 System out println Kate message Count User getCount 8 编写程序 在屏幕上显示带标题的窗口 并添加一个按钮 当用户单击按钮时 结束程序 编写程序 在屏幕上显示带标题的窗口 并添加一个按钮 当用户单击按钮时 结束程序 Programme Name ButtonEventDemo java import javax swing import java awt event public class ButtonEventDemo extends JPanel implements ActionListener protected JButton b1 声明一个按钮对象 public ButtonEventDemo 构造方法 ImageIcon ButtonIcon new ImageIcon images green png 创建按钮的图标对象 b1 new JButton 退出按钮 ButtonIcon 生成按钮对象 b1 setMnemonic KeyEvent VK E 设置 b1 的助记符是 Alt E b1 setToolTipText 这是退出按钮 设置按钮提示条 this add b1 往面板对象中加载按钮 b1 addActionListener this 本类对象注册为按钮的事件监听器 public void actionPerformed ActionEvent e 按钮事件响应方法 System exit 0 按 b1 则退出主程序 private static void createGUI 创建窗体 JFrame setDefaultLookAndFeelDecorated true 设置 java 隐含观感 JFrame frame new JFrame 按钮测试 生成应用程序主窗体 frame setDefaultCloseOperation JFrame EXIT ON CLOSE 设置关闭时隐含操作 ButtonEventDemo CPane new ButtonEventDemo 生成主类对象 面板 CPane setOpaque true 面板要求不透明 frame setContentPane CPane 设置主类对象为主窗体的内容面板 frame pack 主窗体紧缩显示 frame setVisible true 设置主窗体可见 public static void main String args 将 createGUI 列入线程 javax swing SwingUtilities invokeLater new Runnable 6 public void run createGUI 9 编程实现如下功能 利用字节文件流 编程实现如下功能 利用字节文件流 FileOutputStream 在 在 E TEST 文件夹下建立一个文本文件文件夹下建立一个文本文件 info txt 如不存在则创建之 如不存在则创建之 并写入如下两行字符串信息 并写入如下两行字符串信息 使用使用Java 字节流字节流FileOutputStream写入文件写入文件 利用利用Java 字符流字符流FileReader和和BufferedReader读取并显示文件信息读取并显示文件信息 然后利用字符文件流 然后利用字符文件流 FileReader 和和 BufferedReader 读取显示该文件的内容 读取显示该文件的内容 Programme Name StreamDemo java import java io BufferedReader import java io File import java io FileOutputStream import java io FileReader import java io IOException public class StreamDemo static String filename E test info txt static File file new File filename private static void FileOutStreamDemo StringBuffer sb new StringBuffer 使用 Java 字节流 FileOutputStream 写入文件 sb append n 利用 Java 字符流 FileReader 和 BufferedReader 读取并显示文件信息 System out println sb try if file exists file isFile System out printf 指定的文件 s 不存在 n filename File d file getParentFile if d exists d isDirectory d mkdir System out printf s 已创建成功 n d getPath if file createNewFile System out printf s 已创建成功 n filename System out println file getAbsolutePath byte b sb toString getBytes FileOutputStream fos new FileOutputStream file fos write b 直接覆盖原文件 fos close System out println b hashCode catch IOException ie System out println ie getLocalizedMessage private static void FileReaderDemo try FileReader fr new FileReader file int in while in fr read 1 System out print char in fr close g 关闭之前会先 flush BufferedReader br new BufferedReader new FileReader file 更简单一点 7 String s do s br readLine if s null break System out println s while s null catch IOException e System out println e getLocalizedMessage e printStackTrace public static void main String args FileOutStreamDemo System out printf s 文件的大小是 d bytes n filename file length FileReaderDemo 10 定义一个表示学生信息的类定义一个表示学生信息的类 Student 要求如下 要求如下 1 类 类 Student 的成员变量 的成员变量 sNO 表示学号 表示学号 sName 表示姓名 表示姓名 sSex 表示性别 表示性别 sAge 表示年龄 表示年龄 sJava 表示 表示 Java 课程成绩 课程成绩 2 类 类 Student 带参数的构造方法 带参数的构造方法 在构造方法中通过形参完成对成员变量的赋值操作 在构造方法中通过形参完成对成员变量的赋值操作 3 类 类 Student 的方法成员 的方法成员 getNo 获得学号 获得学号 getName 获得姓名 获得姓名 getSex 获得性别 获得性别 getAge 获得年龄 获得年龄 getJava 获得 获得 Java 课程成绩课程成绩 4 根据类根据类 Student 的定义 创建五个该类的对象 输出每个学生的信息 计算并输出这五个学生的定义 创建五个该类的对象 输出每个学生的信息 计算并输出这五个学生 Java 语言成绩的平均值 以及计算并输出他们语言成绩的平均值 以及计算并输出他们 Java 语言成绩的最大值和最小值 语言成绩的最大值和最小值 Pragramme Pragramme namename Student Student publicpublic classclass Student String sNO sName sSex intint sAge sJava publicpublic Student String XH String XM String XB intint NL intint
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中国稀土校招题库及答案
- 正式技术服务合同范本
- 氧气乙炔供气合同范本
- 旅游提前离团协议书
- 施工员聘请合同协议
- 校内礼服租借合同范本
- 模具召回协议书范本
- 沙石代销协议合同范本
- 主债权授信合同范本
- 木门质保协议书范本
- 2025年松原市总工会公开招聘工会社会工作者(10人)笔试考试参考试题附答案解析
- 交通运输行业数据集建设实施方案
- SB/T 10437-2007美容美发行业经营管理技术规范
- LS 8010-2014植物油库设计规范
- GB/T 8028-2010汽油机油换油指标
- GB/T 6070-2007真空技术法兰尺寸
- 北京市建设工程质量检测收费指导价
- 企业内部控制应用指引第11号-工程项目课件
- 复盘反思 抓铁有痕(简)课件 -高三主题班会
- 护理质量考核标准-护理人文关怀
- 高低压电器装配工考试题库
评论
0/150
提交评论