Java语言程序设计基础篇原书第十版第四章编程练习题答案

Java核心编程
决定和java死磕到底了,这是要。最近有几个入门级别的童鞋加我好友取经,其实这本书真的还可以的。可以慢慢的学习,不急于求成,慢慢来懂的就会越来越多的。我会继续把自己做的东西拿出来分享,希望对大家有所帮助吧。
4.1几何:五边形的面积

package nameyu;
import java.util.Scanner;
public class Test {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input=new Scanner(System.in);
		System.out.print("Enter the lenght from the center to a vertex:");
		double r=input.nextDouble();
		double s=2*r*Math.sin(Math.PI/5);
		double area=(5*Math.pow(s, 2))/(4*Math.tan(Math.PI/5));
		System.out.printf("The area of the pentagon is%4.2f ",area);
	}
	}	

4.2几何:最大圆距离

package nameyu;
import java.util.Scanner;
public class Test {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input=new Scanner(System.in);
		System.out.print("Enter point 1(latitude and longitude) in degress ");
		String s1=input.nextLine();
		System.out.print("Enter point 2(latitude and longitude) in degress ");
		String s2=input.nextLine();
		double x1=Double.parseDouble(s1.split(",")[0]);
		//因为运行实例是用逗号隔开,所以这里也用逗号吧,这个split课本中可能漏讲了,自己补充吧
		double y1=Double.parseDouble(s1.split(",")[1]);
		double x2=Double.parseDouble(s2.split(",")[0]);
		double y2=Double.parseDouble(s2.split(",")[1]);
		final double   r=6371.01;
		double result=r*Math.acos(Math.sin(Math.toRadians(x1))*Math.sin(Math.toRadians(x2))+Math.cos(Math.toRadians(x1))*Math.cos(Math.toRadians(x2))*Math.cos(Math.toRadians(y1-y2)));
		System.out.println("The distance between the two points is "+result+"km");
		
		
	}
	}	

4.3几何:估算面积
这题。。。
这道题。。。
这道题吧。。。
自己做好不~懒得去百度这么多东西啦!

4.4几何:六边形面积

package nameyu;
import java.util.Scanner;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        System.out.print("Enter the side:");
        double s=input.nextDouble();
        double area=(6*Math.pow(s, 2))/(4*Math.tan(Math.PI/6));
      
        System.out.printf("The area of the pentagon is%4.2f ",area);
    }
    }  

4.6圆上的随机点

这道题我没有看懂他的要求,有理解能力好点的可以在评论里告诉我。十分感谢

4.7顶点坐标
做这道题目前先了解下三角函数吧,巩固下高中知识做起来就容易很多了。我也是看了很久的。
主要看角度值

package nameyu;
import java.util.Scanner;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    	Scanner input=new Scanner(System.in);
    	System.out.print("Enter the radius of the bounding circle:");
    	double radius=input.nextDouble();
    	double p2x=0;
    	double p2y=100;
    	double p3x=Math.cos(Math.PI/10)*(-1)*radius;
    	double p3y=Math.sin(Math.PI/10)*radius;
    	double p1y=Math.sin(Math.PI/10)*radius;
    	double p1x=Math.cos(Math.PI/10)*radius;
    	double p4y=Math.sin((3*Math.PI)/10)*(-1)*radius;
    	double p4x=Math.cos((3*Math.PI)/10)*(-1)*radius;
    	double p5y=p4y;
    	double p5x=Math.cos((3*Math.PI)/10)*radius;
    	System.out.println("P1:"+"("+p1x+","+p1y+")");
    	System.out.println("P2:"+"("+p2x+","+p2y+")");
    	System.out.println("P3:"+"("+p3x+","+p3y+")");
    	System.out.println("P4:"+"("+p4x+","+p4y+")");
    	System.out.println("P5:"+"("+p5x+","+p5y+")");
    }
    }  

4.8给出ASCII码对应的字符

package nameyu;
import java.util.Scanner;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    	Scanner input=new Scanner(System.in);
    	System.out.print("Enter an ASCII code:");
    	int number=input.nextInt();
    	if(number>=1&&number<=127){
    	char ch=(char)number;
    	System.out.println("The character for ASCII code is :"+ch);
    	}
    	else
    		System.exit(1);
    }
    }  

4.9给出字符的Unicode码

package nameyu;
import java.util.Scanner;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    	Scanner input=new Scanner(System.in);
    	System.out.print("Enter a character :");
    	String character=input.nextLine();
    	if(character.length()!=1){
    		System.out.println("You must enter exactly one character!!!");
    		System.exit(1);
    	}
    	char ch=character.charAt(0);
    	if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')){
    		int interger=(int)ch;
    		System.out.println("The Unicode for the character"+ch+" is"+interger);
    	}else
    		System.exit(1);
    		
    	
    }
    }  

