JAVA語言程序設計(基礎篇) 第十版——第十章 面向對象思考 (參考答案)

 編程練習題:(待更新.........)

(10.2~10.3節)+(10.4~10.8節)+(10.9節)+(第10.10~10.11節)

(10.2~10.3節)

*10.1(時間類Time)

 

首先普及一下(什麼是GMT時間)

GMT時間(格林尼治標準時間)一般指世界時間, 即0時區的區時,比北京時間(東8區)晚8小時;所以,
GMT時間+8小時所得結果就是北京時間。

這是我運行時的時間: 下午15:46

對象1運行的GMT時間與我當前的時間差8小時。(運行正確)

package p10;

public class Time {
	private long hour;
	private long minute;
	private long second;
	
	//無參構造方法
	public Time() {
		//獲取當前時間,可以用 System.currentTimeMillis()方法獲取當前總的毫秒數
		long totalMilliseconds=System.currentTimeMillis();
		
		// 總的毫秒數/1000=總的秒數
		long totalSeconds=totalMilliseconds/1000;
		//總的秒數%60=當前秒數
		long currentSeconds=totalSeconds%60;
		this.second=currentSeconds;
		
		// 總的秒數/60=總的分鐘數
		long totalMinutes=totalSeconds/60;
		//總的分鐘數%60=當前分鐘數
		long currentMinutes=totalMinutes%60;
		this.minute=currentMinutes;
		
		// 總的分鐘數/60=總的小時數
		long totalHours=totalMinutes/60;
		//總小時數%24=當前小時數
		long currentHours=totalHours%24;
		this.hour =currentHours;
	
	}
	
	public Time(long elapseTime) {
		// 總的毫秒數/1000=總的秒數
		long totalSeconds=elapseTime/1000;
		
		//總的秒數%60=當前秒數
		long currentSeconds=totalSeconds%60;
		this.second=currentSeconds;
				
		// 總的秒數/60=總的分鐘數
		long totalMinutes=totalSeconds/60;
		//總的分鐘數%60=當前分鐘數
		long currentMinutes=totalMinutes%60;
		this.minute=currentMinutes;
				
		// 總的分鐘數/60=總的小時數
		long totalHours=totalMinutes/60;
		//總小時數%24=當前小時數
		long currentHours=totalHours%24;
		this.hour =currentHours;		
		
		
	}
	
	public Time(long hour, long minute, long second) {
		this.hour=hour;
		this.minute=minute;
		this.second=second;
	
	}
	
	public long getHour() {
		return hour;
	
	}
	
	public long getMinute() {
		return minute;
	
	}
	
	public long getSecond() {
		return second;
	
	}
	
	public void setTime(long elapseTime) {
		
		// 總的毫秒數/1000=總的秒數
		long totalSeconds=elapseTime/1000;
		
		
		//總的秒數%60=當前秒數
		long currentSeconds=totalSeconds%60;
		this.second=currentSeconds;
				
		// 總的秒數/60=總的分鐘數
		long totalMinutes=totalSeconds/60;
		//總的分鐘數%60=當前分鐘數
		long currentMinutes=totalMinutes%60;
		this.minute=currentMinutes;
				
		// 總的分鐘數/60=總的小時數
		long totalHours=totalMinutes/60;
		//總小時數%24=當前小時數
		long currentHours=totalHours%24;
		this.hour =currentHours;
				
		
	}
}
package p10;

public class Test1 {

	public static void main(String[] args) {
		
		Time object1=new Time();
		
		Time object2=new Time(555550000);
		
		//顯示object1的時間
		System.out.println("object1's Current time is "
		+object1.getHour()+":"+object1.getMinute()+":"+object1.getSecond()+" GMT");
		
		//顯示object2的時間
		System.out.println("object2's Current time is "
		+object2.getHour()+":"+object2.getMinute()+":"+object2.getSecond()+" GMT");
		
	}

}

10.2(BMI類)

 

10.3(MyInteger類)

package p10;

public class MyInteger {
	//存儲這個對象表示的int值
	public int value;
	
	public MyInteger(int value) {
	this.value=value;
	}
	
	public int getInt() {
		return this.value;
	}
	
	//判斷是否是偶數,偶數:能被2整除的數,包括正偶數、負偶數和零
	public boolean isEven() {
		if(getInt() % 2 == 0)
			return true;
		else 
			return false;
		
	}
	
