JAVA programming language (Basic) Tenth Edition - Chapter Four math functions, characters and strings programming exercises (refer to the answer)

 

4.2 + (4.3 to 4.6)

4.2

4.1 (geometry: the pentagon area)

import java.util.Scanner;

public class C1 {

	public static void main(String[] args) {
		
		Scanner input=new Scanner(System.in);
		System.out.print("Enter the length from the center to a vertex: ");
		double r=input.nextFloat();
		input.close();
		
		double s=2*r*Math.sin(Math.PI/5);
		
		double area=(5*s*s)/(4*Math.tan(Math.PI/5));
		
		System.out.printf("The area of the pentagon is %5.2f ", area);
		
		
	}

}

* 4.2 (geometry: the largest circle distance)

This title challenge is how to receive a string of the console will be severed from it, is a string in the (,) and the comma character () space characters deleted.

That is, how to get from a string substring in question, I specifically went to Baidu for a moment (,) comma Unicode value: '\ u002C  '.

Is then obtained by dividing the string into numeric values, nothing of the difficulty (# ^. ^ #)

import java.util.Scanner;

public class C2 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		//用字符串接收s1、s2
		System.out.print("Enter point 1 (latitude and longitude) in degrees: ");
		String s1=input.nextLine();
		System.out.print("Enter point 2 (latitude and longitude) in degrees: ");
		String s2=input.nextLine();
		//input.close();
		
		String s11="";
		String s12="";
		
		//以','字符逗号为分割点,将s1字符串分为两部分
		if(s1.length()!=0 && !s1.equals("")) {
			for(int i=0;i<s1.length();i++) {
				if(s1.charAt(i) == '\u002C') {
					s11=s1.substring(0,i);
					s12=s1.substring(i+1);
				}
			}
		}
		
		//用trim()方法删去字符串两边的空白字符
		String s5=s11.trim();
		String s6=s12.trim();
		
		//将数值型的字符串 转换为 数值
		double degree1=Double.parseDouble(s5);
		double degree2=Double.parseDouble(s6);
		
		
		
		String s21="";
		String s22="";
		
		//以','字符逗号为分割点,将s2字符串分为两部分
		if(s2.length()!=0 && !s2.equals("")) {
			for(int i=0;i<s2.length();i++) {
				if(s2.charAt(i) == '\u002C') {
					s21=s2.substring(0,i);
					s22=s2.substring(i+1);
				}
			}
		}
		
		//用trim()方法删去字符串两边的空白字符
		String s7=s21.trim();
		String s8=s22.trim();
		
		//将数值型的字符串 转换为 数值
		double degree3=Double.parseDouble(s7);
		double degree4=Double.parseDouble(s8);
		
/*上面代码是完成input.nextLine()方法在控制台的接收,
		并将 数值型字符串 转换为  数值,进行下面的计算		*/

		
//下面的代码是完成公式套用计算
		double x1=Math.toRadians(degree1);
		double y1=Math.toRadians(degree2);
		double x2=Math.toRadians(degree3);
		double y2=Math.toRadians(degree4);
		
		double d= 6371.01 * Math.acos(Math.sin(x1)*Math.sin(x2) 
				+ Math.cos(x1)*Math.cos(x2)*Math.cos(y1-y2))  ;
		
		System.out.println("The distance between the two points is " + d +" km ");
	
	
	
	
	
	}

}

* 4.3 (geometry: the estimated area)

First, find out the latitude and longitude of places:

Atlanta Atlanta United States Latitude: 33 ° 46 ' Longitude: 84 ° 25 '
Orlando Orlando United States Latitude: 28 ° 3 ' Longitude: 81 ° 22 '

Place name: Savannah Latitude: 31.9714 Longitude: -81.0716

Names: Charlotte Latitude: 35.2731 Longitude: -80.9571

Then to the first standard point names, etc. so as not to obscure the moment when calculating

The first one point: a place name (latitude, longitude)

The first point: Atlanta (33.46, 84.25)

Second point: Orlando (28.3, 81.22)

The third point: Savannah (31.9714, -81.0716)

The fourth point: Charlotte (35.2731, -80.9571)

In fact, nothing difficult, is to find a formula to find the latitude and longitude trouble some.

import java.util.Scanner;