4.11十进制转十六进制

package nameyu;
import java.util.Scanner;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    	Scanner input=new Scanner(System.in);
    	System.out.print("Enter a decimal value(0 to 15) :");
    	int number=input.nextInt();
    	if(number>=0&&number<=9){
    		System.out.println("The hex value is "+number);
    	}
    	else if(number>=10&&number<=15){
    		char ch=(char)(number+'A'-10);
    		System.out.println("The hex value is "+ch);
    	}
    	else 
    		System.out.println(number+" is an invalid input");
    }
    }  

4.12十六进制转二进制
本人习惯偷懒,这道题虽然没有星星,但是也是让我头痛了很久,最终在API中找到了这两个函数,完美的额解决了这个题,真的很兴奋。API无敌!如果要和书本一致的话,自己增加个循环吧,当输入的不是16进制的时候直接System.exit(1)。

package nameyu;
import java.util.Scanner;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    	Scanner input=new Scanner(System.in);
    	System.out.print("Enter a hex digit :");
    	String number=input.nextLine();
    	int inter = Integer.parseInt(number, 16);//这个函数是把16进制转化为十进制,把这个值给inter
    	String str = Integer.toBinaryString(inter);//把inter这个十进制的值直接转为二进制
    	System.out.println(str);
    }
    }  

4.13判断元音还是辅音

package nameyu;
import java.util.Scanner;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    	Scanner input=new Scanner(System.in);
    	System.out.print("Enter a letter:");
    	String str=input.nextLine();
    	char ch=str.charAt(0);
    	if(ch=='A'||ch=='a'){
    		System.out.println(ch+" is a vowel");
    	}
    	else if(ch=='E'||ch=='e'){
    		System.out.println(ch+" is a vowel");
    	}
    	else if(ch=='I'||ch=='i'){
    		System.out.println(ch+" is a vowel");
    	}
    	else if(ch=='O'||ch=='o'){
    		System.out.println(ch+" is a vowel");
    	}
    	else if(ch=='U'||ch=='u'){
    		System.out.println(ch+" is a vowel");
    	}
    	else
    		System.out.println(ch+" is a consonant");
    }
    }  

4.14转换为字母等级的数字

package nameyu;
import java.util.Scanner;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    	Scanner input=new Scanner(System.in);
    	System.out.print("Enter a letter grade(A、B、C、D、F) :");
    	String str=input.next();
    	char ch=str.charAt(0);
    	if (ch=='A'){
    		System.out.println("The numeric value for grade "+ch+"is 4");
    	}
    	else if(ch=='B'){
    		System.out.println("The numeric value for grade "+ch+"is 3");
    	}
    	else if(ch=='C'){
    		System.out.println("The numeric value for grade "+ch+"is 2");
    	}
    	else if(ch=='D'){
    		System.out.println("The numeric value for grade "+ch+"is 1");
    	}
    	else if(ch=='F'){
    		System.out.println("The numeric value for grade "+ch+"is 0");
    	}
    	else
    		System.out.println(ch+" is an invalid grade");
    
    
    
    }
    }  

4.16随机字母

package nameyu;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    	int number=(int)(65+Math.random()*26);
    	char ch=(char)number;
    	System.out.println(ch);
    }
    }  

4.17一个月中的日期

