Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Constructors, Exercise 01, slide no 49
- */
- /*
- * Adult class
- ******************************************************/
- public class Adult {
- // final class properties
- private static final int NAME_MIN_CHARS = 2; // minimum chars in adult name
- private static final String DEFAULT_NAME = "Missing Name"; // default adult
- // name
- private static final int MINIMUM_HEIGHT = 40; // minimum adult height in cm
- private static final String DEFAULT_PROFISSIN = ""; // default value
- // for adult
- // profession
- // define adult object properties and initialize to valid values
- private String name = Adult.DEFAULT_NAME; // adult name
- private int height = Adult.MINIMUM_HEIGHT; // adult height in cm
- private String profession = Adult.DEFAULT_PROFISSIN; // adult profession or
- // "Unknown"
- // constructors
- public Adult(String name, int height, String profession) {
- super();
- this.setName(name);
- this.setHeight(height);
- this.setProfession(profession);
- }
- public Adult(String name, int height) {
- this(name, height, Adult.DEFAULT_PROFISSIN);
- }
- public Adult(Adult sourceAdult) {
- this.name = sourceAdult.name;
- this.height = sourceAdult.height;
- this.profession = sourceAdult.profession;
- }
- // name getter and setter
- public String getName() {
- return this.name;
- }
- public boolean setName(String name) {
- // if argument is not valid
- if (name.length() < Adult.NAME_MIN_CHARS) {
- return false;
- }
- this.name = name;
- return true;
- }
- // height getter and setter
- public int getHeight() {
- return this.height;
- }
- public boolean setHeight(int height) {
- if (height < Adult.MINIMUM_HEIGHT) {
- return false;
- }
- this.height = height;
- return true;
- }
- // profession getter and setter
- public String getProfession() {
- return profession;
- }
- public boolean setProfession(String profession) {
- this.profession = profession.trim().length() == 0 ? Adult.DEFAULT_PROFISSIN
- : profession;
- return true;
- }
- @Override
- public String toString() {
- String professionString = this.profession.length() > 0 ? String.format(
- ", profession=%s", this.profession) : "";
- return String.format("Adult [name=%s, height=%d%s]", this.name,
- this.height, professionString);
- }
- }
- /*
- * AdultTester class - main()
- ******************************************************/
- public class AdultTester {
- public static void main(String[] args) {
- Adult p1 = new Adult("Mohamad", 180, "Teacher");
- Adult p2 = new Adult("tamar", 150);
- Adult p3 = new Adult(p1);
- System.out.println("person 1: " + p1.toString());
- System.out.println("person 2: " + p2.toString());
- System.out.println("person 3: " + p3.toString());
- // update p1 profession
- if (p1.setProfession("employee")) {
- System.out.println("Success: p1 proession updated.");
- } else {
- System.out.println("Error: failed to update p1 profession!");
- }
- System.out.println("person 1: " + p1.toString());
- System.out.println("person 2: " + p2.toString());
- System.out.println("person 3: " + p3.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement