《Java語(yǔ)言程序設(shè)計(jì)(基礎(chǔ)篇)》(第10版梁勇著)第二章練習(xí)題答案_第1頁(yè)
《Java語(yǔ)言程序設(shè)計(jì)(基礎(chǔ)篇)》(第10版梁勇著)第二章練習(xí)題答案_第2頁(yè)
《Java語(yǔ)言程序設(shè)計(jì)(基礎(chǔ)篇)》(第10版梁勇著)第二章練習(xí)題答案_第3頁(yè)
《Java語(yǔ)言程序設(shè)計(jì)(基礎(chǔ)篇)》(第10版梁勇著)第二章練習(xí)題答案_第4頁(yè)
《Java語(yǔ)言程序設(shè)計(jì)(基礎(chǔ)篇)》(第10版梁勇著)第二章練習(xí)題答案_第5頁(yè)
已閱讀5頁(yè),還剩10頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Java 語(yǔ)言程序設(shè)計(jì)(基礎(chǔ)篇) (第 10 版 梁勇 著)第二章 練習(xí)題答案2.1public class Exercise02_01 / Main methodpublic static void main(String args) java.util.Scanner input = new java.util.Scanner(System.in);/ Enter a temperature in CelsiusSystem.out.print("Enter a temperature in Celsius: ");double celsius = input.next

2、Double();/ Convert it to Fahrenheitdouble fahrenheit = (9.0 / 5) * celsius + 32;/ Display the resultSystem.out.println(celsius +" Celsius is "+fahrenheit +" Fahrenheit" );2.1 附加public class Exercise02_01Extra / Main methodpublic staticvoid main(String args) Scanner input = new Sc