public class C3 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		//用字符串接收s1、s2、s3、s4
		System.out.print("Enter point 1 (latitude and longitude) in degrees: ");
		String s1=input.nextLine();
		System.out.print("Enter point 2 (latitude and longitude) in degrees: ");
		String s2=input.nextLine();
		System.out.print("Enter point 3 (latitude and longitude) in degrees: ");
		String s3=input.nextLine();
		System.out.print("Enter point 4 (latitude and longitude) in degrees: ");
		String s4=input.nextLine();
		input.close();

//以下是 处理s1字符串···············································
		String s11="";
		String s12="";
		
		//以','字符逗号为分割点,将s1字符串分为两部分
		if(s1.length()!=0 && !s1.equals("")) {
			for(int i=0;i<s1.length();i++) {
				if(s1.charAt(i) == '\u002C') {
					s11=s1.substring(0,i);
					s12=s1.substring(i+1);
				}
			}
		}
		
		//用trim()方法删去字符串两边的空白字符
		String s5=s11.trim();
		String s6=s12.trim();
		
		//将数值型的字符串 转换为 数值
		double degree1=Double.parseDouble(s5);
		double degree2=Double.parseDouble(s6);
		
//以下是 处理s2字符串···············································
				String s21="";
				String s22="";
				
				//以','字符逗号为分割点,将s2字符串分为两部分
				if(s2.length()!=0 && !s2.equals("")) {
					for(int i=0;i<s2.length();i++) {
						if(s2.charAt(i) == '\u002C') {
							s21=s2.substring(0,i);
							s22=s2.substring(i+1);
						}
					}
				}
				
				//用trim()方法删去字符串两边的空白字符
				String s7=s21.trim();
				String s8=s22.trim();
				
				//将数值型的字符串 转换为 数值
				double degree3=Double.parseDouble(s7);
				double degree4=Double.parseDouble(s8);		
				
//以下是 处理s3字符串···············································
				String s31="";
				String s32="";
				
				//以','字符逗号为分割点,将s3字符串分为两部分
				if(s3.length()!=0 && !s3.equals("")) {
					for(int i=0;i<s3.length();i++) {
						if(s3.charAt(i) == '\u002C') {
							s31=s3.substring(0,i);
							s32=s3.substring(i+1);
						}
					}
				}
				
				//用trim()方法删去字符串两边的空白字符
				String s9=s31.trim();
				String s10=s32.trim();
				
				//将数值型的字符串 转换为 数值
				double degree5=Double.parseDouble(s9);
				double degree6=Double.parseDouble(s10);
				
//以下是 处理s4字符串···············································
				String s41="";
				String s42="";
				
				//以','字符逗号为分割点,将s4字符串分为两部分
				if(s4.length()!=0 && !s4.equals("")) {
					for(int i=0;i<s4.length();i++) {
						if(s4.charAt(i) == '\u002C') {
							s41=s4.substring(0,i);
							s42=s4.substring(i+1);
						}
					}
				}
				
				//用trim()方法删去字符串两边的空白字符
				String s111=s41.trim();
				String s121=s42.trim();
				
				//将数值型的字符串 转换为 数值
				double degree7=Double.parseDouble(s111);
				double degree8=Double.parseDouble(s121);
				
//下面是求两点间的距离···············································
//先求 点1和点2 之间的距离
				//度数转换成弧度
				double x1=Math.toRadians(degree1);
				double y1=Math.toRadians(degree2);
				double x2=Math.toRadians(degree3);
				double y2=Math.toRadians(degree4);
				
				double d12= 6371.01 * Math.acos(Math.sin(x1)*Math.sin(x2) 
						+ Math.cos(x1)*Math.cos(x2)*Math.cos(y1-y2))  ;
				
//再求 点2和点3 之间的距离
				//度数转换成弧度
				double x3=Math.toRadians(degree5);
				double y3=Math.toRadians(degree6);

				
				double d23= 6371.01 * Math.acos(Math.sin(x2)*Math.sin(x3) 
						+ Math.cos(x2)*Math.cos(x3)*Math.cos(y2-y3))  ;
				
		
//再求 点1和点3 之间的距离
			
				double d13= 6371.01 * Math.acos(Math.sin(x1)*Math.sin(x3) 
						+ Math.cos(x1)*Math.cos(x3)*Math.cos(y1-y3))  ;
							
