deyanmalinov

Problem 1. Person

Jun 25th, 2020
790
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package DPM;
  2. import java.util.Scanner;
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.         String name = scan.nextLine();
  7.         int age = Integer.parseInt(scan.nextLine());
  8.         try {
  9.             Child child = new Child(name, age);
  10.             System.out.println(child.toString());
  11.         }catch (IllegalArgumentException msg ){
  12.             System.out.println(msg.getMessage());
  13.         }
  14.     }
  15. }
  16. -----------------------------------
  17. package DPM;
  18. public class Person {
  19.     private int age;
  20.     private String name;
  21.     public Person(String name, int age){
  22.         this.setName(name);
  23.         this.setAge(age);
  24.     }
  25.     public void setAge(int age) {
  26.         if (age < 1) {
  27.             throw new IllegalArgumentException("Age must be positive!");
  28.         }
  29.         this.age = age;
  30.     }
  31.     public void setName(String name){
  32.         if (name.length() < 3){
  33.             throw new IllegalArgumentException("Name's length should not be less than 3 symbols!");
  34.         }
  35.         this.name=name;
  36.     }
  37.     public int getAge(){
  38.         return age;
  39.     }
  40.     public String getName() {
  41.         return name;
  42.     }
  43.     @Override
  44.     public String toString() {
  45.         return String.format("Name: %s, Age: %d", this.getName(), this.getAge());
  46.     }
  47. }
  48. ------------------------------------------------------
  49. package DPM;
  50. public class Child extends Person{
  51.     public Child (String name, int age){
  52.         super(name, age);
  53.     }
  54.     public void setAge(int age){
  55.         if (age > 15 ){
  56.             throw new IllegalArgumentException("Child's age must be lesser than 15!");
  57.         }
  58.         super.setAge(age);
  59.     }
  60. }
RAW Paste Data