Java-how-to-program(第六版)答案第六章--1_第1頁
Java-how-to-program(第六版)答案第六章--1_第2頁
Java-how-to-program(第六版)答案第六章--1_第3頁
Java-how-to-program(第六版)答案第六章--1_第4頁
Java-how-to-program(第六版)答案第六章--1_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、第六章 方法的深入剖析6.7結果如下:a)7.5b)7.0c)0.0d)0.0e)6.4f)6.0g)-14.06.8程序代碼為:import java.util.Scanner;public class Exercise1_2 public static void main( String args) Exercise1_2 e = new Exercise1_2(); Scanner input = new Scanner( System.in ); int time; double account = 0.0; System.out.println("Enter the par

2、k time or end with -1:"); time = input.nextInt(); while ( time != -1 ) System.out.printf("Charge with $%.3fn", e.calculateCharge( time ); account += e.calculateCharge( time ); System.out.println("Enter the park time or end with -1:"); time = input.nextInt(); System.out.print

3、f("The total park money is $%.3f.", account); public double calculateCharge( int time )double charge;if ( time <= 3 )charge = 2.00;else if ( time <= 24 )charge = 2.00+(time-3)*0.50;elsecharge = 10.00;return charge;6.9程序為:import java.util.Scanner;public class Exercise1 /* * param args

4、 */public static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner(System.in);double number;int result;System.out.println("Enter a number :");number = input.nextDouble();result = (int)Math.floor(number+0.5);System.out.printf("Before %.3fnAfter %dn&quo

5、t;, number, result);6.10程序代碼為:import java.util.Scanner;public class Exercise1 /* * param args */public static void main(String args) / TODO Auto-generated method stubExercise1 e = new Exercise1();Scanner input = new Scanner(System.in);double number;System.out.println("Enter a number :");nu

6、mber = input.nextDouble();System.out.printf("The original number is %.3fn", number); System.out.printf("Rounding to the integer is %dn",e.roundToInteger(number);System.out.printf("Rounding to the tenths is %.1fn", e.roundToTenths(number);System.out.printf("Rounding

7、 to the hundredths is %.2fn", e.roundToHundredths(number);System.out.printf("Rounding to the thousands is %.3fn",e.roundToThousandsths(number);public int roundToInteger( double number )return (int)Math.floor(number+0.5);public double roundToTenths( double number )return Math.floor(num

8、ber*10+0.5)/10;public double roundToHundredths( double number )return Math.floor(number*100+0.5)/100;public double roundToThousandsths( double number )return Math.floor(number*1000+0.5)/1000;6.11 a)隨機意味著公平的原則,即概率相等b)屬于計算機隨機取值,也就保證了游戲的公平性c)為了符合實際情況,故需要縮放或變換random的值d)由于計算機計算的隨機性和公平性對于解決一些實際問題更方便6.12 a

9、) n = 1 + randmNumbers.nextInt(2);b)n = 1 + randmNumbers.nextInt(100);c)n = 1 + randmNumbers.nextInt(9);d)n = 1000 + randmNumbers.nextInt(112);e)n = -1 + randmNumbers.nextInt(2);f)n = -3 + randmNumbers.nextInt(14);6.13語句如下:a)n = 2*(randmNumbers.nextInt(5)+1);b)n = 2*(randmNumbers.nextInt(5)+1)+1;c)n

10、 = 4*(randmNumbers.nextInt(5)+1)+2;6.14程序為:import java.util.Scanner;public class Exercise5 /* * param args */public static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner( System.in );int base;int exponent;int result;System.out.println("Enter base and exponen

11、t number :");base = input.nextInt();exponent = input.nextInt();result = integerPower( base, exponent );System.out.println("The integer power is :" +result);public static int integerPower( int base , int exponent )int i;int result = 1;for ( i=1; i<=exponent; i+ )result *= base;retur

12、n result;6.15程序代碼為:import java.util.Scanner;public class Exercise5 /* * param args */public static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner( System.in );double side1;double side2;double hyp;System.out.println("Enter side1 and side2:");side1 = inpu

13、t.nextDouble();side2 = input.nextDouble();hyp = hypotenuse( side1, side2 );System.out.printf("The hypotenuse is :%.2f", hyp);public static double hypotenuse( double side1, double side2 )double hyp;hyp = Math.pow(side1*side1+side2*side2), 0.5);return hyp;6.16程序代碼為:import java.util.Scanner;p

14、ublic class Exercise5 /* * param args */public static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner( System.in );int num1;int num2;int result;System.out.println("Enter number1 and number2:");num1 = input.nextInt();num2 = input.nextInt();result = mutipl

