收藏 分销(赏)

Java就业培训教程源代码.doc

上传人:pc****0 文档编号:7796061 上传时间:2025-01-17 格式:DOC 页数:71 大小:281KB 下载积分:10 金币
下载 相关 举报
Java就业培训教程源代码.doc_第1页
第1页 / 共71页
Java就业培训教程源代码.doc_第2页
第2页 / 共71页


点击查看更多>>
资源描述
《Java就业培训教程》P34的Java原代码 程序清单:Promote.java class Promote { public static void main(String args[]) { byte b = 50; char c = 'a'; short s = 1024; int i = 50000; float f = 5.67f; double d = .1234; double result = (f * b) + (i / c) - (d * s); System.out.println((f * b) + " + " + (i / c) + " - " + (d * s)); System.out.println("result = " + result); } } 《Java就业培训教程》P35的Java原代码 程序清单:TestScope.java public class TestScope { public static void main(String[] args) { int x = 12; { int q = 96; // x和q都可用 System.out.println("x is "+x); System.out.println("q is "+q); } q = x; /* 错误的行,只有x可用, q 超出了作用域范围 */ System.out.println("x is "+x); } } 《Java就业培训教程》P37的Java原代码 程序清单:TestVar.java public class TestVar { public static void main(String [] args) { int x;//应改为int x=0; x=x+1; //这个x由于没有初始化,编译会报错。 System.out.println("x is "+x); } } P38的Java原代码 程序清单:Func1.java public class Func1 { public static void main(String [] args) { /* 下面是打印出第一个矩形的程序代码*/ for(int i=0;i<3;i++) { for(int j=0;j<5;j++) { System.out.print("*"); } System.out.println(); //换行 } System.out.println(); //下面是打印出第二个矩形的程序代码 for(int i=0;i<2;i++) { for(int j=0;j<4;j++) { System.out.print("*"); } System.out.println(); } System.out.println(); //下面是打印出第三个矩形的程序代码 for(int i=0;i<6;i++) { for(int j=0;j<10;j++) { System.out.print("*"); } System.out.println(); } System.out.println(); } } 《Java就业培训教程》P39的Java原代码 程序清单:Func2.java public class Func2 { public static void drawRectangle(int x,int y) { for(int i=0;i<x;i++) { for(int j=0;j<y;j++) { System.out.print(" * "); } System.out.println(); //换行 } System.out.println(); } public static void main(String [] args) { drawRectangle(3,5); drawRectangle(2,4); drawRectangle(6,10); } } 《Java就业培训教程》P40的Java原代码 程序清单:Func3.java public class Func3 { public static int getArea(int x,int y) { return x*y; } public static void main(String [] args) { int area = getArea(3,5); System.out.println("first Acreage is " + area); System.out.println("second Acreage is "+ getArea(2,4)); getArea(6,10); } } 《Java就业培训教程》P41的Java原代码 程序清单:Func4.java public class Func4 { public static int getArea(int x,int y) { if(x<=0 || y<=0) { return -1; } return x*y; } public static void main(String [] args) { int area = getArea(3,5); System.out.println("first Acreage is " + area); System.out.println("second Acreage is "+ getArea(2,4)); getArea(6,10); } } 《Java就业培训教程》P43的Java原代码 程序清单:Test.java public class Test { public static void main(String [] args) { int isum; double fsum; isum=add(3,5); isum=add(3,5,6); fsum=add(3.2,6.5); } public static int add(int x,int y) { reutrn x+y; } public static int add(int x,int y,int z) { return x+y+z; } public static double add(double x,double y) { return x+y; } } 《Java就业培训教程》P47的Java原代码 public class TestAnd { public static void main(String[] args) { int x=0; int y=0; if(x!=0 && y==y/x) System.out.println("y = "+y); } } 《Java就业培训教程》P48的Java原代码 程序清单:ShiftTest.java public class ShiftTest { public static void main(String [] args) { int x=0x80000000; int y=0x80000000; x=x>>1; y=y>>>1; System.out.println("0x80000000>>1 = " + Integer.toHexString(x)); System.out.println("0x80000000>>>1 = " + Integer.toHexString(y)); } } 《Java就业培训教程》P61的Java原代码 程序清单:TestDo.java public class TestDo { public static void main(String[] args) { int x=3; while(x==0) { System.out.println("ok1"); x++; } int y=3; do { System.out.println("ok2"); y++; } while(y==0); } } 《Java就业培训教程》P64的Java原代码 程序清单:PrintOddNum.java public class PrintOddNum { public static void main(String [] args) { for(int i=0;i<10;i++) { if(i%2==0) continue; System.out.println(i); } } } 《Java就业培训教程》P67的Java原代码 程序清单:TestArray.java public class TestArray { public static void main(String [] args) { int x[]; x=new int[100]; for(int i=0;i<100;i++) { System.out.println("x"+i+" is "+x[i]); } } } 《Java就业培训教程》P73的Java原代码 程序清单:TestArrayCopy.java public class TestArrayCopy { public static void main(String [] args) { int ia[]=new int[]{1,2,3,4,5}; int ib[]=new int[]{9,8,7,6,5,4,3}; System.arraycopy(ia,0,ib,0,3); // 复制源数组中从下标0开始的3个元素到目的数组,从下标0的位置开始存储。 for(int i=0;i<ia.length;i++) System.out.print(ia[i]); System.out.println(); for(int j=0;j<ib.length;j++) System.out.print(ib[j]); System.out.println(); } } 《Java就业培训教程》P81的Java原代码 class Compare { public static void main(String[] args) { String str1 = new String("abc"); String str2 = new String("abc"); String str3 = str1; if(str1==str2) System.out.println("str1==str2"); else System.out.println("str1!=str2"); if(str1==str3) System.out.println("str1==str3"); else System.out.println("str1!=str3"); } } 《Java就业培训教程》P82的Java原代码 class Compare { public static void main(String[] args) { String str1 = new String("abc"); String str2 = new String("abc"); String str3 = str1; if(str1.equals(str2)) System.out.println("str1 equal str2"); else System.out.println("str1 not equal str2"); if(str1.equals(str3)) System.out.println("str1 equal str3"); else System.out.println("str1 not equal str3"); } } 《Java就业培训教程》P86的Java原代码 class Person { private int age; public void setAge(int i) { if(i<0 || i>130) return; age = i; } public int getAge() { return age; } } public class TestPerson { public static void main(String args[]) { Person p1 = new Person(); p1.setAge(3); p1.setAge(-6); System.out.println(p1.getAge()); } } 《Java就业培训教程》P88的Java原代码 class Person { public Person() { System.out.println("the constructor 1 is calling!"); } private int age = 10; public void shout() { System.out.println("age is "+age); } } class TestPerson { public static void main(String[] args) { Person p1=new Person(); p1.shout(); Person p2=new Person(); p2.shout(); Person p3=new Person(); p3.shout(); } } 《Java就业培训教程》P90的Java原代码 class Person { private String name="unknown"; private int age = -1; public Person() { System.out.println("constructor1 is calling"); } public Person(String n) { name = n; System.out.println("constructor2 is calling"); System.out.println("name is "+name); } public Person(String n,int a) { name = n; age = a; System.out.println("constructor3 is calling"); System.out.println("name and age is "+name+";"+age); } public void shout() { System.out.println("listen to me!!"); } } class TestPerson { public static void main(String[] args) { Person p1=new Person(); P1.shout(); Person p2=new Person("Jack"); P2.shout(); Person p3=new Person("Tom",18); P3.shout(); } } 《Java就业培训教程》P94的Java原代码 class Person { private Person() { System.out.println("the constructor 1 is calling!"); } } class TestPerson { public static void main(String[] args) { Person p1=new Person(); } } 《Java就业培训教程》P95的Java原代码 class A { String name; public A(String x) { name = x; } public void func1() { System.out.println("func1 of " + name +" is calling"); } public void func2() { A a2 = new A("a2"); a2.func1(); } } class TestA { public static void main(String [] args) { A a1 = new A("a1"); a1.func2(); } } 《Java就业培训教程》P96的Java原代码 class A { String name; public A(String x) { name = x; } public void func1() { System.out.println("func1 of " + name +" is calling"); } public void func2() { A a2 = new A("a2"); this.func1();//使用this关键字调用func1方法 a2.func1(); } } 《Java就业培训教程》P99的Java原代码 class Container { Component comp; public void addComponent() { comp = new Component(this);//将this作为对象引用传递 } } class Component { Container myContainer; public Component(Container c) { myContainer = c; } } 《Java就业培训教程》P100的Java原代码 public class Person { String name; int age; public Person(String name) { this.name = name; } public Person(String name,int age) { this(name); this.age = age; } } 《Java就业培训教程》P101的Java原代码 class Person { public void finalize() { System.out.println("the object is going!"); } public static void main(String [] args) { new Person(); new Person(); new Person(); System.out.println("the program is ending!"); } } 《Java就业培训教程》P103的Java原代码 class PassValue { public static void main(String [] args) { int x = 5; change(x); System.out.println(x); } public static void change(int x) { x = 3; } } class PassRef { int x ; public static void main(String [] args) { PassRef obj = new PassRef(); obj.x = 5; change(obj); System.out.println(obj.x); } public static void change(PassRef obj) { obj.x=3; } } 《Java就业培训教程》P108的Java原代码 class Chinese { static String country="中国"; String name; int age; void singOurCountry() { System.out.println("啊!,亲爱的" + country); //类中的成员方法也可以直接访问静态成员变量 } } class TestChinese { public Static void main(String [] args) { System.out.println("Chinese country is " + Chinese.country); //上面的程序代码直接使用了"类名.成员"的格式 Chinese ch1 = new Chinese(); System.out.println("Chines country is " + ch1.country); //上面的程序代码直接使用了"对象名.成员"的格式 ch1.singOurCountry(); } } 《Java就业培训教程》P111的Java原代码 class StaticCode { static String country; static { country = "china"; System.out.println("StaticCode is loading"); } } class TestStaticCode { static { System.out.println("TestStaticCode is loading"); } public static void main(String [] args) { System.out.println("begin executing main method"); new StaticCode(); new StaticCode(); } } 《Java就业培训教程》P115的Java原代码 class Outer { int outer_i = 100; void test() { Inner in = new Inner(); in.display(); } class Inner { void display() { System.out.println("display: outer_i = " + outer_i); } } } class InnerClassDemo { public static void main(String[] args) { Outer outer = new Outer(); outer.test(); } } 《Java就业培训教程》P127的Java原代码 程序清单:Student.java class Person { public String name; public int age; public Person(String name,int age) { this.name=name; this.age=age; } public Person() //如果不写这个构造函数,看看对类Student有什么影响。 { } public void getInfo() { System.out.println(name); System.out.println(age); } } class Student extends Person { public void study() { System.out.println("Studding"); } public static void main(String[] args) { Person p=new Person(); p.name="person"; p.age=30; p.getInfo(); Student s=new Student(); s.name="student"; s.age=16; s.getInfo(); s.study(); } } 《Java就业培训教程》P135的Java原代码 interface Animal extends Runner { void breathe(); } class Fish implements Animal { public void run() { System.out.println("fish is swimming"); } public void breathe() { System.out.println("fish is bubbling"); } } abstract LandAnimal implements Animal { public void breathe() { System.out.println("LandAnimal is breathing"); } } 《Java就业培训教程》P138的Java原代码 程序清单:C.java class A { public void func1() { System.out.println("A func1 is calling"); } public void func2() { func1(); } } class B extends A { public void func1() { System.out.println("B func1 is calling"); } public void func3() { System.out.println("B func3 is calling"); } } class C { public static void main(String [] args) { B b=new B(); A a = b; callA(a); callA(new B()); } public static void callA(A a) { a.func1(); a.func2(); } } 《Java就业培训教程》P141的Java原代码 程序清单:Student.java class Student { String name; int age; boolean equals(Object obj) { Student st=null; if(obj instanceof Student) st = (Student)obj; else return false; if(st.name==this.name && st.age==this.age) return true; else return false; } public static void main(String[] args) { Student p=new Student(); Student q=new Student(); p.name="xyz"; p.age=13; q.name="xyz"; q.age=13; if(p.equals(q)) System.out.println("p 与 q 相等"); else System.out.println("p 与 q 不等"); } } 《Java就业培训教程》P144的Java原代码 程序清单:Interface.java interface PCI { void start(); void stop(); } class NetworkCard implements PCI { public void start() { System.out.println("Send ..."); } public void stop() { System.out.println("Network Stop."); } } class SoundCard implements PCI { public void start() { System.out.println("Du du..."); } public void stop() { System.out.println("Sound Stop."); } } class MainBoard { public void usePCICard(PCI p) { p.start(); p.stop(); } } class Assembler { public static void main(String [] args) { MainBoard mb=new MainBoard(); NetworkCard nc=new NetworkCard(); mb.usePCICard(nc); SoundCard sc=new SoundCard(); mb.usePCICard(sc); } } 《Java就业培训教程》P149的Java原代码 public class TestException { public static void main(String [] args) { try { int reslut = new Test().devide( 3, 0 ); System.out.println("the result is" + reslut ); } catch(Exception e) { System.out.println(e.getMessage()); } System.out.println("program is running here ,that is normal !"); } } class Test { public int devide(int x, int y) { int result = x/y; return x/y; } } 《Java就业培训教程》P154的Java原代码 public class TestException { public static void main(String [] args) { try { int result = new Test().devide( 3, 0 ); //int result = new Test().devide( 3, -1 ); //int result = new Test().devide( 3, 1 ); System.out.println("the result is " + result ); } catch(DevideByMinusException e) { System.out.println("program is running into"+ "DevideByMinusException"); System.out.println(e.getMessage()); System.out.println("the d
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 百科休闲 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服