Advertisement
thenewboston

Java Programming Tutorial - 43 - Composition

Aug 22nd, 2014
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. public class apples {
  2.    public static void main (String[] args){
  3.       potpie potObject = new potpie(4,5,6);
  4.       tuna tunaObject = new tuna("Bucky", potObject);
  5.      
  6.       System.out.println(tunaObject);
  7.    }
  8. }
  9.  
  10. public class tuna {
  11.    private String name;
  12.    private potpie birthday;
  13.    
  14.    public tuna(String theName, potpie theDate){
  15.       name = theName;
  16.       birthday = theDate;
  17.    }
  18.    
  19.    public String toString(){
  20.       return String.format("My name is %s, my birthday is %s", name, birthday);
  21.    }
  22. }
  23.  
  24. public class potpie {
  25.    public int month;
  26.    public int day;
  27.    public int year;
  28.    
  29.    public potpie(int m, int d, int y){
  30.       month = m;
  31.       day = d;
  32.       year = y;
  33.      
  34.       System.out.printf("The constructor for this is %s\n", this);
  35.    }
  36.    
  37.    public String toString(){
  38.       return String.format("%d/%d/%d", month, day, year);
  39.    }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement