Android Training (Java) Day eighth-common classes (bottom) and exceptions

Source: Internet
Author: User
Tags getmessage

  1. Math class: All the methods inside are static;

    Common methods:

    ABS Absolute

    sqrt square root

    Pow (double A, double b) A's power of B

    Max (double A, double b) Compares two numbers who are big

    Min (double A, double b) Compare two numbers who are small

    Random () returns a number from 0.0 to 1.0, but cannot fetch 1

    Long round (double a) double type data A is converted to long (rounded)


  2. Random pseudo-Random class (all methods are from Math.random () this method)

    Common construction Methods: New Random ();

    Common methods: nextint (int i) 0 to i-1 return type is int type;

    Note: When doing the project, generally use pseudo-random class, because the method inside it is more flexible, but if the analysis from the memory angle, consider choosing math.random ();

  3. Date class: Java.util.Date: Gets the current system time; System.out.println (new Date ()); At this time, according to the system format;


  4. Format--dateformat--simpledateformat; A format that represents a date

    SimpleDateFormat commonly used construction methods: New SimpleDateFormat (String regx);//date format;

    Common methods of SimpleDateFormat:

    Format (date date); return string type;

    Parse (string str); Returns the date type (parsing the given string str into a date format, which must be consistent with REGX)

    Common fields:

    650) this.width=650; "Src=" http://note.youdao.com/yws/public/resource/719ed66494263eb983dad4d9b8549a6b/ 66725b906b6e4da8b02a7e6d1fdf0ab1/5c086b8046d54e8c8646a8809300788e "style=" white-space:normal;border:none;height : auto; "alt=" 5c086b8046d54e8c8646a8809300788e "/>


  5. The calendar is an abstract class:

    Object creation: Calendar c=calendar.getinstance (); Get current system time

    Common methods:

    Get (int a); Returns the int type, a represents the field

    650) this.width=650; "Src=" http://note.youdao.com/yws/public/resource/719ed66494263eb983dad4d9b8549a6b/ 66725B906B6E4DA8B02A7E6D1FDF0AB1/50B5A1D9C39F4ADDAEF0475162C9E4FC "style=" Border:none;line-height:1.5;height: auto; "alt=" 50B5A1D9C39F4ADDAEF0475162C9E4FC "/>

    650) this.width=650; "Src=" http://note.youdao.com/yws/public/resource/719ed66494263eb983dad4d9b8549a6b/ 66725b906b6e4da8b02a7e6d1fdf0ab1/a429e8c2fe554f3aa33045270094948f "style=" Border:none;height:auto; "alt=" a429e8c2fe554f3aa33045270094948f "/>

    650) this.width=650; "Src=" http://note.youdao.com/yws/public/resource/719ed66494263eb983dad4d9b8549a6b/ 66725b906b6e4da8b02a7e6d1fdf0ab1/a1ae66f8922f457db3bba1f30289ff86 "style=" Border:none;height:auto; "alt=" A1ae66f8922f457db3bba1f30289ff86 "/>

    650) this.width=650; "Src=" http://note.youdao.com/yws/public/resource/719ed66494263eb983dad4d9b8549a6b/ 66725b906b6e4da8b02a7e6d1fdf0ab1/e20ac053358e4b8da7f213a7b1af4c7b "style=" Border:none;height:auto; "alt=" e20ac053358e4b8da7f213a7b1af4c7b "/>



    SetTime (date date), no return value;

    GetTime (); Returns the date type;

    Add (the number of days in the current month, the difference in days); You can output the current time of the forward or backward date


  6. System (evolved from C + +)

    Common methods:

    ArrayCopy (5 parameters)

    Exit (int i), when i=0, indicates normal exit (Java Virtual machine), otherwise abnormal exit;

    Currenttimemillis (); it can be used to test the execution time of the program, noting that the unit is milliseconds;


  7. Exception is not normal

    Classification: Understanding between compile-time exceptions, run-time exceptions, and errors:

    Error: Called a bug, generated and thrown by a Java virtual machine, including dynamic link failure, virtual machine error, and so on, the program does not handle it.

    Exception: The parent class of all exception classes whose subclasses correspond to a wide variety of possible exception events, generally requiring the user to be declared or captured.

    Runtime Exception: A special kind of exception, such as by 0, array subscript beyond the range, it produces more frequent, processing trouble, if the display of the declaration or capture will have a significant impact on program readability and operational efficiency. As a result, systems are automatically detected and handed over to the exception handlers (which the user does not have to handle).

    Grammar:

    try{

    < try to capture exception information >

}catch (Exception class exception object name) {

< captures exception information (Printstacktrace ();), and can also customize exception information (System.out.println (..)) >

}finally{

< is bound to execute a statement block >

}

public void F () throws exception class name {}//similar to Try{}catch () {}, but cannot customize exception information

public void F () {

Throw exception class object; Be sure to throw exception information at this time

}

Precautions:

try{

}catch (Exception Class 1) {


}catch (Exception Class 2) {


}...

Exception class 2> Exception Class 1, subclass in front, parent class behind

Exception Parent class: Class Exception extends throwable{};

GetMessage (); The return value is derived from the formal parameter inside the construction method;

Cases:

public void F () {

try{

throw new Exception ("abc");

}catch (Exception e) {

System.out.println (E.getmessage ()); Output ABC

}

}


After-school exercises:

Known to have an array of int[] a={1,2,3,4,5,6,7,8,9};

(1) Use random to do one such verification code: random number 1+ random number 2 =? For example: 1+2=?

(2) Calculate its value and use scanner input value to determine whether the input value is correct;


Code:

public class yangzhengma{

public static void Main (string[] args) {

Int[] a={1,2,3,4,5,6,7,8,9};

Scanner s = new Scanner (system.in);

Random r = new Random ();

for (int i = 0; i < 2; i++) {

A[i] = R.nextint (9);

}

while (true) {

System.out.print (A[0] + "+" + a[1] + "=");

if (A[0] + a[1] = = S.nextint ()) {

System.out.println ("OK");

Break

}else{

System.out.println ("Re-transmission");

}

}

}

}






This article is from the "Kun" blog, please be sure to keep this source http://linyingkun.blog.51cto.com/2393912/1575177

Android Training (Java) Day eighth-common classes (bottom) and exceptions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.