@DavidMcConnachi/

CSE110_Java_Reference_Assignment_Example

Java

No description

fork
loading
Files
  • Main.java
Main.java
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
class Bunny {
    public int age = 0;
    public String name = "";

    public void hop() {
        System.out.println(name + " hops around.");
    }

    public void eat(String food) {
        System.out.println(name + " eats the " + food + ".");
    }

    public void sleep() {
        System.out.println(name + " takes a nap.");
    }
}

class Main {
  public static void main(String[] args) {
    Bunny flopsy;
    Bunny cottontail;

    flopsy = new Bunny();
    flopsy.name = "Flopsy";
    flopsy.age = 8;

    cottontail = flopsy; // !!!
    cottontail.name = "Cottontail";
    cottontail.age = 5;

    System.out.println(flopsy.name);
    System.out.println(flopsy.hop());
  }
}