




已阅读5页,还剩17页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
JAVA1、public class FuncMain /*public static void main(string args) /-(1)-System.out.println(Hello main() method);*/运行:找不到符号/*public void static main(String args) /-(2)-System.out.println(Hello main() method);*/ 运行:方法声明无效,需要返回类型/*public static void main(String args) /-(3)-System.out.println(Hello main() method);*/ 运行结果:Hello main() method/*public static int main(String args) / -(4)- System.out.println(Hello main() method);*/运行:缺少返回语句2、public class Foo public static void main(String args) String s=null; System.out.println(s=+s); 改前:可能尚未初始化变量 s s =null3、public class Test static int s; public static void main(String args) Test t = new Test(); t.start(); System.out.print(s); void start() int x = 7; twice(x); System.out.print(x + ); void twice(int x) x = x*2; s = x; 运行结果:7 144、public class Foopublic static void main(String args) StringBuffer a = new StringBuffer(A); StringBuffer b = new StringBuffer(B); operate(a,b); System.out.println(a+,+b);static void operate(StringBuffer x, StringBuffer y) y.append(x); y=x;运行结果:A,BA5、package xcom;public class Useful int increment(int x) return +x; import xcom.*; /Needy3与Useful 不在同一个包 / line 1public class Needy3 public static void main(String args) xcom.Useful u = new xcom.Useful(); / line 2 System.out.println(u.increment(5); 运行结果:66、(数组越界)class Fork public static void main(String args) if(args.length = 1 | args1.equals(test) /改为 if(args.length = 0 System.out.println(test case); else System.out.println(production + args0); 改前Exception in thread main java.lang.ArrayIndexOutOfBoundsException: 1既“主线程”java.lang.ArrayIndexOutOfBoundsException异常:1运行结果为test case7、(继承与覆盖)class Clidder public final void flipper() System.out.println (Clidder); public class Clidlet extends Clidder public final void flipper() System.out.println(Clidlet); public static void main(String args) new Clidlet().flipper(); 改前/ Clidlet的flipper()无法覆盖Clidder的flipper(),被覆盖的类型为final去掉finalrun:Clidlet8、(long改为int) class Super public int getLength()return 4;public class Sub extends Superpublic long getLength() return 5;public static void main(String args) Super sooper = new Super(); Sub sub = new Sub(); System.out.println( sooper.getLength()+”,”+sub.getLength();/Sub中的getLength()无法覆盖Super中的getLength()/若改为int,int或long,运行结果:run:4,59、 class Base Base()System.out.print(“Base”); public class Alpha extends Basepublic static void main(String args)new Alpha();new Base();运行结果run:BaseBase10、public class Test public static void aMethod() throws Exception try throw new Exception(); finally System.out.println(“finally”); public static void main(String args) try aMethod(); catch (Exception e) System.out.println(“exception”); System.out.println(“finished”); 运行结果run:finallyexceptionfinished11)public class Pass public static void main(String 1 args) int x = 5; Pass p = new Pass(); p.doStuff(x); System.out.print(” main x = “+ x); void doStuff(int x) System.out.print(” doStuff x = “+ x+); 运行结果run: doStuff x = 5 main x = 512、(加import java.io.IOException;import java.io.RandomAccessFile; )public class TestIOApppublic static void main(String args) throws IOExceptionRandomAccessFile file = new RandomAccessFile(test.dat,rw);file.writeBoolean(true);file.writeInt(123456);file.writeInt(7890);file.writeLong(1000000);file.writeInt(777);file.writeFloat(.0001f);file.seek(5);System.out.println(file.readInt();运行结果run:789013、import java.util.*;public class TreeSet1 public static void main(String args) Set set = new HashSet(); set.add(alpha); set.add(theta); set.add(beta); set.add(alpha); Set sortedSet = new TreeSet(set); Iterator itr = sortedSet.iterator(); while(itr.hasNext() System.out.print(itr.next() + ); itr.remove(); for(String s: sortedSet) System.out.print( + s + ); /main方法和TestSet类定义结束run:alpha beta theta 14、class MyExcep extends Exception public String toString() return negative; public class ExceptionDemo public static void mySqrt(int a) throws MyExcep if(a 0) throw new MyExcep();System.out.println(Math.sqrt(a);public static void main(String args) try mySqrt(25); mySqrt(-5); catch(MyExcep e) System.out.println(Caught + e);运行结果:5.0 Caught negative 15、class Bread String name;Bread() this(汉堡); Bread(String n) name = n;System.out.println (Bread( + name + ); /构造方法和类定义结束class Lettuce Lettuce() System.out.println(Lettuce(); class PortableMeal PortableMeal() System.out.println(PortableMeal(); class Sandwich extends PortableMeal Bread b = new Bread(); Lettuce l = new Lettuce(); public Sandwich() b = new Bread(全麦面包); System.out.println(Sandwich(); /构造方法和类定义结束public class TestObjIniSandwich2 /public类public static void main(String args) /main方法new Sandwich(); /main方法和public类定义结束运行结果:PortableMeal()Bread(汉堡)Lettuce()Bread(全麦面包)Sandwich()16、public class TestArrayCopy public static void main(String args) System.out.println(); int intArr1 = 1,6,2,3,4,5; int intArr2 = new int3; System.arraycopy(intArr1,0,intArr2,0,intArr1.length); /数组拷贝 intArr210 = 111; intArr221 = 100; /修改intArr221元素之值 System.out.println(intArr110+ + intArr111); System.out.println(intArr120+ + intArr121); /main方法和public类定义结束运行结果: 111 34 10017、import java.io.*;public class IOReadString public static void main(String args) BufferedReader br = new BufferedReader(new InputStreamReader(System.in);try System.out.println(br.readLine(); catch(IOException e) 输入 22输出 2218、class Base int i;Base() add(1); void add(int v) i += v; void print() System.out.println(i); class Extension extends Base Extension() add(2); void add(int v) i += v*2; public class Q073 public static void main(String args) bogo(new Extension(); static void bogo(Base b) b.add(8); b.print(); 运行结果:22XML.XQuery Everyday Italian Giada De Laurentiis 2005 30.00 Harry Potter J K. Rowling 2005 29.99 XQuery Kick StartJames McGovern Per Bothner Kurt Cagle James Linn Vaidyanathan Nagarajan 2003 49.99 Learning XML Erik T. Ray 2003 39.95可能脚本与结果反过来(1)【XQuery 使用函数来提取 XML 文档中的数据,doc() 用于打开 books.xml 文件:doc(books.xml)】下面的路径表达式用于在 books.xml 文件中选取所有的 title 元素:doc(books.xml)/bookstore/book/title /bookstore 选取 bookstore 元素,/book 选取 bookstore 元素下的所有 book 元素,而 /title 选取每个 book 元素下的所有 title 元素上面的 XQuery 可提取以下数据:Everyday ItalianHarry PotterXQuery Kick StartLearning XML(2)下面的谓语用于选取 bookstore 元素下的所有 book 元素,并且所选取的 book 元素下的 price 元素的值必须小于 30:doc(books.xml)/bookstore/bookprice30上面的 XQuery 可提取到下面的数据: Harry Potter J K. Rowling 2005 29.99(3)doc(books.xml)/bookstore/bookprice30/title上面这个表达式可选取 bookstore 元素下的 book 元素下所有的 title 元素,并且其中的 price 元素的值必须大于30。下面这个 FLWOR 表达式所选取的数据和上面的路径表达式是相同的:for $x in doc(books.xml)/bookstore/bookwhere $x/price30return $x/title结果是:XQuery Kick StartLearning XML通过 FLWOR,您可以对结果进行排序:for $x in doc(books.xml)/bookstore/bookwhere $x/price30order by $x/titlereturn $x/title填空、选择、程序1.运行Javac生成的是class文件 javac -d directory 与java -cp path 注意:若要将Test.class生成在指定目录下,可以使用javac -d命令,如:javac -d c:/ e:/project/Test.java (在c:/目录下生成Test.class)Javac -cp中的-cp并不是指定Test.java的目录,-cp/-classpath只能是指定类文件(.class文件)的路径 eg: java -cp e:/project Test (指的是调用解释器执行e:/project中的Test.class字节码)【-d表示指定存放生成的类文件的位置,-cp指定查找用户类文件和注释处理程序的位置】2、jvm自动垃圾回收 Java Virtual Machine(Java虚拟机)3、3.Java变量命名规则(Java标识符是大小写敏感,没有最大长度限制,不能和关键字相同不能以数字开头,要以字母、下划线、$开头) 变量(变量的定义包括变量的类型、变量名、值三部分)类型 按作用域分:局部变量、类成员变量、方法参数、异常处理参数 存取权限:public、defalut、protected、private、static(静态)、final(最终、transient(短暂,暂时性变量)、volatile(易变的,用于声明一个多线程共享变量)public:最大的,公共的,共同访问的 l private:最小的,只能在本类中访问 l default:默认的,只能在本包中访问 l protected:在本包,以及不同包的子类中可以访问。注意:Java与C+区别:Java摒弃了C+中容易引发程序错误的地方,如指针和内存管理;Java提供了丰富的类库;Java实现了多线程技术;Java允许程序动态地装入运行过程中所需要的类,Java不支持结构(结构类型struct)和联合(联合类型union),所有内容封装在类中;Java不支持宏,用关键字final声明常量。4、Jdbc全称 区分大小写Java Data Base Connectivity,java数据库连接5、Java 接口中定义方法是public adstract(抽象的)的interfaceJava在接口中声明的方法是public和abstract属性(接口中只能进行方法声明,不提供方法的实现;当一个类通过关键字implements声明自己使用/或实现一个或多个接口,该类必须实现该接口的所有方法即提供方法体)6、 Thread (线程) runnable7、AWT的页面布局方式 BorderLayout8、数组声明:先声明后创建:int array;array = new int10;9. 字符输出流FileWriterOutputStream Writer 文件数据流FileInputStream与FileOutputStream 过滤字节流FilterInputStream与FilterOutputStream数据流(stream)分为输入流输入流的指向称数据源(Data Source)(Input Stream)和输出流输出流的指向称数据接收器(Data Sink)(Out Stream)。10、short x ; short y; short 5 x2; short z2 5; short z ; short y2 = 5;11、boolean b = new boolean5;, 则b3 = _false_布尔值)包括两个值:True和False1、(Extensible Markup Language, XML)2、XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD)。3、XSL 指扩展样式表语言(EXtensibleStylesheetLanguage)。 XSL 包括三部分:XSLT一种用于转换 XML 文档的语言。XPath一种用于在 XML 文档中导航的语言。XSL-FO一种用于格式化 XML 文档的语言。4.XML 与 HTML 的主要差异:XML 不是 HTML 的替代。XML 和 HTML 为不同的目的而设计:XML 被设计为传输和存储数据,其焦点是数据的内容。HTML 被设计用来显示数据,其焦点是数据的外观。HTML 旨在显示信息,而 XML 旨在传输信息。5.如果你把字符 放在 XML 元素中,会发生错误,这是因为解析器会把它当作新元素的开始在 XML 中,有 5 个预定义的实体引用:< 大于 & & 和号 ' 单引号 " 引号 (注释:在 XML 中,只有字符 和 & 确实是非法的。大于号是合法的,但是用实体引用来代替它是一个好习惯。)0、xlt转换 把字变红 产品的例子PRODUCTNAMEfont-family:Arial;font-size:30pt;font-weight:bold;color:red;display:block;padding top:6pt;padding bottom:6ptPRICE, DESCRIPTION, QUANTITYfont-family:Arial;font-size:15pt;color:teal;display:block;padding top:2pt;padding bottom:2pt 迷你巴士/这是为4岁儿童玩具以上描述 productdata 1 、CyberShoppe sells two categories of products, books and toys. Product details include the product name, description, price, and the available quantity on hand. The product price must always be greater than zero. In addition to these details, the data store needs to store the category and product ID. Mini Bus This is a toy for children aged 4 and above 75 54 You have to write the schema file product.xsd against which the XML file product.xml (provided below) can be validated.1、 cybershoppe卖两大类产品,书籍和玩具。产品资料包括产品名称,描述,价格,数量和可用手上。产品的价格必须大于零。除了这些细节,数据存储需要存储类别和产品ID。你必须写架构文件product.xsd对XML文件product.xml(以下)可以验证。(XML Schema 语言也可作为 XSD(XML Schema Definition)来引用。) 2 、Consider the following XML code(cdcatalog.xml): 帝国作戏 艺术家Bob迪伦 /价格价格 1985 CD 隐藏你的内心Empire BurlesqueBob DylanUSAColumbia10.901985Hide your heart 艺术家Bonnie Tyler 英国 /价格价格 1988 CD /Bonnie TylerUKCBS Records9.901988The following figure shows a sample output of title and artist that the CDs price is bigger than 10.第9页Now you have to create a XSLT style sheet(order.xsl) to display the report.2考虑下面的XML代码(cdcatalog。XML):下图显示的标题和艺术家,CD的价格是大于10的样本输出。现在你必须创建一个XSLT样式表(订单。XSL)显示报告。 【 元素定义了一个模板。而 match=/ 属性则把 此模板与 XML 源文档的根相联系。】 My CD Collection 书店哈利波特 Giada De Laurentiis Title Artist th指的是表头。TR指的是表的行,而TD指的是表的列。align就是水平的对齐方式。align=left就是左对齐,align=center 居中 align=right右对齐大1、试编写一段Applet小程序来绘制一面国旗。程序的运行结果如图1所示import java.awt.*;import java.awt.Graphics;import java.awt.event.*;import java.applet.Applet;public class CountryFlag extends Applet public void paint(Graphics g) int xs1=80,85,98,87,92,80,68,73,63,75,80; int ys1=40,54,54,62,76,67,76,62,54,54,40; int xs2=115,117,120,117,118,115,112,113,110,113,115; int ys2=34,38,38,40,44,42,44,40,38,38,34; g.setColor(Color.red); g.fillRect(40,20,250,150); g.setColor(Color.yellow); Polygon p1=new Polygon(xs1,ys1,10); g.fillPolygon(p1); Polygon p2=new Polygon(xs2,ys2,10); g.fillPolygon(p2);/绘制小五角星 g.copyArea(110,34,10,10,10,11);/复制绘出第二个小五星 g.copyArea(110,34,10,10,10,26);/复制绘出第三个小五星 g.copyArea(110,34,10,10,0,38);/复制绘出第四个小五星 import java.awt.*;import java.applet.*;import java.util.*; public class HelloApplet extends Applet public void paint(Graphics g) super.paint(g); g.drawOval(200,300,400,300); /画椭圆 g.drawString(circle,45,20); g.drawRect(40,40,100,100);/画矩形 g.drawString(rectangle,45,20); A Simple Program 大2、编写一个程序,创建一个AWT面板,该面板包含标有三个不同颜色名称的三个按钮,单击每个按钮时应使窗口的背景色显示为相应的颜色。package javaapplication5;import java.awt.*;import java.awt.event.*;public class Chap4 extends Frame implements ActionListener Frame f; Button b1; Button b2; Button b3; public Chap4() f=new Frame(基本GUI编程); b1=new Button(红色); b2=new Button(蓝色); b3=new Button(黄色); f.setLayout(new FlowLayout(); f.setSize(300,200); f.setVisible(true); f.add(b1); f.add(b2); f.add(b3); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); public void actionPerformed(ActionEvent e) if(e.getSource()=b1) f.setBackground(Color.red); else if(e.getSource()=b2) f.setBackground(Color.blue); else if(e.getSource()=b3) f.setBackground(Color.yellow); public static void main (String args) new Chap4(); 大3、 编写一个图形界面应用程序,其中包含一个按钮。当鼠标移到按钮上时,隐藏按钮;当鼠标离开按钮时,显示按钮。package java_study;import java.awt.event.*;import javax.swing.*;public class Java_study private JFrame fm=new JFrame();private JButton bt=new JButton(button1);Java_study()fm.setLayout(null);fm.setDefaultCloseOperation(3);fm.setBounds(300, 200, 450, 300);bt.setBounds(10, 10, 100, 25);bt.addMouseListener(new bt1event();fm.getContentPane().add(bt);fm.show();class bt1event implements MouseListenerpublic void mouseClicked(MouseEvent arg0) public void mouseEntered(MouseEvent arg0) bt.setVisible(false);public void mouseExited(MouseEvent arg0) bt.setVisible(true);public void mousePressed(MouseEvent arg0) public
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 山东省聊城市2024-2025学年一年级第二学期期末语文学业水平检测(含答案)
- 并行内存访问冲突消解-洞察及研究
- 公共政策执行监控-洞察及研究
- 部门内部安全培训课件
- 避孕节育科普知识课件
- 基于大数据的前列腺增生分型与电切镜参数动态匹配研究
- 基于AI的制板滚桶磨损状态多维度实时监测系统开发
- 合成路线的原子经济性优化与催化剂筛选机制
- 可降解反光胸背带的环境效益评估与成本控制平衡点
- 可回收热塑性材料在饰条应用中的性能-成本平衡点
- 神经干细胞课件
- 核能质保监查员考试题及答案
- 青海“8·22”川青铁路尖扎黄河特大桥施工绳索断裂事故案例学习安全警示教育
- 9.3纪念抗日战争胜利80周年阅兵式观后感
- 2025年70周岁以上老年人换长久驾照三力测试题库(含答案)
- 人才匹配算法的优化
- 兵团普通职工考试试题及答案
- 家庭劳动教育的制度性困境与教育主体重构研究
- 桥梁照明系统设计方案
- 时事政治考试题(含答案)
- 生物标本课程讲解
评论
0/150
提交评论