15、e( num1, num2 );if ( result = 1 )System.out.printf("%d is %d mutiple.", num1, num2 );elseSystem.out.printf("%d is not %d mutiple.", num1, num2 );public static int mutiple( int number1, int number2 )if ( number1 % number2 = 0 )return 1;else return 0;6.17程序代碼為:public class Exercise

16、5 /* * param args */public static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner( System.in );int num;int result;System.out.println("Enter number:");num = input.nextInt();while ( num != -1 ) result = isEven( num );if ( result = 1 )System.out.printf(&quo

17、t;%d is an even number.", num );elseSystem.out.printf("%d is an odd number.", num );System.out.println("Enter number:");num = input.nextInt();public static int isEven( int number )if ( number % 2 = 0 )return 1;else return 0;6.18程序代碼為:import java.util.Scanner;public class Exe

18、rcise5 /* * param args */public static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner( System.in );int num;System.out.println("Enter number:");num = input.nextInt();System.out.println("The result is :");squareOfAsterisks( num );public static v

19、oid squareOfAsterisks( int number )int i;int j;for ( i=1; i<=number; i+ )for ( j=1; j<=number; j+ )System.out.print('*');System.out.println();6.19程序代碼為:import java.util.Scanner;public class Exercise5 /* * param args */public static void main(String args) / TODO Auto-generated method st

20、ubScanner input = new Scanner( System.in );int num;System.out.println("Enter number:");num = input.nextInt();System.out.println("The result is :");fillCharacter( num );public static void fillCharacter( int number )int i;int j;for ( i=1; i<=number; i+ )for ( j=1; j<=number;

21、j+ )System.out.print('#');System.out.println();6.20程序代碼為:import java.util.Scanner;public class Exercise5 /* * param args */public static void main(String args) / TODO Auto-generated method stubScanner input = new Scanner( System.in );double radius;System.out.println("Enter radius:"

22、);radius = input.nextDouble();System.out.printf("The area is :%.2f", circleArea( radius );public static double circleArea( double radius )return Math.PI*radius*radius;6.21程序代碼為:public class Exercise5 /* * param args */public static void main(String args) / TODO Auto-generated method stubSc

23、anner input = new Scanner( System.in );int number;System.out.println("Enter a number:");number = input.nextInt();System.out.println("The result is :");displayDigitals( number );public static void displayDigitals( int number )int a;int b;int c;int d;int e;a = number/10000;b = (num

24、ber/1000)%10;c = (number/100)%10;d = (number/10)%10;e = number%10;if ( a > 0 )System.out.printf("%-2d", a);System.out.printf("%-2d", b);System.out.printf("%-2d", c);System.out.printf("%-2d", d);System.out.printf("%-2d", e);else if ( b > 0 )Syst

25、em.out.printf("%-2d", b);System.out.printf("%-2d", c);System.out.printf("%-2d", d);System.out.printf("%-2d", e);else if ( c > 0 )System.out.printf("%-2d", c);System.out.printf("%-2d", d);System.out.printf("%-2d", e);else if ( d

26、 > 0 )System.out.printf("%-2d", d);System.out.printf("%-2d", e);elseSystem.out.printf("%-2d", e);6.22程序代碼為:import java.util.Scanner;public class Exercise1_3 /* * param args */public static void main(String args) / TODO Auto-generated method stubScanner input = new Sc

27、anner( System.in );double temperature;int temp;System.out.println("Enter 1 chage fahrenheit to Celsius");System.out.println("Enter 2 chage Celsius to fahrenheit");temp = input.nextInt();System.out.println("Enter the temperature:");temperature = input.nextDouble();if ( t

28、emp = 1 )celsius( temperature );elsefahrenheit( temperature );public static void celsius( double temperature )double degree;degree = 5.0/9.0*(temperature-32);System.out.printf("After changing to celsius,temperature is %.2f", degree);public static void fahrenheit( double temperature )double

29、 degree;degree = 9.0/5.0*temperature+32;System.out.printf("After changing to fahrenheit,temperature is %.2f", degree);6.23程序代碼為:import java.util.Scanner;public class Exercise1_2 public static void main( String args) Exercise1_2 e = new Exercise1_2();Scanner input = new Scanner( System.in ); double num1;double num2;double num3;double min;System.out.println("Enter three numbers:");num1 = input.nextDouble();num2 = input.nextDouble();num3 = input.nextDouble();min = e.minimum3( num1, num2, num3 );System.out.printf("The minimum number is :%.3f"

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論