forked from rick2785/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimal2.java
More file actions
51 lines (36 loc) · 1.21 KB
/
Animal2.java
File metadata and controls
51 lines (36 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class Animal2 {
private String name;
private double height;
private int weight;
private String favFood;
private double speed;
private String sound;
public void setName(String newName){ name = newName; }
public String getName(){ return name; }
public void setHeight(double newHeight){ height = newHeight; }
public double getHeight(){ return height; }
public void setWeight(int newWeight){
if (newWeight > 0){
weight = newWeight;
} else {
System.out.println("Weight must be bigger than 0");
}
}
public double getWeight(){ return weight; }
public void setFavFood(String newFavFood){ favFood = newFavFood; }
public String getFavFood(){ return favFood; }
public void setSpeed(double newSpeed){ speed = newSpeed; }
public double getSpeed(){ return speed; }
public void setSound(String newSound){ sound = newSound; }
public String getSound(){ return sound; }
// A private method can only be accessed by other public methods
// that are in the same class
private void bePrivate(){
System.out.println("I'm a private method");
}
public static void main(String[] args){
Animal2 dog = new Animal2();
dog.setName("Grover");
System.out.println(dog.getName());
}
}