Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Person;
- class Person {
- String name;
- int age;
- String email;
- public Person(String name, int age) {
- try {
- this.setName(name);
- } catch (Exception e) {
- e.printStackTrace();
- }
- try {
- this.setAge(age);
- } catch (Exception e) {
- e.printStackTrace();
- }
- this.setEmail("no email");
- }
- public Person(String name, int age, String email) {
- try {
- this.setName(name);
- } catch (Exception e) {
- e.printStackTrace();
- }
- try {
- this.setAge(age);
- } catch (Exception e) {
- e.printStackTrace();
- }
- this.setEmail(email);
- }
- public String getName() {
- return name;
- }
- public void setName(String name) throws Exception {
- if (name.equals(null) || name.equals("")) {
- throw new Exception("Invalid name");
- }
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) throws Exception {
- this.age = age;
- if (age < 1 || age > 100) {
- throw new Exception("Invalid age");
- }
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- if (email.equals(null)) {
- this.email = " ";
- } else {
- this.email = email;
- }
- }
- public void toString(String name, int age, String email) {
- System.out.println(getName() + " is " + getAge() + " years old and has email:" + getEmail());
- }
- }
- public class Main {
- public static void main(String[] args) {
- Person pesho = new Person("Pesho", 244, "pesho@abv.bg");
- Person gosho = new Person("Gosho", 30);
- pesho.toString(pesho.getName(), pesho.getAge(), pesho.getEmail());
- gosho.toString(gosho.getName(), gosho.getAge(), gosho.getEmail());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement