博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《Java语言程序设计与数据结构》编程练习答案(第二章)(二)
阅读量:4169 次
发布时间:2019-05-26

本文共 6018 字,大约阅读时间需要 20 分钟。

《Java语言程序设计与数据结构》编程练习答案(第二章)(二)

英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition

2.13

import java.util.Scanner;public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); System.out.print("Enter the monthly saving amount:"); double m = input.nextDouble(); double sum = 0.0; for(int i=0;i<6;i++) sum = (m+sum)*(1+0.00417); System.out.println("After the sixth month, the account value is $"+sum+"."); }}

2.14

import java.util.Scanner;public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); System.out.print("Enter weight in pounds:"); double pounds = input.nextDouble(); System.out.print("Enter height in inches:"); double inches = input.nextDouble(); double bmi = pounds*0.4535927/(inches*inches*0.0254*0.0254); System.out.println("BMI is "+bmi+"."); }}

2.15

import java.util.Scanner;public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); System.out.print("Enter x1 and y1:"); double x1 = input.nextDouble(); double y1 = input.nextDouble(); System.out.print("Enter x2 and y2:"); double x2 = input.nextDouble(); double y2 = input.nextDouble(); double d = Math.pow((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1),0.5); System.out.println("The distance between the two points is "+d+"."); }}

2.16

import java.util.Scanner;public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); System.out.print("Enter the length of the side:"); double s = input.nextDouble(); double a = 3*Math.pow(3,0.5)/2*s*s; System.out.println("The area of the hexagon is "+a+"."); }}

2.17

import java.util.Scanner;public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); System.out.print("Enter the temperature in Fahrenheit:"); double ta = input.nextDouble(); System.out.print("Enter the wind speed (>=2) in miles per hour:"); double v = input.nextDouble(); double twc = 35.74+0.6215*ta-35.75*Math.pow(v,0.16)+0.4275*ta*Math.pow(v,0.16); System.out.println("The wind chill is "+twc); }}

2.18

public class book {
public static void main(String[] args) {
System.out.println("a b pow(a,b)"); for(int i = 1;i <= 5;i++) System.out.println(i+" "+(i+1)+" "+(int)Math.pow(i,i+1)); }}

2.19

import java.util.Scanner;public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); System.out.println("Enter the coordinates of three points separated by spaces"); System.out.print("like x1 y1 x2 y2 x3 y3:"); double x1 = input.nextDouble(); double y1 = input.nextDouble(); double x2 = input.nextDouble(); double y2 = input.nextDouble(); double x3 = input.nextDouble(); double y3 = input.nextDouble(); double s1 = Math.pow((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2),0.5); double s2 = Math.pow((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3),0.5); double s3 = Math.pow((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3),0.5); double s = (s1+s2+s3)/2; double a = Math.pow(s*(s-s1)*(s-s2)*(s-s3),0.5); System.out.println("The area of the triangle is "+a); }}

2.20

import java.util.Scanner;public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); System.out.print("Enter balance and interest rate:"); double balance = input.nextDouble(); double rate = input.nextDouble(); double interest = balance*(rate/1200); System.out.println("The interest is "+interest); }}

2.21

import java.util.Scanner;public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); System.out.print("Enter investment amount:"); double amount = input.nextDouble(); System.out.print("Enter annual interest rate in percentage:"); double ar = input.nextDouble(); System.out.print("Enter number of years:"); int y = input.nextInt(); double fv = amount*Math.pow(1+ar/1200,y*12); System.out.println("Future value is $"+fv); }}

2.22

import java.util.Scanner;public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); System.out.print("Enter an amount in int, for example 114514:"); int amount = input.nextInt(); int old = amount; int numberOfOneDollars = amount/100; amount%=100; int numberOfQuarters = amount/25; amount%=25; int numberOfDimes = amount/10; amount%=10; int numberOfNickles = amount/5; amount%=5; int numberOfPennies = amount; System.out.println("Your amount "+old+" consists of"); System.out.println(" "+numberOfOneDollars+" dollars"); System.out.println(" "+numberOfQuarters+" quarters"); System.out.println(" "+numberOfDimes+" dimes"); System.out.println(" "+numberOfNickles+" nickels"); System.out.println(" "+numberOfPennies+" pennies"); }}

2.23

import java.util.Scanner;public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); System.out.print("Enter the driving distance:"); double distance = input.nextDouble(); System.out.print("Enter miles per gallon:"); double youhao = input.nextDouble(); System.out.print("Enter price per gallon:"); double youjia = input.nextDouble(); double price = distance/youhao*youjia; System.out.println("The cost of driving is $"+price); }}

第二章 完

转载地址:http://qkwai.baihongyu.com/

你可能感兴趣的文章
Decorator模式
查看>>
Template模式
查看>>
Observer模式
查看>>
高性能服务器设计
查看>>
图文介绍openLDAP在windows上的安装配置
查看>>
Pentaho BI开源报表系统
查看>>
Pentaho 开发: 在eclipse中构建Pentaho BI Server工程
查看>>
android中SharedPreferences的简单例子
查看>>
android中使用TextView来显示某个网址的内容,使用<ScrollView>来生成下拉列表框
查看>>
andorid里关于wifi的分析
查看>>
Spring MVC和Struts2的比较
查看>>
Hibernate和IBatis对比
查看>>
Spring MVC 教程,快速入门,深入分析
查看>>
Android 的source (需安装 git repo)
查看>>
LOCAL_PRELINK_MODULE和prelink-linux-arm.map
查看>>
Ubuntu Navicat for MySQL安装以及破解方案
查看>>
java多线程中的join方法详解
查看>>
idea添加gradle模块报错The project is already registered
查看>>
在C++中如何实现模板函数的外部调用
查看>>
HTML5学习之——HTML 5 拖放
查看>>