java2实用教程(第2版)程序代码

上传人:gbs****77 文档编号:10303873 上传时间:2020-04-11 格式:DOC 页数:241 大小:1.12MB
返回 下载 相关 举报
java2实用教程(第2版)程序代码_第1页
第1页 / 共241页
java2实用教程(第2版)程序代码_第2页
第2页 / 共241页
java2实用教程(第2版)程序代码_第3页
第3页 / 共241页
点击查看更多>>
资源描述
1 例子 9 class Fibi public long fibinacii int n long c 0 if n 1 n 2 c 1 else c fibinacii n 1 fibinacii n 2 return c public class Example4 9 public static void main String args Fibi a new Fibi for int i 1 i 10 i System out print a fibinacii i 例子 10 class 三角形 double a b c 三角形 double a double b double c setABC this a b c void setABC 三角形 trangle double a double b double c trangle a a trangle b b trangle c c class Example4 10 public static void main String args 2 三角形 tra new 三角形 3 4 5 System out print 三角形型的三边是 tra a tra b tra c 例子 11 package tom jiafei public class PrimNumber public static void main String args int sum 0 i j for i 1 i 10 i 找出 10 以内的素数 for j 2 ji 2 System out print 素数 i 例子 12 import java applet Applet import java awt public class Example4 12 extends Applet Button redbutton public void init redbutton new Button 我是一个红色的按钮 redbutton setBackground Color red add redbutton public void paint Graphics g g drawString it is a button 30 50 3 例子 13 import tom jiafei 引入包 tom jiafei 中的类 public class Example4 13 public static void main String args PrimNumber num new PrimNumber 用包 tom jiafei 中的类创建对象 String a ok System out println a 0 num main a 例子 14 public class Example4 14 public static void main String args PrimNumber num new PrimNumber 要保证 PrimNuber 类和 Example4 14 类在同一 目录中 String a ok System out println a 0 num main a 例子 15 Trangel java package tom jiafei public class Trangle double sideA sideB sideC boolean boo public Trangle double a double b double c sideA a sideB b sideC c if a b c boo true else System out println 我不是一个三角形 boo false public void 计算面积 if boo double p sideA sideB sideC 2 0 double area Math sqrt p p sideA p sideB p sideC System out println 面积是 area else System out println 不是一个三角形 不能计算面积 public void 修改三边 double a double b double c sideA a sideB b sideC c if a b c else boo false Example4 15 java import tom jiafei class Example4 15 public static void main String args Trangle trangle new Trangle 12 3 1 trangle 计算面积 trangle 修改三边 3 4 5 5 trangle 计算面积 例子 16 class Example4 16 private int money Example4 16 money 2000 private int getMoney return money public static void main String args Example4 16 exa new Example4 16 exa money 3000 int m exa getMoney System out println money m 例子 17 import java applet import java awt class Father private int money float weight height String head String speak String s return s class Son extends Father String hand foot public class Example4 17 extends Applet Son boy 6 public void init boy new Son boy weight 1 80f boy height 120f boy head 一个头 boy hand 两只手 boy foot 两只脚 public void paint Graphics g g drawString boy speak 我是儿子 5 20 g drawString boy hand boy foot boy head boy weight boy height 5 40 例子 18 Father java package tom jiafei public class Father int height protected int money public int weight public Father int m money m protected int getMoney return money void setMoney int newMoney money newMoney Jerry java package import tom jiafei Father 7 public class Jerry extends Father Jerry 和 Father 在不同的包中 public Jerry super 20 public static void main String args Jerry jerry new Jerry jerry height 12 非法 因为 Jerry 没有继承友好的 height jerry weight 200 合法 jerry money 800 合法 int m jerry getMoney 合法 jerry setMoney 300 非法 因为 Jerry 没有继承友好的方法 setMoney System out println m m 例子 19 import java applet import java awt class Chengji float f float x float y return x y class Xiangjia extends Chengji float f float x float y return x y public class Example4 19 extends Applet Xiangjia sum public void init sum new Xiangjia public void paint Graphics g g drawString sum sum f 4 6 100 40 8 例子 20 import java applet import java awt class Area float f float r return 3 14159f r r float g float x float y return x y class Circle extends Area float f float r return 3 14159f 2 0f r public class Example4 20 extends Applet Circle yuan public void init yuan new Circle public void paint Graphics g g drawString yuan yuan f 5 0f 5 20 调用子类重写的方法 f g drawString yuan g 2 0f 8 0f 5 40 调用继承父类的方法 g 例子 21 class 类人猿 private int n 100 void crySpeak String s System out println s class People extends 类人猿 9 void computer int a int b int c a b System out println c void crySpeak String s System out println s class Example4 21 public static void main String args 类人猿 monkey new People monkey 是 People 对象的上转型对象 monkey crySpeak I love this game People people People monkey 把上转型对象强制转化为子类的对象 puter 10 10 例子 22 class 动物 void cry class 狗 extends 动物 void cry System out println 汪汪 class 猫 extends 动物 void cry System out println 喵喵 class Example4 22 public static void main String args 动物 dongwu if Math random 0 5 10 dongwu new 狗 dongwu cry else dongwu new 猫 ongwu cry 例子 23 abstract class 图形 public abstract double 求面积 class 梯形 extends 图形 double a b h 梯形 double a double b double h this a a this b b this h h public double 求面积 return 1 2 0 a b h class 圆形 extends 图形 double r 圆形 double r his r r public double 求面积 return 3 14 r r class 堆 图形 底 11 double 高 堆 图形 底 double 高 this 底 底 this 高 高 void 换底 图形 底 this 底 底 public double 求体积 return 底 求面积 高 3 0 public class Example4 23 public static void main String args 堆 zui 图形 tuxing tuxing new 梯形 2 0 7 0 10 7 System out println 梯形的面积 tuxing 求面积 zui new 堆 tuxing 30 System out println 梯形底的堆的体积 zui 求体积 tuxing new 圆形 10 System out println 半径是 10 的圆的面积 tuxing 求面积 zui 换底 tuxing System out println 圆形底的堆的体积 zui 求体积 例子 24 class Student int number String name Student int number String name this number number this name name System out println I am name my number is number class Univer Student extends Student boolean 婚否 12 Univer Student int number String name boolean b super number name 婚否 b System out println 婚否 婚否 public class Example4 24 public static void main String args Univer Student zhang new Univer Student 9901 和晓林 false 例子 25 class Sum int n float f float sum 0 for int i 1 i n i sum sum i return sum class Average extends Sum int n float f float c super n n c super f return c n float g float c c super f return c 2 public class Example4 25 13 public static void main String args Average aver new Average aver n 100 float result 1 aver f float result 2 aver g System out println result 1 result 1 System out println result 2 result 2 例子 26 import java applet import java awt interface Computable final int MAX 100 void speak String s int f int x float g float x float y class China implements Computable int xuehao public int f int x 不要忘记 public 关键字 int sum 0 for int i 1 i x i sum sum i return sum public float g float x float y return 6 至少有 return 语句 public void speak String s class Japan implements Computable int xuehao public int f int x 14 return 68 public float g float x float y return x y public void speak String s 必须有方法体 但体内可以没有任何语句 public class Example4 26 extends Applet China Li Japan Henlu public void init Li new China Henlu new Japan Li xuehao 991898 Henlu xuehao 941448 public void paint Graphics g g drawString xuehao Li MAX Li xuehao 从 1 到 100 求和 Li f 100 10 20 g drawString xuehao Henlu MAX Henlu xuehao 加法 Henlu g 2 0f 3 0f 10 40 例子 27 interface 收费 public void 收取费用 class 公共汽车 implements 收费 public void 收取费用 System out println 公共汽车 一元 张 不计算公里数 class 出租车 implements 收费 public void 收取费用 System out println 出租车 1 60 元 公里 起价 3 公里 class 电影院 implements 收费 public void 收取费用 15 System out println 电影院 门票 十元 张 class Example4 27 public static void main String args 公共汽车 七路 new 公共汽车 出租车 天宇 new 出租车 电影院 红星 new 电影院 七路 收取费用 天宇 收取费用 红星 收取费用 例子 28 interface ShowMessage void 显示商标 String s class TV implements ShowMessage public void 显示商标 String s System out println s class PC implements ShowMessage public void 显示商标 String s System out println s public class Example4 28 public static void main String args ShowMessage sm 声明接口变量 sm new TV 接口变量中存放对象的引用 sm 显示商标 长城牌电视机 接口回调 sm new PC 接口变量中存放对象的引用 sm 显示商标 联想奔月 5008PC 机 接口回调 16 例子 29 interface Computerable public double 求面积 class 梯形 implements Computerable double a b h 梯形 double a double b double h this a a this b b this h h public double 求面积 return 1 2 0 a b h class 圆形 implements Computerable double r 圆形 double r this r r public double 求面积 eturn 3 14 r r class 堆 Computerable 底 声明一个接口变量 可以回调 求面积 方法 double 高 堆 Computerable 底 double 高 this 底 底 this 高 高 void 换底 Computerable 底 this 底 底 public double 求体积 return 底 求面积 高 3 0 public class Example4 29 17 public static void main String args 堆 zui Computerable bottom bottom new 梯形 2 0 7 0 10 7 接口变量中存放对象的引用 System out println 梯形的面积 bottom 求面积 bottom 接口回调 求面积 zui new 堆 bottom 30 System out println 梯形底的堆的体积 zui 求体积 bottom new 圆形 10 接口变量中存放对象的引用 System out println 半径是 10 的圆的面积 bottom 求面积 zui 换底 bottom System out println 圆形底的堆的体积 zui 求体积 18 第一章 数组与字符串 例子 1 import java applet import java awt public class Example5 1 extends Applet float a public void init a new float 5 a 0 23 9f a 1 34 9f a 2 45f a 3 56 98f a 4 100f public void paint Graphics g g drawString a 0 a 0 a 1 a 1 a 2 a 2 a 3 a 3 a 4 a 4 12 12 例子 2 import java applet import java awt public class Example5 2 extends Applet String tom public void init tom 220302620629021 public void paint Graphics g if tom startsWith 220302 例子 3 class Example5 3 19 public static void main String args int number 0 String s student entropy engage english client for int k 0 k s length k if s regionMatches k en 0 2 number System out println number number 例子 4 class Example5 4 public static void main String args String a boy apple Applet girl Hat for int i 0 i a length 1 i for int j i 1 j a length j if a j compareTo a i 0 String temp a i a i a j a j temp for int i 0 i a length i System out print a i 例子 5 public class Example5 5 public static void main String args double n sum 0 0 for int i 0 i args length i 20 sum sum Double valueOf args i doubleValue n sum args length System out println 平均数 n 例子 6 import java util Date import java awt public class Example5 6 public static void main String args Date date new Date Button button new Button 确定 System out println date toString System out println button toString 例子 7 import java util public class Example5 7 public static void main String args String s I am Geng X y she is my girlfriend StringTokenizer fenxi new StringTokenizer s 空格和逗号做分 int number fenxi countTokens while fenxi hasMoreTokens String str fenxi nextToken System out println str System out println 还剩 fenxi countTokens 个单词 System out println s 共有单词 number 个 21 例子 8 import java util public class Example5 8 public static void main String args String s new String abcABC123 System out println s char a s toCharArray for int i 0 i a length i if Character isLowerCase a i a i Character toUpperCase a i else if Character isUpperCase a i a i Character toLowerCase a i s new String a System out println s 例子 9 class Example5 9 public static void main String args char c d String s 巴西足球队击败德国足球队 c new char 2 s getChars 5 7 c 0 System out println c d new char s length s getChars 7 12 d 0 s getChars 5 7 d 5 s getChars 0 5 d 7 System out println d 22 例子 10 class Example5 10 public static void main String args String s 清华大学出版社 char a s toCharArray for int i 0 i a length i a i char a i t String secret new String a System out println 密文 secret for int i 0 i 3 c f n 1 f n 2 return c 例子 3 import java util class Example6 3 public static void main String args Calendar calendar Calendar getInstance 创建一个日历对象 calendar setTime new Date 用当前时间初始化日历时间 String 年 String valueOf calendar get Calendar YEAR 月 String valueOf calendar get Calendar MONTH 1 日 String valueOf calendar get Calendar DAY OF MONTH 星期 String valueOf calendar get Calendar DAY OF WEEK 1 int hour calendar get Calendar HOUR OF DAY minute calendar get Calendar MINUTE second calendar get Calendar SECOND System out println 现在的时间是 System out println 年 年 月 月 日 日 星期 星期 System out println hour 时 minute 分 second 秒 calendar set 1962 5 29 将日历翻到 1962 年 6 月 29 日 注意 5 表示六月 long time1962 calendar getTimeInMillis calendar set 2003 9 5 将日历翻到 2003 年 10 月 5 日 9 表示十月 long time2003 calendar getTimeInMillis long 相隔天数 time2003 time1962 1000 60 60 24 25 System out println 2003 年 10 月 5 日和 1962 年 6 月 29 日相隔 相隔天数 天 例子 4 import java util class Example6 4 public static void main String args System out println 日 一 二 三 四 五 六 Calendar 日历 Calendar getInstance 创建一个日历对象 日历 set 2004 0 1 将日历翻到 2004年 1月 1日 注意 0表示一月 获取 1日是星期几 get 方法返回的值是 1表示星期日 星期六返回的值是 7 int 星期几 日历 get Calendar DAY OF WEEK 1 String a new String 星期几 31 存放号码的一维数组 for int i 0 i 星期几 i a i for int i 星期几 n 1 i 星期几 31 i if n 9 a i String valueOf n else a i String valueOf n n 打印数组 for int i 0 i a length i if i 7 0 System out println 换行 System out print a i 26 例子 5 import java text NumberFormat class Example6 5 public static void main String args double a Math sqrt 5 System out println 格式化前 a NumberFormat f NumberFormat getInstance f setMaximumFractionDigits 5 f setMinimumIntegerDigits 3 String s f format a System out println 格式化后 s System out println 得到的随机数 int number 8 for int i 1 i 20 i int randomNumber int Math random number 1 产生 1 到 8 之间的随机数 System out print randomNumber if i 10 0 System out println 27 第三章 AWT 工具集简介 例子 import java awt class Example7 1 public static void main String args Frame fr new Frame 媒体新闻 一个容器对象 fr setLayout new FlowLayout Button button1 new Button 确定 Button button2 new Button 取消 fr add button1 fr add button2 fr setSize 200 300 调用方法 setSize int int 设置容器的大 小 fr setBackground Color cyan fr setVisible true fr validate 28 第四章 Java Applet 基础 例子 1 import java applet import java awt public class Example8 1 extends Applet Button button1 Button button2 int sum public void init button1 new Button yes button2 new Button No add button1 add button2 public void start sum 0 for int i 1 i 200 x 5 g drawString 我们在学习 repaint方法 20 x repaint 30 第五章 文本框和文本区 例子 1 import java applet import java awt public class Boy extends Applet TextField text1 text2 text3 public void init text1 new TextField 输入密码 10 text1 setEditable false text2 new TextField 10 text2 setEchoChar text3 new TextField 我是一个文本框 20 add text1 add text2 add text3 text3 setText 重新设置了文本 例子 2 import java applet import java awt import java awt event public class Example9 2 extends Applet implements ActionListener TextField text1 text2 text3 public void init text1 new TextField 10 text2 new TextField 10 text3 new TextField 20 add text1 add text2 add text3 text1 addActionListener this 将主类的实例作为 text1的监视器 因此主类必须实现接口 ActionListener text2 addActionListener this public void actionPerformed ActionEvent e if e getSource text1 31 String word text1 getText if word equals boy text3 setText 男孩 else if word equals girl text3 setText 女孩 else if word equals sun text3 setText 太阳 else text3 setText 没有该单词 else if e getSource text2 String word text2 getText if word equals 男孩 text3 setText boy else if word equals 女孩 text3 setText girl else if word equals 太阳 text3 setText sun else text3 setText 没有该单词 例子 3 import java applet import java awt import java awt event public class Example9 3 extends Applet implements ActionListener TextField text1 text2 text3 PoliceMan police 32 public void init text1 new TextField 10 text2 new TextField 10 text3 new TextField 10 police new PoliceMan this add text1 add text2 add text3 text1 addActionListener this text1 addActionListener police public void actionPerformed ActionEvent e String number e getActionCommand int n Integer parseInt number int m n n text2 setText n 的平方是 m class PoliceMan implements ActionListener Example9 3 a null PoliceMan Example9 3 a this a a public void actionPerformed ActionEvent e String number e getActionCommand int n Integer parseInt number int m n n n a text3 setText n 的立方是 m 例子 4 import java applet import java awt public class Example9 4 extends Applet TextArea text1 text2 public void init text1 new TextArea 我是学生 6 16 text2 new TextArea 6 16 add text1 add text2 text2 append 我在学习 java语言 text1 insert 们 1 33 text1 selectAll int length text2 getText length text2 setSelectionStart 2 text2 setSelectionEnd length 例子 5 import java util import java applet import java awt import java awt event public class Example9 5 extends Applet implements TextListener TextArea text1 text2 public void init text1 new TextArea 6 15 text2 new TextArea 6 15 add text1 add text2 text2 setEditable false text1 addTextListener this public void textValueChanged TextEvent e if e getSource text1 String s text1 getText StringTokenizer fenxi new StringTokenizer s n int n fenxi countTokens String a new String n for int i 0 i n 1 i String temp fenxi nextToken a i temp for int i 0 i n 1 i 按字典序从小到大排序 for int j i 1 j n 1 j if a j compareTo a i 0 String t a j a j a i a i t text2 setText null 刷新显示 34 for int i 0 i n i text2 append a i n 35 第六章 按钮与标签 例子 1 import java applet import java awt import java awt event public class Example10 1 extends Applet implements ActionListener TextField text Button buttonEnter buttonQuit public void init text new TextField 0 10 add text buttonEnter new Button 确定 buttonQuit new Button 清除 add buttonEnter add buttonQuit buttonEnter addActionListener this buttonQuit addActionListener this text addActionListener this public void paint Graphics g g drawString 在文本框输入数字字符回车或单击按钮 10 100 g drawString 第文本框显示该数的平方根 10 120 public void actionPerformed ActionEvent e if e getSource buttonEnter e getSource text double number 0 try number Double valueOf text getText doubleValue text setText Math sqrt number catch NumberFormatException event text setText 请输入数字字符 else if e getSource buttonQuit text setText 0 36 例子 2 import java awt import java applet import java awt event 写一个按扭类的子类 增加一些新的功能 class MyButton extends Button implements ActionListener TextListener TextArea text1 text2 类的成员变量 char save MyButton String s Container con super s text1 new TextArea 6 12 text2 new TextArea 6 12 text2 setEditable false text1 addTextListener this 创建的按扭监视其中一个文本区 this addActionListener this 创建的按扭自己监视自己 con add text1 con add text2 con add this public void textValueChanged TextEvent e 实现接口 String s text1 getText StringBuffer strbuffer new StringBuffer s String temp new String strbuffer reverse text2 setText temp public void actionPerformed ActionEvent e 实现接口 text2 setText null String s text1 getText int length s length save new char length 将字符串拷贝到数组 save s getChars 0 length save 0 for int i 0 i save length i save i char save i 你 String temp new String save text2 setText temp public class Example10 2 extends Applet implements ActionListener MyButton mybutton 37 Button button public void init mybutton new MyButton 加密 this button new Button 解密 button addActionListener this add button public void actionPerformed ActionEvent e 实现接口 for int i 0 i mybutton save length i mybutton save i char mybutton save i 你 String temp new String mybutton save mybutton text2 setText temp 例子 3 import java awt import java applet import java awt event public class Example10 3 extends Applet public void init MyButton1 button1 new MyButton1 MyButton2 button2 new MyButton2 setLayout null add button1 add button2 button1 setLocation 12 12 button2 setLocation 60 12 class MyButton1 extends Button implements ActionListener int n 1 MyButton1 setSize 25 160 addActionListener this public void paint Graphics g g drawString 我 6 14 g drawString 是 6 34 g drawString 一 6 54 g drawString 个 6 74 38 g drawString 竖 6 94 g drawString 按 6 114 g drawString 钮 6 134 g drawString 8 154 public void actionPerformed ActionEvent e n n 1 3 if n 0 this setBackground Color cyan else if n 1 this setBackground Color orange else if n 2 this setBackground Color pink class MyButton2 extends Button MyButton2 setSize 38 80 setBackground Color cyan public void paint Graphics g g setColor Color red g fillOval 10 3 20 20 在按扭上画圆 见 17章 g setColor Color yellow g fillOval 10 28 20 20 g setColor Color green g fillOval 10 53 20 20 例子 4 import java awt import java awt event import java applet class MyLabel extends Label implements ActionListener String 标签上的初始名称 TextField inputNumber TextArea showResult Button button MyLabel String s Container con super s 标签上的初始名称 s inputNumber new TextField 10 showResult new TextArea 10 10 button new Button Enter button addActionListener this inputNumber addActionListener this con add this con add inputNumber con add showResult con add button 39 public void actionPerformed A
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 解决方案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!