Advertisement
mmayoub

classes, Person class, Exercise 03 slide 48

Jul 8th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. package class170706;
  2.  
  3. public class Person {
  4.     // properties
  5.     private String name; // person name, 2 letters at least
  6.     private int height; // person height in cm, positive value
  7.     private double weight; // person weight in kgm, positive value
  8.  
  9.     // get person name
  10.     public String getName() {
  11.         return name;
  12.     }
  13.  
  14.     // set person name
  15.     public boolean setName(String name) {
  16.         // if argument is invalid
  17.         if (name.length() < 2) {
  18.             // no changes, return false
  19.             return false;
  20.         }
  21.  
  22.         // update person name
  23.         this.name = name;
  24.  
  25.         // return true
  26.         return true;
  27.     }
  28.  
  29.     // get person height (cm)
  30.     public int getHeight() {
  31.         return height;
  32.     }
  33.  
  34.     // set person height in cm, should be positive value
  35.     public boolean setHeight(int height) {
  36.         // if invalid argument
  37.         if (height <= 0) {
  38.             // no changes to person, return false
  39.             return false;
  40.         }
  41.  
  42.         // update person height and return true
  43.         this.height = height;
  44.         return true;
  45.     }
  46.  
  47.     // get person weight (in kg)
  48.     public double getWeight() {
  49.         return weight;
  50.     }
  51.  
  52.     // set person weight in kg, should be positive
  53.     public boolean setWeight(double weight) {
  54.         // if invalid argument
  55.         if (weight <= 0) {
  56.             // return false , no changes
  57.             return false;
  58.         }
  59.  
  60.         // update weight and return true
  61.         this.weight = weight;
  62.         return true;
  63.     }
  64.  
  65.     @Override
  66.     public String toString() {
  67.         return String.format(
  68.                 "Person [name=%s, height=%d (cm), weight=%1f (kg)]", this.name,
  69.                 this.height, this.weight);
  70.     }
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement