[工学]java语言程序设计基础篇英文8版课后习题答案.doc

上传人:风**** 文档编号:967153 上传时间:2024-03-19 格式:DOC 页数:64 大小:37.69KB
下载 相关 举报
[工学]java语言程序设计基础篇英文8版课后习题答案.doc_第1页
第1页 / 共64页
[工学]java语言程序设计基础篇英文8版课后习题答案.doc_第2页
第2页 / 共64页
[工学]java语言程序设计基础篇英文8版课后习题答案.doc_第3页
第3页 / 共64页
[工学]java语言程序设计基础篇英文8版课后习题答案.doc_第4页
第4页 / 共64页
[工学]java语言程序设计基础篇英文8版课后习题答案.doc_第5页
第5页 / 共64页
点击查看更多>>
资源描述

1、public class Exercise1_2 public static void main(String args) System.out.println(Welcome to Java); System.out.println(Welcome to Java); System.out.println(Welcome to Java); System.out.println(Welcome to Java); System.out.println(Welcome to Java); public class Exercise1_4 public static void main(Stri

2、ng args) System.out.println(a a2 a3); System.out.println(1 1 1); System.out.println(2 4 8); System.out.println(3 9 27); System.out.println(4 16 64); public class Exercise1_6 public static void main(String args) System.out.println(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9); public class Exercise1_8 public st

3、atic void main(String args) / Display area System.out.println(5.5 * 5.5 * 3.14159); / Display perimeter System.out.println(2 * 5.5 * 3.14159); import javax.swing.JOptionPane;public class Exercise2_1WithDialogBox / Main method public static void main(String args) / Enter a temperatur in Fahrenheit St

4、ring celsiusString = JOptionPane.showInputDialog(null, Enter a temperature in Celsius:, Exercise2_1 Input, JOptionPane.QUESTION_MESSAGE); / Convert string to double double celsius = Double.parseDouble(celsiusString); / Convert it to Celsius double fahrenheit = (9.0 / 5) * celsius + 32; / Display the

5、 result JOptionPane.showMessageDialog(null, The temperature is + fahrenheit + in Fahrenheit); import java.util.Scanner;public class Exercise2_2 public static void main(String args) Scanner input = new Scanner(System.in); / Enter radius of the cylinder System.out.print(Enter radius of the cylinder: )

6、; double radius = input.nextDouble(); / Enter length of the cylinder System.out.print(Enter length of the cylinder: ); double length = input.nextDouble(); double volume = radius * radius * 3.14159 * length; System.out.println(The volume of the cylinder is + volume); public class Exercise2_4 public s

7、tatic void main(String args) / Prompt the input java.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 is + kilograms + kilograms); / Exercise2

8、_6.java: Summarize all digits in an integer 1000public class Exercise2_6 / Main method public static void main(String args) java.util.Scanner input = new java.util.Scanner(System.in); / Read a number System.out.print(Enter an integer between 0 and 1000: ); int number = input.nextInt(); / Find all di

9、gits in number int lastDigit = number % 10; int remainingNumber = number / 10; int secondLastDigit = remainingNumber % 10; remainingNumber = remainingNumber / 10; int thirdLastDigit = remainingNumber % 10; / Obtain the sum of all digits int sum = lastDigit + secondLastDigit + thirdLastDigit; / Displ

10、ay results System.out.println(The sum of all digits in + number + is + sum); public class Exercise2_8 public static void main(String args) java.util.Scanner input = new java.util.Scanner(System.in); / Enter an ASCII code System.out.print(Enter an ASCII code: ); int code = input.nextInt(); / Display

11、result System.out.println(The character for ASCII code + code + is + (char)code); import java.util.Scanner;public class Exercise2_10 /* Main method */ public static void main(String args) Scanner input = new Scanner(System.in); / Receive the amount entered from the keyboard System.out.print(Enter an

12、 amount in double, for example 11.56 ); double amount = input.nextDouble(); int remainingAmount = (int)(amount * 100); / Find the number of one dollars int numberOfOneDollars = remainingAmount / 100; remainingAmount = remainingAmount % 100; / Find the number of quarters in the remaining amount int n

13、umberOfQuarters = remainingAmount / 25; remainingAmount = remainingAmount % 25; / Find the number of dimes in the remaining amount int numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; / Find the number of nickels in the remaining amount int numberOfNickels = remainingAmo

14、unt / 5; remainingAmount = remainingAmount % 5; / Find the number of pennies in the remaining amount int numberOfPennies = remainingAmount; / Display results String output = Your amount + amount + consists of n + numberOfOneDollars + dollarsn + numberOfQuarters + quartersn + numberOfDimes + dimesn +

15、 numberOfNickels + nickelsn + numberOfPennies + pennies; System.out.println(output); import javax.swing.JOptionPane;public class Exercise2_12a public static void main(String args) / Obtain input String balanceString = JOptionPane.showInputDialog(null, Enter balance:); double balance = Double.parseDo

16、uble(balanceString); String interestRateString = JOptionPane.showInputDialog(null, Enter annual interest rate:); double annualInterestRate = Double.parseDouble(interestRateString); double monthlyInterestRate = annualInterestRate / 1200; double interest = balance * monthlyInterestRate; / Display outp

17、ut JOptionPane.showMessageDialog(null, The interest is + (int)(100* interest) / 100.0); import java.util.Scanner;public class Exercise2_12b public static void main(String args) Scanner input = new Scanner(System.in); / Obtain input System.out.print(Enter balance: ); double balance = input.nextDouble

18、(); System.out.print(Enter annual interest rate: ); double annualInterestRate = input.nextDouble(); double monthlyInterestRate = annualInterestRate / 1200; double interest = balance * monthlyInterestRate; / Display output System.out.println(The interest is + (int)(100* interest) / 100.0); import jav

19、a.util.Scanner;public class Exercise2_14 public static void main(String args) Scanner input = new Scanner(System.in); / Prompt the user to enter weight in pounds System.out.print(Enter weight in pounds: ); double weight = input.nextDouble(); / Prompt the user to enter height in inches System.out.pri

20、nt(Enter height in inches: ); double height = input.nextDouble(); double bmi = weight * 0.45359237 / (height * 0.0254 * height * 0.0254); System.out.print(BMI is + bmi); public class Exercise2_16 public static void main(String args) java.util.Scanner input = new java.util.Scanner(System.in); System.

21、out.print( Enter the amount of water in kilograms: ); double mass = input.nextDouble(); System.out.print(Enter the initial temperature: ); double initialTemperature = input.nextDouble(); System.out.print( Enter the final temperature: ); double finalTemperature = input.nextDouble(); double energy = m