3、anner(System.in);System.out.print( "Enter the coordinates for two points: " ); double x1 = input.nextDouble(); double y1 = input.nextDouble(); double x2 = input.nextDouble(); double y2 = input.nextDouble();System.out.println("The slope for the line that connects two points ("+x1

4、+ ", " + y1 + ") and (" + x2 + ", " + y2 +") is " +(y2 - y1) / (x2 - x1);2.2public class Exercise02_02 public staticvoid main(String args) Scanner input = new Scanner(System.in);/ Enter radius of the cylinderSystem.out.print("Enter the radius and length o

5、f a cylinder: ");double radius = input.nextDouble();double length = input.nextDouble();double area = radius * radius * 3.14159; double volume = area * length;System.out.println("The area is "+ area);System.out.println("The volume of the cylinder is "+ volume);2.2 附加public cl

6、ass Exercise02_02Extra public static void main(String args) System.out.print("Enter the ball travel time in seconds: ");Scanner input =new Scanner(System.in);double t = input.nextDouble();final double g = 9.8;double d = g * t * t / 2;System.out.print("The height of the building is &qu

7、ot;+ d + " meters " );2.3public class Exercise02_03 public static void main(String args) / Enter footjava.util.Scanner input = new java.util.Scanner(System.in);System.out.print( "Enter a value for feet: " ); double feet = input.nextDouble();double meter = feet * 0.305;System.out.

8、println(feet +" feet is "+ meter + " meters" );2.3 附加public class Exercise02_03Extra public static void main(String args) Scanner input = new Scanner(System.in);System.out.print( "Enter the friction force in Newtons: "double f = input.nextDouble();System.out.print( &quo

9、t;Enter the object 抯 mass in kg: " ); double m = input.nextDouble();SyStem.out.pr int("En ter the ObjeCt擔(dān) accelerati On in ms2:"double a = input.nextDouble();final dOuble g = 9.8;dOuble u = (f - m * a) (m * g);System.Out.print("The cOefficient fOr frictiOn is "+ u);2.4public

10、 class Exercise02_04 public static vOid main(String args) PrOmpt the inputjava.util.Scanner input =new java.util.Scanner(System.in);System.Out.print("Enter a number in pOunds: ");dOuble pOunds = input.nextDOuble();dOuble kilOgrams = pOunds * 0.454;System.Out.println(pOunds +" pOunds i

11、s "+ kilOgrams +2.5public class Exercise02_05 public static vOid main(String args) Read subtOtaljava.util.Scanner input =new java.util.Scanner(System.in);System.Out.print("Enter subtOtal and gratuity rate: ");dOuble subtOtal = input.nextDOuble();dOuble rate = input.nextDOuble();dOuble

12、 gratuity = subtOtal * rate 100;););" kilOgrams" );dOuble tOtal = subtOtal + gratuity;System.Out.println("The gratuity is "+ gratuity +" total is "+ total);2.6/ Exercise02_06.java: Summarize all digits in an integer < 1000 public class Exercise02_06 / Main methodpubl

13、ic static void main(String args) java.util.Scanner input = new java.util.Scanner(System.in); / Read a numberSystem.out.print("Enter an integer between 0 and 1000: ");int number = input.nextInt();/ Find all digits in numberint lastDigit = number % 10;int remainingNumber = number / 10;int se

14、condLastDigit = remainingNumber % 10;remainingNumber = remainingNumber / 10;int thirdLastDigit = remainingNumber % 10;/ Obtain the sum of all digitsint sum = lastDigit + secondLastDigit + thirdLastDigit;/ Display resultsSystem.out.println( "The sum of all digits in "+ number+" is &quo

15、t;+ sum);2.7public class Exercise02_07 public static void main(String args) / Prompt the user to enter the number of minutesScanner input =new Scanner(System.in);System.out.print("Enter the number of minutes: ");long minutes = input.nextLong();long numberOfDays = minutes / (24 * 60);long n

16、umberOfYears = numberOfDays / 365;/ Display results);+" days" );System.out.println(minutes + " minutes is approximately " numberOfYears +" years and "+ (numberOfDays % 365) +2.8public class Exercise02_08 public static void main(String args) / Prompt the user to enter th

17、e time zone offset to GMTScanner input = new Scanner(System.in);System.out.print("Enter the time zone offset to GMT: "long timeZoneOffset = input.nextInt();/ Obtain the total milliseconds since the midnight, Jan 1, 1970 long totalMilliseconds = System.currentTimeMillis();/ Obtain the total

18、 seconds since the midnight, Jan 1, 1970 long totalSeconds = totalMilliseconds / 1000;/ Compute the current second in the minute in the hour long currentSecond = totalSeconds % 60;/ Obtain the total minuteslong totalMinutes = totalSeconds / 60;/ Compute the current minute in the hour long currentMin

19、ute = totalMinutes % 60;/ Obtain the total hourslong totalHours = totalMinutes / 60;/ Compute the current hourlong currentHour = (totalHours + timeZoneOffset) % 24;/ Display resultsSystem.out.println("Current time is "+ currentHour + currentMinute + ":" + currentSecond);2.9public

20、 class Exercise02_09 public static void main(String args) System.out.print("Enter v0, v1, and t: ");double v0 = input.nextDouble();double v1 = input.nextDouble();double t = input.nextDouble();double a = (v1 - v0) / t;System.out.println( "The average acceleration is " + a); 2.10pu

21、blic class Exercise02_10 public static void main(String args) java.util.Scanner input = new java.util.Scanner(System.in);System.out.print("Enter the amount of water in kilograms: ");double mass = input.nextDouble();System.out.print("Enter the initial temperature: ");double initia

22、lTemperature = input.nextDouble();System.out.print("Enter the final temperature: " );double finalTemperature = input.nextDouble();double energy =mass * (finalTemperature - initialTemperature) * 4184;System.out.print("The energy needed is "+ energy);2.11public class Exercise02_11

23、public static void main(String args) / Prompt the user to enter the time zone offset to GMTScanner input =new Scanner(System.in);System.out.print("Enter the number of years: ");int numberOfYears = input.nextInt();double population = 312032486 + numberOfYears * 365 * 24 * 60 * 60 / 7.0numbe

24、rOfYears * 365 * 24 * 60 * 60 / 13.0 + numberOfYears * 365 * 24 * 60 * 60 / 45.0;/ Display resultsSystem.out.println( int )population);"The population in "+ numberOfYears +years is " +2.12public class Exercise02_12 public staticvoid main(String args) Scanner input = new Scanner(System

25、.in);System.out.print("Enter speed v: ");double v = input.nextDouble();System.out.print( "Enter acceleration a: " );double a = input.nextDouble();double length = v * v / (2 * a);System.out.println( "The minimum runway length for this airplane is " length + " meters

26、" );2.13public class Exercise02_13 public staticvoid main(String args) Scanner input = new Scanner(System.in);System.out.print( "Enter monthly saving amount: " ); double monthlyDeposit = input.nextDouble();double currentValue = monthlyDeposit;/ First month valuecurrentValue = currentV

27、alue * (1 + 0.00417);System.out.println("After the first month, the account value is "currentValue);/ Second month valuecurrentValue = (currentValue + monthlyDeposit) * (1 + 0.05 / 12);System.out.println( "After the second month, the account value is " + currentValue);/ Third mon

28、th valuecurrentValue = (currentValue + monthlyDeposit) * (1 + 0.05 / 12);System.out.println("After the third month, the account value is "+currentValue);/ Fourth month valuecurrentValue = (currentValue + monthlyDeposit) * (1 + 0.05 / 12);/ Fifth month valuecurrentValue = (currentValue + mo

29、nthlyDeposit) * (1 + 0.05 / 12);/ Sixth month valuecurrentValue = (currentValue + monthlyDeposit) * (1 + 0.05 / 12);System.out.println("After the sixth month, the account value is "+currentValue);2.14public class Exercise02_14 public static void main(String args) Scanner input = new Scanne

30、r(System.in);/ Prompt the user to enter weight in poundsSystem.out.print("Enter weight in pounds: ");double weight = input.nextDouble();/ Prompt the user to enter height in inchesSystem.out.print("Enter height in inches: ");double height = input.nextDouble();double bmi = weight *

31、 0.45359237 / (height * 0.0254 * height * 0.0254);"BMI is "+ bmi);System.out.print(2.15public class Exercise02_15 public staticvoid main(String args) Scanner input = new Scanner(System.in);/ Enter the first point with two double valuesSystem.out.print( "Enter x1 and y1: " );doubl

32、e x1 = input.nextDouble();double y1 = input.nextDouble();/ Enter the second point with two double valuesSystem.out.print( "Enter x2 and y2: " );double x2 = input.nextDouble();double y2 = input.nextDouble();/ Compute the distancedouble distance = Math.pow(x1 - x2) * (x1 - x2) +(y1 - y2) * (

33、y1 - y2), 0.5);System.out.println("The distance of the two points is "distance);2.16public class Exercise02_16 public staticvoid main(String args) Scanner input = new Scanner(System.in);/ Enter the side of the hexagonSystem.out.print( "Enter the side: " );double side = input.next

