1.
class ch07_5 {
public static void main(String [] args) {
int[] ori = {5, 2, 0, 7, 8, 9};
int[] dup, dup1;// 參照
dup1=ori; // 參照
dup = duplicateArray(ori); dup[2]=100; dup1[3]=200; ori[4]=45;
System.out.println("dup: " + dup1); showArr(dup1);
System.out.println("ori: " + ori); showArr(ori);
System.out.println("dup: " + dup); showArr(dup);
}
static int[] duplicateArray(int[] a) {
int[] b = new int[a.length];
for(int i=0; i
}
static void showArr(int[] a) {
for(int i=0; i
}
}
2.
class ch07_6 {
int a=10;
short c=3;
public static void main(String [] args) {
ch07_6 obj = new ch07_6();
obj.myMethod(1);
int a=20; obj.mm(a); System.out.println("obj: a="+a);
}
static void mm(int a) { a*=a; }
void myMethod(int a) {
System.out.println("方法內 this.a:" + this.a); //10
int c=30;
{
{
int b=2;
System.out.println("巢狀區塊內 b:" + b);//2
System.out.println("巢狀區塊內 this.b:" + this.b);//2
}
System.out.println("區塊內 b:" + b);//2
System.out.println("區塊內 this.b:" + this.b);//2
}
System.out.println("區塊內 c:" + c);//30
System.out.println("區塊內 this.c:" + this.c);//3
}
public void method() {
int c;
int d=5;
a++; b++; this.c++;
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+this.c);
System.out.println("d="+d);
}
static int b = 2;
}
3.
public class ch07_007 {
public static int m=1000;
public static void main(String[] args){
int n=m++;
System.out.println(m);
System.out.println(n);
}
}
4.
import java.io.*;
import javax.swing.*;
class ch07_7 {
public static void main(String [] args) throws IOException {
FileReader fr=new FileReader("a1.txt"); // read file
BufferedReader br=new BufferedReader(fr);
int n=0;
String str=br.readLine();
if (str != null) n=Integer.parseInt(str);
System.out.println(n + "!=" + fact(n) );
System.out.println("F("+n + ")=" + f(n) );
str=JOptionPane.showInputDialog( "Enter first integer" );
if (str != null) n=Integer.parseInt(str);
System.out.println(n + "!=" + fact(n) );
System.out.println("F("+n + ")=" + f(n) );
}
static long fact(int n) { //factorial
if(n==1) return 1;
else return n*fact(n-1);
}
static int f(int n) { //fibonacci
if ((n==1) || (n==2)) return 1;
else return f(n-1)+f(n-2);
}
}
5.
class ch07_0021{
int i=10;
public static void main(String args[]) {
ch07_0021 t=new ch07_0021();
t.m1();
}
void m1() {
int i=20;
ch07_0021 t=new ch07_0021();
t.i=30;
m2(t,i);
System.out.println(t.i);
}
void m2(ch07_0021 t,int i) {
i=0;
t.i=40;
ch07_0021 d=new ch07_0021();
t=d;
System.out.print(t.i+","+i+",");
}
}
6.
class ch07_8 {
public static void main(String [] args) {
new Box(6);
new Box(5, 4, 3);
new Box(); //error!!!
}
}
class Box {
int height;
int width;
int thickness;
public Box() {
height=width=100 ; thickness=10;
int i = height*width*thickness;
System.out.println(this + "的體積為:" + i);
}
public Box(int n) {//constructor
this();
height = width = thickness = n;
int i = height*width*thickness;
System.out.println(this + "的體積為:" + i);
}
public Box(int height, int width, int thickness) {
this();
this.height = height;
this.width = width;
this.thickness = thickness;
int i = height*width*thickness;
System.out.println(this + "的體積為:" + i);
}
/* void Box() { // method
int i = height*width*thickness;
System.out.println(this + "的體積為:" + i);
} */
}
7.
public class ch07_009 {
public static void main(String [] args){}
public void myTest(int i){}
//A public long myTest(int a){return 168;}
public boolean myTest(){return false;}//B
//C private long myTest(int i){return 99;}
public void main(int i){}//D
//E int main(String s[]){return 1;}
}
8.
9.
public class ch07_0013{
private static int x=7;
ch07_0013(int n){
x=n;
}
public static void main(String [] args){
ch07_0013 t=new ch07_0013(6);
System.out.println(t.x);
}
}