package nameyu;
import java.util.Scanner;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    	Scanner input=new Scanner(System.in);
    	System.out.print("Enter a year:");
    	int year=input.nextInt();
    	Scanner iput=new Scanner(System.in);
    	System.out.print("Enter a month:");
    	String month=iput.nextLine();
    	char ch1=month.charAt(0);
    	char ch2=month.charAt(1);
    	char ch3=month.charAt(2);
    	if(ch1=='J'&&ch2=='a'&&ch3=='n'){
    		System.out.println("Jan "+year+" has 31 days");
    	}
    	else if(ch1=='F'&&ch2=='e'&&ch3=='b'){
    		if((year%4==0&&year%100!=0)||(year%400==0)){
    			System.out.println("Feb "+year+" has 29 days");
    		}else
    			System.out.println("Feb "+year+" has 28 days");
    	}
    	else if(ch1=='M'&&ch2=='a'&&ch3=='r'){
    		System.out.println("Mar "+year+" has 31 days");
    	}
    	else if(ch1=='A'&&ch2=='p'&&ch3=='r'){
    		System.out.println("Apr "+year+" has 30 days");
    	}
    	else if(ch1=='M'&&ch2=='a'&&ch3=='y'){
    		System.out.println("May "+year+" has 31 days");
    	}
    	else if(ch1=='J'&&ch2=='u'&&ch3=='n'){
    		System.out.println("Jun "+year+" has 30 days");
    	}
    	else if(ch1=='J'&&ch2=='u'&&ch3=='l'){
    		System.out.println("Jul "+year+" has 31 days");
    	}
    	else if(ch1=='A'&&ch2=='u'&&ch3=='g'){
    		System.out.println("Aug "+year+" has 31 days");
    	}
    	else if(ch1=='S'&&ch2=='e'&&ch3=='p'){
    		System.out.println("Sep "+year+" has 30 days");
    	}
    	else if(ch1=='O'&&ch2=='c'&&ch3=='t'){
    		System.out.println("Oct "+year+" has 31 days");
    	}
    	else if(ch1=='N'&&ch2=='o'&&ch3=='v'){
    		System.out.println("Nov "+year+" has 30 days");
    	}
    	else if(ch1=='D'&&ch2=='e'&&ch3=='c'){
    		System.out.println("Dec "+year+" has 31 days");
    	}
    }
    }  

4.18学生的专业和状况

package nameyu;
import java.util.Scanner;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    	Scanner input=new Scanner(System.in);
    	System.out.print("Enter two characters:");
        String message=input.nextLine();
        char str1=message.charAt(0);
        char str2=message.charAt(1);
        String major="";
        String grade="";
        if((str1=='M'||str1=='C'||str1=='I')&&(str2=='1'||str2=='2'||str2=='3'||str2=='4')){
        	if(str1=='M'){
        		major="Mathmatics";
        	}else if(str1=='C'){
        		major="Computer";
        	}else if(str1=='I'){
        		major="Infomation technology";
        	}
        	if(str2=='1'){
        		grade="Freshman";
        	}else if(str2=='2'){
        		grade="Sophomore";
        	}else if(str2=='3'){
        		grade="Junior";
        	}else if(str2=='4'){
        		grade="Senior";
        	}
        		
        	System.out.println(major+" "+grade);
        	
        }else
        	System.out.println("Invalid input");
        
        
    }
    }  

4.19商业:检测ISBN-10
字符零的十进制是48。

package nameyu;
import java.util.Scanner;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    	Scanner input=new Scanner(System.in);
    	System.out.print("Enter a string of nine consecutive digits:");
    	String numbers=input.nextLine();
    	int d1=numbers.charAt(0)-48;
    	int d2=numbers.charAt(1)-48;
    	int d3=numbers.charAt(2)-48;
    	int d4=numbers.charAt(3)-48;
    	int d5=numbers.charAt(4)-48;
    	int d6=numbers.charAt(5)-48;
    	int d7=numbers.charAt(6)-48;
    	int d8=numbers.charAt(7)-48;
    	int d9=numbers.charAt(8)-48;
    	 int d10=(d1+d2*2+d3*3+d4*4+d5*5+d6*6+d7*7+d8*8+d9*9)%11;
         if (d10==10){
              System.out.println(d1 + "" + d2 + "" + d3 + "" + d4 + "" + d5 + "" + d6 + "" + d7 + "" + d8 + "" + d9 + "" + "X");}
                 else
                     System.out.println(d1 + "" + d2 + "" + d3 + "" + d4 + "" + d5 +"" + d6 + "" + d7 + "" + d8 + "" + d9 + "" + d10);

     }
  
    }
    

4.20字符串处理

package nameyu;
import java.util.Scanner;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    	Scanner input=new Scanner(System.in);
    	System.out.print("Enter a string:");
    	String numbers=input.nextLine();
        int l=numbers.length();
        char ch=numbers.charAt(0);
        System.out.println("length :"+l);
        System.out.println("first character :"+ch);
     }
  }

4.21检查SSN
这题做的方法比较死板,有好的方法可以私信我。因为我觉得这个真的太低能了。属于暴力破解题目类型,有时候也还可以,但是数据多了就不合适了的。
!!!!!!!!!!!!!!
我知道了!!!这道题使用字符串和数字间的转换就可以了。
int intvalue=Interger.parseInter(intString);
intString是一个数值型字符串。如果不是字符型字符串就会报错。哇爆炸,我不想改了。你们修改下吧,思路已经说了呢。不懂评论里见~