//再求 点3和点4 之间的距离
				//度数转换成弧度
				double x4=Math.toRadians(degree7);
				double y4=Math.toRadians(degree8);

				
				double d34= 6371.01 * Math.acos(Math.sin(x3)*Math.sin(x4) 
						+ Math.cos(x3)*Math.cos(x4)*Math.cos(y3-y4))  ;
				
//再求 点1和点4 之间的距离
				
				double d14= 6371.01 * Math.acos(Math.sin(x1)*Math.sin(x4) 
						+ Math.cos(x1)*Math.cos(x4)*Math.cos(y1-y4))  ;

//距离算完了,就可以利用算三角形面积公式 求出这个四边形的面积
/*三角形面积公式:
               s=(边1+边2+边3)/2   
               面积=根号下(   (s*(s-边1)*(s-边2)*(s-边3))  )
               
               */
		//第一个三角形的面积		
		double ss1=(d12+d23+d13)/2;
		double area1=Math.sqrt((ss1*(ss1-d12)*(ss1-d23)*(ss1-d13)));
		
		//第二个三角形的面积
		double ss2=(d13+d34+d14)/2;
		double area2=Math.sqrt((ss2*(ss2-d13)*(ss2-d34)*(ss2-d14)));
		
		//四边形的面积
		double area= area1 + area2 ;
		
		System.out.println("The total Area is " + area);
	}

}

operation result:

 

 

4.4 (geometry: hexagonal area)

import java.util.Scanner;

public class C4 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter the side: ");
		double s=input.nextDouble();
		input.close();
		
		double area=(6*s*s)/(4*Math.tan(Math.PI/6));
		
		System.out.printf("The area of the hexagon is %5.2f", area);
		
	}

}

* 4.5 (geometry: regular polygon area)

The tan (-) I do not know what to put inside, chaotic series I

I used the same formula and not the book, makes up his own formula I: Area = (n * s * s) / (4 * tan (180 ° / n)

import java.util.Scanner;

public class C5 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter the number of sides: ");
		int n=input.nextInt();
		System.out.print("Enter the side: ");
		double s=input.nextDouble();
		
		double area=(n*s*s)/(4*Math.tan(Math.toRadians(180)/n));
		
		System.out.println("The area of the polygon is " + area);
	}

}

Results and the book of the same

 

4.6 * (random point on the circle)

import java.util.Scanner;

public class C6 {

	public static void main(String[] args) {
	
		int r=40;//半径
		
		//第 1个随机点
		int a1=(int) (Math.random()*2*Math.PI);
		double x1=r*Math.cos(a1);
		double y1=r*Math.sin(a1);
		
		//第 2个随机点
		int a2=(int) (Math.random()*2*Math.PI);
		double x2=r*Math.cos(a2);
		double y2=r*Math.sin(a2);
		
		//第 3个随机点
		int a3=(int) (Math.random()*2*Math.PI);
		double x3=r*Math.cos(a3);
		double y3=r*Math.sin(a3);	
		
		double d12=Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
		double d23=Math.sqrt((x2-x3)*(x2-x3) + (y2-y3)*(y2-y3));
		double d13=Math.sqrt((x1-x3)*(x1-x3) + (y1-y3)*(y1-y3));
		
		double a=d23;
		double b=d13;
		double c=d12;
		
		//求角度
		double la=Math.toDegrees( Math.acos((a*a-b*b-c*c)/(-2*b*c)) );
		double lb=Math.toDegrees( Math.acos((b*b-a*a-c*c)/(-2*a*c)) );
		double lc=Math.toDegrees( Math.acos((c*c-a*a-b*b)/(-2*a*b)) );
		
		System.out.println(" ∠A的度数为 :" + (int)la +"°");
		System.out.println(" ∠B的度数为 :" + (int)lb +"°");
		System.out.println(" ∠C的度数为 :" + (int)lc +"°");
		
		
	}

}

* 4.7 (vertex coordinates)

This question please do not tangle, directly applied on a topic like formulas 4.6 title

X = r * cos (a) and Y = r * sin (a) for use in equation 4.6 * topic in question.

import java.util.Scanner;

