1、《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) + "
2、 + " + (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);
3、 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
4、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("*");
5、 } 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(i
6、nt 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 7、 {
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
{ 8、
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就业培训教 9、程》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 " + 10、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);
}
p 11、ublic 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)
{
12、 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.p 13、rintln("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 14、");
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)
15、 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+" 16、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);
// 复制源数组中从下标 17、0开始的3个元素到目的数组,从下标0的位置开始存储。
for(int i=0;i 18、atic 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");
19、 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("str 20、1 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)
21、
{
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 22、的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( 23、);
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 Pe 24、rson(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 an 25、d 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 Per 26、son("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原代码
cla 27、ss 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 = n 28、ew 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 29、方法
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的 30、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 o 31、bject 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);
S 32、ystem.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)
{
33、 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 34、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 c 35、ountry;
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 S 36、taticCode();
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 37、
{
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;
38、}
public Person() //如果不写这个构造函数,看看对类Student有什么影响。
{
}
public void getInfo()
{
System.out.println(name);
System.out.println(age);
}
}
class Student extends Person
{
public void study()
{
System.out.println("Studdin 39、g");
}
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() 40、
}
}
《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");
}
}
abstr 41、act 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();
}
}
cl 42、ass 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());
}
43、
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;
44、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))
Syst 45、em.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 s 46、top()
{
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();
47、 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
{
p 48、ublic 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 !");
}
49、}
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, 50、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






