JAVA程序实例(代码和效果图)
JAVA编程实例大全及详解答案(50例)

JA V A编程实例大全及详解答案(50例)【程序1】题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?//这是一个菲波拉契数列问题public class lianxi01 {public static void main(String[] args) {System.out.println("第1个月的兔子对数: 1");System.out.println("第2个月的兔子对数: 1");int f1 = 1, f2 = 1, f, M=24;for(int i=3; i<=M; i++) {f = f2;f2 = f1 + f2;f1 = f;System.out.println("第" + i +"个月的兔子对数: "+f2);}}}【程序2】题目:判断101-200之间有多少个素数,并输出所有素数。
程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。
public class lianxi02 {public static void main(String[] args) {int count = 0;for(int i=101; i<200; i+=2) {boolean b = false;for(int j=2; j<=Math.sqrt(i); j++){if(i % j == 0) { b = false; break; }else { b = true; }}if(b == true) {count ++;System.out.println(i );}}System.out.println( "素数个数是: " + count);}}【程序3】题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。
Java高效代码50例

Java⾼效代码50例摘⾃:导读 世界上只有两种物质:⾼效率和低效率;世界上只有两种⼈:⾼效率的⼈和低效率的⼈。
----萧伯纳常量&变量直接赋值常量,禁⽌声明新对象 直接赋值常量值,只是创建了⼀个对象引⽤,⽽这个对象引⽤指向常量值。
反例Long i=new Long(1L);String s=new String("abc");正例Long i=1L;String s="abc";当成员变量值⽆需改变时,尽量定义为静态常量 在类的每个对象实例中,每个成员变量都有⼀份副本,⽽成员静态常量只有⼀份实例。
反例public class HttpConnection{private final long timeout=5L;...}正例public class HttpConnection{private static final long timeout=5L;...}尽量使⽤基本数据类型,避免⾃动装箱和拆箱 Java中的基本数据类型double、float、long、int、short、char、boolean,分别对应包装类Double、Float、Long、Integer、Short、Character、Boolean。
Jvm⽀持基本类型与对象包装类的⾃动转换,被称为⾃动装箱和拆箱。
装箱和拆箱都是需要CPU和内存资源的,所以应尽量避免⾃动装箱和拆箱。
反例Integer sum = 0;int[] values = { 1, 2, 3, 4, 5 };for (int value : values) {sum+=value;}正例int sum = 0;int[] values = { 1, 2, 3, 4, 5 };for (int value : values) {sum+=value;}如果变量的初值会被覆盖,就没有必要给变量赋初值反例public static void main(String[] args) {boolean isAll = false;List<Users> userList = new ArrayList<Users>();if (isAll) {userList = userDAO.queryAll();} else {userList=userDAO.queryActive();}}public class Users {}public static class userDAO {public static List<Users> queryAll() {return null;}public static List<Users> queryActive() {return null;}}正例public static void main(String[] args) {boolean isAll = false;List<Users> userList;if (isAll) {userList = userDAO.queryAll();} else {userList=userDAO.queryActive();}}public class Users {}public static class userDAO {public static List<Users> queryAll() {return null;}public static List<Users> queryActive() {return null;}}尽量使⽤函数内的基本类型临时变量 在函数内,基本类型的参数和临时变量都保存在栈(Stack)中,访问速度较快;对象类型的参数和临时变量的引⽤都保存在栈(Stack)中,内容都保存在堆(Heap)中,访问速度较慢。
java实现音频文件播放功能

java实现⾳频⽂件播放功能本⽂实例为⼤家分享了java实现⾳频⽂件的播放功能的具体代码,供⼤家参考,具体内容如下实现思路1、⾸先获取⾳频⽂件的地址,然后通过IO流读取⾳频⽂件,加缓冲区,实现Player类的对象。
2、Player类主要⽤于播放器的初始化,以及通过它来实现⼀些⾳视频⽂件的播放,这个类需要⼿动去⽹上下载,然后添加路径到我们Eclipse的library中。
3、Player类有两种⽅法⽐较常⽤,play()⽅法和close()⽅法,前者⽤于启动⾳频⽂件,后者⽤于退出⾳频⽂件的播放,这两个⽅法我们在使⽤的时候需要注意,在整个⾳频播放的过程中,程序都会停留在play()⽅法中,类似于在读进度条,close()⽅法可以使得其退出播放,程序往下继续运⾏。
4、假设我们点击了开始按钮,那么程序就不会再去响应你的停⽌操作了,于是,我们可以通过多线程来实现这个启动和停⽌功能,让播放在⼀个线程⾥⾃⼰去执⾏。
5、那么循环呢?如何实现循环播放?答案是while循环,我们需要⼀个参数作为while的循环条件,类似于⼀个开关,只要为true,就⼀直循环播放。
6、我们在执⾏完⼀次播放后就不能再次对这个对象调⽤play()⽅法了,我们需要再次创建新的对象,那么我们要想关闭新的对象就必须让执⾏close()⽅法的对象是这个新的对象,我们每次新建相同名称的对象,player.close()执⾏后关闭的往往只能是最后的那个对象。
在启动和停⽌中我们看不出问题,但是当我们试图关闭在run⽅法⾥循环中的⾳频时,我们会发现停⽌不了!为什么?我们把对象传给线程类,close()⽅法依然可由此对象来执⾏,当他执⾏完play()⽅法后,我们new⼀个新的对象时,⼜开辟了⼀块新的内存空间存放这个对象的数据,再⽤原先的对象close()就不能到达效果了,即⽆法关闭这个⾳频。
解决办法:在每次new新对象后⽤set⽅法把对象传回去,我们可以理解为让close⽅法的调⽤者⼀直是这个新new的对象。
javase综合案例