public class C7 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter the radius of the bounding circle: ");
		double r=input.nextDouble();
		input.close();
		
		//以x轴为基准,得出度数
		double degree1=18;
		double degree2=90;
		double degree3=162;
		double degree4=-126;
		double degree5=-54;
		
		/*与上一题*4.6一样,运用它的 X ,Y 的转换公式,
		你 4.6题懂了,4.7题也就自然懂了,
		很简单的,就是套用公式,想都不用想的那种*/
		double x1=r*Math.cos(Math.toRadians(degree1));
		double y1=r*Math.sin(Math.toRadians(degree1));
		
		double x2=r*Math.cos(Math.toRadians(degree2));
		double y2=r*Math.sin(Math.toRadians(degree2));
		
		double x3=r*Math.cos(Math.toRadians(degree3));
		double y3=r*Math.sin(Math.toRadians(degree3));
		
		double x4=r*Math.cos(Math.toRadians(degree4));
		double y4=r*Math.sin(Math.toRadians(degree4));
		
		double x5=r*Math.cos(Math.toRadians(degree5));
		double y5=r*Math.sin(Math.toRadians(degree5));
		

		System.out.println("The coordinates of five points on the pentagon are ");  
		System.out.printf("( %7.4f , %7.4f) \n", x1, y1 );
		System.out.printf("( %7.4f , %7.4f) \n", x2, y2 );
		System.out.printf("( %7.4f , %7.4f) \n", x3, y3 );
		System.out.printf("( %7.4f , %7.4f) \n", x4, y4 );
		System.out.printf("( %7.4f , %7.4f) \n", x5, y5 );
	
		
	}

}

4.3 to Section 4.6

 4.8 * (ASCII code corresponding to the given character) 

I want to cry ············· (I had foolishly trying to decimal number of hexadecimal characters into another, of course, did not achieve the final step, I do not know Can)

Just two lines of code I want to die, finally came to understand the feeling is that he did not grasp this knowledge before it (¬︿̫̿¬ ☆)

Knowledge points:

ASCII code 'decimal number' into a corresponding 'character' method :( decimal character +) ! ! !

import java.util.Scanner;

public class C8 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter an ASCII code: integer:(0~127) :");
		int number=input.nextInt();
		
		char ch=(char) ('\u0000'+number);
		System.out.println("The character for ASCII code " + number + " is " + ch);
		
	}

}

* 4.9 (Unicode character code given)

With a question on torture, yes, this problem is so happy rude plus simple (● '◡' ●)

Knowledge points:

ASCII code of 'characters' to a 'decimal number' corresponding method :( decimal character +) ! ! !

A comparison of Kazakhstan, as if all the same. 〒_〒

import java.util.Scanner;

public class C9 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a character :");
		String s=input.nextLine();
		input.close();
		
		char ch=s.charAt(0);
		int a=ch+0;
		System.out.println("The Unicode for the character " + ch + " is " + a);
		
		
	}

}

* 4.10 (guess birthday)

slightly

* 4.11 (decimal convert hexadecimal)

import java.util.Scanner;

public class C11 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a decimal value (0 to 15) :" );
		int number=input.nextInt();
		input.close();
		
		if(number>=0 && number<=15) {
			if(number>9) {
				String s = null;
				switch(number) {
				case 10:s="A";
							break;
				case 11:s="B";
							break;
				case 12:s="C";
							break;
				case 13:s="D";
							break;
				case 14:s="E";
							break;
				case 15:s="F";
							break;
				}
				System.out.println(" The hex value is " + s );
			}
			else {
				String s=""+number;
				System.out.println(" The hex value is " + s );
			}
			
			
			
		}
		else {
			System.out.println(" "+ number + " is an invalid input ");
		}
		
		
	}

}

4.12 (hex binary rpm)

import java.util.Scanner;

public class C12 {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter a hex digit: ");
		String s=input.nextLine();
		input.close();
		
		char ch=s.charAt(0);
		
