Home > Back-end >  A Java topics (beginner)
A Java topics (beginner)

Time:09-26

Suppose you have an Animal Animal are defined as follows:

The class Animal {

Public String name;

Public int the age;

Public Animal (String name, int age) {

This. Name=name;

Enclosing the age=age;

}

Public void introduce () {

System. The out. Println (" My name is "+ name +", My age is "+ age);

}

Public void sleep () {

System. Out.println (name + "is sleeping now!" );

}

}

When the instantiation Animal, if age age<0, throws an AgeException abnormalities, try to create this exception class, and rewrite the above Animal class constructor, make it throws a AgeException abnormalities, in addition to write the code from the keyboard input data, create two instance objects, verify this exception, for example, the program runs according to the following:

My name is Tom, My age is 2

Jerry is younger than zero

CodePudding user response:

1, create AgeException Exception class inheritance Exception class
In the construction of the Animal class 2, the method to judge the incoming age if less than 0 will throw AgeException abnormal
3, the keyboard input use Scanner to obtain data into the constructor

To you, do it yourself, you can learn

CodePudding user response:

reference 1st floor also night reply:
1, create AgeException Exception class inheritance Exception class
In the construction of the Animal class 2, the method to judge the incoming age if less than 0 will throw AgeException abnormal
3, the keyboard input use Scanner to obtain data into the constructor

To you, do it yourself, you can learn

Thank you have done
  • Related