34、Double();/ Compute the areadouble area = 3 * 1.732 * side * side / 2;+ area);System.out.println("The area of the hexagon is "2.17public class Exercise02_17 / Main methodpublic static void main(String args) java.util.Scanner input = new java.util.Scanner(System.in);/ Enter the temperature i

35、n FahrenheitSystem.out.print("Enter the temperature in Fahrenheit between -58 and 41:" );double fahrenheit = input.nextDouble();/ Enter the wind speed miles per hourSystem.out.print( "Enter the wind speed miles per hour " + "(must be greater than or equal to 2) : ");dou

36、ble speed = input.nextDouble();/ Compute wind chill indexdouble windChillIndex = 35.74 + 0.6215 * fahrenheit - 35.75 * Math.pow(speed, 0.16) + 0.4275 * fahrenheit * Math.pow(speed, 0.16);/ Display the resultSystem.out.println("The wind chill index is "+ windChillIndex);2.18public class/ Ma

37、in method public static void System.out.println( System.out.println( System.out.println( System.out.println( System.out.println( System.out.println(Exercise02_18 main(String args) "a b pow(a, b)""12 ""23 ""34 ""45 ""5 6 ");+ ( int )Math.pow

38、(1, 2) + ( int )Math.pow(2, 3) + ( int )Math.pow(3, 4) + ( int )Math.pow(4, 5) + ( int )Math.pow(5, 6)2.19public class Exercise02_19 public static void main(String args) / Enter three points for a triangleSystem.out.print("Enter three points for a triangle: ");Scanner input =new Scanner(Sy

39、stem.in);double x1 = input.nextDouble();double y1 = input.nextDouble();double x2 = input.nextDouble();double y2 = input.nextDouble();double x3 = input.nextDouble();double y3 = input.nextDouble();/ Compute the length of the three sidesdoubleside1= Math.pow(x1 -x2) * (x1- x2)+ (y1- y2) * (y1- y2),0.5)

40、;doubleside2= Math.pow(x1 -x3) * (x1- x3)+ (y1- y3) * (y1- y3),0.5);doubleside3= Math.pow(x3 -x2) * (x3- x2)+ (y3- y2) * (y3- y2),0.5);double s = (side1 + side2 + side3) / 2;double area = Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);"The area of the triangle is "+ area);Syste

41、m.out.println(2.20public class Exercise02_20 public staticvoid main(String args) Scanner input = new Scanner(System.in);/ Obtain inputSystem.out.print( "Enter balance and annual interest rate: "doublebalance = input.nextDouble();doubleannualInterestRate = input.nextDouble();doublemonthlyIn

42、terestRate = annualInterestRate / 1200;doubleinterest = balance * monthlyInterestRate;/ Display outputSystem.out.println("The interest is "+ ( int )(100* interest) / 100.0);2.21/ Exercise02_11.java: Create a method for computing future valuepublic class Exercise02_21 public static void mai

43、n(String args) java.util.Scanner input = new java.util.Scanner(System.in);/ Enter the investment amountSystem.out.print("Enter the investment amount, for example 120000.95: " );double investmentAmount = input.nextDouble();/ Enter yearly interest rateSystem.out.print("Enter annual inte

44、rest rate, for example 8.25: ");double annualInterestRate = input.nextDouble();/ Obtain monthly interest ratedouble monthlyInterestRate = annualInterestRate / 1200;/ Enter number of yearsSystem.out.print("Enter number of years as an integer, for example 5: ");int numOfYears = input.ne

45、xtInt();double futureValue =investmentAmount * Math.pow(1 + monthlyInterestRate, numOfYears * 12);System.out.print( "Future value is " +( int )(futureValue * 100) / 100.0);2.22public class Exercise02_22 / Main methodpublic static void main(String args) java.util.Scanner input = new java.util.Scanner(System.in);/ Receive the amount entered from the keyboardSystem.out.print("Enter an amount in integer, for example 1156 nfor 11 dollars and 56 cents:);int amount = inp

溫馨提示

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

最新文檔

評(píng)論

0/150

提交評(píng)論