		if(ch>='0' && ch<='9') {
			int number= ch + 0 ;
			int remainder1=number%2;//求得第1个余数
			int remainder2=number/2%2;//求得第2个余数
			int remainder3=number/2/2%2;//求得第3个余数
			int remainder4=number/2/2/2%2;//求得第4个余数
			System.out.println("The binary value is " + 
			remainder4 + remainder3 + remainder2 + remainder1);
		}
		else if(ch>='A' && ch<='F') {
			int number=0;
			switch(ch) {
			case 'A': number += 10;
						break;
			case 'B': number += 11;
						break;
			case 'C': number += 12;
						break;
			case 'D': number += 13;
						break;
			case 'E': number += 14;
						break;
			case 'F': number += 15;
						break;
			}
			
			int remainder1=number%2;//求得第1个余数
			int remainder2=number/2%2;//求得第2个余数
			int remainder3=number/2/2%2;//求得第3个余数
			int remainder4=number/2/2/2%2;//求得第4个余数
			System.out.println("The binary value is " + 
			remainder4 + remainder3 + remainder2 + remainder1);
		
		}
		else {
			System.out.println(ch+" is an invalid input ");
		}
		
		
	}

}

* 4.13 (judgment vowel or consonant)

import java.util.Scanner;

public class C13 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a letter: ");
		String s=input.nextLine();
		input.close();
		
		char ch=s.charAt(0);
		
		if(ch>='A' && ch<='Z' || ch>='a' && ch<='z') {
			
			if(ch=='A'|| ch=='E'|| ch=='I'|| ch=='O'|| ch=='U'
			|| ch=='a'|| ch=='e'|| ch=='i'|| ch=='o'|| ch=='u') {
		 
				System.out.println(ch+" is a vowel");
						
			}
			else {
				System.out.println(ch+" is a consonant");
			}
			
			
		}
		else {
			System.out.println(ch + " is an invalid input");
		}
		
		
		
		
	}

}

* 4.14 (converted to digital letter grade)

import java.util.Scanner;

public class c14 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a letter grade: ");
		String s=input.nextLine();
		input.close();
		
		char ch=s.charAt(0);
		
		if(ch>='A' && ch<='D' || ch=='F') {
			int number=0;
			switch(ch) {
			case 'A':number+=4;
						break;
			case 'B':number+=3;
						break;
			case 'C':number+=2;
						break;
			case 'D':number+=1;
						break;
			case 'F':number+=0;
						break;
			}
			System.out.println("The numeric value for grade "+ch+" is "+number);
		}
		else {
			System.out.println( ch + " is an invalid grade");
		}
		
		
		
		
	}

}

* 4.15 (phone keypad)

import java.util.Scanner;

public class C15 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a letter: ");
		String s=input.nextLine();
		input.close();
		
		char ch=s.charAt(0);
		
		if(Character.isLetter(ch)) {
			String s1=""+ch;
			if(s1.equalsIgnoreCase("a") 
			|| s1.equalsIgnoreCase("b")
			|| s1.equalsIgnoreCase("c") ) {
				System.out.println("The corresponding number is " + 2 );
			}
			else if(s1.equalsIgnoreCase("d") 
					|| s1.equalsIgnoreCase("e")
					|| s1.equalsIgnoreCase("f") ) {
				System.out.println("The corresponding number is " + 3 );
			}
			else if(s1.equalsIgnoreCase("g") 
					|| s1.equalsIgnoreCase("h")
					|| s1.equalsIgnoreCase("i") ) {
				System.out.println("The corresponding number is " + 4 );
			}
			else if(s1.equalsIgnoreCase("j") 
					|| s1.equalsIgnoreCase("k")
					|| s1.equalsIgnoreCase("l") ) {
				System.out.println("The corresponding number is " + 5 );
			}
			else if(s1.equalsIgnoreCase("m") 
					|| s1.equalsIgnoreCase("n")
					|| s1.equalsIgnoreCase("o") ) {
				System.out.println("The corresponding number is " + 6 );
			}
			else if(s1.equalsIgnoreCase("p") 
					|| s1.equalsIgnoreCase("q")
					|| s1.equalsIgnoreCase("r")
					|| s1.equalsIgnoreCase("s")) {
				System.out.println("The corresponding number is " + 7 );
			}
			else if(s1.equalsIgnoreCase("t") 
					|| s1.equalsIgnoreCase("u")
					|| s1.equalsIgnoreCase("v") ) {
				System.out.println("The corresponding number is " + 8 );
			}
			else if(s1.equalsIgnoreCase("w") 
					|| s1.equalsIgnoreCase("x")
					|| s1.equalsIgnoreCase("y")
					|| s1.equalsIgnoreCase("z")) {
				System.out.println("The corresponding number is " + 9 );
			}
			
		}
		else {
			System.out.println(ch + " is an invalid input");
		}
	}

}

