




已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java语言Under MC-DOS1)Path=C:Javajdk1.6.0_05bin2)set Classpath=C:Javajdk1.6.0_05jrelibrt.jar;.;第一题class People protected String name;protected int age;public int getAge() return age; class Employee extends People protected String empno; class Teacher extends People String teano; String zc; 第二题Application:public class HelloWorld public static void main (String args) System.out.println(Hello World!); Applet:源程序:import java.applet.*;import java.awt.*;public class HelloWorldApplet extends Applet public void paint(Graphics g) g.drawString(Hello World!,10,20); 运行(编写html文件):HelloWorldApplet 第三题源程序:Running OK.import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.lang.Double;public class ExampleThree public static void main (String args) ComputerFrame fr=new ComputerFrame(); fr.setTitle(求绝对值); class ComputerFrame extends JFrame implements ActionListener JTextField text1,text2; JButton button; public ComputerFrame() setLayout(new FlowLayout(); text1=new JTextField(10); text2=new JTextField(10); add(text1); button=new JButton(绝对值); add(button); add(text2); button.addActionListener(this); setSize(400,320); setVisible(true); validate(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); public void actionPerformed(ActionEvent e) Double n; if(e.getSource()=button) Double m; try m=Double.parseDouble(text1.getText(); n=Math.abs(m); text2.setText(String.valueOf(n); catch(NumberFormatException ee) text2.setText(请输入数字字符); validate(); 运行(编写html文件):Running error.ExampleThree 第四题 import java.lang.Double;public class ExampleFourpublic static void main (String args) Fractor r1; r1=new Fractor(-2,9); Fractor r2; r2=new Fractor(3,7); Fractor result1=r1.add(r2); Fractor result2=r1.sub(r2); System.out.printf(两分数之和:); r1.diplay(); System.out.printf(+); r2.diplay(); System.out.printf(=); result1.diplay(); System.out.printf(n); System.out.printf(两分数之差:); r1.diplay(); System.out.printf(-); r2.diplay(); System.out.printf(=); result2.diplay(); System.out.printf(n); class Fractor int m; /分子 int n; /分母 Fractor(int m, int n) /设置分子和分母 int c=f(Math.abs(m),Math.abs(n); /计算最大公约数 this.m=m/c; this.n=n/c; if(m0&n0) m=-m; n=-n; int getNumerator() /得到分子 return m; int getDenominator() /得到分母 return n; public int f(int k,int v) if(kv) int c=k; k=v; v=c; int r=k%v; while(r!=0) k=v; v=r; r=k%v; return v; Fractor add(Fractor a) int k=a.getNumerator(); int v=a.getDenominator(); int newM=m*v+n*k; int newN=n*v; Fractor result=new Fractor(newM,newN); return result; Fractor sub(Fractor a) int k=a.getNumerator(); int v=a.getDenominator(); int newM=m*v-n*k; int newN=n*v; Fractor result=new Fractor(newM,newN); return result; void diplay() int m=getNumerator(); int n=getDenominator(); System.out.printf(0+m+/+n+0); 第五题第六题public class ExampleSix public static void main (String args) int arrayOfInts=32,87,3,589,12,1076,2000,8,622,127; for(int i=0;iarrayOfInts.length;i+) for(int j=i+1;jarrayOfInts.length;j+) if(arrayOfIntsjarrayOfIntsi) int temp=0; temp=arrayOfIntsj; arrayOfIntsj=arrayOfIntsi; arrayOfIntsi=temp; for(int k=0;karrayOfInts.length;k+) System.out.printf(%6d,arrayOfIntsk); 第七题public class ExampleSeven public static void main (String args) double sum=0,a=1; int i=1; while (i=20) sum=sum+a; i+; a=a*i; System.out.println(sum=+sum); 第八题第九题import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.lang.Double;public class ExampleNine public static void main (String args) ComputerFrame fr=new ComputerFrame(); fr.setTitle(计算); class ComputerFrame extends JFrame implements ActionListener JTextField text1,text2; JButton button1,button2; JLabel label; public ComputerFrame() setLayout(new FlowLayout(); text1=new JTextField(10); text2=new JTextField(10); label=new JLabel( ,JLabel.CENTER); label.setBackground(Color.green); add(text1); add(text2); button1=new JButton(平均值); button2=new JButton(最大值); add(button1); add(button2); add(label); button1.addActionListener(this); button2.addActionListener(this); setSize(400,320); setVisible(true); validate(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); public void actionPerformed(ActionEvent e) Double n; if(e.getSource()=button1) Double n1,n2; try n1=Double.parseDouble(text1.getText(); n2=Double.parseDouble(text2.getText(); n=(n1+n2)/2.0; label.setText(String.valueOf(n); catch(NumberFormatException ee) label.setText(请输入数字字符); else if(e.getSource()=button2) Double n1,n2; try n1=Double.parseDouble(text1.getText(); n2=Double.parseDouble(text2.getText(); n=Math.max(n1,n2); label.setText(String.valueOf(n); catch(NumberFormatException ee) label.setText(请输入数字字符); validate(); 第十题 class Point float x,y; Point() x=0.0f; y=0.0f; Point(float x,float y) this.x=x; this.y=y; float getX() return x; float getY() return y; public void show() System.out.println(点的坐标:+(+getX()+,+getY()+); public class ExampleTen public static void main (String args) Point point1=new Point(10.3f,-20.6f); Point point2=new Point(); point1.show(); point2.show(); 第十一题 abstract class Shape abstract double GetArea(); abstract double GetPerimeter();class Rectangle extends Shape double a,b; /长,宽; Rectangle(double a,double b) this.a=a; this.b=b; double GetArea() return a*b; double GetPerimeter() return (a+b)*2; class Circle extends Shape double r; Circle(double r) this.r=r; double GetArea() return 3.14*r*r; double GetPerimeter() return 3.14*r*2; public class ExampleEleven public static void ma
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年竞赛活动策划合同书
- 2025建筑工程合同争议解决法律依据解析
- 化肥厂服务供应商评估规定
- (2024年秋季版)山东省邹平县七年级历史下册 第三单元 第17课 统一多民族国家的巩固和发展说课稿 北师大版
- 2.5 春天的故事 教学设计-2023-2024学年高一上学期音乐湘教版(2019)必修音乐鉴赏
- 二年级品德与生活上册 收获的感觉真好说课稿2 北师大版
- 关于春节放假的通知范文集合4篇
- 公司个人的上半年工作总结
- 中医期末试题及答案
- 安徽省马鞍山市第七中学2024-2025学年部编版九年级上学期期末考试历史试题(含答案)
- 解除定向委培协议书
- 气血疏通中级班教材
- 肾病(血透)专业医疗质量控制指标考核试题
- 杨国语-新生儿心律失常
- 汽车维修店租赁协议
- GB/T 19964-2024光伏发电站接入电力系统技术规定
- 变电站主辅设备监视及一键顺控课件
- 高中英语外研版(2019)必修第一册各单元重点短语整理清单素材
- 二十周年校庆领导致辞
- 马克思的博士论文
- 内科护理学讲义-循环系统疾病病人的护理
评论
0/150
提交评论