Chapter 4 Mathematical Functions, Characters, and Strings
Table of contents
-
- Chapter 4 Mathematical Functions, Characters, and Strings
-
- 4.2 Geometry: maximum circle distance
- 4.5 Geometry: Area of regular polygons
- 4.6 Random points on a circle
- 4.7 Vertex coordinates
- 4.8 Output the characters corresponding to the ASCII code
- 4.9 Unicode codes of output characters
- 4.11 Decimal to hexadecimal (integer between 0-15)
- 4.13 Determine vowel or consonant
- 4.17 Day of the month
- 4.18 Major and Status of Students
- 4.21 Detect SSN
- 4.23 Financial application: honoraria
- Supplementary knowledge points:
Chapter 4 Mathematical Functions, Characters, and Strings
4.2 Geometry: maximum circle distance
The maximum circle distance is the distance between two points on a sphere
Code:
public class Exercise04_02 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter point 1 (latitude and longitude) in degrees: ");
double x1= in.nextDouble();
double y1= in.nextDouble();
System.out.print("Enter point 2 (latitude and longitude) in degrees: ");
double x2= in.nextDouble();
double y2= in.nextDouble();
in.close();
double r=6371.01;
x1=Math .toRadians(x1);
x2=Math .toRadians(x2);
y1=Math .toRadians(y1);
y2=Math .toRadians(y2);
double d=r *(Math.acos(Math.sin(x1) * Math.sin(x2) + Math.cos(x1) * Math.cos(x2) * Math.cos(y1- y2)));
System.out.printf("The distance between the two points is "+d+"km");
}}
4.5 Geometry: Area of regular polygons
Find the area of a regular polygon:
Code:
import java.util.*;
public class Exercise04_05 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter the number of sides:");
int n=in.nextInt();
System.out.print("Enter the side: ");
double s=in.nextDouble();
in.close();
double area=(n*s*s)/(4.0*Math.tan(Math.PI/n));
System.out.printf("The area of the polygon is "+area);
}
}
4.6 Random points on a circle
Generates 3 random points on a circle with center at (0,0 ) and radius 40, and displays the degrees of the three angles of the triangle formed by these three random points.
code:
package firstprogram;
import java.util.Scanner;
public class Exercise04_06 {
public static void main(String[] args) {
float rad1;
float rad2;
float rad3;
while(true) {
//随机产生三个弧度
rad1=2*(float)(Math.PI*Math.random());
rad2=2*(float)(Math.PI*Math.random());
rad3=2*(float)(Math.PI*Math.random());
//防止弧度相等
if(rad1==rad2||rad2==rad3||rad1==rad3) {
continue;
}
else
break;
}
float x1=40*(float)Math.cos(rad1);
float x2=40*(float)Math.cos(rad2);
float x3=40*(float)Math.cos(rad3);
float y1=40*(float)Math.sin(rad1);
float y2=40*(float)Math.sin(rad2);
float y3=40*(float)Math.sin(rad3);
System.out.println("Three points are :");
System.out.printf("A(%.2f,%.2f) B(%.2f,%.2f) C(%.2f,%.2f)",x1,y1,x2,y2,x3,y3);//精度2
//边长
float s1=(float)Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2, 2));
float s2=(float)Math.sqrt(Math.pow(x2-x3,2)+Math.pow(y2-y3, 2));
float s3=(float)Math.sqrt(Math.pow(x1-x3,2)+Math.pow(y1-y3, 2));
//角度
float A=(float)Math.toDegrees(Math.acos((s2*s2+s3*s3-s1*s1)/(2*s2*s3)));
float B=(float)Math.toDegrees(Math.acos((s1*s1+s3*s3-s2*s2)/(2*s1*s3)));
float C=(float)Math.toDegrees(Math.acos((s2*s2+s1*s1-s3*s3)/(2*s2*s1)));
System.out.println("Three angles are: ");
System.out.printf("A:%.2f B:%.2f C:%.2f",A,B,C);
}
}
4.7 Vertex coordinates
Enter the radius of the circumscribed circle of a regular pentagon, the center (0, 0), and display the coordinates of the five vertices.
code:
import java.util.Scanner;
public class Exercise04_07 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter the radius of the bounding circle: ");
double r=in.nextDouble();
System.out.println("The coordinates of five points on the pentagon are");
double angle=Math.PI/2-2*Math.PI/5;
double x=0;
double y=0;
for(int i=0;i<5;i++) {
x=r*Math.cos(angle);
y=r*Math.sin(angle);
System.out.printf("(%.4f,%.4f)\n",x,y);
angle+=2*Math.PI/5;
}
in.close();
}
}
4.8 Output the characters corresponding to the ASCII code
code:
public class Exercise04_08 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter an ASCII code: ");
int c=in.nextInt();
System.out.println("The character for ASCII code "+c+" is "+(char)c);
}
}
4.9 Unicode codes of output characters
code:
import java.util.Scanner;
public class Exercise04_09 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter a character: ");
char c=in.next().charAt(0);
System.out.println("The Unicode for the character "+c+" is "+(int)c);
}
}
4.11 Decimal to hexadecimal (integer between 0-15)
(Note: Conversion of numbers above 10)
Code:
import java.util.Scanner;
public class Exercise04_11 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter a decimal value (0 to 15): ");
int num=in.nextInt();
if(num>15||num<0) {
System.out.println(num+" is an invalid input");
}
else if(num<10){
System.out.println("The hex value is "+num);
}
else {
System.out.println("The hex value is "+(char)('A'+num-10));
}
}
}
4.13 Determine vowel or consonant
Enter letters to judge vowels and consonants
Code:
import java.util.Scanner;
public class Exercise04_08 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter a letter: ");
char l=in.next().charAt(0);
if(Character.toUpperCase(l)=='A'||Character.toUpperCase(l)=='O'||Character.toUpperCase(l)=='E'||Character.toUpperCase(l)=='I'||Character.toUpperCase(l)=='U')
{
System.out.println(l+" is a vowel");
}
else if(Character.isLetter(l))
{
System.out.println(l+" is a consonant");}
else
System.out.println(l+" is an invalid input");
}
}
4.17 Day of the month
Enter the first three letters of a year name to display the number of days in the month.
public class Exercise04_17 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter a year: ");
int year=in.nextInt();
System.out.print("Enter a month: ");
String month=in.next();
if(month.equals("Jan")||month.equals("Mar")||month.equals("May")||month.equals("Jul")||
month.equals("Agu")||month.equals("Oct")||month.equals("Dec"))
{
System.out.println(month+" "+year+" has 31 days");
}
else if(month.equals("Nov")||month.equals("Apr")||month.equals("Jun")||month.equals("Sep"))
{
System.out.println(month+" "+year+" has 30 days");
}
else if(month.equals("Feb")) {
if((year%400==0)||(year%4==0&&year%100!=0)) {
System.out.println(month+" "+year+" has 29 days");}
else {
System.out.println(month+" "+year+" has 28 days");}
}
else {
System.out.println(month+" is not a correct month name");}
}}
4.18 Major and Status of Students
public class Exercise04_18 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter two characters: ");
String s=in.nextLine();
if(s.charAt(0)=='M')
System.out.print("Mathematics ");
else if(s.charAt(0)=='C') {
System.out.print("Computer Science ");
}
else if(s.charAt(0)=='I') {
System.out.print("Information technology ");
}
else {
System.out.print("Invalid input ");
System.exit(1);
}
if(s.charAt(1)=='1') {
System.out.print("Freshman");
}
else if(s.charAt(1)=='2') {
System.out.print("Sophomore ");
}
else if(s.charAt(1)=='3') {
System.out.print("Junior");
}
else if(s.charAt(1)=='4') {
System.out.print("Senior");
}
else {
System.out.print("Invalid status code ");
System.exit(2);
}
}}
4.21 Detect SSN
Social security number, the format is DDD-DD-DDDD, D is a number, and it is judged whether the input is legal.
public class Exercise04_18 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter a SSN: ");
String s=in.nextLine();
if((s.charAt(3)=='-')&&(s.charAt(6)=='-')&&(Character.isDigit(s.charAt(0)))
&&(Character.isDigit(s.charAt(1)))&&(Character.isDigit(s.charAt(2)))
&&(Character.isDigit(s.charAt(4)))&&(Character.isDigit(s.charAt(5)))
&&(Character.isDigit(s.charAt(7)))&&(Character.isDigit(s.charAt(8)))
&&(Character.isDigit(s.charAt(9))))
{
System.out.println(s+" is a valid social security number");
}
else {
System.out.println(s+" is an invalid social security number");
}
}}
4.23 Financial application: honoraria
Output the remuneration statement.
import java.util.Scanner;
public class Exercise04_23 {
public static void main(String[] args) {
String name;
double hours;
double hpay;
double ftaxr;//联邦税率
double staxr;//州税率
System.out.print("Enter employee's name: ");
Scanner in=new Scanner(System.in);
name=in.nextLine();
System.out.print("Enter number of hours worked in a week: ");
hours=in.nextDouble();
System.out.print("Enter hourly pay rate: ");
hpay=in.nextDouble();
System.out.print("Enter federal tax withholding rate: ");
ftaxr=in.nextDouble();
System.out.print("Enter state tax withholding rate: ");
staxr=in.nextDouble();
double total=hours*hpay;
double ftax=ftaxr*total;
double stax=staxr*total;
double alltax=ftax+stax;
double net=total-alltax;
System.out.println("Employee Name: "+name);
System.out.println("Hours Worked: "+hours);
System.out.println("Pay Rate: $"+hpay);
System.out.println("Gross Pay: $"+total);
System.out.println("Deductions:\n"
+ " Federal Withholding ("+(int)(ftaxr*100)/100.0+"%): $"+ftax);
System.out.println(" State Withholding ("+(int)(staxr*100)/100.0+"%): $"+(int)(stax*100)/100.0);
System.out.println(" STotal Deduction: $"+(int)(alltax*100)/100.0);
System.out.print("Net Pay: $"+(int)(net*100)/100.0);
}
}
Supplementary knowledge points:
1. Trigonometric functions
Exercise 4.2 uses the Math.toRadians() method to convert angles into radians,
and also uses the acos() method to calculate arcos inverse trigonometric functions.
Exercise 4.6 uses the Math.toDegrees() method to convert radians to degrees.
2. Read the string
-
If you only need to read one character in a string, you can use the charAt() method.
Example:
String str = "abc";
get character from string
char ch = str.charAt(0); first character
char ch2 = str.charAt(1); second character
ch is a, ch2 is b ; -
When using the next() method, the input string cannot contain spaces.
-
The nextLine() method can read one line of data at a time without being affected by spaces.
-
Java can also use the BufferedReader class to receive strings, and call the readLine() method to obtain strings
3. The method of string conversion case
Exercise 4.11 uses the Character.toUpperCase() method to return a new string in which all letters are capitalized for easy judgment.
Character.isLetter(ch) returns true if the specified character is a letter.
4. exit exit
system.exit(int status) 。
Normal exit:
When the status is 0, the program exits normally, that is, the currently running java virtual machine is terminated.
Abnormal exit:
If the status is other integers other than 0 (including negative numbers, usually 1 or -1), it means that the current program exits abnormally.
5. Keep decimal places
See Example 4.23
(int)(net*100)/100.0) keep 2 decimal places