package nameyu;
import java.util.Scanner;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    	Scanner input=new Scanner(System.in);
    	System.out.print("Enter a SSN:");
    	String ssn=input.nextLine();
    	if(ssn.length()==11){
    		if(ssn.charAt(3)=='-'&&ssn.charAt(6)=='-'){
    			String str1=ssn.substring(0,3);
    			String str2=ssn.substring(4,6);
    			String str3=ssn.substring(7);
    			if(str1.charAt(0)>='0'&&str1.charAt(0)<='9'){
    				if(str1.charAt(1)>='0'&&str1.charAt(1)<='9'){
    					if(str1.charAt(2)>='0'&&str1.charAt(2)<='9'){
    						if(str2.charAt(0)>='0'&&str2.charAt(0)<='9'){
    							if(str2.charAt(1)>='0'&&str2.charAt(1)<='9'){
    								if(str3.charAt(0)>='0'&&str3.charAt(0)<='9'){
    									if(str3.charAt(1)>='0'&&str3.charAt(1)<='9'){
    										if(str3.charAt(2)>='0'&&str3.charAt(2)<='9'){
    											if(str3.charAt(3)>='0'&&str3.charAt(3)<='9'){
 System.out.println(ssn+" is a valid social security number");
    											}
    										}
    									}
    								}
    							}
    						}
    					}
    				}
    			}
    		}
    	}else
    		System.out.println(ssn+" is an invalid social security number");
     
     }
  
    }
    

4.22检测子串

package nameyu;
import java.util.Scanner;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    	Scanner input=new Scanner(System.in);
    	System.out.print("Enter string s1:");
    	String str1=input.next();
    	System.out.print("Enter string s2:");
    	  String str2=input.next();
    	  if(str1.indexOf(str2)>=0){
    	   System.out.println(str2+" is a substring of "+str1);
    	  }
    	  else
    		  System.out.println(str2+" is ont a substring of "+str1);
     }
    }
    

4.24对三个城市排序

package nameyu;
import java.util.Scanner;
public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input=new Scanner(System.in);
		System.out.print("Enter the first city");
		String city1=input.nextLine();
		System.out.print("Enter the second city");
		String city2=input.nextLine();
		System.out.print("Enter the third city");
		String city3=input.nextLine();
		if(city1.compareToIgnoreCase(city2)<0){
			if(city2.compareToIgnoreCase(city3)<0){
				System.out.println("123"+city1+" "+city2+" "+city3);
			}else if(city1.compareToIgnoreCase(city3)>0){
				System.out.println("312"+city3+" "+city1+" "+city2);
			}
		if(city3.compareToIgnoreCase(city2)<0){
			if(city2.compareToIgnoreCase(city1)<0){
				System.out.println("321"+city3+" "+city2+" "+city1);
			}else if(city3.compareToIgnoreCase(city1)>0){
				System.out.println("132"+city1+" "+city3+" "+city2);
			}
		else if(city3.compareToIgnoreCase(city2)>0){
			if(city3.compareToIgnoreCase(city1)<0){
				System.out.println("231"+city2+" "+city3+" "+city1);
			}
			}
		if(city1.compareToIgnoreCase(city3)<0){
			if(city1.compareToIgnoreCase(city2)>0)
				System.out.println("213"+city2+" "+city1+" "+city3);
		}
	
		}
	}
	}
}

4.25生成车牌号码



package nameyu;
public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	char ch1=(char) (65+Math.random()*26);
	char ch2=(char) (65+Math.random()*26);
	char ch3=(char) (65+Math.random()*26);
	String str1=String.valueOf(ch1);
	String str2=String.valueOf(ch2);
	String str3=String.valueOf(ch3);//把字符转换为字符串
	int  number1=(int) (Math.random()*10);
	int  number2=(int) (Math.random()*10);
	int  number3=(int) (Math.random()*10);
	int  number4=(int) (Math.random()*10);
//	String str=str1+str2+str3;
	System.out.println(str1+str2+str3+" "+number1+number2+number3+number4);
	}
}

4.26财务应用:货币单位

package nameyu;
import java.util.Scanner;
public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    	Scanner input=new Scanner(System.in);
    	String str=input.nextLine();
    	if(str.indexOf('.')==-1){
    		System.out.println(str+" dollars"+" no cent");
    	}else if(str.indexOf('.')!=-1){
    		String dollars=str.substring(0,str.indexOf('.'));
    		String cent=str.substring(str.indexOf('.')+1);
    		System.out.println(dollars+" dollars "+cent+" cents");
    	}
    	
    	}
     }
    
    

这一章的题目,差不多都在这里了。如有问题可私信或者评论指出,欢迎大家指正。谢谢大家。

评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Thomas & Friends

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值