22、ass * (finalTemperature - initialTemperature) * 4184; System.out.print(The energy needed is + energy); public class Exercise2_18 / Main method public static void main(String args) System.out.println(a b pow(a, b); System.out.println(1 2 + (int)Math.pow(1, 2); System.out.println(2 3 + (int)Math.pow(2

23、, 3); System.out.println(3 4 + (int)Math.pow(3, 4); System.out.println(4 5 + (int)Math.pow(4, 5); System.out.println(5 6 + (int)Math.pow(5, 6); import java.util.Scanner;public class Exercise2_20 public static void main(String args) Scanner input = new Scanner(System.in); / Enter the first point with

24、 two double values System.out.print(Enter x1 and y1: ); double x1 = input.nextDouble(); double y1 = input.nextDouble(); / Enter the second point with two double values System.out.print(Enter x2 and y2: ); double x2 = input.nextDouble(); double y2 = input.nextDouble(); / Compute the distance double d

25、istance = Math.pow(x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2), 0.5); System.out.println(The distance of the two points is + distance); import java.util.Scanner;public class Exercise2_22 public static void main(String args) Scanner input = new Scanner(System.in); / Enter the side of the hexagon Sys

26、tem.out.print(Enter the side: ); double side = input.nextDouble(); / Compute the area double area = 3 * 1.732 * side * side / 2; System.out.println(The area of the hexagon is + area); import java.util.Scanner;public class Exercise2_24 public static void main(String args) Scanner input = new Scanner(

27、System.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); public class Exercise3_2 /*Mai

28、n method*/ public static void main(String args) java.util.Scanner input = new java.util.Scanner(System.in); / Prompt the user to enter an integer System.out.print(Enter an integer: ); int number = input.nextInt(); / Display results System.out.println(Is + number + an even number? + (number % 2 = 0);

29、 import javax.swing.*;public class Exercise3_4 public static void main(String args) int number1 = (int)(System.currentTimeMillis() % 100); int number2 = (int)(System.currentTimeMillis() * 7 % 100); String resultString = JOptionPane.showInputDialog (What is + number1 + + + number2 + ?); int result =

30、Integer.parseInt(resultString); JOptionPane.showMessageDialog(null, number1 + + + number2 + = + result + is + (number1 + number2 = result); import javax.swing.*;public class Exercise3_5WithJOptionPane public static void main(String args) int number1 = (int)(System.currentTimeMillis() % 10); int numb

31、er2 = (int)(System.currentTimeMillis() * 7 % 10); int number3 = (int)(System.currentTimeMillis() * 3 % 10); String answerString = JOptionPane.showInputDialog (What is + number1 + + + number2 + + + number3 + ?); int answer = Integer.parseInt(answerString); JOptionPane.showMessageDialog(null, number1

32、+ + + number2 + + + number3 + = + answer + is + (number1 + number2 + number3 = answer); import java.util.Scanner;public class Exercise3_6 public static void main(String args) Scanner input = new Scanner(System.in); / Prompt the user to enter weight in pounds System.out.print(Enter weight in pounds:

33、); double weight = input.nextDouble(); / Prompt the user to enter height System.out.print(Enter feet: ); double feet = input.nextDouble(); System.out.print(Enter inches: ); double inches = input.nextDouble(); double height = feet * 12 + inches; / Compute BMI double bmi = weight * 0.45359237 / (heigh

34、t * 0.0254) * (height * 0.0254); / Display result System.out.println(Your BMI is + bmi); if (bmi 16) System.out.println(You are seriously underweight); else if (bmi 18) System.out.println(You are underweight); else if (bmi 24) System.out.println(You are normal weight); else if (bmi 29) System.out.pr

35、intln(You are over weight); else if (bmi num2) int temp = num1; num1 = num2; num2 = temp; if (num2 num3) int temp = num2; num2 = num3; num3 = temp; if (num1 num2) int temp = num1; num1 = num2; num2 = temp; System.out.println(The sorted numbers are + num1 + + num2 + + num3); import javax.swing.JOptio

36、nPane;public class Exercise3_10 public static void main(String args) / 1. Generate two random single-digit integers int number1 = (int)(Math.random() * 10); int number2 = (int)(Math.random() * 10); / 2. Prompt the student to answer 搘hat is number1 + number2? String answerString = JOptionPane.showInp

37、utDialog (What is + number1 + + + number2 + ?); int answer = Integer.parseInt(answerString); / 4. Grade the annser and display the result String replyString; if (number1 + number2 = answer) replyString = You are correct!; else replyString = Your answer is wrong.n + number1 + + + number2 + should be

38、+ (number1 + number2); JOptionPane.showMessageDialog(null, replyString); import java.util.Scanner;public class Exercise3_12 public static void main(String args) Scanner input = new Scanner(System.in); / Prompt the user to enter an integer System.out.print(Enter an integer: ); int number = input.next

39、Int(); if (number % 5 = 0 & number % 6 = 0) System.out.println(number + is divisible by both 5 and 6); else if (number % 5 = 0 number % 6 = 0) System.out.println(number + is divisible by both 5 and 6, but not both); else System.out.println(number + is not divisible by either 5 or 6); public class Exercise3_14 public static void main(String args) / Obtain the random number 0 or 1 int number = (int)(Math.random() * 2); / Prompt the user to enter a guess java.util.Scanner input = new java.util.Scanner(S

展开阅读全文
相关资源
相关搜索
资源标签

当前位置:首页 > 建筑施工 > 施工组织方案

版权声明:以上文章中所选用的图片及文字来源于网络以及用户投稿,由于未联系到知识产权人或未发现有关知识产权的登记,如有知识产权人并不愿意我们使用,如有侵权请立即联系:2622162128@qq.com ,我们立即下架或删除。

Copyright© 2022-2024 www.wodocx.com ,All Rights Reserved |陕ICP备19002583号-1 

陕公网安备 61072602000132号     违法和不良信息举报:0916-4228922