java语言概述2

上传人:san****019 文档编号:70867025 上传时间:2019-01-18 格式:PPT 页数:32 大小:353.31KB
返回 下载 相关 举报
java语言概述2_第1页
第1页 / 共32页
java语言概述2_第2页
第2页 / 共32页
java语言概述2_第3页
第3页 / 共32页
java语言概述2_第4页
第4页 / 共32页
java语言概述2_第5页
第5页 / 共32页
点击查看更多>>
资源描述

《java语言概述2》由会员分享,可在线阅读,更多相关《java语言概述2(32页珍藏版)》请在金锄头文库上搜索。

1、第 2 章,Java 语言概述,Java语言由语法规则和类库两部分组成。 语法规则:确定Java程序的书写规范; 类库:提供了一系列专业开发人员编写或由软件供应商提供的Java 程序模块类。,在编写 Java 程序的时候,需要充分地利用已存在的类,从而提高软件开发的效率。因此学习Java 程序设计需要从两个部分着手。一是语法规则;二是类库的使用。而后者往往是更好地使用 Java 语言编程的关键。,一、Java程序的运行环境,Java是解释型的高级编程语言,运行Java程序的基本过程,1、利用编辑器编写 Java源程序 源文件名:主类名.java 2、利用编译器将源程序编译成字节码 字节码文件名

2、:源文件名.class 3、利用虚拟机(解释器)运行 工作过程:载入、代码校验、解释执行,常见的Java开发环境,从网络上直接下载JDK(Java Developer Kit),并利用命令行方式操作 javac(编译器);java(解释器) Java集成开发环境(IDE) Visual J+、Eclipse、JBuilder Java教学集成开发环境 BlueJ(需要JDK1.4以上版本的支持),二、Applet 和 Application,Applet(小应用程序): 它不是一个完整的应用程序,而是框架程序中的一个模块,因此只能在WWW 浏览器环境下运行。 Application应用程序:

3、它是一个完整的应用程序,可以利用Java虚拟机独立地运行。,Applet 应用程序举例,import java.awt.*; import javax.swing.*; public class MyFirstJavaApplet extends JApplet public void init( ) Container contentPane=getContentPane( ); JLabel label=new JLabel(“This is a Java program.“, SwingConstants.CENTER); contentPane.add(label); ,Applet应

4、用程序的基本结构, MyFirstJavaApplet ,Applet 工作原理,Application应用程序举例,import java.io.*; public class MyFirstJavaProgram public static void main(String args) System.out.println(“Hello World!”); ,Application应用程序的基本结构,编写Java程序需要注意的几点,Java 程序区分大小写 在一个Java 源文件中,可以包含一个或多个类定义,但只能有一个访问权限为public的类 源程序的文件名应该用public 类名+

5、.java作为文件名。,三、Java程序的基本输入输出,Applet 应用程序的输入输出 输入:利用图形用户界面的组件接收用户信息 输出:以图形或图像的形式输出结果 Application应用程序的输入输出 字符界面:利用 System.in.read 输入, System.out.print 和 System.out.println 输出 图形用户界面:与 Applet 类似,Applet 举例,import java.awt.*; import javax.swing.*; public class SimpleGraphicsInOut extends JApplet public vo

6、id init( ) Container contentPane=getContentPane(); String input = JOptionPane.showInputDialog(“Enter a string:“); JLabel label=new JLabel(“String: “+input); contentPane.add(label); ,运行结果,import java.io.*; public class SimpleCharInOut public static void main(String args) throws IOException char c= ;

7、System.out.print(“Enter a character please:“); c=(char)System.in.read( ); System.out.println( “Youve entered character: “+c); ,Application 字符界面举例,接收一个字符,运行结果,import java.io.*; public class ApplicationLineIn public static void main(String args) throws IOException String s=“; System.out.print(“please

8、enter a string:“); BufferedReader in= new BufferedReader( new InputStreamReader(System.in); s=in.readLine( ); System.out.println(“Youve entered string: “+s); ,接收一个文本行,运行结果,import java.io.*; public class getNumber public static void main(String args) throws IOException int i=0; String s; System.out.p

9、rint(“请输入一个整型数:“); BufferedReader br= new BufferedReader(new InputStreamReader(System.in); s=br.readLine( ); i=Integer.parseInt(s); System.out.println(“你输入了数字: “+i ); ,将字符串转换 成数值类型,运行结果,import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GraphicsApplicationInOut public sta

10、tic void main(String args ) MyFrame frame=new MyFrame( ); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); ,Application 图形用户界面举例,public class MyFrame extends JFrame implements ActionListener JLabel prompt, output; JTextField input; Container contentPane; JPanel panel; public MyFra

11、me( ) setTitle(“Application Graphics Frame“); setSize(300,200); contentPane=getContentPane(); panel=new JPanel(); prompt=new JLabel(“Enter a character please:“); input=new JTextField(20); output=new JLabel(“); panel.add(prompt); panel.add(input); panel.add(output); contentPane.add(panel); input.addA

12、ctionListener(this); ,Application 图形用户界面举例,public void actionPerformed(ActionEvent e) if (e.getSource()=input) output.setText(“Youve entered character “+ (JTextField)e.getSource().getText(); ,Application 图形用户界面举例,运行结果,第1章作业,P33 练习与思考 1、2、3、4、5 = 作业要求: 1、用数学作业纸 2、写号学号和姓名 3、每星期三交作业,Perfection,From the

13、 article Number Theory in the 1994 Microsoft Encarta: “If a, b, c are integers such that a = bc, a is called a multiple of b or of c, and b or c is called a divisor or factor of a. If c is not 0, b is called a proper divisor of a. Even integers, which include 0, are multiples of 2, for example, -4,

14、0, 2, 10; an odd integer is an integer that is not even, for example, -5, 1, 3, 9. A perfect number is a positive integer that is equal to the sum of all its positive, proper divisors; for example, 6, which equals 1 + 2 + 3, and 28, which equals 1 + 2 + 4 + 7 + 14, are perfect numbers. A positive nu

15、mber that is not perfect is imperfect and is deficient or abundant according to whether the sum of its positive, proper divisors is smaller or larger than the number itself. Thus, 9, with proper divisors 1, 3, is deficient; 12, with proper divisors 1, 2, 3, 4, 6, is abundant.“ Problem Statement: Giv

16、en a number, determine if it is perfect,abundant, or deficient.,We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three ways: 1) no intersection because they are parallel, 2) intersect in a line because they are on top of one another (i.e. they are the same line), 3) intersect in a point.

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号