<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/*************************
 * Encapsulation example *
 *************************/

class Person { // declare public if used in another file

  /*****************************
   * instance variables (hide) *
   *****************************/
  
   private String firstname;
   private String lastname;
  
  /****************
   * constructors *
   ****************/
   Person() { }

  /**************************
   * utility methods (hide) *
   **************************/

  // this method adds a string to blanks spaces followed by another string
   
   private String addstrings(int spaces, String start, String stop) {

    // create middle string of blank spaces
     String middle = "";
     for(int s = 1; s &lt;= spaces; s++)
       middle = middle + " ";
     return start + middle + stop;

  } // method addstrings

  /*******************
   * service methods *
   *******************/

   public void set_firstname(String name) {
     firstname = name;
   } // method set_firstname

   public void set_lastname(String name) {
     lastname = name;
   } // method set_lastname

   public String get_firstname() {
     return firstname;
   } // method get_firstname

   public String get_lastname() {
     return lastname;
   } // method get_lastname
    
   public String get_fullname() {
     String fn = get_firstname();
     String ln = get_lastname();
     int spaces = 1;
     return addstrings(spaces, fn, ln);
   } // method get_fullname
  
} // class Person


public class Encaps {

  public static void main(String args[]) {

    // NEW OBJECTS
    // use constructor Person()
    Person dis = new Person();

    // Assign values in object dis
    /* System.out.println(dis.firstname); */  // this line wouldn't compile
    
    dis.set_firstname("David");
    dis.set_lastname("Schwartz");
    
    // Retrieve individual values from dis
    
    System.out.println(dis.get_firstname());
    System.out.println(dis.get_lastname());

    // Determine fullname
    /* System.out.println(dis.addstrings(1,"David", "Schwartz")); */
    // the above line wouldn't compile -- would have to make addstrings public!

    String fullname = dis.get_fullname();
    System.out.println(fullname);

  } // method main
} // class Encaps


/* OUTPUT
David
Schwartz
David Schwartz
*/
</pre></body></html>