// 很好的例子,因为compareTo只是Integer和Double的方法,并不是Number的方法publicclassTest{publicstaticvoidmain(String[] args){Number x =newInteger(3);System.out.println(x.intValue());System.out.println(x.compareTo(newInteger(4)));}}
13.8 下面代码中有什么错误?
// 很好的例子,11章讲过,“.”是成员对象访问符,运算顺序优先于类型转化。所以编译器会先检查x.compareTo,自然不通过,理由在上一题//应该改成((Integer)x).compareTo(new Integer(4))publicclassTest{publicstaticvoidmain(String[] args){Number x =newInteger(3);System.out.println(x.intValue());System.out.println((Integer)x.compareTo(newInteger(4)));}}
13.4 章节习题
13.9 可以使用Calendar类来创建一个Calendar对象吗?
不可以,Calendar是一个抽象类
13.10 Calendar类中哪个方法是抽象的?
add方法
13.11 如何为当前时间创建一个 Calendar 对象?
GregorianCalendar的无参构造方法
13.12 对于一个 Calendar 对象 c 而言,如何得到它的年、月、日期 、小时 、分钟以及秒钟?
packagelearning;importjava.util.Calendar;importjava.util.GregorianCalendar;publicclassTest{publicstaticvoidmain(String[] args){Calendar c =newGregorianCalendar();System.out.println(c.get(Calendar.YEAR));System.out.println(c.get(Calendar.MONTH));System.out.println(c.get(Calendar.DAY_OF_MONTH));System.out.println(c.get(Calendar.HOUR));System.out.println(c.get(Calendar.MINUTE));System.out.println(c.get(Calendar.SECOND));}}
ArrayList<String> list =newArrayList<>();
list.add("New York");ArrayList<String> list1 = list;ArrayList<String> list2 =(ArrayList<String>)(list.clone());
list.add("Atlanta");System.out.println(list == list1);System.out.println(list == list2);System.out.println("list is "+ list);System.out.println("list1 is "+ list1);System.out.println("list2.get(0) is "+ list2.get(0));System.out.println("list2.size() is "+ list2.size());
true
false
list is [New York, Atlanta]
list1 is [New York, Atlanta]
list2.get(0) is New York
list2.size() is 1
13.26 下面的代码有什么错误?
// 实现接口,重写方法publicclassTest{publicstaticvoidmain(String[] args){GeometricObject x =newCircle(3);GeometricObject y = x.clone();System.out.println(x == y);}}
packagelearning;//__________________________UML DIAGRAM_____________________________*/* |
* /GeometricObject/ |
*-------------------------------------------------------------------|
* color: String |
* filled: boolean |
*-------------------------------------------------------------------|
* /getArea()/: double/ |
* /getPerimeter(): double/ |
*___________________________________________________________________|
^
^
^
//______________________________________________________________________________________________|
/* |
* Triangle |
* ----------------------------------------------------------------------------------------------|
* side1: double |
* side2: double |
* side3: double |
* ----------------------------------------------------------------------------------------------|
* Triangle() |
* Triangle(side1:double, side2:double, side3:double, color:String, filled:boolean) |
* getPerimeter(): double |
* getArea(): double |
* +toString(): String |
*_______________________________________________________________________________________________|*/classTriangleextendsGeometricObject{double side1;double side2;double side3;Triangle(){}Triangle(double side1,double side2,double side3,String color,boolean filled){this.side1 = side1;this.side2 = side2;this.side3 = side3;this.color = color;this.filled = filled;}@OverridedoublegetPerimeter(){return side1 + side2 + side3;}@OverridedoublegetArea(){returnMath.pow((getPerimeter()*0.5)*(getPerimeter()- side1)*(getPerimeter()- side2)*(getPerimeter()- side3),0.5);}@OverridepublicStringtoString(){if(filled ==true){return"The Perimeter is "+getPerimeter()+" and the area is "+getArea()+".\nThis triangle is "+ color +" and filled.";}return"The Perimeter is "+getPerimeter()+" and the area is "+getArea()+".\nThis triangle is "+ color +" and not filled.";}}
输出结果:
Input 3 sides of a triangle accordingly: 3 4 5
Input the color: blue
Input the fill status(true / false): true
The Perimeter is 12.0 and the area is 54.99090833947008.
This triangle is blue and filled.
*13.2 (打乱ArrayList)
编写以下方法,打乱ArrayList里面保存的数字
publicstaticvoidshuffle(ArrayList<Number> list)
packagelearning;importjava.util.ArrayList;// 代码的大意是,把初始的list数组创建为1-10的顺序数列,再使用temp数组随机生成0-9的乱序数列,用temp的元素作为list的参数,依次传递给afterShuffle数组,所以打乱完成// 例如,temp的第一个元素是5,那么就把原数组list中的第5号元素,挪动到新数组afterShuffle中,相当于temp数组充当了参数publicclassTest{// 用户可以设置希望打乱多大的数组,本例中设置为10privatefinalstaticint SIZE =10;publicstaticvoidmain(String[] args){// 把1-10按顺序加进初始list数组ArrayList<Number> list =newArrayList<>();for(int i =1; i <= SIZE; i++){
list.add(i);}System.out.print("The original array is: ");System.out.println(list.toString());// 执行打乱方法shuffle(list);}publicstaticvoidshuffle(ArrayList<Number> list){// 创建一个暂时的数组temp,用于储存随机的参数ArrayList<Number> temp =newArrayList<>();// 只要这个temp的长度没有超过用户设置的“10”,就执行循环,添加新的随机参数到数组中while(temp.size()< SIZE){// 随机生成一个0 - 10的数字,如果这个数字没有被添加过,就把这个数字添加进去// 循环结束后,temp数组由0-9的乱序数列组成int index =(int)(Math.random()* SIZE);if(!temp.contains(index)){
temp.add(index);}}ArrayList<Number> afterShuffle =newArrayList<>();/* 循环解释:
* 假设
* list = [1, 2, 3, 4, 5]
* temp = [3, 1, 4, 5, 2]
* 那么
* 把temp[0]元素3拿出来,执行list.get(3),结果是4,把结果赋值给afterShuffle的第一位元素,因此afterShuffle的第一位元素是4
* 把temp[1]元素1拿出来,执行list.get(1),结果是2,把结果赋值给afterShuffle的第一位元素,因此afterShuffle的第二位元素是2
* ......以此类推
*/for(int i =0; i < SIZE; i++){//这里需要类型转换的原因是,内圈temp.get(i)得到的数字类型是Number,而外圈list.get()方法需要一个Integer参数
afterShuffle.add(list.get((int) temp.get(i)));}System.out.print("The shuffled array is: ");System.out.println(afterShuffle.toString());}}
输出结果:
The original array is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The shuffled array is: [9, 5, 2, 3, 8, 1, 4, 6, 7, 10]
*13.3 (排序ArrayList)
编写以下方法,对 ArrayList 里面保存的数字进行排序。
publicstaticvoidsort(ArrayList<Number> list)
packagelearning;importjava.util.ArrayList;importjava.util.Arrays;//使用冒泡排序,在方法内先把ArrayList转化成int[]publicclassTest{publicstaticvoidmain(String[] args){ArrayList<Number> list =newArrayList<>();
list.add(5);
list.add(9);
list.add(2);
list.add(7);
list.add(3);System.out.print("初始排序 :");System.out.println(list.toString());sort(list);}publicstaticvoidsort(ArrayList<Number> list){int[] arr =newint[list.size()];for(int i =0; i < list.size(); i++){
arr[i]=(int)list.get(i);}for(int i = arr.length -1; i >0; i--){//外圈决定循环次数:规律是(length-1)次for(int j =0; j < i; j++){// 内圈决定交换次数,规律一个length为n的数组只需交换(n-1)次(i次,j初始值为0所以无“=”)即可实现最大值冒泡if(arr[j]> arr[j +1]){// 交换int temp = arr[j];
arr[j]= arr[j +1];
arr[j +1]= temp;}}}System.out.println("最终排序 :"+Arrays.toString(arr));//for外只输出最终数组}}
packagelearning;importjava.util.Calendar;importjava.util.GregorianCalendar;publicclassTest{/** Main method */publicstaticvoidmain(String[] args){Calendar calendar =newGregorianCalendar();if(args.length ==2){printMonth(Integer.parseInt(args[1]),Integer.parseInt(args[0]));}if(args.length ==1){printMonth(calendar.get(Calendar.YEAR),Integer.parseInt(args[0]));}if(args.length ==0){printMonth(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1);}}/** Print the calendar for a month in a year */publicstaticvoidprintMonth(int year,int month){// Print the headings of the calendarprintMonthTitle(year, month);// Print the body of the calendarprintMonthBody(year, month);}/** Print the month title, e.g., May, 1999 */publicstaticvoidprintMonthTitle(int year,int month){System.out.println(" "+getMonthName(month)+" "+ year);System.out.println("-----------------------------");System.out.println(" Sun Mon Tue Wed Thu Fri Sat");}/** Get the English name for the month */publicstaticStringgetMonthName(int month){String monthName ="";switch(month){case1: monthName ="January";break;case2: monthName ="February";break;case3: monthName ="March";break;case4: monthName ="April";break;case5: monthName ="May";break;case6: monthName ="June";break;case7: monthName ="July";break;case8: monthName ="August";break;case9: monthName ="September";break;case10: monthName ="October";break;case11: monthName ="November";break;case12: monthName ="December";}return monthName;}/** Print month body */publicstaticvoidprintMonthBody(int year,int month){// Get start day of the week for the first date in the monthint startDay =getStartDay(year, month);// Get number of days in the monthint numberOfDaysInMonth =getNumberOfDaysInMonth(year, month);// Pad space before the first day of the monthint i =0;for(i =0; i < startDay; i++)System.out.print(" ");for(i =1; i <= numberOfDaysInMonth; i++){System.out.printf("%4d", i);if((i + startDay)%7==0)System.out.println();}System.out.println();}/** Get the start day of month/1/year */publicstaticintgetStartDay(int year,int month){finalint START_DAY_FOR_JAN_1_1800 =3;// Get total number of days from 1/1/1800 to month/1/yearint totalNumberOfDays =getTotalNumberOfDays(year, month);// Return the start day for month/1/yearreturn(totalNumberOfDays + START_DAY_FOR_JAN_1_1800)%7;}/** Get the total number of days since January 1, 1800 */publicstaticintgetTotalNumberOfDays(int year,int month){int total =0;// Get the total days from 1800 to 1/1/yearfor(int i =1800; i < year; i++)if(isLeapYear(i))
total = total +366;else
total = total +365;// Add days from Jan to the month prior to the calendar monthfor(int i =1; i < month; i++)
total = total +getNumberOfDaysInMonth(year, i);return total;}/** Get the number of days in a month */publicstaticintgetNumberOfDaysInMonth(int year,int month){if(month ==1|| month ==3|| month ==5|| month ==7||
month ==8|| month ==10|| month ==12)return31;if(month ==4|| month ==6|| month ==9|| month ==11)return30;if(month ==2)returnisLeapYear(year)?29:28;return0;// If month is incorrect}/** Determine if it is a leap year */publicstaticbooleanisLeapYear(int year){return year %400==0||(year %4==0&& year %100!=0);}}
packagelearning;classSquareextendsGeometricObjectimplementsColorable{double sides;Square(){}Square(double sides){this.sides = sides;}@OverridepublicvoidhowToColor(){System.out.println("Color all four sides");}}
packagelearning;publicclassRationalextendsNumberimplementsComparable<Rational>{// Data fields for numerator and denominatorprivatelong numerator =0;privatelong denominator =1;/** Construct a rational with default properties */publicRational(){this(0,1);}/** Construct a rational with specified numerator and denominator */publicRational(long numerator,long denominator){long gcd =gcd(numerator, denominator);this.numerator =(denominator >0?1:-1)* numerator / gcd;this.denominator =Math.abs(denominator)/ gcd;}/** Find GCD of two numbers */privatestaticlonggcd(long n,long d){long n1 =Math.abs(n);long n2 =Math.abs(d);int gcd =1;for(int k =1; k <= n1 && k <= n2; k++){if(n1 % k ==0&& n2 % k ==0)
gcd = k;}return gcd;}/** Return numerator */publiclonggetNumerator(){return numerator;}/** Return denominator */publiclonggetDenominator(){return denominator;}/** Add a rational number to this rational */publicRationaladd(Rational secondRational){long n = numerator * secondRational.getDenominator()+
denominator * secondRational.getNumerator();long d = denominator * secondRational.getDenominator();returnnewRational(n, d);}/** Subtract a rational number from this rational */publicRationalsubtract(Rational secondRational){long n = numerator * secondRational.getDenominator()- denominator * secondRational.getNumerator();long d = denominator * secondRational.getDenominator();returnnewRational(n, d);}/** Multiply a rational number to this rational */publicRationalmultiply(Rational secondRational){long n = numerator * secondRational.getNumerator();long d = denominator * secondRational.getDenominator();returnnewRational(n, d);}/** Divide a rational number from this rational */publicRationaldivide(Rational secondRational){long n = numerator * secondRational.getDenominator();long d = denominator * secondRational.numerator;returnnewRational(n, d);}@OverridepublicStringtoString(){if(denominator ==1)return numerator +"";elsereturn numerator +"/"+ denominator;}@Override// Override the equals method in the Object class publicbooleanequals(Object other){if((this.subtract((Rational)(other))).getNumerator()==0)returntrue;elsereturnfalse;}@Override// Implement the abstract intValue method in Number publicintintValue(){return(int)doubleValue();}@Override// Implement the abstract floatValue method in Number publicfloatfloatValue(){return(float)doubleValue();}@Override// Implement the doubleValue method in Number publicdoubledoubleValue(){return numerator *1.0/ denominator;}@Override// Implement the abstract longValue method in NumberpubliclonglongValue(){return(long)doubleValue();}@Override// Implement the compareTo method in ComparablepublicintcompareTo(Rational o){if(this.subtract(o).getNumerator()>0)return1;elseif(this.subtract(o).getNumerator()<0)return-1;elsereturn0;}}
a + bi + c + di = (a + c) + (b + d)i
a + bi - (c + di) = (a - c) + (b - d)i
(a + bi)*(c + di) = (ac - bd) + (bc + ad)i
(a + bi)/(c + di) = (ac + bd)/(c2 + d2) + (bc - ad)i/(c^2 + d^2)
还可以使用下面的公式得到复数的绝对值:
∣ a + b i ∣ = a 2 + b 2 |a + bi| = \sqrt{a^2 + b^2} ∣a+bi∣=a2+b2
packagelearning;classComplex{publicdouble a;publicdouble b;//会依次调用之后的两个构造方法,注意如果没有传入值,默认0.0publicComplex(){this(0);}publicComplex(double a){this(a,0);}publicComplex(double a,double b){this.a = a;this.b = b;}publicComplexadd(Complex c2){double c = c2.a;double d = c2.b;double complex1 = a + c;double complex2 = b + d;returnnewComplex(complex1, complex2);}publicComplexsub(Complex c2){double c = c2.a;double d = c2.b;double complex1 = a - c;double complex2 = b - d;returnnewComplex(complex1, complex2);}publicComplexmul(Complex c2){double c = c2.a;double d = c2.b;double complex1 = a * c - b * d;double complex2 = b * c + a * d;returnnewComplex(complex1, complex2);}publicComplexdiv(Complex c2){double c = c2.a;double d = c2.b;double complex1 =(a * c + b * d)/(c * c + d * d);double complex2 =(b * c - a * d)/(c * c + d * d);returnnewComplex(complex1, complex2);}publicdoubleabs(){returnMath.pow(a * a + b * b,0.5);}publicdoublegetRealPart(){return a;}publicdoublegetImaginaryPart(){return b;}@Override// 输出对象时,会自动调用toStringpublicStringtoString(){if(b ==0)return a +"";elsereturn a +" + "+ b +"i";}}
packagelearning;importjava.util.Scanner;publicclassTest{publicstaticvoidmain(String[] args){Scanner in =newScanner(System.in);System.out.print("Enter a decimal number: ");String decVal = in.next();try{System.out.println("The fraction number is "+decimalToFraction(decVal));}catch(Exception e){System.out.println(e.getMessage());}
in.close();}privatestaticStringdecimalToFraction(String val)throwsException{boolean isNegative = val.startsWith("-");String[] decimalNumberParts = val.split("\\.");if(decimalNumberParts.length <2){thrownewException("You must enter a decimal number like: 123.12");}if(val.startsWith("-")){
isNegative =true;}Rational leftSideOfDecimal =newRational(Long.parseLong(decimalNumberParts[0]));String denominatorRightSide ="1";for(int i =0; i < decimalNumberParts[1].length(); i++){
denominatorRightSide +="0";}Rational rightSideOfDecimal =newRational(Long.parseLong(decimalNumberParts[1]),Long.parseLong(denominatorRightSide));Rational result = leftSideOfDecimal.add(rightSideOfDecimal);return(isNegative ?"-":"")+ result.toString();}}
输出结果:
Enter a decimal number: 3.25
The fraction number is 13/4
packagelearning;importjava.util.Scanner;publicclassTest{publicstaticvoidmain(String[] args){Scanner in =newScanner(System.in);System.out.print("Enter a, b, c (as integers): ");int a = in.nextInt();int b = in.nextInt();int c = in.nextInt();Rational aRational =newRational(a);Rational bRational =newRational(b);Rational cRational =newRational(c);Rational h =newRational(bRational.multiply(newRational(-1)).longValue(),
aRational.multiply(newRational(2)).longValue());Rational k = aRational.multiply(h.multiply(h)).add(bRational.multiply(h)).add(cRational);System.out.println("h is "+ h +", k is "+ k);}}
输出结果:
Enter a, b, c (as integers): 1 3 1
h is -3/2, k is -5/4
用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具。 tcpdump可以将网络中传送的数据包的“头”完全截获下来提供分析。它支 持针对网络层、协议、主机、网络或端口的过滤,并提供and、or、not等逻辑语句来帮助你去掉无用的信息。
实用命令实例
默认启动
tcpdump
普通情况下,直
MO= Mobile originate,上行,即用户上发给SP的信息。MT= Mobile Terminate,下行,即SP端下发给用户的信息;
上行:mo提交短信到短信中心下行:mt短信中心向特定的用户转发短信,你的短信是这样的,你所提交的短信,投递的地址是短信中心。短信中心收到你的短信后,存储转发,转发的时候就会根据你填写的接收方号码寻找路由,下发。在彩信领域是一样的道理。下行业务:由SP
import java.util.Arrays;
import java.util.Random;
public class MinKElement {
/**
* 5.最小的K个元素
* I would like to use MaxHeap.
* using QuickSort is also OK
*/
public static void
添加没有默认值:alter table Test add BazaarType char(1)
有默认值的添加列:alter table Test add BazaarType char(1) default(0)
删除没有默认值的列:alter table Test drop COLUMN BazaarType
删除有默认值的列:先删除约束(默认值)alter table Test DRO
Spring Boot 1.2.4已于6.4日发布,repo.spring.io and Maven Central可以下载(推荐使用maven或者gradle构建下载)。
这是一个维护版本,包含了一些修复small number of fixes,建议所有的用户升级。
Spring Boot 1.3的第一个里程碑版本将在几天后发布,包含许多