2.1
import java.util.Scanner;
public class dierzhang {
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 dierzhang {
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 dierzhang {
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 dierzhang {
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 dierzhang {
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 dierzhang {
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 dierzhang {
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 dierzhang {
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 dierzhang {
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 dierzhang {
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 dierzhang {
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 dierzhang {
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+".");
}
}
2.13
import java.util.Scanner;
public class dierzhang {
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 dierzhang {
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 dierzhang {
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 dierzhang {
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 dierzhang {
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 dierzhang {
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 dierzhang {
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 dierzhang {
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 dierzhang {
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 dierzhang {
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 dierzhang {
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);
}
}