	//判斷是否是奇數,奇數:不能被2整除的數
	public boolean isOdd() {
		if(getInt() % 2 != 0)
			return true;
		else
			return false;
		
	}
	
	//素數:在大於1的整數中,只能被1和這個數本身整除的數,如2,3,5,7,11
	public boolean isPrime() {
		for(int divisor=2; divisor<=getInt()/2; divisor++) {
			if(getInt()%divisor==0) {
				return false;
			}
		}
		
		return true;
	}
	
	public static boolean isEven(int value) {
		if(value%2==0)
			return true;
		else
			return false;
	}
	
	public static boolean isOdd(int value) {
		if(value%2!=0)
			return true;
		else
			return false;
	}
	
	public static boolean isPrime(int value) {
		for(int divisor=2; divisor<=value/2; divisor++) {
			if(value%divisor==0) {
				return false;
			}
		}
		
		return true;
	}
	
	public static boolean isEven(MyInteger myInteger) {
		if(myInteger.value % 2 == 0)
			return true;
		else
			return false;
	}
	
	public static boolean isOdd(MyInteger myInteger) {
		if(myInteger.value % 2 != 0)
			return true;
		else
			return false;
	}
	
	public static boolean isPrime(MyInteger myInteger) {
		for(int divisor=2; divisor<=myInteger.value/2; divisor++) {
			if(myInteger.value % divisor == 0) {
				return false;
			}
		}
		
		return true;
	}
	
	public boolean equals(int count) {
		if(this.value==count)
			return true;
		else
			return false;
	}
	public boolean equals(MyInteger myInteger) {
		if(myInteger.value==this.value)
			return true;
		else
			return false;
	}
	
	public static int parseInt(char[] ch) {
		return Integer.valueOf(new String(ch));
	}
	
	public static int parseInt(String s) {
		return Integer.valueOf(s);
	}
}

 

10.4(MyPoint類) 

 

package p10;

public class MyPoint {
	
	public double x;
	public double y;
	
	public double getX() {
		return x;
	
	}
	
	public double getY() {
		return y;
	
	}
	
	public MyPoint() {
		this.x=0;
		this.y=0;
	}
	
	public MyPoint(double x, double y) {
		this.x=x;
		this.y=y;
	}
	
	public double distance(MyPoint myPoint) {
		
		return Math.sqrt(Math.pow((this.x-myPoint.x),2) + Math.pow((this.y-myPoint.y),2));
		
	}
	
	public double distance(double x, double y) {
		
		return Math.sqrt(Math.pow((this.x-x),2) + Math.pow((this.y-y),2));
	}
}
package p10;

public class Test4 {

	public static void main(String[] args) {
		
		MyPoint a=new MyPoint();
		
		MyPoint b=new MyPoint(10,30.5);
		
		System.out.printf("兩點間的距離爲:%.2f", a.distance(b));

	}

}

 

(10.4~10.8節)

*10.5(顯示素數因子)

書上寫有這個類,在本書第320頁,類的實現也有:

package p10;

public class StackOfIntegers {
	
	private int[] elements;
	private int size;
	public static final int DEFAULT_CAPACITY=16;
	
	public StackOfIntegers() {
		this(DEFAULT_CAPACITY);
	}
	
	public StackOfIntegers(int capacity) {
		elements=new int[capacity];
	}

	public void push(int value) {
		if(size>=elements.length) {
			int[] temp=new int[elements.length*2];
			System.arraycopy(elements, 0, temp, 0, elements.length);
			elements=temp;
		}
		elements[size++]=value;
	}
	
	public int pop() {
		return elements[--size];
		
	}
	
	
	public int peek() {
		return elements[size-1];
	}
	
	public boolean empty() {
		return size==0;
		
	}
	
	public int getSize() {
		return size;
	
	}
}
package p10;

import java.util.Scanner;

public class Test5 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("請輸入一個正整數:");
		int number=input.nextInt();
		input.close();
		
		//創建一個棧類
		StackOfIntegers stack=new StackOfIntegers();
		
		int i=2;
		while(number/i!=0) {
			if(number%i==0) {
				number=number/i;
				
				stack.push(i);
				//System.out.println(i);顯示最小因子
			}
			else if(number%i!=0){
				i++;
			}
			
			
		}
		
		//先進先出後進後出
		while(!stack.empty()) {
			System.out.print(stack.pop()+" ");
		}
			
		

	}

}

 

*10.5(顯示素數)

package p10;

public class Test6 {