updateButton.addActionListener(this);
deleteButton.addActionListener(this);
}
}
```
2. 实现业务逻辑
接下来,我们需要实现学生信息的添加、查询、修改和删除等功能。这些功能可以通过Java的方法来实现,例如使用`Scanner`类来读取用户输入的数据,使用`ArrayList`类来管理学生列表等。以下是实现业务逻辑的代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class StudentGUI extends JFrame implements ActionListener {
private JTextField nameField;
buttonPane.add(queryButton);
buttonPane.add(updateButton);
buttonPane.add(deleteButton);
pane.add(buttonPane, BorderLayout.SOUTH);
addButton.addActionListener(this);
private JTextField idField;
private JButton addButton;
private JButton queryButton;
private JButton updateButton;
private JButton deleteButton;
Java基础教程(第3版)_第4章_类与对象

如果有一个类是 public 类 , 那 么源文件的名字必须与这个类的 名字完全相同,扩展名是.java。编 译源文件将生成多个扩展名 为.class的字节码文件,每个字节 码文件的名字与源文件中对应的 类的名字相同,这些字节码文件 被存放在与源文件相同的目录中 。
2018/11/2
第 14 页
例子3 例子3中有两个Java源文件Example4_3.java和Rectangle.java,其 中Rectangle.java含有Rectangle类、Example4_3.java含有Circle类和 主类。程序运行效果如图4.5。
构造方法和对象的创建密切相关 。
2018/11/2
第 16 页
4.3.1 构造方法
构造方法是一种特殊方法,它的名字必须与它所在的类的名 字完全相同,而且没有类型。 允许一个类中编写若干个构造方法,但必须保证他们的参数 不同,即参数的个数不同,或者是参数的类型不同。
需要注意的是 ( 1) 如果类中没有编写构造方法, 系统会默认该类只有一个构造方法, 该默认的构造方法是无参数的,且方 法体中没有语句。 ( 2 )如果类里定义了一个或多个构 造方法,那么 Java 不提供默认的构造 方法 。
4.1.1
例子1 一个能输出圆的面积的 Java应用程序。
public class ComputerCircleArea { public static void main(String args[]) { double radius; //半径 double area; //面积 radius=163.16; area=3.14*radius *radius; //计算面积 System.out.printf("半径是%5.3f的园的面积:\n%5.3f\n",radius,area); } } 事实上, 如果其他Java应用程序也想计算圆的面积,同样需要知道计算 圆面积的算法,即也需要编写和这里同样多的代码。 现在提出如下问题: 能否将和圆有关的数据以及计算圆面积的代码进行封装,使得需要计 算圆面积的Java应用程序的主类无需编写计算面积的代码就可以计算出圆的 面积呢?
20个java案例

20个java案例以下是20个Java案例,涵盖了不同的主题和功能。
每个案例都有一个简要的描述和示例代码。
1. 计算两个数的和。
描述,编写一个程序,计算两个整数的和并输出结果。
示例代码:java.int num1 = 10;int num2 = 5;int sum = num1 + num2;System.out.println("两个数的和为," + sum);2. 判断一个数是否为偶数。
描述,编写一个程序,判断一个整数是否为偶数,并输出结果。
示例代码:java.int num = 6;if (num % 2 == 0) {。
System.out.println(num + "是偶数。
");} else {。
System.out.println(num + "不是偶数。
");}。
3. 求一个数的阶乘。
描述,编写一个程序,计算一个正整数的阶乘,并输出结果。
示例代码:java.int num = 5;int factorial = 1;for (int i = 1; i <= num; i++) {。
factorial = i;}。
System.out.println(num + "的阶乘为," + factorial);4. 判断一个字符串是否为回文字符串。
描述,编写一个程序,判断一个字符串是否为回文字符串,并输出结果。
示例代码:java.String str = "level";boolean isPalindrome = true;for (int i = 0; i < str.length() / 2; i++) {。
if (str.charAt(i) != str.charAt(str.length() 1 i)) {。
isPalindrome = false;break;}。
Java面向对象经典案例10个

J a v a面向对象经典案例10个------------------------------------------作者xxxx------------------------------------------日期xxxx1class Anthropoid //类人猿{private int n=100;void crySpeak(String s){System.out.println(s);}}class People extends Anthropoid{void computer(int a,int b){int c=a*b;System.out.println(c);}void crySpeak(String s){System.out.println("**"+s+"**");}}public class Monkey{public static void main(String args[]){Anthropoid monkey=new People();//monkey是People对象的上转型对象//puter(10,10); //非法monkey.crySpeak("我喜欢这个运动");People people=(People)monkey;//把上转型对象强制转化为子类的对象puter(10,10);}}2class ManyArea{public double area(double radius){return Math.PI*radius*radius;}public double area(double len,double width){return len*width;}public double area(int len,int width){return len*width;}public double area(double len,double width,double height){return len*width*height;}}public class OverLoad{public static void main(String args[]){ManyArea ob=new ManyArea();System.out.println("半径为3.0的圆的面积:"+ob.area(3.0)); System.out.println("长2.0、宽3.0的面积:"+ob.area(2.0,3.0)); System.out.println("长2、宽3的面积:"+ob.area(2,3));System.out.println("立方体的面积:"+ob.area(2.0,3.0,4.0));}}3class Animal{public void shout(){}}class Dog extends Animal{public void newDog(){System.out.println("Dog的新特性");}public void shout(){System.out.println("汪");}}class Cat extends Animal{public void shout(){System.out.println("喵");}}class Test{public void animalshout(Animal a){a.shout();}}public class PolyEx{public static void main(String[] args){Animal d=new Dog();//(1)Dog d1= (Dog)d;//(3)父类对象强制转换成子类对象d1.newDog();//d.newDog();d.shout();Test t=new Test();t.animalshout(d);//(2)t.animalshout(d1);}}4class ArrayEx{public int[] subarray(int a[],int start,int end){int subarr[] = new int[end-start];for(int i=0,j=start;j<end;i++,j++){subarr[i] = a[j];}return subarr;}}public class Test{public static void main(String args[]){ArrayEx arrex = new ArrayEx();int arr[] = new int[10];for(int i = 0;i<arr.length;i++){arr[i] = i+10;}int sub[] = arrex.subarray(arr,2,6);for(int temp:sub){System.out.println(temp);}}}5class Box{int length;int width;int height;void set(int len,int wid,int hei){length = len;width = wid;height = hei;}}class ShowBox{void show(Box b){System.out.println(b.length+" "+b.width+" "+b.height); }}class TestTwo{public static void main(String args[]){Box a = new Box();a.set(3,4,5);ShowBox sbox = new ShowBox();sbox.show(a);}}6.class One{int a = 5;void showB(){int a = 3;int b = this.a;System.out.println("b = "+b);}}public class ThisOne{public static void main(String args[]){One test = new One();test.showB();}}7.class Mystatic{private int x=3;public static void showx(){System.out.println("x="+x);}public static int add(int m){return m+x;}}class UseMystatic{public static void main(String args[]){Mystatic.showx();System.out.println("add="+Mystatic.add(2));}}8.class Point{int x;int y;Point(){x=0;y=0;//this(1,1);}Point(int a,int b){x=a;y=b;}void show(){System.out.println("x="+x+" y="+y); }}public class UsePoint{public static void main(String args[]){ Point p = new Point();p.show();}}9.class Point{private int x,y;Point(){x=1;y=3;}void showPoint(Point t){System.out.println("x="+t.x+" y="+t.y);}void seeit(){showPoint(this);}}public class UsePointThis{public static void main(String args[]){Point p=new Point();p.seeit();}}10class Point{static int x=2;int y=0;}public class UseStatic{public static void main(String args[]){System.out.println("利用类调用静态变量"); System.out.println("x="+Point.x);//System.out.println("y="+Point.y);Point p1=new Point();System.out.println("利用对象调用");System.out.println("x="+p1.x);System.out.println("y="+p1.y);Point p2=new Point();p2.y=3;System.out.println("对象p1中y的值"+"y="+p1.y); System.out.println("对象p2中y的值"+"y="+p2.y); p1.x=6;System.out.println("对象p1中x的值"+"x="+p1.x); System.out.println("对象p2中x的值"+"x="+p2.x);}}。
java基础编程 第四章 面向对象(下) 案例

案例4-1 super访问父类成员变量一、案例描述1、考核知识点编号:029004003名称:super关键字2、练习目标➢掌握使用super关键字访问父类成员变量的方法3、需求分析子类可以继承父类的非私有成员变量,如果在子类中修改了继承自父类的成员变量的值,再想要访问父类的该成员变量时,可以通过super.成员变量来实现。
为了让初学者熟悉super关键字的用法,本案例将分别设计Fu类及其子类Zi,并在Zi类的方法中使用super关键字访问Fu类的成员变量。
4、设计思路(实现原理)1)编写一个Fu类,在类中定义无参构造和一个初始值为20的num成员变量。
2)Zi类继承Fu类,在子类中对num值进行了修改,同时在子类中定义无参构造和一个无返回值的method()方法,method()方法中使用super关键字调用了Fu类的num成员变量。
3)定义测试类Example03。
二、案例实现1、编写Fu类及其子类Zi,在Zi类中使用super关键字调用Fu类成员变量,代码如下class Fu {Fu() {}int num = 20;}class Zi extends Fu {Zi() {}int num = 30;// 修改num的值void method() {System.out.println("method");// super关键字调用父类成员变量System.out.println("Fu类中num值为:" + super.num);System.out.println("Zi类中num值为:" + num);}}2、定义测试类Example03,代码如下:class Example03{public static void main(String[] args) {Zi z = new Zi();z.method();}}运行结果如图4-3所示。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
jta.setFont((new Font("宋体", Font.BOLD, 12)));
else if("楷体".equals(actionCommand))
jta.setFont((new Font("楷体", Font.PLAIN, 12)));
else if("Arial".equals(actionCommand))
else if("Pink".equals(actionCommand))
jta.setForeground(Color.pink);
else if("Blue".equals(actionCommand))
jta.sLeabharlann tForeground(Color.blue);
else if("宋体".equals(actionCommand))
jmb.add(jm3);
jta=new JTextArea("Hello Java!!
");
con.add(jp,BorderLayout.SOUTH);
jmi1.addActionListener(this);
jmi2.addActionListener(this);
jmi3.addActionListener(this);
JTextArea jta; JMenuItem jmi1,jmi2,jmi3,jmi4,jmi5,jmi6,jmi7,jmi8; public MenuShow(){
setTitle("菜单演示"); Container con=getContentPane(); BorderLayout bl=new BorderLayout(); JPanel jp=new JPanel(); JMenuBar jmb=new JMenuBar(); setJMenuBar(jmb); JMenu jm1=new JMenu("Color"); jmi1=new JMenuItem("Yellow"); jmi2=new JMenuItem("Orange"); jmi3=new JMenuItem("Pink"); jmi4=new JMenuItem("Blue"); jm1.add(jmi1); jm1.add(jmi2); jm1.add(jmi3); jm1.add(jmi4); JMenu jm2=new JMenu("Style"); jmi5=new JMenuItem("宋体"); jmi6=new JMenuItem("楷体"); jmi7=new JMenuItem("Arial"); jmi8=new JMenuItem("Times New Roman"); jm2.add(jmi5); jm2.add(jmi6); jm2.add(jmi7); jm2.add(jmi8); JMenu jm3=new JMenu("Exit"); jmb.add(jm1); jmb.add(jm2);
jmi4.addActionListener(this);
jmi5.addActionListener(this);
jmi6.addActionListener(this);
jmi7.addActionListener(this);
jmi8.addActionListener(this);
jp.add(jta);
setSize(300,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
String actionCommand=e.getActionCommand();
jta.setFont((new Font("Arial", Font.ITALIC, 12)));
else if("Times New Roman".equals(actionCommand))
jta.setFont((new Font("Times New Roman", Font.BOLD, 12)));
}
}
public static void main(String args[]){
MenuShow ms=new MenuShow();
}
}
if(e.getSource() instanceof JMenuItem ){
if("Yellow".equals(actionCommand))
jta.setForeground(Color.yellow);
else if("Orange".equals(actionCommand))
jta.setForeground(Color.orange);
编写程序,建立一个带有菜单的窗体。当用户选择“Color”或“Style”菜单的相关选项时, 标签中文字的字体和颜色会发生相应的变化。运行界面如下。
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MenuShow extends JFrame implements ActionListener{