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

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

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

上一篇

2.1

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a degree in Celsius:");
        double c = input.nextDouble();
        double h = (9.0/5)*c+32;
        System.out.println(c+" Celsius is "+h+" Fahrenheit.");
    }
}

2.2

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        double pi = 3.141592654;
        System.out.print("Enter the radius and length of a cylinder:");
        Scanner input = new Scanner(System.in);
        double r = input.nextDouble();
        double l = input.nextDouble();
        double s = r*r*pi;
        double v = s*l;
        System.out.println("The area is "+s);
        System.out.println("The volume is "+v);
    }
}

2.3

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a value for feet:");
        double feet = input.nextDouble();
        double meters = feet*0.305;
        System.out.println(feet+" feet is "+meters+" meters.");
    }
}

2.4

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a value in pounds:");
        double pounds = input.nextDouble();
        double kilograms = pounds*0.454;
        System.out.println(pounds+" pounds is "+kilograms+" kilograms.");
    }
}

2.5

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the subtotal and a gratuity rate:");
        double t = input.nextDouble();
        double g = input.nextDouble();
        double extra = t*g/100.0;
        double s = extra+t;
        System.out.println("The gratuity is $"+extra+" and total is $"+s+".");
    }
}

2.6

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a number between 0 and 1000:");
        int num = input.nextInt();
        int d1 = num%10;
        num/=10;
        int d2 = num%10;
        num/=10;
        int s = num+d1+d2;
        System.out.println("The sum of the digits is "+s+".");
    }
}

2.7

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of minutes:");
        int min = input.nextInt();
        int days = min/60/24;
        int years = days/365;
        days %= 365;
        System.out.println(min+" minutes is approximately "+years+" years and "+days+" days.");
    }
}

2.8

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the time zone offset to GMT:");
        long offset = input.nextLong();
        long totalMilliseconds = System.currentTimeMillis();
        totalMilliseconds+=offset*60*60*1000;
        long totalSeconds = totalMilliseconds/1000;
        long currentSecond = totalSeconds%60;
        long totalMinutes = totalSeconds/60;
        long currentMinute = totalMinutes%60;
        long totalHours = totalMinutes/60;
        long currentHour = totalHours%24;
        System.out.println("Current time is "+currentHour+":"+currentMinute+":"+currentSecond+" GMT.");
    }
}

2.9

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        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.10

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the amount of water in kilograms:");
        double m = input.nextDouble();
        System.out.print("Enter the initial temperature:");
        double it = input.nextDouble();
        System.out.print("Enter the final temperature:");
        double ft = input.nextDouble();
        double q = m*(ft-it)*4184;
        System.out.println("The energy needed is "+q+".");
    }
}

2.11

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of years:");
        int year = input.nextInt();
        double base = 312032486;
        int secnum = 365*24*3600;
        for(int i=1;i<=year;i++)
            base=base+secnum/7.0-secnum/13.0+secnum/45.0;
        System.out.println("The population in "+year+" years is "+base+".");
    }
}

2.12

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter speed and acceleration:");
        double v = input.nextDouble();
        double a = input.nextDouble();
        double l = v*v/(2*a);
        System.out.println("The minimum runway length for this airplane is "+l+".");
    }
}
  • 6
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值