4.16 (random characters)

public class C16 {

	public static void main(String[] args) {
		int random= (int) (65 + Math.random()*26);//大写字母的十进制编码值从65~90
		char ch=(char) ('\u0000'+random);
		System.out.println("这个随机字符是: " + ch);
		
		
	}

}

* 4.17 (the date of the month)

I am in the process of coding problems found:

When I enter an integer value to the console, then enter a string: console does not seem to receive the second string data, in fact, is to receive the string "Enter key", it seems as if there is no reception data.

Solution: 1 allows the console to receive the first data type is a string, and then convert the string to a numeric type.

Solution: 2 may create two objects Scanner receive different data types.

I use the following code Solution 1. 

import java.util.Scanner;

public class C17 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a year: ");
		String yearString=input.nextLine();
		
		System.out.print("Enter a month: ");
		String month=input.nextLine();
		input.close();
		
		int days=0;
		
		//将字符串转为整数
		int year=Integer.parseInt(yearString);
		
		//判定是否闰年
		boolean isLeapYear=( (year%4==0 && year%100!=0) || (year%400==0) );  
	
		switch(month) {
		case "Jan":days=31;
					break;
		case "Feb":	if(isLeapYear) {
							days=29;
						}
						else {
							days=28;
							}
					break;
		case "Mar":days=31;
					break;
		case "Apr":days=30;
					break;
		case "May":days=31;
					break;
		case "Jun":days=30;
					break;
		case "Jul":days=31;
					break;
		case "Aug":days=31;
					break;
		case "Sep":days=30;
					break;
		case "Oct":days=31;
					break;
		case "Nov":days=30;
					break;
		case "Dec":days=31;
					break;
		
		}  
		
		System.out.println(month +" "+ year + " has " + days +  " days");
		
	}

}

 

* 4.18 (students and professional status)

import java.util.Scanner;

public class C18 {

	public static void main(String[] args) {
		Scanner input =new Scanner(System.in);
		System.out.print("Enter two character:");
		String s=input.nextLine();
		input.close();
		
		char ch1=s.charAt(0);
		char ch2=s.charAt(1);
		int number=ch2+0;
		String major=null;
		String student=null;
		
		if((ch1=='M'||ch1=='C'||ch1=='I') 
			&& (ch2=='1'||ch2=='2'||ch2=='3'||ch2=='4')) {
			
		switch(ch1) {
		case 'M':major="Mathmatics";
				break;
		case 'C':major="Computer Science";
				break;
		case 'I':major="Information Technology";
				break;
		}
		
		
		switch(ch2) {
		case '1':student="Freshman";
				break;
		case '2':student="Sophomore";
				break;
		case '3':student="Junior";
				break;
		case '4':student="Senior Student";
				break;
			}
		
		System.out.println(major+" "+student);
		
		}
		else {
			System.out.println("Invalid input");
		}
		
		
	}

}

4.19 (Commercial: detecting ISBN-10)

slightly

4.20 (string processing)

import java.util.Scanner;

public class C20 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("请输入一个字符串: ");
		String s=input.nextLine();
		int length=s.length();
		char ch1=s.charAt(0);
		System.out.println("该字符串的长度为: "+length);
		System.out.println("该字符串的第一个字符为: "+ch1);
	}

}

 4.21 * (check substring)

import java.util.Scanner;

public class C21 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter a SSN: ");
		String s=input.nextLine();
		input.close();
		
		if(s.length()==11) {
			if(Character.isDigit(s.charAt(0)) 
				&& Character.isDigit(s.charAt(1)) 
				&& Character.isDigit(s.charAt(2)) 
				&& (s.charAt(3)=='-')
				&& Character.isDigit(s.charAt(4)) 
				&& Character.isDigit(s.charAt(5)) 
				&& (s.charAt(6)=='-') 
				&& Character.isDigit(s.charAt(7)) 
				&& Character.isDigit(s.charAt(8)) 
				&& Character.isDigit(s.charAt(9)) 
				&& Character.isDigit(s.charAt(10)) ) {
				System.out.println(s+" is a valid social security number");
			}
			else {
				System.out.println(s+" is an invalid social security number");
			}
		}
		else {
			System.out.println(s+" is an invalid social security number");
		}
		
	}

}

