Advertisement
mmayoub

Constructors, ex01, slide 49

Jul 11th, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. /*
  2.  * Constructors, Exercise 01, slide no 49
  3.  */
  4.  
  5. /*
  6.  * Adult class
  7.  ******************************************************/
  8. public class Adult {
  9.     // final class properties
  10.     private static final int NAME_MIN_CHARS = 2; // minimum chars in adult name
  11.     private static final String DEFAULT_NAME = "Missing Name"; // default adult
  12.                                                                 // name
  13.     private static final int MINIMUM_HEIGHT = 40; // minimum adult height in cm
  14.     private static final String DEFAULT_PROFISSIN = ""; // default value
  15.                                                         // for adult
  16.                                                         // profession
  17.  
  18.     // define adult object properties and initialize to valid values
  19.     private String name = Adult.DEFAULT_NAME; // adult name
  20.     private int height = Adult.MINIMUM_HEIGHT; // adult height in cm
  21.     private String profession = Adult.DEFAULT_PROFISSIN; // adult profession or
  22.                                                             // "Unknown"
  23.  
  24.     // constructors
  25.     public Adult(String name, int height, String profession) {
  26.         super();
  27.         this.setName(name);
  28.         this.setHeight(height);
  29.         this.setProfession(profession);
  30.     }
  31.  
  32.     public Adult(String name, int height) {
  33.         this(name, height, Adult.DEFAULT_PROFISSIN);
  34.     }
  35.  
  36.     public Adult(Adult sourceAdult) {
  37.         this.name = sourceAdult.name;
  38.         this.height = sourceAdult.height;
  39.         this.profession = sourceAdult.profession;
  40.     }
  41.  
  42.     // name getter and setter
  43.     public String getName() {
  44.         return this.name;
  45.     }
  46.  
  47.     public boolean setName(String name) {
  48.         // if argument is not valid
  49.         if (name.length() < Adult.NAME_MIN_CHARS) {
  50.             return false;
  51.         }
  52.         this.name = name;
  53.         return true;
  54.     }
  55.  
  56.     // height getter and setter
  57.     public int getHeight() {
  58.         return this.height;
  59.     }
  60.  
  61.     public boolean setHeight(int height) {
  62.         if (height < Adult.MINIMUM_HEIGHT) {
  63.             return false;
  64.         }
  65.         this.height = height;
  66.         return true;
  67.     }
  68.  
  69.     // profession getter and setter
  70.     public String getProfession() {
  71.         return profession;
  72.     }
  73.  
  74.     public boolean setProfession(String profession) {
  75.         this.profession = profession.trim().length() == 0 ? Adult.DEFAULT_PROFISSIN
  76.                 : profession;
  77.         return true;
  78.     }
  79.  
  80.     @Override
  81.     public String toString() {
  82.         String professionString = this.profession.length() > 0 ? String.format(
  83.                 ", profession=%s", this.profession) : "";
  84.  
  85.         return String.format("Adult [name=%s, height=%d%s]", this.name,
  86.                 this.height, professionString);
  87.  
  88.     }
  89. }
  90.  
  91.  
  92. /*
  93.  * AdultTester class - main()
  94.  ******************************************************/
  95.  
  96. public class AdultTester {
  97.     public static void main(String[] args) {
  98.         Adult p1 = new Adult("Mohamad", 180, "Teacher");
  99.         Adult p2 = new Adult("tamar", 150);
  100.         Adult p3 = new Adult(p1);
  101.  
  102.         System.out.println("person 1: " + p1.toString());
  103.         System.out.println("person 2: " + p2.toString());
  104.         System.out.println("person 3: " + p3.toString());
  105.  
  106.         // update p1 profession
  107.         if (p1.setProfession("employee")) {
  108.             System.out.println("Success: p1 proession updated.");
  109.         } else {
  110.             System.out.println("Error: failed to update p1 profession!");
  111.         }
  112.  
  113.         System.out.println("person 1: " + p1.toString());
  114.         System.out.println("person 2: " + p2.toString());
  115.         System.out.println("person 3: " + p3.toString());
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement