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

《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);
    }
}

第二章 完

  • 7
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值