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

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

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

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

你可能感兴趣的文章
分析若干没面试机会和没体现实力的简历
查看>>
用python的matplotlib和numpy库绘制股票K线均线
查看>>
以互联网公司的经验告诉大家,架构师究竟比高级开发厉害在哪?
查看>>
GanttProject 使用的控件第三方包:jdnc-modifBen.jar
查看>>
ps、grep和kill联合使用杀掉进程
查看>>
openfire中的mina框架使用
查看>>
去掉Windows Messager的自动登录
查看>>
dspace可以检索中文了
查看>>
利用Eclipse编辑中文资源,配置文件
查看>>
将中文转为unicode 及转回中文函数
查看>>
《程序员》专访金蝶:是谁不相信国产软件?
查看>>
debian的gnome下的xmms乱码解决方案
查看>>
实习日记
查看>>
ln中符号链接与硬链接的区别
查看>>
solr cloud zk管理
查看>>
使用HBase Indexer建立二级索引(整合最新版本的HBase1.2.6及Solr 7.2.1)
查看>>
CentOS7 安装 VirtualBox5.2.8启动系统是出错
查看>>
SLF4j多个log4j2的实现类导致日志生成不了
查看>>
Quartz定时调度CronExpression配置格式说明与实例
查看>>
UIViewController的生命周期
查看>>