	public static void main(String[] args) {
		
		//創建一個棧類
		StackOfIntegers stack=new StackOfIntegers();
		
		PrintPrimeNumbers(50);
		
		//先進先出後進後出
		while(!stack.empty()) {
			System.out.print(stack.pop()+" ");
		}
	}

	//書上183頁代碼
	private static void PrintPrimeNumbers(int i) {
		final int NUMBER_OF_PRIMES_PER_LINE=10;
		int count=0;
		int number=2;
		
		while(count<i) {
			if(isPrime(number)) {
				count++;
					
				if(count % NUMBER_OF_PRIMES_PER_LINE==0) {
					
					//顯示小於120的所有素數
					if(number<120) { 
						System.out.printf("%-5s\n", number);
					}
						
				}
				else
					//顯示小於120的所有素數
					if(number<120) { //顯示小於120的所有素數
						System.out.printf("%-5s", number);
					}
				
			}
			
			number++;
		}
		
	}

	private static boolean isPrime(int number) {
		for(int divisor=2; divisor<=number/2; divisor++) {
			if(number%divisor==0) {
				return false;
			}
		}
		
		return true;
	}
	
}

 

**10.7(遊戲:ATM機)

演示結果:

 

 

 

package p10;

import java.util.Scanner;

import p9.Account;//賬戶類

public class Test7 {

	public static void main(String[] args) {
		
		//首先創建一個對象數組:創建一個有10個賬戶的數組
		Account[] accountArray=new Account[100];
		
		//初始化對象數組
		for(int i=0; i<accountArray.length; i++) {
			accountArray[i]=new Account(i,100);
		}
		
		//我讓這個大循環進行100次
		int n=0;
		while(n<100) {
			n++;
			System.out.println("循環第"+n+"次");
			
		int id=-1;
		Scanner input=new Scanner(System.in);
		System.out.print("Enter an id:");
		
		//判斷 id 是否正確
		while(id==-1) {
			String s=input.nextLine();
			
			if( Character.isDigit(s.charAt(0))) {
				id=s.charAt(0);
			}
			else {
				System.out.print("Please input an id that is valid:");
			}
		}
		
		
		//主菜單循環
		int choose=-2;
		while(choose!=4) {
		System.out.println();
		
		//輸出主菜單
		System.out.println(" Main menu \n"
						+" 1: check balance \n"
						+" 2: withdraw \n"
						+" 3: deposit \n"
						+" 4: exit");
		System.out.print("Enter a choice: ");
		choose=input.nextInt();
		
		if(choose==1) {
		System.out.println("The balance is "+accountArray[id].getBalance());  
		}
		else if(choose==2) {
			System.out.print("Enter an amount to withdraw:");
			int withdraw=input.nextInt();
			accountArray[id].withDraw(withdraw);
		}
		else if(choose==3) {
			System.out.print("Enter an amount to deposit:");
			int deposit=input.nextInt();
			accountArray[id].deposit(deposit);
		}
		else if(choose==4) {
			System.out.println();
			break;//退出循環
		}
	} 
	
		
			}
		}
	}
package p9;

import java.util.Date;

public class Account {
		
	public int id=0;//用戶名
	public double balance=0;//餘額
	public double annualInteresRate=0;//當前利率
	public java.util.Date dateCreated;//存儲開戶時間
	
	
	//無參構造方法
	public Account(){
		
	}
	
	//有參構造方法
	public Account(int id, double balance){
		this.id=id;
		this.balance=balance;
	}
	
	public int getId(){
		return id;
		
	}
	
	public double getBalance() {
		return balance;
	}
	
	
	public double getAnualInterestRate() {
		return annualInteresRate;
		
	}
	
	
	public void setId(int id){
		this.id=id;
	}
	
	
	public void setBalance(double balance) {
		this.balance=balance;
	}
	
	
	public void setAnnualInterestRate(double annualInterestRate) {
		this.annualInteresRate=annualInterestRate;
	}
	
	
	 public void setDateCreated(Date dateCreated){
		System.out.println(dateCreated.toString());
	}
	
	//月利息
	public double getMonthlyInterestRate() {
		return annualInteresRate/12;
		
	}
	
	//取錢
	public double withDraw(double withdraw) {
		return this.balance=this.balance-withdraw;
	}
	
	//存錢
	public double deposit(double deposit) {
		return this.balance=this.balance+deposit;
	}
	
	
}

 

 ***10.8(財務:稅款類Tax)

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章