Hands-on brain interface and inheritance

Source: Internet
Author: User

(1) You can use the instanceof operator to determine whether an object can be converted to the specified type:

See example : Testinstanceof.java

public class Testinstanceof

{

public static void Main (string[] args)

{

when declaring Hello , use the object class, then the compilation type of Hello is object , Object is the parent class of all classes

but The actual type of the Hello variable is String

Object Hello = "Hello";

String is a Subclass of the Object class, so it returns true.

System.out.println (" whether the string is an instance of the Object class:" + (hello instanceof Object));

returns true.

System.out.println (" string is an instance of String class:" + (hello instanceof string));

returns false.

System.out.println (" string is an instance of the Math class:" + (hello instanceof Math));

String implements the comparable interface, so returns true.

System.out.println (" string is an instance of the comparable interface:" + (hello instanceof comparable));

String a = "Hello";

The String class is neither the math class nor the parent class of the math class, so the following code compilation does not pass

System.out.println (" string is an instance of the Math class:" + (a instanceof math));

}

}

(2) Which of the following statements will cause a compilation error ? Why ? which will cause a run-time error ? Why ?

M=d;

D=m;

D= (Dog) m;

D=c;

C= (Cat) m;

First make a self-judgment, draw a conclusion, run the testcast.java Instance code to see if your judgment is correct

Judgment:d=m will make an error, because the base class object cannot be assigned directly to the variables of the subclass;d=c will go wrong because the conversation is not related, cannot be assigned;c= (Cat) m will go wrong, Because the coercion type was converted before.

(3) Hands-on brain: Read and run the aboutexception.java Sample, then follow the next few pages of PPT to learn about Java The basics of implementing exception handling in the

Import javax.swing.*;

Class Aboutexception {

public static void Main (string[] a)

{

int I=1, j=0, K;

k=i/j;

Try

{

K = i/j; Causes Division-by-zero exception

throw new Exception ("hello.exception!");

}

catch (ArithmeticException e)

{

System.out.println (" by 0 " divide . "+ e.getmessage ());

}

catch (Exception e)

{

if (e instanceof arithmeticexception)

System.out.println (" by 0 divide ");

Else

{

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

}

}

Finally

{

Joptionpane.showconfirmdialog (null, "OK");

}

}

}

Operation Result:

(3) Hands-on brain: multilayer anomaly capture -1

Read the following code (Catchwho.java) to write out the results of the program run:

public class Catchwho {

public static void Main (string[] args) {

try {

try {

throw new ArrayIndexOutOfBoundsException ();

}

catch (ArrayIndexOutOfBoundsException e) {

System.out.println ("arrayindexoutofboundsexception" + "/ inner layer try-catch");

}

throw new ArithmeticException ();

}

catch (ArithmeticException e) {

System.out.println (" occurring arithmeticexception");

}

catch (ArrayIndexOutOfBoundsException e) {

System.out.println ("arrayindexoutofboundsexception" + "/ outer try-catch");

}

}

}

(3) Hands-on brain: multilayer anomaly capture -2

Write The results of the Catchwho2.java program run

public class CatchWho2 {

public static void Main (string[] args) {

try {

try {

throw new ArrayIndexOutOfBoundsException ();

}

catch (ArithmeticException e) {

System.out.println ("arrayindexoutofboundsexception" + "/ inner layer try-catch");

}

throw new ArithmeticException ();

}

catch (ArithmeticException e) {

System.out.println (" occurring arithmeticexception");

}

catch (ArrayIndexOutOfBoundsException e) {

System.out.println ("arrayindexoutofboundsexception" + "/ outer try-catch");

}

}

}

(3) Discrimination:finally statement block will be executed?

public class Systemexitandfinally {

public static void Main (string[] args)

{

try{

SYSTEM.OUT.PRINTLN ("in Main");

throw new Exception ("Exception is thrown in main");

System.exit (0);

}

catch (Exception e)

{

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

System.exit (0);

}

Finally

{

System.out.println ("in finally");

}

}

}

The Finally statement block is not necessarily executed.

(7) to write a program, the program at run time requires the user to enter an integer, representing a course of examination results, the program then gives "failed", "Pass", "Medium", "good", "excellent" conclusion.

Requires that the program be robust enough that it does not crash regardless of what the user enters.

Source:

Import Java.util.Scanner;

public class Score {

public static void Main (string[] args) {

TODO auto-generated method stubs

for (;;)

{Try

{

System.out.println (" Please enter your score:");

Scanner s=new Scanner (system.in);

int Sc=s.nextint ();

if (sc>=0&&sc<=100)

{

if (sc<60) {System.out.println (" less than lattice! "); break;}

if (sc<70&&sc>=60) {System.out.println (" Pass! "); break;}

if (sc<80&&sc>=70) {System.out.println (" Medium! "); break;}

if (sc<90&&sc>=80) {System.out.println ("Good ! "); break;}

if (sc<=100&&sc>=90) {System.out.println (" Excellent! "); break;}

}

Else

System.out.println (" you entered the wrong result ! ") Please re-enter ");

}

catch (Exception e)

{

System.out.println (" Sorry! Your operation is wrong! Please re-enter ");

}

}

}

}

Hands-on brain interface and inheritance

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.