首页 > 代码库 > java语言程序设计基础课后习题第二章
java语言程序设计基础课后习题第二章
1 //exercise 2.2 2 package secondchapterexercise1; 3 4 public class first01 { 5 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 double miles=100; 9 double KILOMETERS_PER_MILE=1.609;10 double kilometers=miles*KILOMETERS_PER_MILE;11 System.out.println(kilometers);12 final int SIZE=20;13 14 }15 16 }
//exercise 2.1package secondchapterexercise1;import java.util.Scanner;public class second02 { public static void main(String[] args) { // TODO Auto-generated method stub double celsius; double fahreheit; Scanner in=new Scanner(System.in); System.out.print("Enter a degree in Celsius:"); celsius=in.nextDouble(); fahreheit=(9.0/5)*celsius+32; System.out.println(celsius+"Celsius is "+fahreheit+" Fahrenheit"); }}
//exercise 2.2package secondchapterexercise1;import java.util.Scanner;public class third03 { public static void main(String[] args) { // TODO Auto-generated method stub final double PI =3.1415926; System.out.print("Enter the radius and length of a cylinder:"); Scanner in =new Scanner (System.in); double radius=in.nextDouble(); double length=in.nextDouble(); double area=radius*radius*PI; System.out.println("The area is "+(int)(area*10000)/10000.0); double volume =area *length; System.out.println("The volume is "+(int)(volume*10)/10.0); }}
//exercise 2.3package secondchapterexercise1;import java.util.Scanner;public class fourth04 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Enter a value for feet:"); double feet=in.nextDouble(); double meter=feet*0.305; System.out.print(feet+" feet is "+meter+ "meters."); }}
//exercise 2.4package secondchapterexercise1;import java.util.Scanner;public class fifth05 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Enter a number in pounds :"); double pounds=in.nextDouble(); double kilograms=pounds*0.454; System.out.println(pounds+"pounds is "+(int)(kilograms*1000)/1000.0+"kilograms"); }}
//exercise 2-5package secondchapterexercise1;import java.util.Scanner;public class sixth06 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Enter the subtotal and a gratuity rate :"); double subtotal=in.nextDouble(); double gratuity=in.nextDouble()/100.0; double gratuity1=subtotal*gratuity; double total=subtotal+gratuity1; System.out.println("The gratuity is "+gratuity1+"and total is "+total); }}
//exercise 2.6package secondchapterexercise1;import java.util.Scanner;public class seventh07 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Enter a number between 0 and 1000:"); int integer=in.nextInt(); int a=integer%10,sum; if(integer/10==0) { sum=a; System.out.print("The sum of the figits is "+sum); System.exit(0); } integer=integer/10; int b=integer%10; if(integer/10==0) { sum=a+b; System.out.print("The sum of the figits is "+sum); System.exit(0); } integer=integer/10; sum=a+b+integer; System.out.print("The sum of the figits is "+sum); }}
//exercise 2.7package secondchapterexercise1;import java.util.Scanner;public class eighth08 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Enter the number of minues :"); long minues = in.nextLong(); long day=minues/1440; long year=day/365; day=day%365; System.out.print(minues+"minues is approximately "+year+" years and "+day+" days"); }}
//exercise 2.8package secondchapterexercise1;import java.util.Scanner;public class ninth09 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Enter an ASCII code:"); int integer=in.nextInt(); char a=(char)integer; System.out.println("The character for ASCII code "+integer +" is "+a); }}
//exercise 2-10package secondchapterexercise1;import javax.swing.JOptionPane;public class tenth10 { public static void main(String[] args) { // TODO Auto-generated method stub String annualInterestRateString=JOptionPane.showInputDialog("Enter yearly int" + "erest rate,for example 8.25:"); double annualInterestRate=Double.parseDouble(annualInterestRateString); double monthlyInterestRate=annualInterestRate/1200; String numberOfYearsString=JOptionPane.showInputDialog("Enter number of years " + "as an integer,\nfor example 5:"); int numberOfYears=Integer.parseInt(numberOfYearsString); String loanString =JOptionPane.showInputDialog("Enter loan amountt,for examp" + "le 120000.95:"); double loanAmount =Double.parseDouble(loanString); double monthlyPayment=loanAmount*monthlyInterestRate/(1 -1/Math.pow(1+monthlyInterestRate,numberOfYears*12)); double totalPayment=monthlyPayment*numberOfYears*12; monthlyPayment=(int)(monthlyPayment*100)/100.0; totalPayment=(int)(totalPayment*100)/100.0; String output="The monthly payment is "+monthlyPayment+"\nThe total " + "payment is "+totalPayment; JOptionPane.showMessageDialog(null,output); }}
//exercise 2.11package secondchapterexercise2;import java.util.Scanner;public class first { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in =new Scanner(System.in); System.out.print("Enter emplouee‘s name :"); String name=in.next(); System.out.print("Enter number of hours worked in a week:"); int hour =in.nextInt(); System.out.print("Enter hourly pay rate :"); double hourlypayrate=in.nextDouble(); System.out.print("Enter federal tax withholding rate :"); double federaltaxrate=in.nextDouble(); System.out.print("Enter state tax withholding rate:"); double statetaxrate=in.nextDouble(); System.out.println("Employee Name:"+name); System.out.println("Hours Worled :"+hour); System.out.println("Pay Rate : $"+hourlypayrate); System.out.println("Gross Pay: $"+hourlypayrate*10); System.out.println("Deductions:"); double federalWithholding=hour*hourlypayrate*federaltaxrate; System.out.println(" Federal Withholding ("+(federaltaxrate*1000)/10.0+"%):" + " $"+federalWithholding); double StateWithholding=hour*hourlypayrate*statetaxrate; System.out.println(" State Withholding ("+statetaxrate*1000/10.0+"%)" + ": $"+StateWithholding); double totalDeduction=federalWithholding+StateWithholding; System.out.println(" Total Deduction: $"+totalDeduction); double NetPay=hour*hourlypayrate-totalDeduction; System.out.println("Net Pay: $"+NetPay); }}
//exercise 2.11package secondchapterexercise2;import javax.swing.JOptionPane;public class second { public static void main(String[] args) { // TODO Auto-generated method stub //input name String name=JOptionPane.showInputDialog("Enter employee‘s name:"); //input hours String hours=JOptionPane.showInputDialog("Enter number of hours worked i" + "n a week:"); int hour=Integer.parseInt(hours); //input hourly pay rate String hourlypayrate1=JOptionPane.showInputDialog("Enter hourly pay rateL"); double hourlypayrate=Double.parseDouble(hourlypayrate1); //input federal tax withholding rate String federaltaxwithholdingrate1=JOptionPane.showInputDialog("Enter federal tax wit" + "hholding rate:"); double federaltaxwithholdingrate=Double.parseDouble(federaltaxwithholdingrate1); //input state tax withholding rate String statetaxwithholdingrate1=JOptionPane.showInputDialog("Enter state tax withh" + "olding rate:"); double statetaxwithholdingrate=Double.parseDouble(statetaxwithholdingrate1); //assignment double federalWithholding=hour*hourlypayrate*federaltaxwithholdingrate; //assignment double StateWithholding=hour*hourlypayrate*statetaxwithholdingrate; //assignment double totalDeduction=federalWithholding+StateWithholding; //assignment double NetPay=hour*hourlypayrate-totalDeduction; //assignment String output ="Employee Name:"+name+"\nHours Worked:"+hour+"\nPay Rate :"+hourlypayrate+ "\nGross Pay:"+hourlypayrate*10+"\nDeductions:\n Federal Withholding(" +federaltaxwithholdingrate*1000/10.0+"%): $"+federalWithholding+"\n State Wi" + "thholding ("+statetaxwithholdingrate*1000/10.0+"%): $" + ""+StateWithholding+"\n Total Deduction: $"+totalDeduction+ "\nNet Pay: $"+NetPay; //output JOptionPane.showMessageDialog(null,output); }}
//exercise 2.12package secondchapterexercise2;import java.util.Scanner;import javax.swing.JOptionPane;public class third { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Enter balance and interest rate (e.g., 3for3%):"); int income =in.nextInt(); double yearrate=in.nextDouble(); double interest=income*(yearrate/1200); System.out.println("The interest is "+interest); //Dialog input String income1 =JOptionPane.showInputDialog("Enter income:"); String interestrate=JOptionPane.showInputDialog("Enter interest rate"); int incomefirst=Integer.parseInt(income1); double interestrate1=Double.parseDouble(interestrate); double interest1 =incomefirst*(interestrate1/1200); JOptionPane.showMessageDialog(null, "The interest is"+interest1); }}
//exercise 2.13package secondchapterexercise2;import java.util.Scanner;public class fifth { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("input investment amount and annually interest rate and number of" + " years:"); int investmentAmount=in.nextInt(); double annuallyInterestrate=in.nextDouble()/100; int numberOfYears=in.nextInt(); double futureinvestmentValue=http://www.mamicode.com/investmentAmount* (Math.pow((1+annuallyInterestrate),numberOfYears)); System.out.println("Accumulatted value is "+futureinvestmentValue); }}
//exercise 2.14package secondchapterexercise2;import java.util.Scanner;public class sixth { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Enter weight in pounds:"); double weight=in.nextDouble(); System.out.print("Enter height in inches:"); int height =in.nextInt(); double kilograms=weight*0.45359237; double meter=height*0.0254; double bmi=kilograms/Math.pow(meter, 2); System.out.println("BMI is "+bmi); }}
//exercise 2.15package secondchapterexercise2;public class seventh { public static void main(String[] args) { // TODO Auto-generated method stub int mouthlysalary=100; double sum=0; double mouthlyinterestrate=0.00417; sum=mouthlysalary*(1+mouthlyinterestrate); //After the first mouth System.out.println("After the first mouth,money is :"+sum); sum=(sum+mouthlysalary)*(1+mouthlyinterestrate);//After the second mouth System.out.println("After the second mouth,money is :"+sum); sum=(sum+mouthlysalary)*(1+mouthlyinterestrate);//After the third mouth System.out.println("After the third mouth,money is :"+sum); sum=(sum+mouthlysalary)*(1+mouthlyinterestrate);//After the fourth mouth System.out.println("After the fourth mouth,money is :"+sum); sum=(sum+mouthlysalary)*(1+mouthlyinterestrate);//After the fifth mouth System.out.println("After the fifth mouth,money is :"+sum); sum=(sum+mouthlysalary)*(1+mouthlyinterestrate);//After the sixth mouth System.out.println("After the six mouth,money is :"+sum); }}
//exercise 2.16package secondchapterexercise2;import java.util.Scanner;public class eighth { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Enter the amount of water in killograms :"); double kilograms=in.nextDouble(); System.out.print("Enter the initial temperature:"); double initialtemperature=in.nextDouble(); System.out.print("Enter final temperature:"); double finaltemperature=in.nextDouble(); double energy=kilograms*(finaltemperature-initialtemperature)*4184; System.out.println("The energy needed is "+energy); }}
//exercise 2.17package secondchapterexercise2;import java.util.Scanner;public class ninth { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.println("please input -58F to 41F of temperature and better than 2 of " + "wind speed!!"); //input Fahrenheit System.out.print("Enter the temperature in Fahrenheit:"); double fahreheit=in.nextDouble(); //input wind speed System.out.print("Enter the wind speed miles per hours:"); double windspeed=in.nextDouble(); double windchillindex=35.74+0.6215*fahreheit-35.75*Math.pow(windspeed,0.16)+ 0.4275*fahreheit*Math.pow(windspeed,0.16); System.out.print("The wind chill index is "+windchillindex); }}
//exercise 2.18package secondchapterexercise2;public class tenth { public static void main(String[] args) { // TODO Auto-generated method stub int a=1,b=2; int c; System.out.println("a\tb\tpow(a,b)"); for(int i=1;i<=5;i++){ c=(int)Math.pow(a, b); System.out.println(a+"\t"+b+"\t"+c); a++; b++; } }}
//exercise 2.19package secondchapterexercise3;public class first { public static void main(String[] args) { // TODO Auto-generated method stub long time=System.currentTimeMillis(); long a=time%26+65; char b=(char)a; System.out.print(b); }}
//exercise 2.20package secondchapterexercise3;import java.util.Scanner;public class second { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Enter x1 and y1:"); double x1=in.nextDouble(),y1=in.nextDouble(); System.out.print("Enter x2 and y2:"); double x2=in.nextDouble(),y2=in.nextDouble(); //calculation distance of two points double distance; distance=Math.pow((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1),0.5); System.out.println("The distance of the two points is"+distance); }}
//exercise 2.21package secondchapterexercise3;import java.util.Scanner;;public class third { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Enter three points for a triangle:"); //input three points double x1=in.nextDouble(); double y1=in.nextDouble(); double x2=in.nextDouble(); double y2=in.nextDouble(); double x3=in.nextDouble(); double y3=in.nextDouble(); //calculation three side double side1=Math.pow((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1),0.5); double side2=Math.pow((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1),0.5); double side3=Math.pow((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3),0.5); double s=(side1+side2+side3)/2; //calculation area double area=Math.pow(s*(s-side1)*(s-side2)*(s-side3),0.5); System.out.println("The area of the triangle is "+area); }}
//exercise 2.22package secondchapterexercise3;import java.util.Scanner;public class fouth { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Enter the side:"); double side=in.nextDouble(); double area =3*(Math.pow(3, 0.5))/2*side*side; System.out.println("The area of the hexagon is "+area); }}
//exercise 2.23package secondchapterexercise3;import java.util.Scanner;public class fifth { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in =new Scanner(System.in); System.out.print("Enter v0,v1,and t:"); double v0=in.nextDouble(); double v1=in.nextDouble(); double time =in.nextDouble(); double a=(v1-v0)/time; System.out.println("The average acceleration is "+a); }}
//exercise 2.24package secondchapterexercise3;import java.util.Scanner;public class sixth { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Enter v and a:"); double v=in.nextDouble(); double a=in.nextDouble(); double length=Math.pow(v,2)/(2*a); System.out.println("The minimum runway length for this airplane is "+length); }}
java语言程序设计基础课后习题第二章
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。