Java第二章练习习题

时间:2021-07-05 16:08:11

2.1

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

2.2

package practice;
import java.util.Scanner;
public class practice2_2 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter the radius and length of a cylinder:");
double radius=input.nextDouble();
double length=input.nextDouble();
double area,volume;
area=radius*radius*Math.PI;
volume=area*length;
System.out.println("The area is "+area);
System.out.println("The volume is "+volume);
}
}

2.3

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

2.4

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

2.5

package practice;
import java.util.Scanner;
public class practice2_5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("Enter the subtotal and a pratuity rate:");
double subtotal=input.nextDouble();
double gratuity_rate=input.nextDouble();
double gratuity=gratuity_rate*subtotal*0.01;
double total=subtotal+gratuity;
System.out.println("The gratuity is $"+gratuity+" and total is $"+total);
}
}

2.6

2.7

2.8

2.9

2.10

2.11

2.12

2.13

2.14

2.15

2.16

2.17

2.18

2.19

2.20

2.21

2.22

2.23