版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、import java.applet.*;import java.awt.*;public class Boy extends Applet public void paint (Graphics g) g.setColor(Color.red); g.drawString(我一边喝着咖啡,一边学Java呢,5,30); g.setColor(Color.blue); g.drawString(我学得很认真,10,50); public class Ex3 public static void main (String args) long sum=1,temp=1; for (int i=2
2、;i=10;i+) temp=temp*i; sum=sum+temp; System.out.println(1!+2!+.+10!=+sum); public class Ex4 public static void main(String args) int i=1;int j; while(i=1000) i+; for (j=2;ji;j+) if (i%j=0) break; if(i=j) System.out.println(i+是素数); public class Ex6 public static void main(String args) int sum; for(in
3、t i=2;i=1000;i+) sum=0; for( int j=1;jc?a:c;d=d2b?d:b;System.out.println(b);public class Hello public static void main (String args) System.out.println(你好,很高兴学习Java); import java.lang.*;class Trangle double a,b,c,p; boolean s; Trangle(double a,double b,double c) this.a=a;this.b=b;this.c=c; public do
4、uble 周长() return a+b+c;public double 求面积() p=(a+c+b)/2; return Math.sqrt(p*(p-a)*(p-b)*(p-c);void judgeTrangle() if (a+b)c & (a+c)b & (b+c)a) s=true; else s=false; class Leder double ub,db,h; Leder(double ub,double db,double h) this.ub=ub;this.db=db;this.h=h; double 面积() return (ub+db)*h)/2; class C
5、ircle double r; double pi=3.; Circle(double r) this.r=r; double 求周长() return 2*r*pi; double area() return r*r*pi; public class Example public static void main (String args) Trangle sanjiao; Leder tixing;Circle yuan;sanjiao=new Trangle(3.0,4.0,5.0);System.out.println(三角形的周长:+sanjiao.周长();System.out.p
6、rintln(三角形的面积:+sanjiao.求面积();tixing=new Leder(2.0,4.0,1.0);System.out.println(梯形的面积:+tixing.面积();yuan=new Circle(3.0);System.out.println(圆的周长:+yuan.求周长();System.out.println(圆的面积:+yuan.area();class Company Employee employee; double salaries=0; Company(Employee employee) this.employee=employee; public
7、 double salariesPay() salaries=0; for (int i = 0; i employee.length; i+) if (YearWorker.class.isInstance(employeei) salaries += employeei.earnings(); if (MonthWorker.class.isInstance(employeei) salaries += employeei.earnings() * 12; if (WeekWorker.class.isInstance(employeei) salaries += employeei.ea
8、rnings() * 52; /计算salaries。 return salaries; abstract class Employee public abstract double earnings(); /声明一个公有的抽象方法earnings,方法返回类型为doublepublic class HardWork public static void main(String args) Employee employee=new Employee20; for(int i=0;i=0) root1=(-b+Math.sqrt(disk)/(2*a); root2=(-b-Math.sqrt
9、(disk)/(2*a); System.out.printf(方程的根:%f,%fn,root1,root2); else System.out.printf(方程没有实根n); else System.out.println(不是一元2次方程); public void setCoefficient(double a,double b,double c) this.a=a; this.b=b; this.c=c; if(a!=0) boo=true; else boo=false; import tom.jiafei.*;class SunRise public static void m
10、ain(String args ) SquareEquation equation=new SquareEquation(4,5,1); equation.getRoots(); equation.setCoefficient(-3,4,5); equation.getRoots(); public class EditString public static StringBuffer edit(String s) StringBuffer result=new StringBuffer(); for(int i=0;i=a & s.charAt(i)=A & s.charAt(i)=Z) r
11、esult.append(s.charAt(i); return result; public class Example public static void main(String args) String s=student Z 12 name 56, java.; System.out.println(EditString.edit(s); import java.util.Date;import java.text.SimpleDateFormat;class Example1 public static void main(String args) Date nowTime=new
12、 Date(); SimpleDateFormat matter1=new SimpleDateFormat(北京时间:E HH:mm:ss); System.out.println(matter1.format(nowTime); import java.io.*;import java.util.*;public class Example2 public static void main(String args) throws IOException int year,month; String str; BufferedReader br=new BufferedReader(new
13、InputStreamReader(System.in); str=br.readLine(); year=Integer.parseInt(str); str=br.readLine(); month=Integer.parseInt(str); System.out.println(year+年+month+月); int allDay; if(year%4=0&year%100!=0)|(year%400=0)if (month=2) allDay=29; if(month=1|month=3|month=5|month=7|month=8|month=10|month=12) allD
14、ay=31; else allDay=30;elseif(month=2) allDay=28; else if(month=1|month=3|month=5|month=7|month=8|month=10|month=12) allDay=31; else allDay=30; System.out.println( 日 一 二 三 四 五 六); Calendar 日历=Calendar.getInstance();日历.set(year,month-1,1);int 星期几=日历.get(Calendar.DAY_OF_WEEK)-1;String a=new String星期几+a
15、llDay;for(int i=0;i星期几;i+) ai=*;for(int i=星期几,n=1;i星期几+allDay;i+)if(n=9)ai=String.valueOf(n)+ ;elseai=String.valueOf(n) ;n+;for(int i=0;ia.length;i+)if(i%7=0)System.out.println();System.out.print( +ai); import java.awt.*; import java.awt.event.*; import java.util.*; class WindowTextArea extends Fram
16、e implements TextListener TextArea text1,text2; WindowTextArea(String s) super(s); setLayout(new FlowLayout();text1=new TextArea(5,23); text2=new TextArea(5,23); add(text1); add(text2); text2.setEditable(false); text1.addTextListener(this); addWindowListener(new WindowAdapter() public void windowClo
17、sing(WindowEvent e) System.exit(0); ); setBounds(100,100,400,160); setVisible(true); validate(); public void textValueChanged(TextEvent e) String s=text1.getText(); StringTokenizer geshu=new StringTokenizer(s); int n=geshu.countTokens(); double a=new doublen; for(int i=0;in;i+) String temp=geshu.nex
18、tToken(); double date=Double.parseDouble(temp); ai=date; double sum=0,average; for(int i=0;in;i+) sum=sum+ai; average=sum/n; text2.setText(null); text2.append(和:+sum+n+平均数:+average); public class Example public static void main(String args) WindowTextArea win=new WindowTextArea(计算的窗口); import java.a
19、wt.*; import java.awt.event.*; class WindowTextArea extends Frame implements ActionListener TextField text1,text2,text3; Button button1,button2,button3,button4; WindowTextArea(String s) super(s); setLayout(new FlowLayout(); text1=new TextField(10); text2=new TextField(10); text3=new TextField(17); b
20、utton1=new Button(加); button2=new Button(差); button3=new Button(积); button4=new Button(除); add(text1); add(text2); add(text3); text3.setEditable(false); add(button1); add(button2); add(button3); add(button4); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener
21、(this); button4.addActionListener(this); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); setBounds(100,100,160,180); setVisible(true); validate(); public void actionPerformed(ActionEvent e) double num1=0,num2=0,totle=0; try num1= Double.parseDouble(t
22、ext1.getText(); num2= Double.parseDouble(text2.getText(); if(e.getSource()=button1) totle=num1+num2; text3.setText(和:+totle); else if(e.getSource()=button2) totle=num1-num2; text3.setText(差:+totle); else if(e.getSource()=button3) totle=num1*num2; text3.setText(积:+totle); else if(e.getSource()=button
23、4) totle=num1/num2; text3.setText(商:+totle); catch(NumberFormatException event) System.out.println(event);text3.setText(请输入数字!); public class Example public static void main(String args) WindowTextArea win=new WindowTextArea(计算); import java.awt.*; import java.awt.event.*; class Mycanvas extends Can
24、vas int x,y,w,h; Mycanvas() setBackground(Color.pink); setForeground(Color.YELLOW); public void setX(int x) this.x=x; public void setY(int y) this.y=y; public void setW(int w) this.w=w; public void setH(int h) this.h=h; public void paint(Graphics g) g.fillRect(x,y,w,h); class WindowCanvas extends Fr
25、ame implements ActionListener Mycanvas canvas; TextField text1,text2,text3,text4; Button button; WindowCanvas() canvas=new Mycanvas(); text1=new TextField(4); text2=new TextField(4); text3=new TextField(5); text4=new TextField(5);text1.setBackground(Color.red);text1.setForeground(Color.black);text2.
26、setBackground(Color.red);text2.setForeground(Color.cyan);text3.setBackground(Color.red);text3.setForeground(Color.blue);text4.setBackground(Color.red);text4.setForeground(Color.gray);Panel pNorth=new Panel(),pSouth=new Panel();pNorth.setBackground(Color.green);pNorth.setForeground(Color.orange);pSou
27、th.setBackground(Color.green);pSouth.setForeground(Color.orange);button=new Button(确定); button.addActionListener(this);button.setBackground(Color.yellow);button.setForeground(Color.blue);pNorth.add(new Label(矩形的宽: ); pNorth.add(text3); pNorth.add(new Label(矩形的高: ); pNorth.add(text4); pSouth.add(new
28、Label(左上角位置坐标:); pSouth.add(text1); pSouth.add(text2); pSouth.add(button); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); add(canvas,BorderLayout.CENTER); add(pNorth,BorderLayout.NORTH); add(pSouth,BorderLayout.SOUTH); setBounds(100,100,500,500); se
29、tVisible(true); validate(); public void actionPerformed(ActionEvent e) int x,y,w,h; try x=Integer.parseInt(text1.getText(); y=Integer.parseInt(text2.getText(); w=Integer.parseInt(text3.getText(); h=Integer.parseInt(text4.getText(); canvas.setX(x); canvas.setY(y); canvas.setW(w); canvas.setH(h); canv
30、as.repaint(); catch(NumberFormatException ee) x=0;y=0;w=0;h=0; public class Example7_18 public static void main(String args) new WindowCanvas(); import java.awt.*;import java.awt.event.*;class MyWin extends Frame implements ActionListener Button button; Label label; MyWin() setLayout(null); Cursor c
31、=Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); setCursor(c); button=new Button(横向走动); button.setBackground(Color.pink); button.addActionListener(this); label=new Label(我可以被碰掉,Label.CENTER); label.setBackground(Color.yellow); button.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR); add(butt
32、on); add(label); button.setBounds(20,80,80,20); label.setBounds(150,80,80,20); setVisible(true); setBounds(100,120,300,300); validate(); public void actionPerformed(ActionEvent e) Rectangle rect=button.getBounds(); int x=(int)rect.getX(); int y=(int)rect.getY(); if(ersects(label.getBounds()
33、label.setVisible(false); if(label.isVisible() x=x+2; button.setLocation(x,y); else y=y+3; button.setLocation(x,y); button.setLabel(纵向走动); public class Example7_23 public static void main(String args) MyWin win=new MyWin(); import java.awt.*; import java.awt.event.*; class WindowCursor extends Frame implements ActionListener Button button1,button2; TextField text; int n=-1; WindowCursor() button1=new Button(单击我); text=new TextField(将鼠标放在上面的按钮上看形状); button2=new Button(看看鼠标的形状); add(button1,BorderLayout.NORTH); add(button2,BorderLayout.CENTER);
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 基因治疗载体体内分布特征论文
- 教育公平数字鸿沟现状X报告论文
- 城市流动摊贩监管论文
- 海洋微塑料长期检测技术论文
- 工业物联网安全架构评估X标准论文
- 供应链中断创新模式论文
- TCSTC-芒果全产业链标准综合体 第4部分:主要病虫害防治技术指南
- 植物工厂光照优化路径论文
- 共享经济价值创造论文
- 幼儿园环保教育主题活动教案
- 2026年度个人所得税专项附加扣除全解析与实操指南【课件文档】
- 旅游介绍文案
- 2025年1月-12月时事政治归纳总结(备考必背)
- 征兵体检外科培训
- 2024年克州直属机关遴选公务员笔试真题汇编附答案解析
- 医院首问负责制课件
- 网络安全防护设备清单选择参考模板
- 模具厂安全生产规章制度
- 湖州市中医院肺功能报告解读考核
- 资产管理知识培训总结课件
- 2025贵州省黔晟国有资产经营有限责任公司选聘模拟试卷及答案详解(新)
评论
0/150
提交评论