《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+".");
    }
}
发布了75 篇原创文章 · 获赞 61 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/swy_swy_swy/article/details/104435664