4.22 (detection substring)

Knowledge: in the string s1.contain (s2): If s1 S2 is a substring of a string, returns true.

import java.util.Scanner;

public class C22 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter string s1: ");
		String s1=input.nextLine();
		
		System.out.print("Enter string s2: ");
		String s2=input.nextLine();
		input.close();
		
		if(s1.contains(s2)) {
			System.out.println(s2+" is a substring of "+s1);	
		}
		else{
			System.out.println(s2+" is not a substring of "+s1);
		}
	
	}

}

* 4.23 (financial applications: remuneration)

import java.util.Scanner;

public class C23 {

	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.print("Enter employee's name:");
		String name=input.nextLine();
		
		System.out.print("Enter number of hours worked in a week:");
		double hour=input.nextDouble();
		
		System.out.print("Enter hourly pay rate:");
		double payRate=input.nextDouble();
		
		System.out.print("Enter federal tax withholding rate:");
		double federalRate=input.nextDouble();
		
		System.out.print("Enter state tax withholding rate:");
		double stateRate=input.nextDouble();
		input.close();
		
		double grossPay=hour*payRate;
		
		System.out.printf("Employee Name: %s \n"
				+ "Hours Worked: %3.1f \n"
				+"Pay Rate: $%4.2f \n"
				+"Gross Pay: $%4.1f \n"
				+"Deductions: \n"
				+"  Federal Withholding (%4.1f%%): $%4.1f \n"
				+"  State Withholding (%3.1f%%): $%4.2f \n"
				+"  Total Dedution: $%5.2f \n"
				+"Net Pay: $%5.2f ", name, hour, payRate, grossPay, federalRate*100, federalRate*grossPay, stateRate*100, stateRate*grossPay, federalRate*grossPay+stateRate*grossPay, grossPay-(stateRate*grossPay+federalRate*grossPay) );
				
		
		
	}

}

* 4.24 (sort of three cities)

import java.util.Scanner;

public class C24 {

	public static void main(String[] args) {
		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.compareTo(city2)>0) {
			//System.out.println("The order is city2, city1.");
			
			if(city2.compareTo(city3)>0) {
				System.out.println("The three cities in alphabetical order are "+city3+", "+city2+", "+city1);
				
			}
			else if(city2.compareTo(city3)<0) {
				if(city1.compareTo(city3)<0) {
					System.out.println("The three cities in alphabetical order are "+city2+", "+city1+", "+city3);
				}
				else if(city1.compareTo(city3)>0){
					System.out.println("The three cities in alphabetical order are "+city2+", "+city3+", "+city1);
				}
			}
			
		}
		else if (city1.compareTo(city2)<0){
			//System.out.println("The order is city1, city2.");
			
			if(city1.compareTo(city3)>0) {
				System.out.println("The three cities in alphabetical order are "+city3+", "+city1+", "+city2);
			}
			else if(city1.compareTo(city3)<0) {
				
				if(city2.compareTo(city3)>0) {
					System.out.println("The three cities in alphabetical order are "+city1+", "+city3+", "+city2);
	
				}
				else if(city2.compareTo(city3)<0){
					System.out.println("The three cities in alphabetical order are "+city1+", "+city2+", "+city3);

				}
			}
			
		}
			
		
		
	}

}

4.25 * (generated license number)

public class C25 {

	public static void main(String[] args) {

		//产生3个随机大写字母 和 4个随机数字
		char ch1=(char) (65+Math.random()*26);
		char ch2=(char) (65+Math.random()*26);
		char ch3=(char) (65+Math.random()*26);
		int number=(int) (1000+Math.random()*9000);
		System.out.println("一个随机的车牌号是: "+ch1+ch2+ch3+number);

	}

}

* 4.26 (financial applications: Currency Unit)

slightly

 

 

 

Released four original articles · won praise 2 · Views 1993

Guess you like

Origin blog.csdn.net/cxj18867076391/article/details/104998491