Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

1/17/2021 Design Patterns Video Tutorial

01 public class Animal {


02
03 private String name;
04 private double height;
05 private int weight;
06 private String favFood;
07 private double speed;
08 private String sound;
09
10 public void setName(String newName){ name = newName; }
11 public String getName(){ return name; }
12
13 public void setHeight(double newHeight){ height = newHeight; }
14 public double getHeight(){ return height; }
15
16 public void setWeight(int newWeight){
17 if (newWeight > 0){
18 weight = newWeight;
19 } else {
20 System.out.println("Weight must be bigger than 0");
21 }
22 }
23 public double getWeight(){ return weight; }
24
25 public void setFavFood(String newFavFood){ favFood = newFavFood; }
26 public String getFavFood(){ return favFood; }
27
28 public void setSpeed(double newSpeed){ speed = newSpeed; }
29 public double getSpeed(){ return speed; }
30
31 public void setSound(String newSound){ sound = newSound; }
32 public String getSound(){ return sound; }
33
34 // A private method can only be accessed by other public methods
35 // that are in the same class
36
37 private void bePrivate(){
38 System.out.println("I'm a private method");
39 }
40
41 public static void main(String[] args){
42
43 Animal dog = new Animal();
44
45 dog.setName("Grover");
46
47 System.out.println(dog.getName());
48
49 }
50
51 }

www.newthinktank.com/2012/08/design-patterns-video-tutorial/ 1/1

You might also like